【学习笔记】CLIP代码解读

CLIP

此文章为CLIP模型github网页中的colab代码解读,包含CLIP的主要思想。

CLIP保存预训练模型

clip.available_models()

['RN50', 'RN101', 'RN50x4', 'RN50x16', 'RN50x64', 'ViT-B/32', 'ViT-B/16', 'ViT-L/14', 'ViT-L/14@336px']
数据预处理

图片——resize输入图片,并进行裁剪,totensor,归一化

Compose(
	Resize(size=224, interpolation=bicubic, max_size=None, antialias=warn)    			
	CenterCrop(size=(224, 224))
	<function _convert_image_to_rgb at 0x7c4f5656cc10>
	ToTensor()
	Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
)

文字——编码,长度为77

clip.tokenize(“Hello World!”)

tensor([[49406, 3306, 1002, 256, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=torch.int32)
输入数据集

图像文本对——一个图片有着对应的文本描述
在这里插入图片描述

网络架构

图片和文本编码

image_input = torch.tensor(np.stack(images)).cuda() # stack把原本的list转变为矩阵
text_tokens = clip.tokenize(["This is " + desc for desc in texts]).cuda() # 形成一个句子

image_input——[8, 3,244,244]

text_tokens——[8, 77]

预训练编码器编码

with torch.no_grad():
  image_features = model.encode_image(image_input).float()
  text_features = model.encode_text(text_tokens).float()

两个特征的shape均为[8, 512]

计算相似度

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

归一化:将每一行转化为单位向量

将两个矩阵相乘计算相似度[8, 512] @ @ @[512, 8]->[8,8]

CLIP Zero-Shot图片分类

数据——cifar100

进行100分类,text_tokens——[100,77]

text_descriptions = [f"This is a photo of a {label}" for label in cifar100.classes]
text_tokens = clip.tokenize(text_descriptions).cuda()

图片分类

计算100分类的text_features,与原先的image_features计算相似度
其中,100是一个缩放因子,若将100除去,我们发现得到的相似度分数比较相近,无法很好将正样本与负样本隔开。在CLIP源代码中其为一个网络参数。

with torch.no_grad():
  text_features = model.encode_text(text_tokens).float()
  text_features /= text_features.norm(dim=-1, keepdim=True)

text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1) #100是一个缩放因子
top_probs, top_labels = text_probs.cpu().topk(5, dim=-1)# 选出前五

在这里插入图片描述

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,感谢您对eclipse插件开发学习的关注!下面是一些学习笔记配套代码的例子。 1. 创建一个最简单的插件: ```java public class HelloWorldPlugin implements IStartup { @Override public void earlyStartup() { System.out.println("Hello, Eclipse Plugin Development!"); } } ``` 2. 在Eclipse菜单中添加一个新的命令: ```java public class MyCommandHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "My Command", "Hello, Eclipse Plugin Development!"); return null; } } ``` 3. 在编辑器右键菜单中添加一个新的动作: ```java public class MyEditorAction extends Action { public MyEditorAction() { setText("My Action"); } @Override public void run() { IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if (editor instanceof ITextEditor) { ITextEditor textEditor = (ITextEditor) editor; IDocumentProvider documentProvider = textEditor.getDocumentProvider(); IDocument document = documentProvider.getDocument(textEditor.getEditorInput()); selectCurrentWord(textEditor, document); } } private void selectCurrentWord(ITextEditor editor, IDocument document) { ISelectionProvider selectionProvider = editor.getSelectionProvider(); ITextSelection selection = (ITextSelection) selectionProvider.getSelection(); int offset = selection.getOffset(); try { int lineStartOffset = document.getLineOffset(selection.getStartLine()); int lineEndOffset = lineStartOffset + document.getLineLength(selection.getStartLine()); String lineText = document.get(lineStartOffset, lineEndOffset - lineStartOffset); int wordStartOffset = lineText.lastIndexOf(' ', offset - lineStartOffset) + 1; int wordEndOffset = lineText.indexOf(' ', offset - lineStartOffset); if (wordEndOffset < wordStartOffset) { // The word is the last or only word on the line wordEndOffset = lineEndOffset - lineStartOffset; } else { wordEndOffset += lineStartOffset; } selectionProvider.setSelection(new TextSelection(wordStartOffset, wordEndOffset - wordStartOffset)); } catch (BadLocationException e) { e.printStackTrace(); } } } ``` 此处仅提供了一些简单的示例代码,以帮助您开始使用eclipse插件开发。有关更详细的学习和开发指导,请参阅Eclipse官方文档和教程。祝您在插件开发的学习过程中取得成功!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值