🚀 CLIP-ViT-Base-Patch16とTransformers.js
このプロジェクトは、openai/clip-vit-base-patch16 のモデルをONNX形式に変換し、Transformers.jsと互換性を持たせることを目的としています。
🚀 クイックスタート
Transformers.jsを使用するには、まず Transformers.js JavaScriptライブラリを NPM からインストールする必要があります。
npm i @xenova/transformers
💻 使用例
基本的な使用法
pipeline
APIを使用してゼロショット画像分類を行う例です。
const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch16');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url, ['tiger', 'horse', 'dog']);
高度な使用法
CLIPModel
を使用したゼロショット画像分類
import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@xenova/transformers';
const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16');
const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16');
const model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16');
const texts = ['a photo of a car', 'a photo of a football match'];
const text_inputs = tokenizer(texts, { padding: true, truncation: true });
const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
const image_inputs = await processor(image);
const output = await model({ ...text_inputs, ...image_inputs });
CLIPTextModelWithProjection
を使用したテキスト埋め込みの計算
import { AutoTokenizer, CLIPTextModelWithProjection } from '@xenova/transformers';
const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16');
const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16');
const texts = ['a photo of a car', 'a photo of a football match'];
const text_inputs = tokenizer(texts, { padding: true, truncation: true });
const { text_embeds } = await text_model(text_inputs);
CLIPVisionModelWithProjection
を使用したビジョン埋め込みの計算
import { AutoProcessor, CLIPVisionModelWithProjection, RawImage } from '@xenova/transformers';
const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16');
const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16');
const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
const image_inputs = await processor(image);
const { image_embeds } = await vision_model(image_inputs);
📚 ドキュメント
⚠️ 重要提示
ONNX重み用に別のリポジトリを用意するのは、WebMLが普及するまでの一時的な解決策です。モデルをWeb対応にする場合は、 🤗 Optimum を使用してONNXに変換し、このリポジトリのように構成することをお勧めします(ONNX重みは onnx
というサブフォルダに配置します)。