1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Llama 3.2 visionで異常検知(画像)できるの?

Last updated at Posted at 2024-10-03

きっかけ

Llama3.2の画像に対応したモデルがHuggingFaceに上がってきたのは知っていた。Xって情報早くて素晴らしい。
一方、某マナビDXクエストで異常検知のタスクにチャレンジ中。でも普通に画像分類やら物体検出しても面白くないので「使えんじゃね?」と思って試してみた結果です。

目次

  • 環境
  • Llama 3.2 visionとは
  • 何やるの?
  • 結果は?
  • どうやって使うの?
  • ソースコード
  • 終わりに

環境

PC: MacBookAir M1 16GB 2020
Python==3.10.8
環境構築: venv

Llama 3.2 visionとは

詳しくはここ

機械翻訳してみると・・・

モデル情報
Llama 3.2-Vision マルチモーダル大規模言語モデル (LLM) コレクションは、11B および 90B サイズ (テキスト + 画像入力 / テキスト出力) の事前トレーニング済みで命令調整済みの画像推論生成モデルのコレクションです。Llama 3.2-Vision 命令調整済みモデルは、視覚認識、画像推論、キャプション作成、および画像に関する一般的な質問への回答に最適化されています。このモデルは、一般的な業界ベンチマークで、利用可能なオープン ソースおよびクローズド マルチモーダル モデルの多くよりも優れたパフォーマンスを発揮します

画像に関する一般的な質問への回答に最適化されている
「一般的な」が気になるけど、画像に対して何かしらの理解をしてくれるのかな。

追記:
Llama-3.2-11B-Vision-Instructも試してみました

何やるの?

今回の課題はハンダの異常を見つけるというもの。
参加規約上、そのまんまのデータがお見せできないのですが以下リンクのようなハンダを見つけたいのです。

結果は?

output:Llama-3.2-11B-Vision
The image is not meant to be used for training. It is a test image. <OCR/> 0.65mm 0. 65mm 8 8 5 6 

いや、OCRはお願いしてない。
ん?OCRできるのか!w

output:Llama-3.2-11B-Vision-Instruct
<|begin_of_text|>
<|image|><|begin_of_text|>You are a solder visual inspector. 
Abnormal solder such as potato solder, square solder, and bridge solder can be accurately identified. 
Please output only "1" if there is an abnormality in the solder in the following image, or 0 if it is normal."

Answer: 0

Explanation: 
The solder in the image appears to be normal, with no visible signs of abnormalities such as potato solder, square solder, or bridge solder. The solder joints are properly formed and do not show any signs of defects or irregularities. Therefore, the answer

お、なんとなくできてるけど・・・いやその画像は「1」って出力して欲しいんだな。

どうやって使うの?

まぁダメだったんだけど、使い方はメモしておこう。
使い方はLlama-3.2-11B-VisionLlama-3.2-11B-Vision-Instructionもほぼ同じ

Llama-3.2-11B-Visionで説明。
まずはいつものHuggingFaceからllama3.2 visionのページにいこう。

最初の方にライセンスやら、使用申請があるのでよく読んでからポチッと申請。

で、ハマったのはここ

スクリーンショット 2024-10-03 8.53.40.png

切り取り画像の下の方にあるRepositories permissionsのところで使いたいllama3.2-11B-Visionを選択しておくことをお忘れなく。僕は忘れてたのでアクセストークンの修正を行いました。

ソースコード

基本的にHugggingFaceに載っているコードのパスとプロンプトだけ改変しただけ。
venvで環境構築して、pipでライブラリインストール

requirements.txt
transformers[torch]
pillow
ipywidgets

transfromersはバージョン指定があるので確認してね。僕は4.50.1が入りました。

Use with transformers
Starting with transformers >= 4.45.0 onward, you can run inference to generate text based on an image and a starting prompt you supply.

ライブラリのインポート

import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
from huggingface_hub import notebook_login

HuggingFaceにログインしてトークンを入力

notebook_login()

モデルをダウンロードして

model_id = "meta-llama/Llama-3.2-11B-Vision"

model = MllamaForConditionalGeneration.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)

processor = AutoProcessor.from_pretrained(model_id)
img_file = "./test/001.png"
image = Image.open(img_file)

prompt = '''
<|image|><|begin_of_text|>IYou are a solder visual inspector. 
Abnormal solder such as potato solder, square solder, and bridge solder can be accurately identified. 
Please output only "1" if there is an abnormality in the solder in the following image, or 0 if it is normal."
'''
inputs = processor(image, prompt, return_tensors="pt").to(model.device)

output = model.generate(**inputs, max_new_tokens=40)

出力

print(processor.decode(output[0]))

この結果が最初のやつ

output
The image is not meant to be used for training. It is a test image. <OCR/> 0.65mm 0. 65mm 8 8 5 6 

全然ダメだけど、楽しい。w

終わりに

今回はマナビDXクエストをきっかけにこんなことをやってみました。このようなアイデアは何かに取り組むと簡単に生まれますね。
実はGPT−4o-miniのAPIでもやってみたんですが、ある程度推論できてましたよ。

去年も同じ課題に取り組みましたが、物体検出モデルを使って96%くらいのF1-Scoreでしたが、GPT-4o-miniだと75%くらい。
まだまだ、物体検出モデルや異常検知モデル、画像分類モデルの方がはるかに精度がいいですね。
そもそも言語モデルだし。

ではまた

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?