Chinese-CLIP icon indicating copy to clipboard operation
Chinese-CLIP copied to clipboard

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

Open laitianan opened this issue 1 year ago • 8 comments

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]]

laitianan avatar Aug 14 '24 03:08 laitianan

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

ZYF-fmg avatar Sep 04 '24 01:09 ZYF-fmg

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'

xiaoAugenstern avatar Sep 11 '24 08:09 xiaoAugenstern

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

ZYF-fmg avatar Sep 11 '24 09:09 ZYF-fmg

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

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)

GameHab avatar Sep 26 '24 07:09 GameHab

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

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' 好像不太行

xiaoAugenstern avatar Sep 26 '24 08:09 xiaoAugenstern

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

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

GameHab avatar Sep 26 '24 16:09 GameHab

请问你们有遇到 ModuleNotFoundError: No module named 'flash_attn.flash_attention' 报错么?请问你们是如何解决的?谢谢!

Charimanhua avatar Dec 05 '24 02:12 Charimanhua

请问你们有遇到 ModuleNotFoundError: No module named 'flash_attn.flash_attention' 报错么?请问你们是如何解决的?谢谢!

pip install ninja pip install flash_attn == 0.2.8

jeborit avatar Jan 03 '25 03:01 jeborit