cloneofsimo
commited on
Commit
•
1c6ea99
1
Parent(s):
74c3939
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,84 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
size_categories:
|
3 |
+
- 1M<n<10M
|
4 |
+
viewer: false
|
5 |
+
license: apache-2.0
|
6 |
+
---
|
7 |
+
|
8 |
+
|
9 |
+
# Tiny Cosmos-Tokenized Imagenet
|
10 |
+
|
11 |
+
|
12 |
+
<p align="center">
|
13 |
+
<img src="https://cdn-uploads.huggingface.co/production/uploads/6311151c64939fabc00c8436/2Wrz6bzvwIHVATbtYAujs.png" alt="small" width="800">
|
14 |
+
</p>
|
15 |
+
|
16 |
+
Similar fashion to [Simo's Imagenet.int8](https://github.com/cloneofsimo/imagenet.int8), here we provide [Cosmos-tokenized](https://github.com/NVIDIA/Cosmos-Tokenizer) imagenet for rapid prototyping.
|
17 |
+
Noticeably, the discrete tokenizer is able to compress entire imagenet into **shocking 2.45 GB of data!**
|
18 |
+
|
19 |
+
# How to use
|
20 |
+
|
21 |
+
This time, we dumped it all on simple pytorch safetensor format.
|
22 |
+
|
23 |
+
```python
|
24 |
+
import torch
|
25 |
+
import torch.nn as nn
|
26 |
+
from safetensors.torch import safe_open
|
27 |
+
|
28 |
+
# for continuous tokenizer
|
29 |
+
with safe_open("tokenize_dataset/imagenet_ci8x8.safetensors", framework="pt") as f:
|
30 |
+
data = f.get_tensor("latents") * 16.0 / 255.0
|
31 |
+
labels = f.get_tensor("labels")
|
32 |
+
|
33 |
+
print(data.shape) # 1281167, 16, 32, 32
|
34 |
+
print(labels.shape) # 1281167
|
35 |
+
```
|
36 |
+
|
37 |
+
To decode, you would need to install cosmos tokenizer.
|
38 |
+
|
39 |
+
```bash
|
40 |
+
git clone https://github.com/NVIDIA/Cosmos-Tokenizer.git
|
41 |
+
cd Cosmos-Tokenizer
|
42 |
+
apt-get install -y ffmpeg
|
43 |
+
pip install -e .
|
44 |
+
```
|
45 |
+
|
46 |
+
And decode using either `"Cosmos-Tokenizer-CI8x8"` or `"Cosmos-Tokenizer-DI8x8"`
|
47 |
+
|
48 |
+
|
49 |
+
**IMPORTANT**
|
50 |
+
* For continuous token, we've quantized & normalized to int8 format. Thus, you need to multiply 16.0 / 255.0
|
51 |
+
* For discrete token, saved format is int16. To use it properly just do uint16. Example below:
|
52 |
+
|
53 |
+
|
54 |
+
```python
|
55 |
+
|
56 |
+
model_name = "Cosmos-Tokenizer-CI8x8" if is_continuous else "Cosmos-Tokenizer-DI8x8"
|
57 |
+
decoder = ImageTokenizer(
|
58 |
+
checkpoint_dec=f"pretrained_ckpts/{model_name}/decoder.jit"
|
59 |
+
).to(device)
|
60 |
+
|
61 |
+
with safe_open("imagenet_ci8x8.safetensors", framework="pt") as f:
|
62 |
+
if tokenizer_type == "continuous":
|
63 |
+
data = f.get_tensor("latents").to(torch.bfloat16) * 16.0 / 255.0
|
64 |
+
else:
|
65 |
+
data = f.get_tensor("indices").to(torch.uint16)
|
66 |
+
labels = f.get_tensor("labels")
|
67 |
+
|
68 |
+
data = data[:1]
|
69 |
+
|
70 |
+
if is_continuous:
|
71 |
+
data = data.reshape(1, 16, 32, 32).to(device)
|
72 |
+
else:
|
73 |
+
# For discrete tokenizer, reshape to [1, 32, 32]
|
74 |
+
data = data.reshape(1, 32, 32).to(device).long()
|
75 |
+
# Decode the image
|
76 |
+
with torch.no_grad():
|
77 |
+
reconstructed = decoder.decode(data)
|
78 |
+
|
79 |
+
img = (
|
80 |
+
((reconstructed[0].cpu().float() + 1) * 127.5).clamp(0, 255).to(torch.uint8)
|
81 |
+
)
|
82 |
+
img = img.permute(1, 2, 0).numpy()
|
83 |
+
img = Image.fromarray(img)
|
84 |
+
```
|