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-256-Cell-ControlNet",
13 vae=sd3_vae,
14 custom_pipeline="StonyBrook-CVLab/PixCell-pipeline-ControlNet",
15 trust_remote_code=True,
16)
17
18pipeline.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)
21uni_transforms = create_transform(**resolve_data_config(uni_model.pretrained_cfg, model=uni_model))
22uni_model.eval()
23uni_model.to(device);1# Load image
2import numpy as np
3from PIL import Image
4from huggingface_hub import hf_hub_download
5
6# This is an example image/mask pair we provide
7image_path = hf_hub_download(repo_id="StonyBrook-CVLab/PixCell-256-Cell-ControlNet", filename="test_image.png")
8mask_path = hf_hub_download(repo_id="StonyBrook-CVLab/PixCell-256-Cell-ControlNet", filename="test_mask.png")
9image = Image.open(image_path).convert("RGB")
10mask = np.asarray(Image.open(mask_path).convert("RGB"))
11
12# Extract UNI embedding from the image
13uni_inp = uni_transforms(image).unsqueeze(dim=0)
14with torch.inference_mode():
15 uni_emb = uni_model(uni_inp.to(device))
16
17# reshape UNI to (bs, 1, D)
18uni_emb = uni_emb.unsqueeze(1)
19print("Extracted UNI:", uni_emb.shape)
20
21# Get unconditional embedding for classifier-free guidance
22uncond = pipeline.get_unconditional_embedding(uni_emb.shape[0])
23# Generate new samples using the given mask
24samples = pipeline(uni_embeds=uni_emb, controlnet_input=mask, negative_uni_embeds=uncond, guidance_scale=2.5, num_images_per_prompt=1).images