OFA-Sys / Chinese-CLIP

Chinese version of CLIP which achieves Chinese cross-modal retrieval and representation generation.
MIT License
4.59k stars 474 forks source link

使用下列代码,如何加载自己训练的 'ViT-L-14'模型指定路径的.pt文件。(比如加载Epoch为2的./data/model/epoch2.pt模型) #345

Open laitianan opened 3 months ago

laitianan commented 3 months ago

import torch from PIL import Image

import cn_clip.clip as clip from cn_clip.clip import load_from_name, available_models print("Available models:", available_models())

Available models: ['ViT-B-16', 'ViT-L-14', 'ViT-L-14-336', 'ViT-H-14', 'RN50']

device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = load_from_name("ViT-B-16", device=device, download_root='./') model.eval() image = preprocess(Image.open("examples/pokemon.jpeg")).unsqueeze(0).to(device) text = clip.tokenize(["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]).to(device)

with torch.no_grad(): image_features = model.encode_image(image) text_features = model.encode_text(text)

对特征进行归一化,请使用归一化后的图文特征用于下游任务

image_features /= image_features.norm(dim=-1, keepdim=True) 
text_features /= text_features.norm(dim=-1, keepdim=True)    

logits_per_image, logits_per_text = model.get_similarity(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()

print("Label probs:", probs) # [[1.268734e-03 5.436878e-02 6.795761e-04 9.436829e-01]]

ZYF-fmg commented 2 months ago

you can try the code next. model, preprocess = clip.from_pretrained("model_path")

xiaoAugenstern commented 2 months ago

you can try the code next. model, preprocess = clip.from_pretrained("model_path")

model, preprocess = clip.from_pretrained(model_path)

AttributeError: module 'cn_clip.clip' has no attribute 'from_pretrained'

ZYF-fmg commented 2 months ago

you can try the code next. model, preprocess = clip.from_pretrained("model_path")

model, preprocess = clip.from_pretrained(model_path)

AttributeError: module 'cn_clip.clip' has no attribute 'from_pretrained'

image try clip.load() function

GameHab commented 2 months ago

提前设置好你模型文件的位置,然后用下面的代码:

clip_path  = ''/data/model/epoch2.pt' 
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = load_from_name(clip_path, device=device, vision_model_name="ViT-B-16", text_model_name="RoBERTa-wwm-ext-base-chinese", input_resolution=224)
xiaoAugenstern commented 2 months ago

提前设置好你模型文件的位置,然后用下面的代码:

clip_path  = ''/data/model/epoch2.pt' 
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = load_from_name(clip_path, device=device, vision_model_name="ViT-B-16", text_model_name="RoBERTa-wwm-ext-base-chinese", input_resolution=224)
model, preprocess = load_from_name(clip_path, device=device, vision_model_name="ViT-B-16",

TypeError: load_from_name() got an unexpected keyword argument 'vision_model_name' 好像不太行

GameHab commented 2 months ago

提前设置好你模型文件的位置,然后用下面的代码:

clip_path  = ''/data/model/epoch2.pt' 
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = load_from_name(clip_path, device=device, vision_model_name="ViT-B-16", text_model_name="RoBERTa-wwm-ext-base-chinese", input_resolution=224)
model, preprocess = load_from_name(clip_path, device=device, vision_model_name="ViT-B-16",

TypeError: load_from_name() got an unexpected keyword argument 'vision_model_name' 好像不太行

没有这个vision_model_name?你导入cn_clip了吗

import cn_clip.clip as clip
from cn_clip.clip import load_from_name, available_models