最近想用 clip-interrogator 來批次解析圖片
找到這個
https://github.com/pharmapsychotic/clip-interrogator
依照官方範例
from PIL import Image from clip_interrogator import Config, Interrogator image = Image.open(image_path).convert('RGB') ci = Interrogator(Config(clip_model_name="ViT-L-14/openai")) print(ci.interrogate(image))照理講這樣應該能用
但是執行下去就跑出
The size of tensor a (8) must match the size of tensor b (64) at non-singleton dimension 0這個錯誤
很明顯是模型的構造跟權重檔案對不上
但是這個配對應該都是固定的阿
沒理由出錯
然後我找到這篇文章
https://github.com/pharmapsychotic/clip-interrogator-ext/issues/59
就是說
transformers 版本要在 4.26.1 以下
我測試後發現可行
但是執行起來很慢
預測會在 flavor chain 這動作上卡很久
發現預測時候可以用
ci.interrogate_fast這樣會快很多
然後模型也不用太大就是
這邊貼上我的程式碼
from PIL import Image from clip_interrogator import Config, Interrogator import torch import csv import os print("torch.cuda.is_available() = ", torch.cuda.is_available()) inputDir = 'images/' csv_file_path = 'data.csv' ci = Interrogator(Config( clip_model_name = "ViT-L-14/openai", ) ) with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file: writer = csv.writer(file) for root, dirs, files in os.walk(inputDir): for imgF in files: image = Image.open(root + imgF).convert('RGB') # imgDesc = ci.interrogate(image) imgDesc = ci.interrogate_fast(image) writer.writerow([imgF, imgDesc])
就是讀取後寫入CSV
給大家參考囉
留言板
歡迎留下建議與分享!希望一起交流!感恩!