Chris1 commited on
Commit
5109dc0
1 Parent(s): 16425b6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +174 -13
README.md CHANGED
@@ -8,43 +8,204 @@ tags:
8
  license: mit
9
  ---
10
 
11
- # MyModelName
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- ## Model description
14
-
15
- Describe the model here (what it does, what it's used for, etc.)
16
 
17
  ## Intended uses & limitations
18
 
19
  #### How to use
20
 
21
  ```python
22
- # You can include sample code which will be formatted
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  ```
24
 
 
 
25
  #### Limitations and bias
26
 
27
- Provide examples of latent issues and potential remediations.
 
28
 
29
  ## Training data
30
 
31
- Describe the data you used to train the model.
32
- If you initialized it with pre-trained weights, add a link to the pre-trained model card or repository with description of the pre-training data.
 
 
 
 
 
 
 
 
 
 
33
 
34
  ## Training procedure
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- Preprocessing, hardware used, hyperparameters...
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  ## Eval results
39
 
40
- ## Generated Images
41
 
42
- You can embed local or remote images using `![](...)`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  ### BibTeX entry and citation info
45
 
46
  ```bibtex
47
- @inproceedings{...,
48
- year={2020}
 
49
  }
50
  ```
 
8
  license: mit
9
  ---
10
 
11
+ # CycleGAN for unpaired image-to-image translation.
12
+
13
+ ## Model description
14
+
15
+ CycleGAN for unpaired image-to-image translation.
16
+ Given two image domains A and B, the following components are trained end2end to translate between such domains:
17
+ - A generator A to B, named G_AB conditioned on an image from A
18
+ - A generator B to A, named G_BA conditioned on an image from B
19
+ - A domain classifier D_A, associated with G_AB
20
+ - A domain classifier D_B, associated with G_BA
21
+ At inference time, G_AB or G_BA are relevant to translate images, respectively A to B or B to A.
22
+ In the general setting, this technique provides style transfer functionalities between the selected image domains A and B.
23
+ This allows to obtain a generated translation by G_AB, of an image from domain A that resembles the distribution of the images from domain B, and viceversa for the generator G_BA.
24
+ Under these framework, these aspects have been used to perform style transfer between NFT collections.
25
+ A collection is selected as domain A, another one as domain B and the CycleGAN provides forward and backward translation between A and B.
26
+ This has showed to allows high quality translation even in absence of paired sample-ground-truth data.
27
+ In particular, the model performs well with stationary backgrounds (no drastic texture changes in the appearance of backgrounds) as it is capable of recognizing the attributes of each of the elements of an NFT collections.
28
+ An attribute can be a variation in type of dressed fashion items such as sunglasses, earrings, clothes and also face or body attributes with respect to a common template model of the given NFT collection).
29
 
 
 
 
30
 
31
  ## Intended uses & limitations
32
 
33
  #### How to use
34
 
35
  ```python
36
+ import torch
37
+ from PIL import Image
38
+ from huggan.pytorch.cyclegan.modeling_cyclegan import GeneratorResNet
39
+ from torchvision import transforms as T
40
+ from torchvision.transforms import Compose, Resize, ToTensor, Normalize
41
+ from torchvision.utils import make_grid
42
+ from huggingface_hub import hf_hub_download, file_download
43
+ from accelerate import Accelerator
44
+ import json
45
+
46
+ def load_lightweight_model(model_name):
47
+ file_path = file_download.hf_hub_download(
48
+ repo_id=model_name,
49
+ filename="config.json"
50
+ )
51
+ config = json.loads(open(file_path).read())
52
+ organization_name, name = model_name.split("/")
53
+ model = Trainer(**config, organization_name=organization_name, name=name)
54
+ model.load(use_cpu=True)
55
+ model.accelerator = Accelerator()
56
+ return model
57
+ def get_concat_h(im1, im2):
58
+ dst = Image.new('RGB', (im1.width + im2.width, im1.height))
59
+ dst.paste(im1, (0, 0))
60
+ dst.paste(im2, (im1.width, 0))
61
+ return dst
62
+
63
+
64
+ n_channels = 3
65
+ image_size = 256
66
+ input_shape = (image_size, image_size)
67
+
68
+ transform = Compose([
69
+ T.ToPILImage(),
70
+ T.Resize(input_shape),
71
+ ToTensor(),
72
+ Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
73
+ ])
74
+
75
+ # load the translation model from source to target images: source will be generated by a separate Lightweight GAN, w
76
+ # while the target images are the result of the translation applied by the GeneratorResnet to the generated source images.
77
+ # Hence, given the source domain A and target domain B,
78
+ # B = Translator(GAN(A))
79
+ translator = GeneratorResNet.from_pretrained(f'huggingnft/{model_name}',
80
+ input_shape=(n_channels, image_size, image_size),
81
+ num_residual_blocks=9)
82
+
83
+ # sample noise that is used to generate source images by the
84
+ z = torch.randn(nrows, 100, 1, 1)
85
+ # load the GAN generator of source images that will be translated by the translation model
86
+ model = load_lightweight_model(f"huggingnft/{model_name.split('__2__')[0]}")
87
+ collectionA = model.generate_app(
88
+ num=timestamped_filename(),
89
+ nrow=nrows,
90
+ checkpoint=-1,
91
+ types="default"
92
+ )[1]
93
+ # resize to translator model input shape
94
+ resize = T.Resize((256, 256))
95
+ input = resize(collectionA)
96
+
97
+ # translate the resized collectionA to collectionB
98
+ collectionB = translator(input)
99
+
100
+ out_transform = T.ToPILImage()
101
+ results = []
102
+ for collA_image, collB_image in zip(input, collectionB):
103
+ results.append(
104
+ get_concat_h(out_transform(make_grid(collA_image, nrow=1, normalize=True)), out_transform(make_grid(collB_image, nrow=1, normalize=True)))
105
+ )
106
  ```
107
 
108
+
109
+
110
  #### Limitations and bias
111
 
112
+ Translation between collections provides exceptional output images in the case of NFT collections that portray subjects in the same way.
113
+ If the backgrounds vary too much within either of the collections, performance degrades or many more training iterations re required to achieve acceptable results.
114
 
115
  ## Training data
116
 
117
+
118
+ The CycleGAN model is trained on an unpaired dataset of samples from two selected NFT collections: colle tionA and collectionB.
119
+ To this end, two collections are loaded by means of the function load_dataset in the huggingface library, as follows.
120
+ A list of all available collections is available at [huggingNFT](https://huggingface.co/huggingnft)
121
+ ```python
122
+ from datasets import load_dataset
123
+
124
+ collectionA = load_dataset("huggingnft/COLLECTION_A")
125
+ collectionB = load_dataset("huggingnft/COLLECTION_B")
126
+ ```
127
+
128
+
129
 
130
  ## Training procedure
131
+ #### Preprocessing
132
+ The following transformations are applied to each input sample of collectionA and collectionB.
133
+ The input size is fixed to RGB images of height, width = 256, 256
134
+ ```python
135
+ n_channels = 3
136
+ image_size = 256
137
+ input_shape = (image_size, image_size)
138
+
139
+ transform = Compose([
140
+ T.ToPILImage(),
141
+ T.Resize(input_shape),
142
+ ToTensor(),
143
+ Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
144
+ ])
145
+ ```
146
 
147
+ #### Hardware
148
+ The configuration has been tested on single GPU setup on a RTX5000 and A5000, as well as multi-gpu single-rank distributed setups composed of 2 of the mentioned GPUs.
149
+
150
+ #### Hyperparameters
151
+ The following configuration has been kept fixed for all translation models:
152
+ - learning rate 0.0002
153
+ - number of epochs 200
154
+ - learning rate decay activation at epoch 80
155
+ - number of residual blocks of the cyclegan 9
156
+ - cycle loss weight 10.0
157
+ - identity loss weight 5.0
158
+ - optimizer ADAM with beta1 0.5 and beta2 0.999
159
+ - batch size 8
160
+ - NO mixed precision training
161
 
162
  ## Eval results
163
 
 
164
 
165
+ #### Training reports
166
+
167
+ [Cryptopunks to boreapeyachtclub](https://wandb.ai/chris1nexus/experiments--experiments_cyclegan_punk_to_apes_HQ--0/reports/CycleGAN-training-report--VmlldzoxODUxNzQz?accessToken=vueurpbhd2i8n347j880yakggs0sqdf7u0hpz3bpfsbrxcmk1jk4obg18f6wfk9w)
168
+
169
+
170
+ [Boreapeyachtclub to mutant-ape-yacht-club](https://wandb.ai/chris1nexus/experiments--my_paperspace_boredapeyachtclub__2__mutant-ape-yacht-club--11/reports/CycleGAN-training-report--VmlldzoxODUxNzg4?accessToken=jpyviwn7kdf5216ycrthwp6l8t3heb0lt8djt7dz12guu64qnpdh3ekecfcnoahu)
171
+
172
+
173
+ #### ## Generated Images
174
+
175
+ In the provided images, row0 and row2 represent real images from the respective collections.
176
+ Row1 is the translation of the immediate above images in row0 by means of the G_AB translation model.
177
+ Row3 is the translation of the immediate above images in row2 by means of the G_BA translation model.
178
+
179
+ Visualization over the training iterations for [boreapeyachtclub to mutant-ape-yacht-club](https://wandb.ai/chris1nexus/experiments--my_paperspace_boredapeyachtclub__2__mutant-ape-yacht-club--11/reports/Shared-panel-22-04-15-08-04-99--VmlldzoxODQ0MDI3?accessToken=45m3kxex5m3rpev3s6vmrv69k3u9p9uxcsp2k90wvbxwxzlqbqjqlnmgpl9265c0)
180
+
181
+ Visualization over the training iterations for [Cryptopunks to boreapeyachtclub](https://wandb.ai/chris1nexus/experiments--experiments_cyclegan_punk_to_apes_HQ--0/reports/Shared-panel-22-04-17-11-04-83--VmlldzoxODUxNjk5?accessToken=o25si6nflp2xst649vt6ayt56bnb95mxmngt1ieso091j2oazmqnwaf4h78vc2tu)
182
+
183
+
184
+ ### References
185
+
186
+ @misc{https://doi.org/10.48550/arxiv.1703.10593,
187
+ doi = {10.48550/ARXIV.1703.10593},
188
+
189
+ url = {https://arxiv.org/abs/1703.10593},
190
+
191
+ author = {Zhu, Jun-Yan and Park, Taesung and Isola, Phillip and Efros, Alexei A.},
192
+
193
+ keywords = {Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
194
+
195
+ title = {Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks},
196
+
197
+ publisher = {arXiv},
198
+
199
+ year = {2017},
200
+
201
+ copyright = {arXiv.org perpetual, non-exclusive license}
202
+ }
203
 
204
  ### BibTeX entry and citation info
205
 
206
  ```bibtex
207
+ @InProceedings{huggingnft,
208
+ author={Aleksey Korshuk, Christian Cancedda}
209
+ year=2022
210
  }
211
  ```