OFA-Sys / Chinese-CLIP

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

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

Open laitianan opened 4 weeks ago

laitianan commented 4 weeks 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 1 week ago

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

xiaoAugenstern commented 1 day 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 1 day 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