CLIP モデルのエクスポート ONNX モデル

オープンナイクリップ

GitHub - openai/CLIP: CLIP (Contrastive Language-Image Pretraining)、画像から最も関連性の高いテキスト スニペットを予測します

import torch
import torch.nn as nn
import clip
from PIL import Image

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

model.float()
model.eval()

image = preprocess(Image.open("clip_dog.png")).unsqueeze(0).to(device)
text = clip.tokenize(["a dog", "a cat"]).to(device)

print("text:", text)

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

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

print("Label probs:", probs)  # prints: [[0.9927937  0.00421068 0.00299572]]

# export to ONNX


class ImgModelWrapper(nn.Module):
    def __init__(self):
        super(ImgModelWrapper, self).__init__()
        self.model = model

    def forward(self, image):
        image_features = model.encode_image(image)
        return image_features


class TxtModelWrapper(nn.Module):
    def __init__(self):
        super(TxtModelWrapper, self).__init__()
        self.model = model

    def forward(self, image):
        text_features = model.encode_text(text)
        return text_features


img_model = ImgModelWrapper()
txt_model = TxtModelWrapper()

torch.onnx.export(img_model,               # model being run
                  image,                         # model input (or a tuple for multiple inputs)
                  "openai_vit_img.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=12,          # the ONNX version to export the model to
                  do_constant_folding=False,  # whether to execute constant folding for optimization
                  input_names=['input'],   # the model's input names
                  output_names=['output'],  # the model's output names
                  dynamic_axes={'input': {0: 'batch'}})
torch.onnx.export(txt_model,               # model being run
                  text,                         # model input (or a tuple for multiple inputs)
                  "openai_vit_txt.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=12,          # the ONNX version to export the model to
                  do_constant_folding=False,  # whether to execute constant folding for optimization
                  input_names=['input'],   # the model's input names
                  output_names=['output'],  # the model's output names
                  dynamic_axes={'input': {0: 'batch'}})

中国語クリップは上記と同じ方法を使用します

GitHub - OFA-Sys/ Chinese-CLIP: 中国語のクロスモーダル検索と表現生成を実現する CLIP の中国語版。

CLIPモデルの研究の方向性としては、次のようなことになるのではないかと思います。異なる言語で記述された同じ文章から得られる意味特徴ベクトルは似ていますが、現在は各言語と対応する画像を使用してクリップのtxtとimgを学習させています。モデルと中間の特徴ベクトルは異なるため、異なる言語モデルで異なるモデルが使用されることになります。各言語が同様のセマンティック ベクトルを生成できる場合は、異なる txt エンコーダを置き換えて同じ img エンコーダを使用するだけで済みます。同様に、この CLIP モデルを安定拡散と組み合わせる場合、各言語に応じて安定拡散を再トレーニングする必要はなく、テキスト エンコーダーを再トレーニングする必要があります。

似ている:

マスターのFlagAI/README.md · FlagAI-Open/FlagAI · GitHub

ただし、この作業は少数の多言語モデルのセマンティクスを統合するだけであり、安定拡散はまだ再トレーニングする必要があります。私の理想的な状況は、安定拡散を再トレーニングする必要はないが、どの言語も整列された TXT エンコーダーを再トレーニングするだけでよいということです。 。

おすすめ

転載: blog.csdn.net/u013701860/article/details/129764269