みらいテックラボ

音声・画像認識や機械学習など, 週末プログラマである管理人が興味のある技術の紹介や実際にトライしてみた様子などメモしていく.

Fashion-MNISTやってみた(1)

8月末頃に, TwitterだったかFacebookだったかで, Fashion-MNIST[1][2]なるデータセットの存在を知った.

以前に, ファッション画像における洋服の「色」分類にチャレンジ[3]したこともあり, 少し調べてみた.


関連記事:
・Fashion-MNISTやってみた(1)
Fashion-MNISTやってみた(2)


0. Fashion-MNISTって何?
オリジナルのMNIST(手書き数字)は簡単すぎるので, ファッション画像でMNISTと置き換え可能なデータセットを作成したとのこと.

データセットの概要

  • 訓練セット :60,000サンプル
  • テストセット:10,000サンプル
  • 画像サイズ :28×28(グレースケール)
  • ラベル   :10カテゴリ

ラベル記述
0T-shirt/top
1Trouser
2Pullover
3Dress
4Coat
5Sandal
6Shirt
7Sneaker
8Bag
9Ankle boot

例)
f:id:moonlight-aska:20170918003033p:plain
[引用:https://github.com/zalandoresearch/fashion-mnist]

手書き数字をファッション画像に置き換えただけで, ファイル名も全く同じなので, MNISTデータセットを置き換えるだけで簡単に試すことができるそうだ.


1. KerasでFashion-MNIST
Keras[4]のexamples/mnist_cnn.pyを使ってどの程度識別できるか試してみた.

コード:

'''Trains a simple convnet on the MNIST dataset.

Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
# from keras.datasets import mnist
from utils import mnist_reader

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

# the data, shuffled and split between train and test sets
# (x_train, y_train), (x_test, y_test) = mnist.load_data()
# Fashion mnist
x_train, y_train = mnist_reader.load_mnist('./data/mnist', kind='train')
x_test, y_test = mnist_reader.load_mnist('./data/mnist', kind='t10k')

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
print('x_test', x_test.shape)
print('y_test', y_test.shape)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

注) データ読み込み部分のみ, zalandoresearch/fashion-mnist[1]の"Loading data with Python(requires NumPy)"のコードを使用.

学習条件
データセット  :MNIST と Fashion-MNIST
学習データ   :60,000サンプル
ミニバッチサイズ:128サンプル
学習回数    :12 epochs

評価条件と結果
データセット  :MNIST と Fashion-MNIST
テストデータ  :10,000サンプル

データセット精度
MNIST0.9901
Fashion-MNIST0.9173

オリジナルのMNISTに比べると, かなり難しそう.
zalandoresearch/fashion-mnist[1]のBenchmarkのところを見ると, 現時点(2017/9/18)ではWRN40-4 8.9Mによる0.967がTopスコアのようである.

時間のあるときに, 少しチャレンジしてみようと思う.

----
参照URL:
[1] GitHub - zalandoresearch/fashion-mnist: A MNIST-like fashion product database. Benchmark
[2] http://tensorflow.classcat.com/category/fashion-mnist/
[3] 第1回FR FRONTIER:ファッション画像における洋服の「色」分類にチャレンジ!!(1)
[4] GitHub - fchollet/keras: Deep Learning library for Python. Runs on TensorFlow, Theano, or CNTK.








60分でわかる!  機械学習&ディープラーニング 超入門 (60分でわかる! IT知識)

60分でわかる! 機械学習&ディープラーニング 超入門 (60分でわかる! IT知識)