Spaces:
Running
Running
Merge pull request #36 from borisdayma/chore-cleanup
Browse filesChore: repo cleanup
Former-commit-id: 8f1d9a59610dff1f33c1d2b51ed75184776f1a26
- README.md +62 -28
- dalle_mini/vqgan_jax/README.md +5 -0
- dalle_mini/vqgan_jax/convert_pt_model_to_jax.py +0 -109
- requirements.txt +0 -12
- environment.yaml → seq2seq/environment.yaml +0 -0
- seq2seq/requirements.txt +14 -7
README.md
CHANGED
@@ -1,42 +1,76 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
* [Conceptual 12M](https://github.com/google-research-datasets/conceptual-12m) Dataset (already loaded and preprocessed in TPU VM by Luke).
|
7 |
-
* [YFCC100M Subset](https://github.com/openai/CLIP/blob/main/data/yfcc100m.md)
|
8 |
-
* [Coneptual Captions 3M](https://github.com/google-research-datasets/conceptual-captions)
|
9 |
|
10 |
-
|
11 |
-
* Use the Taming Transformers VQ-GAN (with 16384 tokens)
|
12 |
-
* Use a seq2seq (language encoder --> image decoder) model with a pretrained non-autoregressive encoder (e.g. BERT) and an autoregressive decoder (like GPT).
|
13 |
|
14 |
-
|
15 |
-
* Whether to freeze the text encoder?
|
16 |
-
* Whether to finetune the VQ-GAN?
|
17 |
-
* Which text encoder to use (e.g. BERT, RoBERTa, etc.)?
|
18 |
-
* Hyperparameter choices for the decoder (e.g. positional embedding, initialization, etc.)
|
19 |
|
20 |
-
##
|
21 |
|
22 |
-
|
23 |
-
* work on dataset loading - [see suggested datasets](https://discuss.huggingface.co/t/dall-e-mini-version/7324/4)
|
24 |
-
* Optionally create the OpenAI YFCC100M subset (see [this post](https://discuss.huggingface.co/t/dall-e-mini-version/7324/30?u=boris))
|
25 |
-
* work on text/image encoding
|
26 |
-
* concatenate inputs (not sure if we need fixed length for text or use a special token separating text & image)
|
27 |
-
* adapt training script
|
28 |
-
* create inference function
|
29 |
-
* integrate CLIP for better results (only if we have the time)
|
30 |
-
* work on a demo (streamlit or colab or maybe just HF widget)
|
31 |
-
* document (set up repo on model hub per instructions, start on README writeup…)
|
32 |
-
* help with coordinating activities & progress
|
33 |
|
|
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
```
|
39 |
$ pip install -r requirements.txt -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
|
40 |
```
|
41 |
|
42 |
If you use `conda`, you can create the virtual env and install everything using: `conda env update -f environments.yaml`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Dalle Mini
|
3 |
+
emoji: 🎨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
+
sdk: streamlit
|
7 |
+
app_file: app/app.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
|
11 |
+
# DALL-E Mini
|
12 |
|
13 |
+
_Generate images from a text prompt_
|
|
|
|
|
|
|
14 |
|
15 |
+
TODO: add some cool example
|
|
|
|
|
16 |
|
17 |
+
## [Create my own images with the demo →](TODO)
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
## How does it work?
|
20 |
|
21 |
+
Refer to [our report](TODO).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
## Development
|
24 |
|
25 |
+
This section is for the adventurous people wanting to look into the code.
|
26 |
+
|
27 |
+
### Dependencies Installation
|
28 |
+
|
29 |
+
The root folder and associated `requirements.txt` is only for the app.
|
30 |
+
|
31 |
+
You will find necessary requirements in each sub-section.
|
32 |
+
|
33 |
+
You should create a new python virtual environment and install the project dependencies inside the virtual env. You need to use the `-f` (`--find-links`) option for `pip` to be able to find the appropriate `libtpu` required for the TPU hardware.
|
34 |
+
|
35 |
+
Adapt the installation to your own hardware and follow library installation instructions.
|
36 |
|
37 |
```
|
38 |
$ pip install -r requirements.txt -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
|
39 |
```
|
40 |
|
41 |
If you use `conda`, you can create the virtual env and install everything using: `conda env update -f environments.yaml`
|
42 |
+
|
43 |
+
### Training of VQGAN
|
44 |
+
|
45 |
+
The VQGAN was trained using [taming-transformers](https://github.com/CompVis/taming-transformers).
|
46 |
+
|
47 |
+
We recommend using the latest version available.
|
48 |
+
|
49 |
+
### Conversion of VQGAN to JAX
|
50 |
+
|
51 |
+
Use [patil-suraj/vqgan-jax](https://github.com/patil-suraj/vqgan-jax).
|
52 |
+
|
53 |
+
### Training of Seq2Seq
|
54 |
+
|
55 |
+
Refer to `seq2seq` folder (some parameters may have been hardcoded for convenience when training on our TPU VM).
|
56 |
+
|
57 |
+
### Inference
|
58 |
+
|
59 |
+
Refer to the demo notebooks.
|
60 |
+
TODO: add links
|
61 |
+
|
62 |
+
## Authors
|
63 |
+
|
64 |
+
- [Boris Dayma](https://github.com/borisdayma)
|
65 |
+
- [Suraj Patil](https://github.com/patil-suraj)
|
66 |
+
- [Pedro Cuenca](https://github.com/pcuenca)
|
67 |
+
- [Khalid Saifullah](https://github.com/khalidsaifullaah)
|
68 |
+
- [Tanishq Abraham](https://github.com/tmabraham)
|
69 |
+
- [Phúc Lê Khắc](https://github.com/lkhphuc)
|
70 |
+
- [Luke Melas](https://github.com/lukemelas)
|
71 |
+
- [Ritobrata Ghosh](https://github.com/ghosh-r)
|
72 |
+
|
73 |
+
## Acknowledgements
|
74 |
+
|
75 |
+
- 🤗 Hugging Face for organizing [the FLAX/JAX community week](https://github.com/huggingface/transformers/tree/master/examples/research_projects/jax-projects)
|
76 |
+
- Google Cloud team for providing access to TPU's
|
dalle_mini/vqgan_jax/README.md
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## vqgan-jax
|
2 |
+
|
3 |
+
Files copied from [patil-suraj/vqgan-jax](https://github.com/patil-suraj/vqgan-jax/tree/main/vqgan_jax)
|
4 |
+
|
5 |
+
Required for VQGAN Jax model.
|
dalle_mini/vqgan_jax/convert_pt_model_to_jax.py
DELETED
@@ -1,109 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
|
3 |
-
import jax.numpy as jnp
|
4 |
-
from flax.traverse_util import flatten_dict, unflatten_dict
|
5 |
-
|
6 |
-
import torch
|
7 |
-
|
8 |
-
from modeling_flax_vqgan import VQModel
|
9 |
-
from configuration_vqgan import VQGANConfig
|
10 |
-
|
11 |
-
|
12 |
-
regex = r"\w+[.]\d+"
|
13 |
-
|
14 |
-
|
15 |
-
def rename_key(key):
|
16 |
-
pats = re.findall(regex, key)
|
17 |
-
for pat in pats:
|
18 |
-
key = key.replace(pat, "_".join(pat.split(".")))
|
19 |
-
return key
|
20 |
-
|
21 |
-
|
22 |
-
# Adapted from https://github.com/huggingface/transformers/blob/ff5cdc086be1e0c3e2bbad8e3469b34cffb55a85/src/transformers/modeling_flax_pytorch_utils.py#L61
|
23 |
-
def convert_pytorch_state_dict_to_flax(pt_state_dict, flax_model):
|
24 |
-
# convert pytorch tensor to numpy
|
25 |
-
pt_state_dict = {k: v.numpy() for k, v in pt_state_dict.items()}
|
26 |
-
|
27 |
-
random_flax_state_dict = flatten_dict(flax_model.params)
|
28 |
-
flax_state_dict = {}
|
29 |
-
|
30 |
-
remove_base_model_prefix = (flax_model.base_model_prefix not in flax_model.params) and (
|
31 |
-
flax_model.base_model_prefix in set([k.split(".")[0] for k in pt_state_dict.keys()])
|
32 |
-
)
|
33 |
-
add_base_model_prefix = (flax_model.base_model_prefix in flax_model.params) and (
|
34 |
-
flax_model.base_model_prefix not in set([k.split(".")[0] for k in pt_state_dict.keys()])
|
35 |
-
)
|
36 |
-
|
37 |
-
# Need to change some parameters name to match Flax names so that we don't have to fork any layer
|
38 |
-
for pt_key, pt_tensor in pt_state_dict.items():
|
39 |
-
pt_tuple_key = tuple(pt_key.split("."))
|
40 |
-
|
41 |
-
has_base_model_prefix = pt_tuple_key[0] == flax_model.base_model_prefix
|
42 |
-
require_base_model_prefix = (flax_model.base_model_prefix,) + pt_tuple_key in random_flax_state_dict
|
43 |
-
|
44 |
-
if remove_base_model_prefix and has_base_model_prefix:
|
45 |
-
pt_tuple_key = pt_tuple_key[1:]
|
46 |
-
elif add_base_model_prefix and require_base_model_prefix:
|
47 |
-
pt_tuple_key = (flax_model.base_model_prefix,) + pt_tuple_key
|
48 |
-
|
49 |
-
# Correctly rename weight parameters
|
50 |
-
if (
|
51 |
-
"norm" in pt_key
|
52 |
-
and (pt_tuple_key[-1] == "bias")
|
53 |
-
and (pt_tuple_key[:-1] + ("bias",) in random_flax_state_dict)
|
54 |
-
):
|
55 |
-
pt_tensor = pt_tensor[None, None, None, :]
|
56 |
-
elif (
|
57 |
-
"norm" in pt_key
|
58 |
-
and (pt_tuple_key[-1] == "bias")
|
59 |
-
and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
|
60 |
-
):
|
61 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("scale",)
|
62 |
-
pt_tensor = pt_tensor[None, None, None, :]
|
63 |
-
elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
|
64 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("scale",)
|
65 |
-
pt_tensor = pt_tensor[None, None, None, :]
|
66 |
-
if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
|
67 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("embedding",)
|
68 |
-
elif pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and pt_tuple_key not in random_flax_state_dict:
|
69 |
-
# conv layer
|
70 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("kernel",)
|
71 |
-
pt_tensor = pt_tensor.transpose(2, 3, 1, 0)
|
72 |
-
elif pt_tuple_key[-1] == "weight" and pt_tuple_key not in random_flax_state_dict:
|
73 |
-
# linear layer
|
74 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("kernel",)
|
75 |
-
pt_tensor = pt_tensor.T
|
76 |
-
elif pt_tuple_key[-1] == "gamma":
|
77 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("weight",)
|
78 |
-
elif pt_tuple_key[-1] == "beta":
|
79 |
-
pt_tuple_key = pt_tuple_key[:-1] + ("bias",)
|
80 |
-
|
81 |
-
if pt_tuple_key in random_flax_state_dict:
|
82 |
-
if pt_tensor.shape != random_flax_state_dict[pt_tuple_key].shape:
|
83 |
-
raise ValueError(
|
84 |
-
f"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape "
|
85 |
-
f"{random_flax_state_dict[pt_tuple_key].shape}, but is {pt_tensor.shape}."
|
86 |
-
)
|
87 |
-
|
88 |
-
# also add unexpected weight so that warning is thrown
|
89 |
-
flax_state_dict[pt_tuple_key] = jnp.asarray(pt_tensor)
|
90 |
-
|
91 |
-
return unflatten_dict(flax_state_dict)
|
92 |
-
|
93 |
-
|
94 |
-
def convert_model(config_path, pt_state_dict_path, save_path):
|
95 |
-
config = VQGANConfig.from_pretrained(config_path)
|
96 |
-
model = VQModel(config)
|
97 |
-
|
98 |
-
state_dict = torch.load(pt_state_dict_path, map_location="cpu")["state_dict"]
|
99 |
-
keys = list(state_dict.keys())
|
100 |
-
for key in keys:
|
101 |
-
if key.startswith("loss"):
|
102 |
-
state_dict.pop(key)
|
103 |
-
continue
|
104 |
-
renamed_key = rename_key(key)
|
105 |
-
state_dict[renamed_key] = state_dict.pop(key)
|
106 |
-
|
107 |
-
state = convert_pytorch_state_dict_to_flax(state_dict, model)
|
108 |
-
model.params = unflatten_dict(state)
|
109 |
-
model.save_pretrained(save_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
# Note: install with the following command:
|
2 |
-
# pip install -r requirements.txt -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
|
3 |
-
# Otherwise it won't find the appropriate libtpu_nightly
|
4 |
-
requests
|
5 |
-
jax[tpu]>=0.2.16
|
6 |
-
-e git+https://github.com/huggingface/transformers.git@master#egg=transformers
|
7 |
-
-e git+https://github.com/huggingface/datasets.git@master#egg=datasets
|
8 |
-
flax
|
9 |
-
jupyter
|
10 |
-
|
11 |
-
# Inference
|
12 |
-
ftfy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
environment.yaml → seq2seq/environment.yaml
RENAMED
File without changes
|
seq2seq/requirements.txt
CHANGED
@@ -1,8 +1,15 @@
|
|
1 |
-
|
2 |
-
jax
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
wandb
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Note: install with the following command:
|
2 |
+
# pip install -r requirements.txt -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
|
3 |
+
# Otherwise it won't find the appropriate libtpu_nightly
|
4 |
+
requests
|
5 |
+
jax[tpu]>=0.2.16
|
6 |
+
-e git+https://github.com/huggingface/transformers.git@master#egg=transformers
|
7 |
+
-e git+https://github.com/huggingface/datasets.git@master#egg=datasets
|
8 |
+
flax
|
9 |
+
jupyter
|
10 |
wandb
|
11 |
+
nltk
|
12 |
+
optax
|
13 |
+
|
14 |
+
# Inference
|
15 |
+
ftfy
|