iTAC_Technical_Documents

アイタックソリューションズ株式会社

ブログ名

【第11回】簡易体積算出アルゴリズム チューニング(前半)

簡易体積算出の調整

Khold部を再検討

データの偏りによる精度の変化を見るために、あえてKhold、split関数のshuffleをFalseにしました。
そこで5つのfoldにわけ、クロスバリデーションを行いました。
途中でメモリ不足により落ちてしまう不具合も修正済みです。

その結果は下記の通りです。
検証精度の良いfoldと悪いfoldに分かれていることがわかります。

結果1

Epoch 7/200
800/800 [==============================] - 32s 40ms/step - loss: 0.2233 - acc: 0.6650 - val_loss: 0.0866 - val_acc: 0.5600

Epoch 2/200
800/800 [==============================] - 32s 40ms/step - loss: 0.2530 - acc: 0.6362 - val_loss: 0.0262 - val_acc: 0.9050

Epoch 5/200
800/800 [==============================] - 32s 40ms/step - loss: 0.2288 - acc: 0.6825 - val_loss: 0.0147 - val_acc: 1.0000

Epoch 20/200
800/800 [==============================] - 32s 40ms/step - loss: 0.0381 - acc: 0.9562 - val_loss: 0.1250 - val_acc: 0.4450

Epoch 10/200
800/800 [==============================] - 32s 40ms/step - loss: 0.0401 - acc: 0.8988 - val_loss: 0.1145 - val_acc: 0.9800

パラメータチューニング

ベイズ最適化を用いたパラメータチューニング

ResNet50の全結合層を学習させることである程度の効果を確認することができたため、ベイズ最適化を用いたパラメータチューニングを行います。
最適化に用いたソースコードは、下記の通りです。

パラメータ範囲
    hidden_neurons = int(trial.suggest_discrete_uniform("hidden_neurons", 100, 2000, 50))

    optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam", "rmsprop"])
    
    activation = trial.suggest_categorical("activation", ["linear", "sigmoid"])
python
import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import re
import csv
from sklearn.model_selection import KFold
import keras.backend as K
import optuna

from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications import ResNet50
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import optimizers
from sklearn.model_selection import train_test_split

def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'):
    return [os.path.join(root, f)
            for root, _, files in os.walk(directory) for f in files
            if re.match(r'([\w]+\.(?:' + ext + '))', f.lower())]

def ResNet_dance(activation = "sigmoid",optimizer="adam",hidden_neurons = 1000, out_neurons = 3):
    input_tensor = Input(shape=(224, 224, 3))
    resnet50 = ResNet50(include_top=False, weights='imagenet', input_tensor=input_tensor)
    top_model = Sequential()
    top_model.add(Flatten(input_shape=resnet50.output_shape[1:]))
    top_model.add(Dense(hidden_neurons))
    top_model.add(Activation(activation))
    top_model.add(Dense(out_neurons))
    top_model.add(Activation("linear"))
    # top_model.summary()

    model = Model(input=resnet50.input, output=top_model(resnet50.output))
    model.compile(loss="mean_squared_error", metrics = ['accuracy'], optimizer=optimizer)
    # model.summary()

    return model

def objective(trial):

    K.clear_session()

    # 最適化するパラメータの設定

    # 中間層1のユニット数
    hidden_neurons = int(trial.suggest_discrete_uniform("hidden_neurons", 100, 2000, 50))

    # optimizer
    optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam", "rmsprop"])

    activation = trial.suggest_categorical("activation", ["linear", "sigmoid"])

    model = ResNet_dance(activation = activation ,optimizer=optimizer,hidden_neurons = hidden_neurons,out_neurons = 3)

    history = model.fit(x_train,
                       y_train,
                       batch_size=batch_size,
                       epochs=nb_epochs,
                       verbose=1,
                       validation_split=0.2,
                       shuffle=True)

    # 検証用データに対する正答率が最大となるハイパーパラメータを求める
    return 1 - history.history["val_acc"][-1]

X = []
Y = np.zeros((1000,3))
Y_pre = []

hidden_neurons = 1000
out_neurons = 3
batch_size = 16
nb_epochs = 200
fold_num = 5

es = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')
modelCheckpoint = ModelCheckpoint(filepath = 'Checkpoint.h5',
                                  monitor='val_loss',
                                  verbose=1,
                                  save_best_only=True,
                                  save_weights_only=False,
                                  mode='min',
                                  period=1)
picture_name = []
path = './test_dataset/'
for picture in list_pictures(path):
    picture_name.append(float(picture[picture.find(path)+len(path):picture.find('_0_')]))
    img = img_to_array(load_img(picture, target_size=(224,224)))
    X.append(img)

with open('DB.csv',encoding="utf-8_sig") as f:
    for row in csv.reader(f, quoting=csv.QUOTE_NONNUMERIC):
        Y_pre.append(row)

Y_pre = np.array(Y_pre)
for i,name in enumerate(picture_name):
    Y[i,:]=Y_pre[np.where(Y_pre==name)[0],1:4]
X = np.asarray(X)
X = X / 255.0

print(X.shape)
print(Y.shape)

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0, shuffle=False)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

study = optuna.create_study()
study.optimize(objective, n_trials=100)
print("End!!")
print("All trials")
print(study.trials)
print("Best Parameters")
print(study.best_params)
print("Best Value")
print(study.best_value)

print("end of script")

パラメータチューニングの改善

全結合層を1層でパラメータチューニング実行

パラメータチューニングは長時間かかりましたが、結局ResNet50の専有GPUメモリ率が想定よりも高かったことから正常に実行することができませんでした。
また、バッチ数を少なくしてもいずれ全結合層数の検討も行う必要があり、このままでは実現不可と思われたため入力サイズがコンパクトながら高い画像認識精度を示しているVGG19を用います。
これで、全結合層を1層でパラメータチューニングを行いました。

実行したソースコードは下記です(VGG19_optuna.py)。

python:VGG19_optuna.py
import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import re
import csv
from sklearn.model_selection import KFold
import keras.backend as K
import optuna

from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications import ResNet50, VGG19
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import optimizers
from sklearn.model_selection import train_test_split

import tensorflow as tf

batch_size = 8
nb_epochs = 200
fold_num = 5

def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'):
    return [os.path.join(root, f)
            for root, _, files in os.walk(directory) for f in files
            if re.match(r'([\w]+\.(?:' + ext + '))', f.lower())]

def ResNet_dance(activation = "sigmoid",optimizer="adam",hidden_neurons = 1000, out_neurons = 3):
    input_tensor = Input(shape=(224, 224, 3))
    resnet50 = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)
    top_model = Sequential()
    top_model.add(Flatten(input_shape=resnet50.output_shape[1:]))
    #top_model.add(Dropout(0.2))
    top_model.add(Dense(hidden_neurons))
    top_model.add(Activation(activation))
    #top_model.add(Dropout(0.2))
    top_model.add(Dense(out_neurons))
    top_model.add(Activation("linear"))
    #top_model.summary()

    model = Model(input=resnet50.input, output=top_model(resnet50.output))
    model.compile(loss="mean_squared_error", metrics = ['accuracy'], optimizer=optimizer)
    #model.summary()

    return model

def objective(trial):

    # config = tf.ConfigProto()
    # config.gpu_options.allow_growth = True
    # sess = tf.Session(config=config)
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    K.set_session(sess)

    es = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')
    modelCheckpoint = ModelCheckpoint(filepath = 'Checkpoint.h5',
                                      monitor='val_loss',
                                      verbose=1,
                                      save_best_only=True,
                                      save_weights_only=False,
                                      mode='min',
                                      period=1)

    #最適化するパラメータの設定

    #中間層1のユニット数
    hidden_neurons = int(trial.suggest_discrete_uniform("hidden_neurons", 100, 2000, 50))

    #optimizer
    optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam", "rmsprop"])

    activation = trial.suggest_categorical("activation", ["linear", "sigmoid"])

    model = ResNet_dance(activation = activation ,optimizer=optimizer,hidden_neurons = hidden_neurons,out_neurons = 3)

    history = model.fit(x_train,
                       y_train,
                       batch_size=batch_size,
                       epochs=nb_epochs,
                       verbose=1,
                       validation_split=0.2,
                       shuffle=True,
                       callbacks=[modelCheckpoint,es])

    #検証用データに対する正答率が最大となるハイパーパラメータを求める
    loss = history.history["val_loss"]
    loss.sort()

    return loss[0]

X = []
Y = np.zeros((1000,3))
Y_pre = []

hidden_neurons = 1000
out_neurons = 3

picture_name = []
path = './test_dataset/'
for picture in list_pictures(path):
    picture_name.append(float(picture[picture.find(path)+len(path):picture.find('_0_')]))
    img = img_to_array(load_img(picture, target_size=(224,224)))
    X.append(img)

with open('DB.csv',encoding="utf-8_sig") as f:
    for row in csv.reader(f, quoting=csv.QUOTE_NONNUMERIC):
        Y_pre.append(row)

Y_pre = np.array(Y_pre)
for i,name in enumerate(picture_name):
    Y[i,:]=Y_pre[np.where(Y_pre==name)[0],1:4]
X = np.asarray(X)
X = X / 255.0

print(X.shape)
print(Y.shape)

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0, random_state=0, shuffle=False)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

study = optuna.create_study()
study.optimize(objective, n_trials=50)
print("End!!")
print("All trials")
print(study.trials)
print("Best Parameters")
print(study.best_params)
print("Best Value")
print(study.best_value)

print("end of script")

交差検証

結果、"{'hidden_neurons': 150.0, 'optimizer': 'sgd', 'activation': 'sigmoid'}"が最適パラメータと算出されました。
learning_rate(lr)が高いせいか収束が早く、最適解に届いていないと考えlrを低く設定し、dropoutを追加しました。
次に、そのモデルについて詳しく分析するため交差検証を行います。
実行したソースコードは下記です(VGG19_cross.py)。

python:VGG19_cross.py
import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import re
import csv
import tensorflow as tf

from sklearn.model_selection import KFold
from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications import ResNet50, VGG19
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import optimizers
from sklearn.model_selection import train_test_split
from keras.backend import tensorflow_backend

config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
session = tf.Session(config=config)
tensorflow_backend.set_session(session)

def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'):
    return [os.path.join(root, f)
            for root, _, files in os.walk(directory) for f in files
            if re.match(r'([\w]+\.(?:' + ext + '))', f.lower())]

X = []
Y = np.zeros((1000,3))
Y_pre = []

hidden_neurons = 150
out_neurons = 3
batch_size = 8
nb_epochs = 200
fold_num = 5

es = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')

picture_name = []
path = './test_dataset/'
for picture in list_pictures(path):
    picture_name.append(float(picture[picture.find(path)+len(path):picture.find('_0_')]))
    img = img_to_array(load_img(picture, target_size=(224,224)))
    X.append(img)

with open('DB.csv',encoding="utf-8_sig") as f:
    for row in csv.reader(f, quoting=csv.QUOTE_NONNUMERIC):
        Y_pre.append(row)

Y_pre = np.array(Y_pre)
for i,name in enumerate(picture_name):
    Y[i,:]=Y_pre[np.where(Y_pre==name)[0],1:4]
X = np.asarray(X)
X = X / 255.0

print(X.shape)
print(Y.shape)

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0, shuffle=False)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

kf = KFold(n_splits=fold_num, random_state = 71, shuffle=False)
cvscores = []
count = 0

input_tensor = Input(shape=(224, 224, 3))
resnet50 = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)

for train, test in kf.split(x_train):
    modelCheckpoint = ModelCheckpoint(filepath = 'Checkpoint.h5',
                                        monitor='val_loss',
                                        verbose=1,
                                        save_best_only=True,
                                        save_weights_only=False,
                                        mode='min',
                                        period=1)
    top_model = Sequential()
    top_model.add(Flatten(input_shape=resnet50.output_shape[1:]))
    top_model.add(Dropout(0.2))
    top_model.add(Dense(hidden_neurons))
    top_model.add(Activation("sigmoid"))
    top_model.add(Dropout(0.2))
    top_model.add(Dense(out_neurons))
    top_model.add(Activation("linear"))
    #top_model.summary()

    model = Model(input=resnet50.input, output=top_model(resnet50.output))
    model.compile(loss="mean_squared_error", metrics = ['accuracy'], optimizer=optimizers.SGD(lr=0.0001))
    #model.summary()
    result = model.fit(x_train[train],
                       y_train[train],
                       batch_size=batch_size,
                       epochs=nb_epochs,
                       verbose=1,
                       validation_data=(x_train[test], y_train[test]),
                       shuffle=True,
                       callbacks=[modelCheckpoint,es])

    model.load_weights('Checkpoint.h5')
    scores = model.evaluate(x_train[test], y_train[test], verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    cvscores.append(scores[1] * 100)

    result.history.keys() # ヒストリデータのラベルを見てみる]
    ep =len(result.history['acc'])
    plt.plot(range(1, ep+1), result.history['acc'], label="training")
    plt.plot(range(1, ep+1), result.history['val_acc'], label="validation")
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.savefig('acc_'+str(count)+'.png')
    plt.show()
    plt.plot(range(1, ep+1), result.history['loss'], label="training")
    plt.plot(range(1, ep+1), result.history['val_loss'], label="validation")
    plt.xlabel('Epochs')
    plt.ylabel('loss')
    plt.legend()
    plt.savefig('loss_'+str(count)+'.png')
    plt.show()

    count = count + 1

print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))

可視化
model.summary()
evascores=[]
# モデル評価
for eva in range(1,6):
    #print(eva)
    model.load_weights('Checkpoint_'+ str(eva) +'.h5')
    loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
    evascores.append(scores[1] * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(evascores), np.std(evascores)))
print("end of script")

階層の検討

結果、accuracyは67.88% (+/- 33.36%)で、90%以上が3つ、30%以下が2つと分散が大きい結果となりました。
これは、画像から堆積情報抽出ではなく、画像自体の類似度で識別している可能性があります。

次に層数の検討を行います。
1層追加し、全結合層2層でパラメータチューニングを行いました。
実行したソースコードは下記です(VGG19_optuna2.py)。

python:VGG19_optuna2.py
import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import re
import csv
from sklearn.model_selection import KFold
import keras.backend as K
import optuna

from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications import ResNet50, VGG19
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import optimizers
from sklearn.model_selection import train_test_split

import tensorflow as tf

batch_size = 8
nb_epochs = 200
fold_num = 5

def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'):
    return [os.path.join(root, f)
            for root, _, files in os.walk(directory) for f in files
            if re.match(r'([\w]+\.(?:' + ext + '))', f.lower())]

def ResNet_dance(activation = "sigmoid",optimizer="adam",hidden_neurons1 = 1000,hidden_neurons2 = 1000,out_neurons = 3):
    input_tensor = Input(shape=(224, 224, 3))
    resnet50 = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)
    top_model = Sequential()
    top_model.add(Flatten(input_shape=resnet50.output_shape[1:]))
    #top_model.add(Dropout(0.2))
    top_model.add(Dense(hidden_neurons1))
    top_model.add(Activation(activation))
    top_model.add(Dense(hidden_neurons2))
    top_model.add(Activation(activation))
    #top_model.add(Dropout(0.2))
    top_model.add(Dense(out_neurons))
    top_model.add(Activation("linear"))
    #top_model.summary()

    model = Model(input=resnet50.input, output=top_model(resnet50.output))
    model.compile(loss="mean_squared_error", metrics = ['accuracy'], optimizer=optimizer)
    #model.summary()

    return model

def objective(trial):

    # config = tf.ConfigProto()
    # config.gpu_options.allow_growth = True
    # sess = tf.Session(config=config)
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    K.set_session(sess)

    es = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')
    modelCheckpoint = ModelCheckpoint(filepath = 'Checkpoint.h5',
                                      monitor='val_loss',
                                      verbose=1,
                                      save_best_only=True,
                                      save_weights_only=False,
                                      mode='min',
                                      period=1)

    #最適化するパラメータの設定

    #中間層1のユニット数
    hidden_neurons1 = int(trial.suggest_discrete_uniform("hidden_neurons1", 100, 5000, 100))

    hidden_neurons2 = int(trial.suggest_discrete_uniform("hidden_neurons2", 100, 5000, 100))

    #optimizer
    optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam", "rmsprop"])

    activation = trial.suggest_categorical("activation", ["linear", "sigmoid"])

    model = ResNet_dance(activation = activation ,optimizer=optimizer,hidden_neurons1 = hidden_neurons1,hidden_neurons2 = hidden_neurons2,out_neurons = 3)

    history = model.fit(x_train,
                       y_train,
                       batch_size=batch_size,
                       epochs=nb_epochs,
                       verbose=1,
                       validation_split=0.2,
                       shuffle=True,
                       callbacks=[modelCheckpoint,es])

    #検証用データに対する正答率が最大となるハイパーパラメータを求める
    loss = history.history["val_loss"]
    loss.sort()

    return loss[0]

X = []
Y = np.zeros((1000,3))
Y_pre = []

hidden_neurons = 1000
out_neurons = 3

picture_name = []
path = './test_dataset/'
for picture in list_pictures(path):
    picture_name.append(float(picture[picture.find(path)+len(path):picture.find('_0_')]))
    img = img_to_array(load_img(picture, target_size=(224,224)))
    X.append(img)

with open('DB.csv',encoding="utf-8_sig") as f:
    for row in csv.reader(f, quoting=csv.QUOTE_NONNUMERIC):
        Y_pre.append(row)

Y_pre = np.array(Y_pre)
for i,name in enumerate(picture_name):
    Y[i,:]=Y_pre[np.where(Y_pre==name)[0],1:4]
X = np.asarray(X)
X = X / 255.0

print(X.shape)
print(Y.shape)

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0, random_state=0, shuffle=False)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

study = optuna.create_study()
study.optimize(objective, n_trials=50)
print("End!!")
print("All trials")
print(study.trials)
print("Best Parameters")
print(study.best_params)
print("Best Value")
print(study.best_value)

print("end of script")

モデルの詳細分析

その結果、"{'hidden_neurons1': 4000.0, 'hidden_neurons2': 600.0, 'optimizer': 'sgd', 'activation': 'sigmoid'}"となりました。
1層と2層でlossを比較すると、それぞれ0.06840029045939446と0.055774277579039334で、2層のほうが良い算出結果となりそうです。

最後に、このモデルについて詳しく分析するため、交差検証を行います。
実行したソースコードは下記です(VGG19_cross2.py)。

python:VGG19_cross2.py
import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import re
import csv
import tensorflow as tf

from sklearn.model_selection import KFold
from keras.utils import np_utils
from keras.models import Sequential, Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.applications import ResNet50, VGG19
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import optimizers
from sklearn.model_selection import train_test_split
from keras.backend import tensorflow_backend

config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
session = tf.Session(config=config)
tensorflow_backend.set_session(session)

def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'):
    return [os.path.join(root, f)
            for root, _, files in os.walk(directory) for f in files
            if re.match(r'([\w]+\.(?:' + ext + '))', f.lower())]

X = []
Y = np.zeros((1000,3))
Y_pre = []

hidden_neurons1 = 4000
hidden_neurons2 = 600
out_neurons = 3
batch_size = 8
nb_epochs = 200
fold_num = 5

es = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')

picture_name = []
path = './test_dataset/'
for picture in list_pictures(path):
    picture_name.append(float(picture[picture.find(path)+len(path):picture.find('_0_')]))
    img = img_to_array(load_img(picture, target_size=(224,224)))
    X.append(img)

with open('DB.csv',encoding="utf-8_sig") as f:
    for row in csv.reader(f, quoting=csv.QUOTE_NONNUMERIC):
        Y_pre.append(row)

Y_pre = np.array(Y_pre)
for i,name in enumerate(picture_name):
    Y[i,:]=Y_pre[np.where(Y_pre==name)[0],1:4]
X = np.asarray(X)
X = X / 255.0

print(X.shape)
print(Y.shape)

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0, random_state=0, shuffle=False)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

kf = KFold(n_splits=fold_num, random_state = 71, shuffle=False)
cvscores = []
count = 0

input_tensor = Input(shape=(224, 224, 3))
resnet50 = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)

for train, test in kf.split(x_train):
    modelCheckpoint = ModelCheckpoint(filepath = 'Checkpoint.h5',
                                        monitor='val_loss',
                                        verbose=1,
                                        save_best_only=True,
                                        save_weights_only=False,
                                        mode='min',
                                        period=1)
    top_model = Sequential()
    top_model.add(Flatten(input_shape=resnet50.output_shape[1:]))
    top_model.add(Dropout(0.2))
    top_model.add(Dense(hidden_neurons1))
    top_model.add(Activation("sigmoid"))
    top_model.add(Dropout(0.2))
    top_model.add(Dense(hidden_neurons2))
    top_model.add(Activation("sigmoid"))
    top_model.add(Dropout(0.2))
    top_model.add(Dense(out_neurons))
    top_model.add(Activation("linear"))
    #top_model.summary()

    model = Model(input=resnet50.input, output=top_model(resnet50.output))
    model.compile(loss="mean_squared_error", metrics = ['accuracy'], optimizer=optimizers.SGD(lr=0.0001))
    #model.summary()
    result = model.fit(x_train[train],
                       y_train[train],
                       batch_size=batch_size,
                       epochs=nb_epochs,
                       verbose=1,
                       validation_data=(x_train[test], y_train[test]),
                       shuffle=True,
                       callbacks=[modelCheckpoint,es])

    model.load_weights('Checkpoint.h5')
    scores = model.evaluate(x_train[test], y_train[test], verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    cvscores.append(scores[1] * 100)

    result.history.keys() # ヒストリデータのラベルを見てみる]
    ep =len(result.history['acc'])
    plt.plot(range(1, ep+1), result.history['acc'], label="training")
    plt.plot(range(1, ep+1), result.history['val_acc'], label="validation")
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.savefig('acc_'+str(count)+'.png')
    plt.show()
    plt.plot(range(1, ep+1), result.history['loss'], label="training")
    plt.plot(range(1, ep+1), result.history['val_loss'], label="validation")
    plt.xlabel('Epochs')
    plt.ylabel('loss')
    plt.legend()
    plt.savefig('loss_'+str(count)+'.png')
    plt.show()

print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))

可視化
model.summary()
evascores=[]
# モデル評価
for eva in range(1,6):
    #print(eva)
    model.load_weights('Checkpoint_'+ str(eva) +'.h5')
    loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
    evascores.append(scores[1] * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(evascores), np.std(evascores)))
print("end of script")

結果、87.50% (+/- 11.37%)と1層の全結合層より分散が少なく、よい精度で算出されました。
3層のパラメータチューニングを行った結果は”{'hidden_neurons1': 3100.0, 'hidden_neurons2': 4500.0, 'hidden_neurons3': 1000.0, 'optimizer': 'sgd', 'activation': 'sigmoid'}”のようになっています。
lossは,0.05656835810281336と2層に比べると高い事がわかります。

次回の予定

次回は、3層の交差検証と4層以降の交差検証の評価をPCのスペックが足りるまで検証して、精度の頂点を見つけます。


次の記事へ

前の記事へ 戻る