Views
No views yet
1from transformers import ImageGPTImageProcessor, ImageGPTForCausalImageModeling
2import torch
3import matplotlib.pyplot as plt
4import numpy as np
5
6processor = ImageGPTImageProcessor.from_pretrained('openai/imagegpt-small')
7model = ImageGPTForCausalImageModeling.from_pretrained('openai/imagegpt-small')
8
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model.to(device)
11
12# unconditional generation of 8 images
13batch_size = 8
14context = torch.full((batch_size, 1), model.config.vocab_size - 1) #initialize with SOS token
15context = torch.tensor(context).to(device)
16output = model.generate(pixel_values=context, max_length=model.config.n_positions + 1, temperature=1.0, do_sample=True, top_k=40)
17
18clusters = processor.clusters
19n_px = processor.size
20
21samples = output[:,1:].cpu().detach().numpy()
22samples_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
23
24f, axes = plt.subplots(1, batch_size, dpi=300)
25for img, ax in zip(samples_img, axes):
26 ax.axis('off')
27 ax.imshow(img)1@InProceedings{pmlr-v119-chen20s,
2 title = {Generative Pretraining From Pixels},
3 author = {Chen, Mark and Radford, Alec and Child, Rewon and Wu, Jeffrey and Jun, Heewoo and Luan, David and Sutskever, Ilya},
4 booktitle = {Proceedings of the 37th International Conference on Machine Learning},
5 pages = {1691--1703},
6 year = {2020},
7 editor = {III, Hal Daumé and Singh, Aarti},
8 volume = {119},
9 series = {Proceedings of Machine Learning Research},
10 month = {13--18 Jul},
11 publisher = {PMLR},
12 pdf = {http://proceedings.mlr.press/v119/chen20s/chen20s.pdf},
13 url = {https://proceedings.mlr.press/v119/chen20s.html
14}1@inproceedings{deng2009imagenet,
2 title={Imagenet: A large-scale hierarchical image database},
3 author={Deng, Jia and Dong, Wei and Socher, Richard and Li, Li-Jia and Li, Kai and Fei-Fei, Li},
4 booktitle={2009 IEEE conference on computer vision and pattern recognition},
5 pages={248--255},
6 year={2009},
7 organization={Ieee}
8}