この質問を改めて質問させていただきます。
Jupyter notebookでデータ拡張した場合の予測正解確率を調べています。
以下のプログラムを複数回実行すれば、その都度、予測正解率は異なるは思いますが、毎回大きく異なります。また、プログラムを最後まで1度実行したあと、(Jupyterを閉じずに)model.fitから2回目(複数回)を実行した時、既にaccuracyが高いように思われます(以下参照)。この場合、accuracyが1回前の学習状態を引き続いて実行される?ことはあるのでしょうか。
# import library import keras import matplotlib.pyplot as plt import numpy as np import os import pandas as pd import seaborn as sn import shutil import tensorflow as tf from datetime import datetime, timedelta, timezone from keras import backend as ke from keras.callbacks import Callback, ModelCheckpoint, EarlyStopping from keras.datasets import mnist from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, BatchNormalization from keras.models import Sequential from keras.optimizers import RMSprop from keras.preprocessing.image import ImageDataGenerator from keras.utils import np_utils from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split from tqdm import tqdm # MNIST 読込み mnist=keras.datasets.mnist (x_train,y_train),(x_test,y_test)=mnist.load_data() (x_train,y_train),(x_test,y_test)=(x_train[:80],y_train[:80]),(x_test[:20], y_test[:20]) #(x_train,y_train),(x_test,y_test)=(x_train[:160],y_train[:160]),(x_test[:40], y_test[:40]) #(x_train,y_train),(x_test,y_test)=(x_train[:800],y_train[:800]),(x_test[:200], y_test[:200]) #(x_train,y_train),(x_test,y_test)=(x_train[:8000],y_train[:8000]),(x_test[:2000], y_test[:2000]) x_train=x_train.reshape(x_train.shape[0],28,28,1) x_test=x_test.reshape(x_test.shape[0],28,28,1) x_train=x_train/255 x_test=x_test/255 print("x_train",x_train.shape) print("x_test",x_test.shape) # model model = Sequential() model.add(Conv2D(64, (3, 3), input_shape=(28,28,1), padding='same')) BatchNormalization(axis=-1) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3), padding='same')) BatchNormalization(axis=-1) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.20)) model.add(Conv2D(64, (3, 3), padding='same')) BatchNormalization(axis=-1) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3), padding='same')) BatchNormalization(axis=-1) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Dropout(0.20)) model.add(Conv2D(128, (3, 3), padding='same')) BatchNormalization(axis=-1) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax')) model.summary() # model compile model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # model fit model.fit(x_train,y_train,epochs=40) # evoluate for test data loss,acc=model.evaluate(x_test,y_test,verbose=2) print('loss:','{:.3f}'.format(loss),'accuracy:','{:.3f}'.format(acc)) # ImageDataGenerator datagen = ImageDataGenerator( featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, zoom_range=[2.0,0.1], horizontal_flip=False, vertical_flip=False) datagen.fit(x_train) datagent = ImageDataGenerator( featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, zoom_range=[2.0,0.1], horizontal_flip=False, vertical_flip=False) datagent.fit(x_test) # parameter # [sample] / [iteration] = [batch size] # train : 80 / 5 = 16 # test : 20 / 2 = 10 # train : 160 / 10 = 16 # test : 40 / 5 = 8 # train : 800 / 50 = 16 # test : 200 / 10 = 20 # train : 8000 / 250 = 32 # test : 2000 / 125 = 16 # train : 60,000 / 500 = 120 # test : 10,000 / 200 = 50 epochs = 1000 iteration_train = 5 iteration_test = 2 batch_size_train = int(x_train.shape[0] / iteration_train) batch_size_test = int(x_test.shape[0] / iteration_test) #プログラムを最後まで1度実行したあと、Jupyterを閉じずにここから2回目(複数回)を実行する gen_train_flow = datagen.flow(x_train, y_train, batch_size=batch_size_train) gen_test_flow = datagent.flow(x_test, y_test, batch_size=batch_size_test) history = model.fit(gen_train_flow, steps_per_epoch=iteration_train, epochs=epochs, validation_data=gen_test_flow, validation_steps=iteration_test)#, #callbacks=callbacks) # evoluate for test data loss,acc=model.evaluate(x_test,y_test,verbose=2) print('loss:','{:.3f}'.format(loss),'accuracy:','{:.3f}'.format(acc)) # graph for training acc=history.history['accuracy']#acc val_acc=history.history['val_accuracy']#val_acc epochs=range(1,len(acc)+1) plt.plot(epochs,acc,'b',label='Training accuracy') plt.plot(epochs,val_acc,'r',label='Val accuracy') plt.legend() plt.show()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/04 17:36 編集
2021/03/04 21:28
2021/03/05 18:47