Views
No views yet

1import torch
2
3from diffusers import DiffusionPipeline
4from diffusers import AutoencoderKL
5
6device = torch.device('cuda')
7
8# We do not host the weights of the SD3 VAE -- load it from StabilityAI
9sd3_vae = AutoencoderKL.from_pretrained("stabilityai/stable-diffusion-3.5-large", subfolder="vae")
10
11pipeline = DiffusionPipeline.from_pretrained(
12 "StonyBrook-CVLab/PixCell-1024",
13 vae=sd3_vae,
14 custom_pipeline="StonyBrook-CVLab/PixCell-pipeline",
15 trust_remote_code=True,
16 torch_dtype=torch.float16,
17)
18
19pipeline.to(device);1import timm
2from timm.data import resolve_data_config
3from timm.data.transforms_factory import create_transform
4
5timm_kwargs = {
6 'img_size': 224,
7 'patch_size': 14,
8 'depth': 24,
9 'num_heads': 24,
10 'init_values': 1e-5,
11 'embed_dim': 1536,
12 'mlp_ratio': 2.66667*2,
13 'num_classes': 0,
14 'no_embed_class': True,
15 'mlp_layer': timm.layers.SwiGLUPacked,
16 'act_layer': torch.nn.SiLU,
17 'reg_tokens': 8,
18 'dynamic_img_size': True
19 }
20uni_model = timm.create_model("hf-hub:MahmoodLab/UNI2-h", pretrained=True, **timm_kwargs)
21transform = create_transform(**resolve_data_config(uni_model.pretrained_cfg, model=uni_model))
22uni_model.eval()
23uni_model.to(device);1uncond = pipeline.get_unconditional_embedding(1)
2with torch.amp.autocast('cuda'):
3 samples = pipeline(uni_embeds=uncond, negative_uni_embeds=None, guidance_scale=1.0)1# Load image
2import numpy as np
3import einops
4from PIL import Image
5from huggingface_hub import hf_hub_download
6
7# This is an example image we provide
8path = hf_hub_download(repo_id="StonyBrook-CVLab/PixCell-1024", filename="test_image.png")
9image = Image.open(path).convert("RGB")
10
11
12# Rearrange 1024x1024 image into 16 256x256 patches
13uni_patches = np.array(image)
14uni_patches = einops.rearrange(uni_patches, '(d1 h) (d2 w) c -> (d1 d2) h w c', d1=4, d2=4)
15uni_input = torch.stack([transform(Image.fromarray(item)) for item in uni_patches])
16
17# Extract UNI embeddings
18with torch.inference_mode():
19 uni_emb = uni_model(uni_input.to(device))
20
21# reshape UNI to (bs, 16, D)
22uni_emb = uni_emb.unsqueeze(0)
23print("Extracted UNI:", uni_emb.shape)
24
25# Get unconditional embedding for classifier-free guidance
26uncond = pipeline.get_unconditional_embedding(uni_emb.shape[0])
27# Generate new samples
28with torch.amp.autocast('cuda'):
29 samples = pipeline(uni_embeds=uni_emb, negative_uni_embeds=uncond, guidance_scale=1.5).images
30