论文链接: 2103.Learning Transferable Visual Models From Natural Language Supervision
项目官网: CLIP: Contrastive Language–Image Pre-training——Connecting Text and Images
代码:https://github.com/openai/CLIP
Contrastive
Language–Image Pre-training)基于对比学习的语言-图像预训练
)建立在零样本迁移(zero-shot transfer)、自然语言监督学习( natural language supervision, ) 和多模态学习方面的大量工作之上。CLIP是一个预训练模型,就像BERT、GPT、ViT等预训练模型一样。首先使用大量数据训练这些模型,然后训练好的模型就能实现,
输入一段文本(或者一张图像),输出文本(图像)的向量表示
。CLIP和BERT、GPT、ViT的区别在于,CLIP是多模态的,包含图像处理以及文本处理两个方面内容,而BERT、GPT是单文本模态的,ViT是单图像模态的。
————————————————
原文链接:https://blog.csdn.net/me_yundou/article/details/123033447
原始文本
(raw text )中学习关于图像的知识是一种很有前途的选择,它利用了(leverages)更广泛的监督来源(broader source of supervusion)。预测哪个标题(文字)与哪个图像相匹配
,是一种有效的和可伸缩的方法,从互联网收集的4亿(图像-文本对
(pairs))数据集学习SOTA图像表示。zero-shot
迁移到下游任务(downstrem tasks)不需要任何特定数据集的训练
Open AI团队通过收集4亿
(400 million)个文本-图像对
((image, text) pairs),以用来训练其提出的CLIP模型。文本-图像对的示例如下:
CLIP联合训练一个图像编码器和一个文本编码器来预测一批
(图像( T 1 , . . . T N T_1,...T_N T1,...TN)、文本( I 1 , . . . , I N I_1,...,I_N I1,...,IN))训练示例的正确配对。我们通过计算与 I i 与 T j I_i与T_j Ii与Tj的之间的余弦相似度
(cosine similarity) 用来度量相应的文本与图像之间的对应关系。余弦相似度越大.
pepper the aussie pup : 给澳大利亚小狗胡椒
自然语言提示
,使zero-shot transfer 到许多现有数据集。官方代码示例
结果是 “a diagram”
import torch
import clip
from PIL import Image
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
image = preprocess(Image.open("CLIP.png")).unsqueeze(0).to(device)
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)
with torch.no_grad():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
logits_per_image, logits_per_text = model(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
print("Label probs:", probs) # prints: [[0.9927937 0.00421068 0.00299572]]
# images in skimage to use and their textual descriptions
descriptions = {
"page": "a page of text about segmentation",
"chelsea": "a facial photo of a tabby cat",
"astronaut": "a portrait of an astronaut with the American flag",
"rocket": "a rocket standing on a launchpad",
"motorcycle_right": "a red motorcycle standing in a garage",
"camera": "a person looking at a camera on a tripod",
"horse": "a black-and-white silhouette of a horse",
"coffee": "a cup of coffee on a saucer"
}
.......................略去部分代码,见colab完整代码
for filename in [filename for filename in os.listdir(skimage.data_dir) if filename.endswith(".png") or filename.endswith(".jpg")]:
name = os.path.splitext(filename)[0]
if name not in descriptions:
continue
image = Image.open(os.path.join(skimage.data_dir, filename)).convert("RGB")
original_images.append(image)
images.append(preprocess(image))
texts.append(descriptions[name])
image_input = torch.tensor(np.stack(images)).cuda()
text_tokens = clip.tokenize(["This is " + desc for desc in texts]).cuda()
with torch.no_grad():
image_features = model.encode_image(image_input).float()
text_features = model.encode_text(text_tokens).float()
# Calculating cosine similarity
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
similarity = text_features.cpu().numpy() @ image_features.cpu().numpy().T
# 结果可视化
count = len(descriptions)
plt.figure(figsize=(20, 14))
plt.imshow(similarity, vmin=0.1, vmax=0.3)
# plt.colorbar()
plt.yticks(range(count), texts, fontsize=18)
plt.xticks([])
for i, image in enumerate(original_images):
plt.imshow(image, extent=(i - 0.5, i + 0.5, -1.6, -0.6), origin="lower")
for x in range(similarity.shape[1]):
for y in range(similarity.shape[0]):
plt.text(x, y, f"{similarity[y, x]:.2f}", ha="center", va="center", size=12)
for side in ["left", "top", "right", "bottom"]:
plt.gca().spines[side].set_visible(False)
plt.xlim([-0.5, count - 0.5])
plt.ylim([count + 0.5, -2])
plt.title("Cosine similarity between text and image features", size=20)