CIFAR-10 データセット(Python を使用)
【概要】
CIFAR-10 データセットを紹介する。利用条件は利用者で確認すること。
CIFAR-10 データセット(Canadian Institute for Advanced Research, 10 classes)は、公開されているデータセット(オープンデータ)である。
CIFAR-10 データセット(Canadian Institute for Advanced Research, 10 classes)は、クラス数 10 のカラー画像と、各画像に付いたラベルから構成されるデータセットである。機械学習での画像分類の学習や検証に利用できる。
- 画像の枚数:合計 60000 枚。内訳は、教師データ(教師データ)50000 枚、検証データ 10000 枚である。
- 画像のサイズ:32×32。カラー画像である。
- クラス数:10(飛行機、自動車、鳥、猫、鹿、犬、カエル、馬、船、トラック)(airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck)。各画像に 1 つのラベルが付いている。
- 0: airplane(飛行機)
- 1: automobile(自動車)
- 2: bird(鳥)
- 3: cat(猫)
- 4: deer(鹿)
- 5: dog(犬)
- 6: frog(カエル)
- 7: horse(馬)
- 8: ship(船)
- 9: truck(トラック)
【文献】
Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton. 'Learning multiple layers of features from tiny images', Alex Krizhevsky, 2009.
【目次】
【関連する外部ページ】
- CIFAR-10 データセットの URL: https://www.cs.toronto.edu/~kriz/cifar.html
- Papers With Code の CIFAR-10 データセットのページ: https://paperswithcode.com/dataset/cifar-10
- PyTorch(torchvision)の CIFAR-10: https://pytorch.org/vision/stable/datasets.html#cifar
【サイト内の関連情報】
1. 前準備
Python 3.12 のインストール(Windows 上) [クリックして展開]
以下のいずれかの方法で Python 3.12 をインストールする。Python がインストール済みの場合、この手順は不要である。
方法1:winget によるインストール
管理者権限でコマンドプロンプトを起動する
(手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。
winget install --id Python.Python.3.12 -e --scope machine --silent --accept-source-agreements --accept-package-agreements --override "/quiet InstallAllUsers=1 PrependPath=1 Include_test=0 Include_pip=1 Include_launcher=1 InstallLauncherAllUsers=1 TargetDir=\"C:\Program Files\Python312\""
powershell -Command "$p='C:\Program Files\Python312'; $s=\"$p\Scripts\"; $m=[Environment]::GetEnvironmentVariable('Path','Machine'); if($m -notlike \"*$s*\") { [Environment]::SetEnvironmentVariable('Path', \"$p;$s;$m\", 'Machine') }"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
方法2:インストーラーによるインストール
- Python 公式サイト(https://www.python.org/downloads/)にアクセスし、「Download Python 3.x.x」ボタンから Windows 用インストーラーをダウンロードする。
- ダウンロードしたインストーラーを実行する。
- 初期画面の下部に表示される「Add python.exe to PATH」にチェックを入れてから「Customize installation」を選択する。このチェックを入れ忘れると、コマンドプロンプトから
pythonコマンドを実行できない。 - 「Install Python 3.xx for all users」にチェックを入れ、「Install」をクリックする。
インストールの確認
コマンドプロンプトで以下を実行する。
python --version
バージョン番号(例:Python 3.12.x)が表示されればインストール成功である。「'python' は、内部コマンドまたは外部コマンドとして認識されていません。」と表示される場合は、インストールが正常に完了していない。
AIエディタ Windsurf のインストール(Windows 上) [クリックして展開]
Python プログラムの編集・実行には、AIエディタの利用を推奨する。ここでは、Windsurf のインストールを説明する。Windsurf がインストール済みの場合、この手順は不要である。
管理者権限でコマンドプロンプトを起動する
(手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。
winget install --scope machine --id Codeium.Windsurf -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --custom "/SP- /SUPPRESSMSGBOXES /NORESTART /CLOSEAPPLICATIONS /DIR=""C:\Program Files\Windsurf"" /MERGETASKS=!runcode,addtopath,associatewithfiles,!desktopicon"
powershell -Command "$env:Path=[System.Environment]::GetEnvironmentVariable('Path','Machine')+';'+[System.Environment]::GetEnvironmentVariable('Path','User'); windsurf --install-extension MS-CEINTL.vscode-language-pack-ja --force; windsurf --install-extension ms-python.python --force; windsurf --install-extension Codeium.windsurfPyright --force"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
【関連する外部ページ】
Windsurf の公式ページ: https://windsurf.com/
PyTorch(torch, torchvision), numpy, matplotlib のインストール
Windows の場合
Windows では、管理者権限でコマンドプロンプトを起動する
(手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。続いて、次のコマンドを実行する。
python -m pip install -U --no-user torch torchvision numpy matplotlib
NVIDIA GPU(CUDA 対応)を使う場合は、PyTorch 公式サイトの選択画面(OS・パッケージ・CUDA バージョン)に従ったコマンドを実行する。例えば CUDA 12.6 向けは次のとおりである。
python -m pip install -U --no-user torch torchvision --index-url https://download.pytorch.org/whl/cu126
Ubuntu の場合
Ubuntu では、次のコマンドを実行する。
# パッケージリストの情報を更新
sudo apt update
sudo apt -y install python3-numpy python3-matplotlib
sudo pip3 install -U --no-user torch torchvision
2. CIFAR-10 データセットのロード
- パッケージのインポート、PyTorch のバージョン確認など
import torch import torchvision import numpy as np import matplotlib.pyplot as plt print(torch.__version__)
- torchvision から CIFAR-10 データセットをロード
- trainset: 訓練用データセット(50000 枚)。trainset.data はサイズ 32×32 の 50000 枚のカラー画像(形状 (50000, 32, 32, 3) の NumPy 配列)、trainset.targets は各画像の種類番号(0 から 9 のどれか)のリストである。
- testset: 検証用データセット(10000 枚)。testset.data はサイズ 32×32 の 10000 枚のカラー画像(形状 (10000, 32, 32, 3) の NumPy 配列)、testset.targets は各画像の種類番号(0 から 9 のどれか)のリストである。
rootはデータの保存先ディレクトリ、trainは訓練用(True)か検証用(False)かの指定、download=Trueは未取得のときダウンロードする指定である。trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True) x_train, y_train = trainset.data, trainset.targets x_test, y_test = testset.data, testset.targets print(x_train.shape, x_test.shape)
3. CIFAR-10 データセットの確認
- データセットの中の画像を表示
Matplotlib を用いて、0 番目の画像を表示する。
NUM = 0 plt.figure() plt.imshow(x_train[NUM]) plt.colorbar() plt.gca().grid(False) plt.show()
- データセットの情報を表示
print(trainset.classes) print(len(trainset.classes)) print(x_train.shape)
- データの確認表示
Matplotlib を用いて、複数の画像を並べて表示する。
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] plt.style.use('default') plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(x_train[i]) plt.xlabel(class_names[y_train[i]]) plt.show()
- trainset, testset の確認
print(trainset) print(testset)