<jupyter_start><jupyter_text>MobileNetV2で欅坂46とけやき坂46のメンバーの顔認識<jupyter_code>import keras
from keras.applications.mobilenetv2 import MobileNetV2
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from utils.utils import load_data

%matplotlib inline<jupyter_output>/home/ishiyama/tensorflow/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.<jupyter_text>モデルと学習の設定EPOCHを2000  テストとバリデーションのデータサイズはともに0.1（＝全体の10%）を指定した。<jupyter_code># モデルの設定
NUMBER_OF_MEMBERS = 41             # 漢字とひらがな合わせたメンバー数
CLASSES = NUMBER_OF_MEMBERS + 1    # one hot表現は0から始まるため
LOG_DIR = './logs/1.2_0.8/'        # LossとAccuracyのログ

# 学習の設定
EPOCHS = 2000
TEST_SIZE = 0.1
VALIDATION_SPLIT = 0.1<jupyter_output><empty_output><jupyter_text>データの読み込み<jupyter_code>X, Y = load_data('/home/ishiyama/notebooks/keyakizaka_member_detection/image/mobilenet/')
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=TEST_SIZE, shuffle=True)<jupyter_output><empty_output><jupyter_text>モデル構築<jupyter_code>model = MobileNetV2(alpha=1.2, depth_multiplier=0.8, include_top=True, weights=None, classes=CLASSES)
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
callbacks = keras.callbacks.TensorBoard(log_dir=LOG_DIR)<jupyter_output><empty_output><jupyter_text>学習させる<jupyter_code>fit_result = model.fit(
    x=X_train,
    y=Y_train,
    epochs=EPOCHS,
    validation_split=VALIDATION_SPLIT,
    verbose=2,
    callbacks=[callbacks]
)<jupyter_output>Train on 10347 samples, validate on 1150 samples
Epoch 1/2000
 - 81s - loss: 3.5545 - acc: 0.0928 - val_loss: 10.5082 - val_acc: 0.0452
Epoch 2/2000
 - 75s - loss: 3.0752 - acc: 0.1892 - val_loss: 11.7327 - val_acc: 0.0270
Epoch 3/2000
 - 77s - loss: 2.7427 - acc: 0.2714 - val_loss: 13.5474 - val_acc: 0.0409
Epoch 4/2000
 - 77s - loss: 2.3778 - acc: 0.3503 - val_loss: 12.7074 - val_acc: 0.0513
Epoch 5/2000
 - 78s - loss: 2.0237 - acc: 0.4396 - val_loss: 15.2051 - val_acc: 0.0339
Epoch 6/2000
 - 78s - loss: 1.7212 - acc: 0.5312 - val_loss: 14.2476 - val_acc: 0.0496
Epoch 7/2000
 - 78s - loss: 1.4406 - acc: 0.5966 - val_loss: 15.4948 - val_acc: 0.0200
Epoch 8/2000
 - 78s - loss: 1.2141 - acc: 0.6552 - val_loss: 13.2945 - val_acc: 0.0730
Epoch 9/2000
 - 77s - loss: 1.0061 - acc: 0.7165 - val_loss: 14.6152 - val_acc: 0.0504
Epoch 10/2000
 - 78s - loss: 0.8021 - acc: 0.7734 - val_loss: 14.6252 - val_acc: 0.0513
Epoch 11/2000
 - 78s - loss: 0.6670 - acc: 0.8034 - val_loss: 13.3202 - val_acc:[...]<jupyter_text>LossとAccuracyのグラフを表示する（参考）[MNISTでハイパーパラメータをいじってloss/accuracyグラフを見てみる](https://qiita.com/hiroyuki827/items/213146d551a6e2227810)<jupyter_code>fig, (axL, axR) = plt.subplots(ncols=2, figsize=(16,5))

# loss
def plot_history_loss(fit):
    # Plot the loss in the history
    axL.plot(fit.history['loss'],label="loss for training")
    axL.plot(fit.history['val_loss'],label="loss for validation")
    axL.set_title('model loss')
    axL.set_xlabel('epoch')
    axL.set_ylabel('loss')
    axL.legend(loc='upper right')

# acc
def plot_history_acc(fit):
    # Plot the loss in the history
    axR.plot(fit.history['acc'],label="accuracy for training")
    axR.plot(fit.history['val_acc'],label="accuracy for validation")
    axR.set_title('model accuracy')
    axR.set_xlabel('epoch')
    axR.set_ylabel('accuracy')
    axR.legend(loc='upper right')

plot_history_loss(fit_result)
plot_history_acc(fit_result)
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>周期的に精度が下がる原因を調査する。  テストデータで精度を確認する<jupyter_code>test_result = model.evaluate(
    x=X_test,
    y=Y_test
)
print('loss for test:', test_result[0])
print('accuracy for test:', test_result[1])<jupyter_output>1278/1278 [==============================] - 2s 2ms/step
loss for test: 2.562832520414779
accuracy for test: 0.73082942078371<jupyter_text>今回学習したモデルを保存する<jupyter_code>model.save('keyakizaka_member_detection_mobilenetv2.h5')<jupyter_output><empty_output><jupyter_text>混同行列を作成する<jupyter_code>from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline

plt.rcParams['figure.figsize'] = (12, 10)

predicted = model.predict(X_test)
predicted_member_id = predicted.argmax(axis=1)
true_member_id = Y_test.argmax(axis=1)
predicted_result = pd.DataFrame(
    data={
        'y_true': true_member_id,
        'y_pred': predicted_member_id,
        'cnt': 1
    }
)
confusion_matrix = pd.pivot_table(
    data=predicted_result,
    index=['y_pred'],
    columns=['y_true'],
    values='cnt',
    aggfunc='count',
    fill_value=0
)
sns.heatmap(confusion_matrix)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13262599469496023, 'fraction_numerical': 0.08875739644970414, 'mean_word_length': 4.78066037735849, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '51670', 'license': 'MIT', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2101, 'n_words': 343, 'path': 'Keyakizaka_Member_Face_Detection003.ipynb', 'repo_name': 'Katsuya-Ishiyama/keyakizaka_member_detection', 'size': '4901'}