0
0

More than 3 years have passed since last update.

TensorFlow2.0系でCIFAR-10データを学習して予測するまで(1)

Posted at

概要(TL;DR)

tensorflow2系でCIFAR-10の分類テストをしてみた。(個人的な覚書程度)
まずは、CIFAR-10の中身を確認する

下記の作業はすべて、Google Colabで実施しています。★GPUも使ってます。(ランタイム-> ランタイムのタイプを変更 -> ハードウェアアクセラレータでGPUを選択)

自前のMacBook Air(1.6 GHz DualCore Intel Core i5,16GB 2133MHz LPDDR3では、学習時間がかかり過ぎて待てない・・・。話には聞いていましたが、GPUの凄さを改めて感じました。

cifar-10

そもそもCIFAR-10とは

CIFAR-10

32x32サイズのカラー画像を10クラス(分類)で各6,000枚(全60,000枚)が含まれている学習用セットだそうな。

クラスは{airplane,automobile,bird,cat,dog,frog,horse,ship,truck}の10種類。SUVはAutomobileに入るし、TruckにはBigTrucksしか入らないなど、重複が無いような工夫がほどかされているとのことでした。

それぞれのクラスのサンプルを見てみる

from tensorflow.python.keras.datasets import cifar10
from tensorflow.python.keras.preprocessing.image import array_to_img

# x_train,y_trainは学習用のデータセット
# x_test,y_testは学習結果の検証用データセット
# (x_は入力データ, y_は出力データ)
(x_train,y_train),(x_test,y_test) = cifar10.load_data()

# CIFAR-10のクラス
cifar10_labels = [
        'airplane',
        'automobile',
        'bird',
        'cat',
        'deer',
        'dog',
        'frog',
        'horse',
        'ship',
        'truck']

index = 1
print (f"この画像は{ cifar10_labels[int(y_train[index])] }です.")
array_to_img(x_train[index])

スクリーンショット 2020-04-28 9.29.37.png

indexを変更すれば次々と新しい画像が見れることに気づくと同時に、後々も確認したい時に使えるので、下記のようにCIFAR-10のデータを確認できる関数を作りまし。

def show_sample_image(index,x_data,y_data):
  '''指定されたindexの画像データとラベルを表示する

    Args:
      index(int):指定インデックス
      x_data(numpy.ndarray):インプットデータ
      y_data(numpy.ndarray):アウトプットデータ
    Returns:

  '''
  CIFAR10_LABELS = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'] 

  print (f"index{index}:この画像は{ CIFAR10_LABELS[int(y_data[index])] }です.")
  display(array_to_img(x_data[index]))

もちろん、Google Colabを使っているので、CIFAR10_LABELSや引数にあるx_data,y_dataは不要ですが、今後Google Colabだけでなく、プログラムに落とし込む時のために依存しない関数にしています。
スクリーンショット 2020-04-28 9.44.56.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