clip-vit-base-patch32
clip-vit-base-patch32是OpenAI推出的CLIP模型ViT架构变体,可实现零样本图像分类,匹配图像与文本的语义相似度,目前定位为研究用途。
这个项目值得继续研究吗?
clip-vit-base-patch32是OpenAI推出的CLIP模型ViT架构变体,可实现零样本图像分类,匹配图像与文本的语义相似度,目前定位为研究用途。
- 解决什么问题
- 传统图像分类需要为每个细分场景标注大量训练样本,更换分类类目就要重新训练模型,跨模态图文匹配的需求也需要从零搭建相关能力,整体研发成本高、落地周期长。
- 适合什么团队
- 适合计算机视觉领域的AI研究团队,以及需要开展零样本图像分类、图文匹配相关功能预研的技术团队使用。
- 使用前注意
- 未明确标注许可证,当前不支持任何商用或非商用部署场景,仅支持英文文本输入,存在种族、性别相关的偏见风险,禁止用于监控、人脸识别类场景。
本页用于缩短初步筛选时间,不构成技术、采购或法律结论。 正式使用前请在真实业务数据上验证,并以官方说明与许可证为准。
从官方资料看清能力、部署与采用边界
以下内容依据项目公开 README 或模型卡翻译整理,代码、命令和产品名保持原样。
1. 项目定位
clip-vit-base-patch32是OpenAI在2021年1月发布的CLIP(Contrastive Language-Image Pre-training,对比语言-图像预训练模型,可同时处理图像和文本信息,匹配二者的语义相似度)模型变体,采用ViT-B/32视觉Transformer作为图像编码器,掩码自注意力Transformer作为文本编码器,通过对比损失训练优化图文对的相似度匹配效果。该模型最初的研发目标是研究计算机视觉任务的鲁棒性,以及模型在零样本图像分类任务上的泛化能力,并非为直接落地部署设计。
2. 核心能力
该模型的核心能力分为两类:
- 图文语义相似度匹配:输入单张图像和任意数量的文本描述,可输出该图像与每条文本的匹配得分,进一步转换为对应概率;
- 零样本图像分类(Zero-Shot Image Classification,指无需针对特定分类任务标注样本、单独训练模型,直接输入自定义文本类目即可完成图像分类):不需要为目标分类场景准备标注数据集训练,直接给定自定义的文本分类标签,即可实现图像的自动分类。
3. 典型使用方式
该模型可通过Hugging Face的Transformers库快速调用,示例代码如下:
from PIL import Image
import requests
from transformers import CLIPProcessor, CLIPModel
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # 图文相似度得分
probs = logits_per_image.softmax(dim=1) # 转换为标签匹配概率上述代码实现了输入一张图片,判断图片是猫还是狗的分类功能,仅需修改text参数内的标签内容即可切换分类场景。
4. 使用范围与边界
该模型的预期使用人群为AI研究人员,预期用途为研究计算机视觉模型的鲁棒性、泛化能力、偏见与局限性,以及探索零样本图像分类相关的技术方向。
以下场景属于禁止/不推荐使用范围:
- 任何商用或非商用的落地部署场景均不支持,即使是受限环境下的图像搜索场景,也仅在针对固定分类类目完成充分的域内测试后才可尝试;
- 严禁用于监控、人脸识别相关的场景,该类场景缺乏统一的测试规范与公平性校验标准,不适合用该模型实现;
- 仅支持英文文本输入,不支持中文等其他语言的文本标签匹配。
5. 性能与局限性
性能表现
该模型已在Food101、CIFAR10、CIFAR100、ImageNet等数十个主流计算机视觉基准数据集上完成测试,覆盖细粒度分类、OCR、动作识别、纹理识别等多个任务场景。在人种性别分类任务上,所有人种的准确率均超过96%,种族分类平均准确率约93%,年龄分类平均准确率约63%。
局限性
- 细粒度分类、物体计数类任务的表现较差;
- 训练数据主要来自公开互联网资源,偏向发达国家、年轻男性用户群体,存在明显的种族、性别相关偏见,不同人群的分类准确率存在差异,甚至可能出现特定人群被错误归类为犯罪相关、非人类动物类目的情况;
- 当前主流的线性探针评估方式可能低估模型的实际性能;
- 模型的训练数据集不会公开发布,无法基于原始训练数据开展进一步的调优训练。
6. 采用建议
该项目未标注明确的许可证,商用授权风险不明确,仅适合用于研究或技术预研场景,不建议直接用于任何生产环境部署。如果需要开展相关预研,需先针对自身业务的分类类目完成充分的测试,验证准确率与公平性问题后再使用,严禁用于监控、人脸识别等违规场景。
官方资料与来源
- transformers
- pytorch
- tf
- jax
- clip
- zero-shot-image-classification
- vision
- endpoints_compatible
核对上游原始说明节选
任务类型:zero-shot-image-classification
Model Card: CLIP
Disclaimer: The model card is taken and modified from the official CLIP repository, it can be found here.
Model Details
The CLIP model was developed by researchers at OpenAI to learn about what contributes to robustness in computer vision tasks. The model was also developed to test the ability of models to generalize to arbitrary image classification tasks in a zero-shot manner. It was not developed for general model deployment - to deploy models like CLIP, researchers will first need to carefully study their capabilities in relation to the specific context they’re being deployed within.
Model Date
January 2021
Model Type
The model uses a ViT-B/32 Transformer architecture as an image encoder and uses a masked self-attention Transformer as a text encoder. These encoders are trained to maximize the similarity of (image, text) pairs via a contrastive loss.
The original implementation had two variants: one using a ResNet image encoder and the other using a Vision Transformer. This repository has the variant with the Vision Transformer.
Documents
- Blog Post
- CLIP Paper
Use with Transformers
from PIL import Image
import requests
from transformers import CLIPProcessor, CLIPModel
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilitiesModel Use
Intended Use
The model is intended as a research output for research communities. We hope that this model will enable researchers to better understand and explore zero-shot, arbitrary image classification. We also hope it can be used for interdisciplinary studies of the potential impact of such models - the CLIP paper includes a discussion of potential downstream impacts to provide an example for this sort of analysis.
Primary intended uses
The primary intended users of these models are AI researchers.
We primarily imagine the model will be used by researchers to better understand robustness, generalization, and other capabilities, biases, and constraints of computer vision models.
Out-of-Scope Use Cases
Any deployed use case of the model - whether commercial or not - is currently out of scope. Non-deployed use cases such as image search in a constrained environment, are also not recommended unless there is thorough in-domain testing of the model with a specific, fixed class taxonomy. This is because our safety assessment demonstrated a high need for task specific testing especially given the variability of CLIP’s performance with different class taxonomies. This makes untested and unconstrained deployment of the model in any use case currently potentially harmful.
Certain use cases which would fall under the domain of surveillance and facial recognition are always out-of-scope regardless of performance of the model. This is because the use of artificial intelligence for tasks such as these can be premature currently given the lack of testing norms and checks to ensure its fair use.
Since the model has not been purposefully trained in or evaluated on any languages other than English, its use should be limited to English language use cases.
Data
The model was trained on publicly available image-caption data. This was done through a combination of crawling a handful of websites and using commonly-used pre-existing image datasets such as YFCC100M. A large portion of the data comes from our crawling of the internet. This means that the data is more representative of people and societies most connected to the internet which tend to skew towards more developed nations, and younger, male users.
Data Mission Statement
Our goal with building this dataset was to test out robustness and generalizability in computer vision tasks. As a result, the focus was on gathering large quantities of data from different publicly-available internet data sources. The data was gathered in a mostly non-interventionist manner. However, we only crawled websites that had policies against excessively violent and adult images and allowed us to filter out such content. We do not intend for this dataset to be used as the basis for any commercial or deployed model and will not be releasing the dataset.
Performance and Limitations
Performance
We have evaluated the performance of CLIP on a wide range of benchmarks across a variety of computer vision datasets such as OCR to texture recognition to fine-grained classification. The paper describes model performance on the following datasets:
- Food101
- CIFAR10
- CIFAR100
- Birdsnap
- SUN397
- Stanford Cars
- FGVC Aircraft
- VOC2007
- DTD
- Oxford-IIIT Pet dataset
- Caltech101
- Flowers102
- MNIST
- SVHN
- IIIT5K
- Hateful Memes
Limitations
CLIP and our analysis of it have a number of limitations. CLIP currently struggles with respect to certain tasks such as fine grained classification and counting objects. CLIP also poses issues with regards to fairness and bias which we discuss in the paper and briefly in the next section. Additionally, our approach to testing CLIP also has an important limitation- in many cases we have used linear probes to evaluate the performance of CLIP and there is evidence suggesting that linear probes can underestimate model performance.
Bias and Fairness
We find that the performance of CLIP - and the specific biases it exhibits - can depend significantly on class design and the choices one makes for categories to include and exclude. We tested the risk of certain kinds of denigration with CLIP by classifying images of people from Fairface into crime-related and non-human animal categories. We found significant disparities with respect to race and gender. Additionally, we found that these disparities could shift based on how the classes were constructed. (Details captured in the Broader Impacts Section in the paper).
We also tested the performance of CLIP on gender, race and age classification using the Fairface dataset (We default to using race categories as they are constructed in the Fairface dataset.) in order to assess quality of performance across different demographics. We found accuracy >96% across all races for gender classification with ‘Middle Eastern’ having the highest accuracy (98.4%) and ‘White’ having the lowest (96.5%). Additionally, CLIP averaged 93% for racial classification and 63% for age classification. Our use of evaluations to test for gender, race and age classification as well as denigration harms is simply to evaluate performance of the model across people and surface potential risks and not to demonstrate an endorsement/enthusiasm for such tasks.
Feedback
Where to send questions or comments about the model
Please use this Google Form