Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,99 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
### Load PixCell-256 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-diffusers",
|
20 |
+
vae=sd3_vae,
|
21 |
+
custom_pipeline="StonyBrook-CVLab/pixcell-pipeline",
|
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 |
+
### Unconditional generation
|
56 |
+
```python
|
57 |
+
uncond = pipeline.get_unconditional_embedding(1)
|
58 |
+
samples = pipeline(uni_embeds=uncond, negative_uni_embeds=None, guidance_scale=1.0)
|
59 |
+
```
|
60 |
+
|
61 |
+
### Conditional generation
|
62 |
+
```python
|
63 |
+
# Load image
|
64 |
+
import numpy as np
|
65 |
+
from PIL import Image
|
66 |
+
from huggingface_hub import hf_hub_download
|
67 |
+
|
68 |
+
# This is an example image we provide
|
69 |
+
path = hf_hub_download(repo_id="StonyBrook-CVLab/pixcell-256-diffusers", filename="test_image.jpg")
|
70 |
+
image = Image.open(path)
|
71 |
+
|
72 |
+
# Extract UNI from random patches in the image
|
73 |
+
n_patches = 8
|
74 |
+
patches = []
|
75 |
+
uni_emb = []
|
76 |
+
for k in range(n_patches):
|
77 |
+
# Extract random crop
|
78 |
+
sz = pipeline.transformer.config.sample_size * pipeline.vae_scale_factor
|
79 |
+
|
80 |
+
x1 = np.random.randint(0, image.size[0] - sz+1)
|
81 |
+
y1 = np.random.randint(0, image.size[1] - sz+1)
|
82 |
+
image_patch = image.crop((x1, y1, x1+sz, y1+sz))
|
83 |
+
patches.append(image_patch)
|
84 |
+
print("Extracted patch:", patches[-1].size)
|
85 |
+
|
86 |
+
# For 256x256 directly pass through UNI
|
87 |
+
uni_image = transform(image_patch).unsqueeze(dim=0)
|
88 |
+
with torch.inference_mode():
|
89 |
+
feature_emb = uni_model(uni_image.to(device))
|
90 |
+
uni_emb.append(feature_emb)
|
91 |
+
|
92 |
+
uni_emb = torch.stack(uni_emb, dim=0)
|
93 |
+
print("Extracted UNI:", uni_emb.shape)
|
94 |
+
|
95 |
+
# Get unconditional embedding for classifier-free guidance
|
96 |
+
uncond = pipeline.get_unconditional_embedding(uni_emb.shape[0])
|
97 |
+
# Generate new samples
|
98 |
+
samples = pipeline(uni_embeds=uni_emb, negative_uni_embeds=uncond, guidance_scale=3., num_images_per_prompt=1)
|
99 |
+
```
|