空飛ぶロボットのつくりかた

ロボットをつくるために必要な技術をまとめます。ロボットの未来についても考えたりします。

kerasを動かしてみる~その2:CNN~

tensorflowをバックエンドで動かす

.keras/keras.jsonを以下のように書き換え

{
    "image_dim_ordering": "tf", 
    "epsilon": 1e-07, 
    "floatx": "float32", 
    "backend": "tensorflow"
}

GPUで使う

pip install tensorflow-gpu

(TBD)

CNNのサンプル

GitHub - fchollet/keras: Deep Learning library for Python. Runs on TensorFlow, Theano, or CNTK.

cifar10

from keras.datasets import cifar10

  • CIFAR-10は32x32ピクセルのカラー画像のデータセット
  • クラスラベルはairplane, automobile, bird, cat, deer, dog, frog, horse, ship, truckの10種類
  • 訓練用データ5万枚、テスト用データ1万枚

x_train shape: (50000, 32, 32, 3)

50000 train samples

10000 test samples

y_train shape: (50000, 1)

50000 train samples

10000 test samples

[3] ‘label -> ex: 3 ’

[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] one_hot

DataAugumentation

from keras.preprocessing.image import ImageDataGenerator

参考:画像の前処理 - Keras Documentation

keras-examples/test_datagen3.py at master · aidiary/keras-examples · GitHub

Keras Model

from keras.models import Sequential

from keras.layers import Dense, Dropout, Activation, Flatten

from keras.layers import Conv2D, MaxPooling2D

一層目のみinput_shape=x_train.shape[1:]のようにinput_shapeの指定がいる。

x_train shape: (50000, 32, 32, 3)の後ろ3つを取り出している。

モデルの保存・読み込み

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

参考:[TF]KerasでModelとParameterをLoad/Saveする方法 - Qiita

コード例

'''Train a simple deep CNN on the CIFAR10 small images dataset.
GPU run command with Theano backend (with TensorFlow, the GPU is automatically used):
    THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatx=float32 python cifar10_cnn.py
It gets down to 0.65 test logloss in 25 epochs, and down to 0.55 after 50 epochs.
(it's still underfitting at that point, though).
'''

from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.models import load_model
import numpy as np


batch_size = 32
num_classes = 10
epochs = 2
data_augmentation = True

# The data, shuffled and split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print('y_train shape:', y_train.shape)
print(y_train.shape[0], 'train samples')
print(y_test.shape[0], 'test samples')
print(y_test[0], 'label -> ex: 3 ')

# 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)
print(y_test[0], 'one_hot')

model = Sequential()

model.add(Conv2D(32, (3, 3), padding='same',
                 input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

if not data_augmentation:
    print('Not using data augmentation.')
    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=(x_test, y_test),
              shuffle=True)
else:
    print('Using real-time data augmentation.')
    # This will do preprocessing and realtime data augmentation:
    datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images

    # Compute quantities required for feature-wise normalization
    # (std, mean, and principal components if ZCA whitening is applied).
    datagen.fit(x_train)

    # Fit the model on the batches generated by datagen.flow().
    model.fit_generator(datagen.flow(x_train, y_train,
                                     batch_size=batch_size),
                        steps_per_epoch=x_train.shape[0] // batch_size,
                        epochs=epochs,
validation_data=(x_test, y_test))

"""
# save the model
model.save('./model.hdf5')
"""

"""
# load model 
model = load_model('model.hdf5')

#loss, acc = model.evaluate(x_test, y_test, verbose=0)
#print('Test loss:', loss)
#print('Test acc:', acc)

# only one test
x_test = x_test[0].reshape(1, 32, 32, 3)
print(model.predict(x_test))

# predict 
result = model.predict(x_test)
print(np.argmax(result))
"""
配列の入れ替え

shape: (1,2,3,4) -> (1,4,2,3)

データ構造

keras -> train shape: (50000, 32, 32, 3)

chainer -> train shape: (50000, 3, 32, 32)

import numpy as np

x = np.arange(4*6).reshape(1,2,3,4)
print x.shape

x = np.swapaxes(x,1,3)
y = np.swapaxes(x,1,2)
print y.shape

参考:

KerasでVGG16を使う - 人工知能に関する断創録

Deep learningで画像認識⑧〜Kerasで畳み込みニューラルネットワーク vol.4〜 - IMACEL Academy -人工知能・画像解析の技術応用に向けて-|LPixel(エルピクセル)

Kerasで学ぶAutoencoder