Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
### Load PixCell-256-Cell-ControlNet model
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from diffusers import DiffusionPipeline
|
| 11 |
+
from diffusers import AutoencoderKL
|
| 12 |
+
|
| 13 |
+
device = torch.device('cuda')
|
| 14 |
+
|
| 15 |
+
# We do not host the weights of the SD3 VAE -- load it from StabilityAI
|
| 16 |
+
sd3_vae = AutoencoderKL.from_pretrained("stabilityai/stable-diffusion-3.5-large", subfolder="vae")
|
| 17 |
+
|
| 18 |
+
pipeline = DiffusionPipeline.from_pretrained(
|
| 19 |
+
"StonyBrook-CVLab/PixCell-256-Cell-ControlNet",
|
| 20 |
+
vae=sd3_vae,
|
| 21 |
+
custom_pipeline="StonyBrook-CVLab/PixCell-pipeline-ControlNet",
|
| 22 |
+
trust_remote_code=True,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
pipeline.to(device);
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
### Load [[UNI-2h]](https://huggingface.co/MahmoodLab/UNI2-h) for conditioning
|
| 29 |
+
```python
|
| 30 |
+
import timm
|
| 31 |
+
from timm.data import resolve_data_config
|
| 32 |
+
from timm.data.transforms_factory import create_transform
|
| 33 |
+
|
| 34 |
+
timm_kwargs = {
|
| 35 |
+
'img_size': 224,
|
| 36 |
+
'patch_size': 14,
|
| 37 |
+
'depth': 24,
|
| 38 |
+
'num_heads': 24,
|
| 39 |
+
'init_values': 1e-5,
|
| 40 |
+
'embed_dim': 1536,
|
| 41 |
+
'mlp_ratio': 2.66667*2,
|
| 42 |
+
'num_classes': 0,
|
| 43 |
+
'no_embed_class': True,
|
| 44 |
+
'mlp_layer': timm.layers.SwiGLUPacked,
|
| 45 |
+
'act_layer': torch.nn.SiLU,
|
| 46 |
+
'reg_tokens': 8,
|
| 47 |
+
'dynamic_img_size': True
|
| 48 |
+
}
|
| 49 |
+
uni_model = timm.create_model("hf-hub:MahmoodLab/UNI2-h", pretrained=True, **timm_kwargs)
|
| 50 |
+
transform = create_transform(**resolve_data_config(uni_model.pretrained_cfg, model=uni_model))
|
| 51 |
+
uni_model.eval()
|
| 52 |
+
uni_model.to(device);
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
### Mask-conditioned generation
|
| 57 |
+
```python
|
| 58 |
+
# Load image
|
| 59 |
+
import numpy as np
|
| 60 |
+
from PIL import Image
|
| 61 |
+
from huggingface_hub import hf_hub_download
|
| 62 |
+
|
| 63 |
+
# This is an example image/mask pair we provide
|
| 64 |
+
image_path = hf_hub_download(repo_id="StonyBrook-CVLab/PixCell-256-Cell-ControlNet", filename="test_image.png")
|
| 65 |
+
mask_path = hf_hub_download(repo_id="StonyBrook-CVLab/PixCell-256-Cell-ControlNet", filename="test_mask.png")
|
| 66 |
+
image = Image.open(image_path).convert("RGB")
|
| 67 |
+
mask = np.asarray(Image.open(mask_path).convert("RGB"))
|
| 68 |
+
|
| 69 |
+
# Extract UNI embedding from the image
|
| 70 |
+
uni_inp = uni_transforms(image).unsqueeze(dim=0)
|
| 71 |
+
with torch.inference_mode():
|
| 72 |
+
uni_emb = uni_model(uni_inp.to(device))
|
| 73 |
+
|
| 74 |
+
# reshape UNI to (bs, 1, D)
|
| 75 |
+
uni_emb = uni_emb.unsqueeze(1)
|
| 76 |
+
print("Extracted UNI:", uni_emb.shape)
|
| 77 |
+
|
| 78 |
+
# Get unconditional embedding for classifier-free guidance
|
| 79 |
+
uncond = pipeline_controlnet.get_unconditional_embedding(uni_emb.shape[0])
|
| 80 |
+
# Generate new samples using the given mask
|
| 81 |
+
samples = pipeline_controlnet(uni_embeds=uni_emb, controlnet_input=mask, negative_uni_embeds=uncond, guidance_scale=2.5, num_images_per_prompt=1).images
|
| 82 |
+
```
|