Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
tags:
|
4 |
+
datasets:
|
5 |
+
- imagenet-21k
|
6 |
+
---
|
7 |
+
|
8 |
+
# ImageGPT (medium-sized model)
|
9 |
+
|
10 |
+
ImageGPT (iGPT) model pre-trained on ImageNet ILSVRC 2012 (14 million images, 21,843 classes) at resolution 32x32. It was introduced in the paper [Generative Pretraining from Pixels](https://cdn.openai.com/papers/Generative_Pretraining_from_Pixels_V2.pdf) by Chen et al. and first released in [this repository](https://github.com/openai/image-gpt). See also the official [blog post](https://openai.com/blog/image-gpt/).
|
11 |
+
|
12 |
+
Disclaimer: The team releasing ImageGPT did not write a model card for this model so this model card has been written by the Hugging Face team.
|
13 |
+
|
14 |
+
## Model description
|
15 |
+
|
16 |
+
The ImageGPT (iGPT) is a transformer decoder model (GPT-like) pretrained on a large collection of images in a self-supervised fashion, namely ImageNet-21k, at a resolution of 32x32 pixels.
|
17 |
+
|
18 |
+
The goal for the model is simply to predict the next pixel value, given the previous ones.
|
19 |
+
|
20 |
+
By pre-training the model, it learns an inner representation of images that can then be used to:
|
21 |
+
- extract features useful for downstream tasks: one can either use ImageGPT to produce fixed image features, in order to train a linear model (like a sklearn logistic regression model or SVM). This is also referred to as "linear probing".
|
22 |
+
- perform (un)conditional image generation.
|
23 |
+
|
24 |
+
## Intended uses & limitations
|
25 |
+
|
26 |
+
You can use the raw model for either feature extractor or (un) conditional image generation. See the [model hub](https://huggingface.co/models?search=openai/imagegpt) to all ImageGPT variants.
|
27 |
+
|
28 |
+
### How to use
|
29 |
+
|
30 |
+
Here is how to use this model in PyTorch to perform unconditional image generation:
|
31 |
+
|
32 |
+
```python
|
33 |
+
from transformers import ImageGPTFeatureExtractor, ImageGPTForCausalImageModeling
|
34 |
+
import torch
|
35 |
+
import matplotlib.pyplot as plt
|
36 |
+
import numpy as np
|
37 |
+
|
38 |
+
feature_extractor = ImageGPTFeatureExtractor.from_pretrained('openai/imagegpt-medium')
|
39 |
+
model = ImageGPTForCausalImageModeling.from_pretrained('openai/imagegpt-medium')
|
40 |
+
|
41 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
42 |
+
model.to(device)
|
43 |
+
|
44 |
+
# unconditional generation of 8 images
|
45 |
+
batch_size = 8
|
46 |
+
context = torch.full((batch_size, 1), model.config.vocab_size - 1) #initialize with SOS token
|
47 |
+
context = torch.tensor(context).to(device)
|
48 |
+
output = model.generate(pixel_values=context, max_length=model.config.n_positions + 1, temperature=1.0, do_sample=True, top_k=40)
|
49 |
+
|
50 |
+
clusters = feature_extractor.clusters
|
51 |
+
n_px = feature_extractor.size
|
52 |
+
|
53 |
+
samples = output[:,1:].cpu().detach().numpy()
|
54 |
+
samples_img = [np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples] # convert color cluster tokens back to pixels
|
55 |
+
|
56 |
+
f, axes = plt.subplots(1, batch_size, dpi=300)
|
57 |
+
for img, ax in zip(samples_img, axes):
|
58 |
+
ax.axis('off')
|
59 |
+
ax.imshow(img)
|
60 |
+
```
|
61 |
+
|
62 |
+
## Training data
|
63 |
+
|
64 |
+
The ImageGPT model was pretrained on [ImageNet-21k](http://www.image-net.org/), a dataset consisting of 14 million images and 21k classes.
|
65 |
+
|
66 |
+
## Training procedure
|
67 |
+
|
68 |
+
### Preprocessing
|
69 |
+
|
70 |
+
Images are first resized/rescaled to the same resolution (32x32) and normalized across the RGB channels. Next, color-clustering is performed. This means that every pixel is turned into one of 512 possible cluster values. This way, one ends up with a sequence of 32x32 = 1024 pixel values, rather than 32x32x3 = 3072, which is prohibitively large for Transformer-based models.
|
71 |
+
|
72 |
+
### Pretraining
|
73 |
+
|
74 |
+
Training details can be found in section 3.4 of v2 of the paper.
|
75 |
+
|
76 |
+
## Evaluation results
|
77 |
+
|
78 |
+
For evaluation results on several image classification benchmarks, we refer to the original paper.
|
79 |
+
|
80 |
+
### BibTeX entry and citation info
|
81 |
+
|
82 |
+
```bibtex
|
83 |
+
@InProceedings{pmlr-v119-chen20s,
|
84 |
+
title = {Generative Pretraining From Pixels},
|
85 |
+
author = {Chen, Mark and Radford, Alec and Child, Rewon and Wu, Jeffrey and Jun, Heewoo and Luan, David and Sutskever, Ilya},
|
86 |
+
booktitle = {Proceedings of the 37th International Conference on Machine Learning},
|
87 |
+
pages = {1691--1703},
|
88 |
+
year = {2020},
|
89 |
+
editor = {III, Hal Daumé and Singh, Aarti},
|
90 |
+
volume = {119},
|
91 |
+
series = {Proceedings of Machine Learning Research},
|
92 |
+
month = {13--18 Jul},
|
93 |
+
publisher = {PMLR},
|
94 |
+
pdf = {http://proceedings.mlr.press/v119/chen20s/chen20s.pdf},
|
95 |
+
url = {https://proceedings.mlr.press/v119/chen20s.html
|
96 |
+
}
|
97 |
+
```
|
98 |
+
|
99 |
+
```bibtex
|
100 |
+
@inproceedings{deng2009imagenet,
|
101 |
+
title={Imagenet: A large-scale hierarchical image database},
|
102 |
+
author={Deng, Jia and Dong, Wei and Socher, Richard and Li, Li-Jia and Li, Kai and Fei-Fei, Li},
|
103 |
+
booktitle={2009 IEEE conference on computer vision and pattern recognition},
|
104 |
+
pages={248--255},
|
105 |
+
year={2009},
|
106 |
+
organization={Ieee}
|
107 |
+
}
|
108 |
+
```
|