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

Food-101 で料理画像を分類してみたけどダメだった(日記)

Posted at

はじめに

本記事では、101種類の料理画像を含む有名な画像データセット「Food-101」を使用し、PyTorchの事前学習済みモデル(ResNet18) を用いて料理画像の分類を行います。
Google Colab を使うことで、GPU環境で簡単に試すことができます。


使用する技術スタック

ツール / ライブラリ 用途
Python 実装言語
PyTorch 深層学習フレームワーク
torchvision ResNetなどのモデル提供
Google Colab クラウド実行環境(GPU可)
Food-101 Dataset 101種類の料理画像データセット

Food-101とは?

  • スイスのチューリッヒ工科大学が提供
  • 101種類の料理カテゴリ
  • 各カテゴリに 1000枚の画像(合計101,000枚)
  • データセットは /images/meta/classes.txt で構成

Colabセルで実行するコード

# --- STEP 1: 必要なライブラリのインストール ---
!pip install torchvision --quiet
!pip install torch --quiet
!pip install matplotlib --quiet
!pip install opencv-python --quiet

# --- STEP 2: Food-101 データセットのダウンロード ---
!wget -nc --no-check-certificate http://data.vision.ee.ethz.ch/cvl/food-101.tar.gz
!tar -xzf food-101.tar.gz

# --- STEP 3: モデルと前処理の定義 ---
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image
import matplotlib.pyplot as plt

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = models.resnet18(pretrained=True)
model.fc = torch.nn.Linear(model.fc.in_features, 101)  # 出力をFood-101の101クラスに変更
model = model.to(device)
model.eval()

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor()
])

# --- STEP 4: クラスラベルの読み込み ---
with open('food-101/meta/classes.txt', 'r') as f:
    classes = [line.strip() for line in f.readlines()]

# --- STEP 5: 画像をアップロード(手元の画像を使える) ---
from google.colab import files
uploaded = files.upload()
img_path = list(uploaded.keys())[0]

# --- STEP 6: 推論と表示 ---
image = Image.open(img_path).convert("RGB")
input_tensor = transform(image).unsqueeze(0).to(device)

with torch.no_grad():
    output = model(input_tensor)
    predicted_idx = output.argmax(1).item()
    predicted_class = classes[predicted_idx]

print(f"🍽 予測された料理カテゴリ: {predicted_class}")
plt.imshow(image)
plt.axis('off')
plt.title(f"Predicted: {predicted_class}")
plt.show()

実行結果

使った画像
image (1).jpg

結果

image.png

要検討

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