1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

metaのPixioの使い方編

Last updated at Posted at 2025-12-25

はじめに

こんにちは、しゅんです。今回はMetaのPixioを「公式サンプルに沿って動かす」記事です。
前編はこちら:

PixioMAEを拡張した自己教師ありビジョンモデルで、
patch token + class token を含む特徴が簡単に取り出せます。
詳しいことは前編と論文へ

論文

unnamed.png

モデル一覧(Hugging Face)

一覧:

  • facebook/pixio-vitb16 (85.9M)
  • facebook/pixio-vitl16 (0.3B)
  • facebook/pixio-vith16 (0.6B)
  • facebook/pixio-vit1b16
  • facebook/pixio-vit5b16 (5B)

今回は軽めで試しやすい facebook/pixio-vitl16 を使用します。
モデルページ:

セットアップ

meta/pixio で作業する前提。

git clone https://github.com/facebookresearch/pixio.git
cd pixio
python3.10 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

追加で必要になりやすいもの

公式requirementsに transformers が入っていないため、環境によっては自分で入れます。

pip install git+https://github.com/huggingface/transformers.git
pip install pillow requests

Hugging Face ログイン(必要な場合)

huggingface-cli login

実行(公式の使い方)

公式READMEのコードをほぼそのまま使うのが try_pixio-vitl16.py です。

python try_pixio-vitl16.py

実行コード(修正版)

READMEのサンプルは hidden_states を返さないので、そのままだと None で落ちます。
対策として output_hidden_states=True を渡します。

from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

model_id = "facebook/pixio-vitl16"
processor = AutoImageProcessor.from_pretrained(model_id, trust_remote_code=True)
model = AutoModel.from_pretrained(model_id, trust_remote_code=True)

inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs, output_hidden_states=True)

last_hidden_states_norm = outputs.last_hidden_state
last_hidden_states = outputs.hidden_states[-1]
print(outputs.last_hidden_state.shape)

何が得られるか

  • outputs.last_hidden_state
    LayerNorm後の特徴(class tokens + patch tokens)
  • outputs.hidden_states[-1]
    LayerNorm前の特徴(class tokens + patch tokens)

出力例:

torch.Size([1, 264, 1024])

形状の意味

  • batch=1
  • tokens=264
  • dim=1024

256x256 画像を使うと、パッチ数は 16 * 16 = 256
Pixioは class token を8個持つため 256 + 8 = 264 になります。
vitl16 は埋め込み次元が 1024 なのでこの形です。

よくある注意点

1) hidden_states が None になる

デフォルトだと hidden_states を返さないため、
output_hidden_states=True が必須。

outputs = model(**inputs, output_hidden_states=True)

2) slow image processor の警告

use_fast 未指定だと警告が出ることがあります。
必要なら明示的に指定してください。

processor = AutoImageProcessor.from_pretrained(
    model_id,
    trust_remote_code=True,
    use_fast=True,
)

3) 画像サイズ

Pixioはパッチ分割するため、入力サイズが16の倍数だと分かりやすいです。
AutoImageProcessor を使えば自動処理されます。

最後に

今回は公式のコードと環境構築の解決を重心となった記事でした。
次回

取り出した特徴を使ってパッチ類似度の可視化や分析に進めます。
今回も最後まで読んでくれてありがとうございます。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?