オムライスの備忘録

数学・統計学・機械学習・プログラミングに関することを記す

【深層学習】CLIP #実装編 #02

Index

CLIP とは

タスク

#実装編 #01 では、画像分類 / 物体認識の予測の実行を確認した.

yhayato1320.hatenablog.com

今回は、API 化を行う.

環境構築

API フレームワーク

公式のソースコードでは、PyTorch が利用されていたので、Torch Serve を使ってみる.

Docker での環境構築

Docker 環境での構築を考える.

Torch Serve は、Docker Image 作成手順を公開している.

CLIP の install

この shell script では、Docker File から Docker Image を作成している.

そこで、Docker File に 以下のように CLIP をインストールする命令を追加して、build_image.sh を実行する.

RUN python -m pip install ftfy regex tqdm
RUN python -m pip install git+https://github.com/openai/CLIP.git



git も必要なので、install しておく.



./build_image.sh -b v0.5.0



以下、修正済みのDocker File.



ライブラリ

ライブラリの確認.

clip                 1.0
torch                1.11.0+cpu
torch-model-archiver 0.5.3
torchserve           0.5.3
torchtext            0.12.0
torchvision          0.12.0+cpu

実装

docker run --rm -it -v $(pwd):/home/handler -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 7070:7070 -p 7071:7071 pytorch/torchserve:latest-cpu /bin/bash

handler の作成

mar file の作成

torch-model-archiver \
--model-name ClipClassification \
--version 1.0 \
--handler ./Clip_classification_handler.py

モデルの登録

mv ClipClassification.mar /home/model-server/model-store/
cd /home/model-server/

torchserve \
--start \
--model-store model-store \
--models my_tc=ClipClassification.mar \
--ncs

request

curl -X POST -H "Content-Type: application/json" -d '{"tags": ["dog", "cat"], "image" : "xxxxxx"}' http://127.0.0.1:8080/predictions/my_tc