模型:
openai/imagegpt-medium
ImageGPT (iGPT) 是在ImageNet ILSVRC 2012数据集(1400万张图片,21843个类别)上以32x32的分辨率进行预训练的模型。它由陈等人在论文 Generative Pretraining from Pixels 中介绍,并第一次在 this repository 发布。详见官方网站 blog post 。
免责声明:发布ImageGPT模型的团队未为此模型编写模型卡片,因此该模型卡片是由Hugging Face团队编写的。
ImageGPT (iGPT) 是一个transformer解码器模型(类似于GPT),在一个自监督的方式下,即ImageNet-21k数据集上以32x32像素的分辨率进行预训练。
模型的目标就是根据之前的像素值预测下一个像素值。
通过预训练,模型学习到了图像的内部表示,可以用于以下用途:
您可以将原始模型用于特征提取或(非)条件图像生成。有关ImageGPT各个变种的详细信息,请参阅 model hub 。
以下是如何在PyTorch中使用此模型进行非条件图像生成的示例:
from transformers import ImageGPTImageProcessor, ImageGPTForCausalImageModeling
import torch
import matplotlib.pyplot as plt
import numpy as np
processor = ImageGPTImageProcessor.from_pretrained('openai/imagegpt-medium')
model = ImageGPTForCausalImageModeling.from_pretrained('openai/imagegpt-medium')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# unconditional generation of 8 images
batch_size = 8
context = torch.full((batch_size, 1), model.config.vocab_size - 1) #initialize with SOS token
context = torch.tensor(context).to(device)
output = model.generate(pixel_values=context, max_length=model.config.n_positions + 1, temperature=1.0, do_sample=True, top_k=40)
clusters = processor.clusters
n_px = processor.size
samples = output[:,1:].cpu().detach().numpy()
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
f, axes = plt.subplots(1, batch_size, dpi=300)
for img, ax in zip(samples_img, axes):
ax.axis('off')
ax.imshow(img)
ImageGPT模型在14百万张图像和21000个类别的数据集 ImageNet-21k 上进行了预训练。
首先将图像调整/重新缩放到相同的分辨率(32x32),并在RGB通道上进行归一化。然后进行颜色聚类。这意味着每个像素被转换为512个可能的聚类值之一。这样,最终得到的是一个32x32 = 1024个像素值的序列,而不是32x32x3 = 3072个像素值,对于基于Transformer的模型来说这是过大的。
训练细节可在论文v2的第3.4节中找到。
有关几个图像分类基准测试的评估结果,请参阅原论文。
@InProceedings{pmlr-v119-chen20s,
title = {Generative Pretraining From Pixels},
author = {Chen, Mark and Radford, Alec and Child, Rewon and Wu, Jeffrey and Jun, Heewoo and Luan, David and Sutskever, Ilya},
booktitle = {Proceedings of the 37th International Conference on Machine Learning},
pages = {1691--1703},
year = {2020},
editor = {III, Hal Daumé and Singh, Aarti},
volume = {119},
series = {Proceedings of Machine Learning Research},
month = {13--18 Jul},
publisher = {PMLR},
pdf = {http://proceedings.mlr.press/v119/chen20s/chen20s.pdf},
url = {https://proceedings.mlr.press/v119/chen20s.html
}
@inproceedings{deng2009imagenet,
title={Imagenet: A large-scale hierarchical image database},
author={Deng, Jia and Dong, Wei and Socher, Richard and Li, Li-Jia and Li, Kai and Fei-Fei, Li},
booktitle={2009 IEEE conference on computer vision and pattern recognition},
pages={248--255},
year={2009},
organization={Ieee}
}