Distilbert Base Turkish Cased Clip
基于dbmdz/distilbert-base-turkish-cased微调的土耳其语文本编码器,用于与CLIP的ViT-B/32图像编码器配合使用
下载量 2,354
发布时间 : 3/2/2022
模型简介
该模型是一个针对土耳其语优化的文本编码器,专门设计用于与CLIP模型的图像编码器配合,实现跨模态的文本-图像匹配任务。
模型特点
土耳其语优化
专门针对土耳其语文本进行微调优化
CLIP兼容
设计用于与CLIP的ViT-B/32图像编码器配合使用
轻量级架构
基于DistilBERT,在保持性能的同时减少模型大小
模型能力
土耳其语文本编码
跨模态文本-图像匹配
多模态表示学习
使用案例
跨模态检索
土耳其语图像搜索
使用土耳其语文本查询搜索相关图像
内容推荐
土耳其语内容推荐
基于文本描述推荐相关视觉内容
🚀 土耳其语文本编码器模型
本项目是一个微调后的模型,基于 dbmdz/distilbert-base-turkish-cased 进行微调,可作为土耳其语文本编码器,与 CLIP 的 ViT - B/32
图像编码器配合使用。
🚀 快速开始
本模型是 dbmdz/distilbert-base-turkish-cased 的微调版本,可作为土耳其语的文本编码器,与 CLIP 的 ViT-B/32
图像编码器配合使用。它需要与 [我在 GitHub 上的配套仓库] 中的 clip_head.h5
一起使用。前往该仓库可获取完整的工作示例,以下是一个简单的使用示例:
from transformers import AutoTokenizer, TFAutoModel
import tensorflow as tf
import numpy as np
from PIL import Image
import torch
import clip
model_name = "mys/distilbert-base-turkish-cased-clip"
base_model = TFAutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
head_model = tf.keras.models.load_model("./clip_head.h5")
def encode_text(base_model, tokenizer, head_model, texts):
tokens = tokenizer(texts, padding=True, return_tensors='tf')
embs = base_model(**tokens)[0]
attention_masks = tf.cast(tokens['attention_mask'], tf.float32)
sample_length = tf.reduce_sum(attention_masks, axis=-1, keepdims=True)
masked_embs = embs * tf.expand_dims(attention_masks, axis=-1)
base_embs = tf.reduce_sum(masked_embs, axis=1) / tf.cast(sample_length, tf.float32)
clip_embs = head_model(base_embs)
clip_embs /= tf.norm(clip_embs, axis=-1, keepdims=True)
return clip_embs
demo_images = {
"bilgisayarda çalışan bir insan": "myspc.jpeg",
"sahilde bir insan ve bir heykel": "mysdk.jpeg"
}
clip_model, preprocess = clip.load("ViT-B/32")
images = {key: Image.open(f"images/{value}") for key, value in demo_images.items()}
img_inputs = torch.stack([preprocess(image).to('cpu') for image in images.values()])
with torch.no_grad():
image_embs = clip_model.encode_image(img_inputs).float().to('cpu')
image_embs /= image_embs.norm(dim=-1, keepdim=True)
image_embs = image_embs.detach().numpy()
text_embs = encode_text(base_model, tokenizer, head_model, list(images.keys())).numpy()
similarities = image_embs @ text_embs.T
logits = tf.nn.softmax(tf.convert_to_tensor(similarities)).numpy()
idxs = np.argmax(logits, axis=-1).tolist()
for i, (key, value) in enumerate(demo_images.items()):
print("path: ", value, "true label: ", key, "prediction: ", list(demo_images.keys())[idxs[i]], "score: ", logits[i, idxs[i]])
上述代码片段中引用的示例图像可以在 GitHub 仓库的 images
目录下找到。
💻 使用示例
基础用法
from transformers import AutoTokenizer, TFAutoModel
import tensorflow as tf
import numpy as np
from PIL import Image
import torch
import clip
model_name = "mys/distilbert-base-turkish-cased-clip"
base_model = TFAutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
head_model = tf.keras.models.load_model("./clip_head.h5")
def encode_text(base_model, tokenizer, head_model, texts):
tokens = tokenizer(texts, padding=True, return_tensors='tf')
embs = base_model(**tokens)[0]
attention_masks = tf.cast(tokens['attention_mask'], tf.float32)
sample_length = tf.reduce_sum(attention_masks, axis=-1, keepdims=True)
masked_embs = embs * tf.expand_dims(attention_masks, axis=-1)
base_embs = tf.reduce_sum(masked_embs, axis=1) / tf.cast(sample_length, tf.float32)
clip_embs = head_model(base_embs)
clip_embs /= tf.norm(clip_embs, axis=-1, keepdims=True)
return clip_embs
demo_images = {
"bilgisayarda çalışan bir insan": "myspc.jpeg",
"sahilde bir insan ve bir heykel": "mysdk.jpeg"
}
clip_model, preprocess = clip.load("ViT-B/32")
images = {key: Image.open(f"images/{value}") for key, value in demo_images.items()}
img_inputs = torch.stack([preprocess(image).to('cpu') for image in images.values()])
with torch.no_grad():
image_embs = clip_model.encode_image(img_inputs).float().to('cpu')
image_embs /= image_embs.norm(dim=-1, keepdim=True)
image_embs = image_embs.detach().numpy()
text_embs = encode_text(base_model, tokenizer, head_model, list(images.keys())).numpy()
similarities = image_embs @ text_embs.T
logits = tf.nn.softmax(tf.convert_to_tensor(similarities)).numpy()
idxs = np.argmax(logits, axis=-1).tolist()
for i, (key, value) in enumerate(demo_images.items()):
print("path: ", value, "true label: ", key, "prediction: ", list(demo_images.keys())[idxs[i]], "score: ", logits[i, idxs[i]])
🔧 技术细节
encode_text()
函数聚合了 Distilbert 模型输出的每个标记的隐藏状态,为每个序列生成一个单一向量。然后,clip_head.h5
模型通过一个全连接层将该向量投影到与 CLIP 的文本编码器相同的向量空间中。首先,冻结所有 Distilbert 层,并对头部全连接层进行几个 epoch 的训练。然后,解除冻结,将全连接层与 Distilbert 层一起再训练几个 epoch。我通过将 COCO 字幕机器翻译成土耳其语来创建数据集。在训练期间,使用原始 CLIP 文本编码器输出的英语字幕的向量表示作为目标值,并最小化这些向量与 clip_head.h5
输出之间的均方误差(MSE)。
📚 详细文档
数据集
数据集和训练笔记本将很快发布。
📄 致谢
本工作得到了 Google 的支持,他们提供了 Google Cloud 信用额度。感谢 Google 对开源项目的支持!🎉
Clip Vit Large Patch14 336
基于Vision Transformer架构的大规模视觉语言预训练模型,支持图像与文本的跨模态理解
文本生成图像
Transformers

C
openai
5.9M
241
Fashion Clip
MIT
FashionCLIP是基于CLIP开发的视觉语言模型,专门针对时尚领域进行微调,能够生成通用产品表征。
文本生成图像
Transformers 英语

F
patrickjohncyh
3.8M
222
Gemma 3 1b It
Gemma 3是Google推出的轻量级先进开放模型系列,基于与Gemini模型相同的研究和技术构建。该模型是多模态模型,能够处理文本和图像输入并生成文本输出。
文本生成图像
Transformers

G
google
2.1M
347
Blip Vqa Base
Bsd-3-clause
BLIP是一个统一的视觉语言预训练框架,擅长视觉问答任务,通过语言-图像联合训练实现多模态理解与生成能力
文本生成图像
Transformers

B
Salesforce
1.9M
154
CLIP ViT H 14 Laion2b S32b B79k
MIT
基于OpenCLIP框架在LAION-2B英文数据集上训练的视觉-语言模型,支持零样本图像分类和跨模态检索任务
文本生成图像
Safetensors
C
laion
1.8M
368
CLIP ViT B 32 Laion2b S34b B79k
MIT
基于OpenCLIP框架在LAION-2B英语子集上训练的视觉-语言模型,支持零样本图像分类和跨模态检索
文本生成图像
Safetensors
C
laion
1.1M
112
Pickscore V1
PickScore v1 是一个针对文本生成图像的评分函数,可用于预测人类偏好、评估模型性能和图像排序等任务。
文本生成图像
Transformers

P
yuvalkirstain
1.1M
44
Owlv2 Base Patch16 Ensemble
Apache-2.0
OWLv2是一种零样本文本条件目标检测模型,可通过文本查询在图像中定位对象。
文本生成图像
Transformers

O
google
932.80k
99
Llama 3.2 11B Vision Instruct
Llama 3.2 是 Meta 发布的多语言多模态大型语言模型,支持图像文本到文本的转换任务,具备强大的跨模态理解能力。
文本生成图像
Transformers 支持多种语言

L
meta-llama
784.19k
1,424
Owlvit Base Patch32
Apache-2.0
OWL-ViT是一个零样本文本条件目标检测模型,可以通过文本查询搜索图像中的对象,无需特定类别的训练数据。
文本生成图像
Transformers

O
google
764.95k
129
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98