ローカルのディレクトリから画像を読み込んで、trainingとvalidationに分けた後でimagedatagenerationを使用したいのですが、どのようなコードに変えたらいいのか教えていただきたいです。
python
1# coding:utf-8 2 3import keras 4from keras.utils import np_utils 5from keras.layers.convolutional import Conv2D, MaxPooling2D 6from keras.models import Sequential 7from keras.layers.core import Dense, Dropout, Activation, Flatten 8from keras.preprocessing.image import array_to_img, img_to_array, list_pictures, load_img 9import numpy as np 10import pandas as pd 11from sklearn.model_selection import train_test_split 12import matplotlib.pyplot as plt 13from sklearn.neighbors import KNeighborsClassifier 14 15#【学習データの読み込み】 16X = [] 17Y = [] 18 19 20ep = 30 21size = 80,80 22 23#a 24for picture in list_pictures('/Users/train/a'): 25 img = img_to_array(load_img(picture, target_size=(size))) 26 X.append(img) 27 28 Y.append(0) 29 30#b 31for picture in list_pictures('/Users/train/b'): 32 img = img_to_array(load_img(picture, target_size=(size))) 33 X.append(img) 34 35 Y.append(1) 36 37 38#c 39for picture in list_pictures('/Users/train/c'): 40 img = img_to_array(load_img(picture, target_size=(size))) 41 X.append(img) 42 43 Y.append(2) 44 45# load_imgをarrayに変換 46X = np.asarray(X) 47Y = np.asarray(Y) 48 49 50# 画素値を0から1の範囲に変換 51X = X.astype('float32') 52X = X / 255.0 53 54# クラスの形式を変換 55Y = np_utils.to_categorical(Y, 8) 56 57# 学習用データとテストデータ 58X_train, y_train= train_test_split(X, Y, test_size=0.2) 59 60# 【CNNを構築】 61model = Sequential() 62#モデルを作るためのモジュール、モデル層を積み重ねたもの 63 64model.add(Conv2D(32, (3, 3), padding='same', 65 input_shape=X_train.shape[1:])) 66model.add(Activation('relu')) 67model.add(Conv2D(32, (3, 3))) 68model.add(Activation('relu')) 69model.add(MaxPooling2D(pool_size=(2, 2))) 70model.add(Dropout(0.4)) 71model.add(Conv2D(64, (3, 3), padding='same')) 72model.add(Activation('relu')) 73model.add(Conv2D(64, (3, 3))) 74model.add(Activation('relu')) 75model.add(MaxPooling2D(pool_size=(2, 2))) 76model.add(Dropout(0.4)) 77model.add(Flatten()) 78model.add(Dense(512)) 79model.add(Activation('relu')) 80model.add(Dropout(0.4)) 81model.add(Dense(8)) 82model.add(Activation('softmax')) 83 84# 【コンパイル】 85model.compile(loss='categorical_crossentropy', 86 optimizer='adam', 87 metrics=['accuracy']) 88 89#【学習実行】 90history = model.fit(X_train, y_train, batch_size=64, epochs=ep, verbose = 1) 91 92 93 94model.save_weights('abc.weight.h5') 95 96#【学習をグラフ表示】 97%matplotlib inline 98plt.plot(history.history['acc']) 99plt.plot(history.history['val_acc']) 100plt.xlabel('epoch') 101plt.ylabel('accuracy') 102plt.title('model accuracy') 103plt.legend(['train', 'test'], loc='upper left') 104plt.show() 105plt.plot(history.history['loss']) 106plt.plot(history.history['val_loss']) 107plt.xlabel('epoch') 108plt.ylabel('loss') 109plt.title('model loss') 110plt.legend(['loss', 'val_loss'], loc='upper left') 111plt.show() 112 113
a.b.cの3カテゴリーに計300枚の画像が入っているとした時に
test_split=0.2でtrain=240枚、validation=60枚に分け、その後で
オーギュメンテーションをして1枚の画像から5枚の水増し画像を生成したいです。
結果的にtrain=1200枚、validation=300枚で学習をスタートさせ、最終的に
testカテゴリに入っている自分が選択した画像でtestを行いたいです
オーギュメンテーションの組み込み方がわからないので、ご教授頂きたいです。
あなたの回答
tips
プレビュー