模型:
openai/imagegpt-large
ImageGPT(iGPT)模型在32x32分辨率下,通过ImageNet ILSVRC 2012(1400万张图像,21843个类别)进行预训练。它在Chen等人的论文「 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-large') model = ImageGPTForCausalImageModeling.from_pretrained('openai/imagegpt-large') 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模型是在包含1400万张图像和21k个类别的「 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} }