前提・実現したいこと
現在CNN、Kerasを用いて画像認識を行っています。
その中でホールアウト法では学習を行うことはできたのですが、
クロスバリデーションで学習をさせてみたいです。
その中で以下のエラーが発生したので、
教えていただけると幸いです。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "learning2.py", line 161, in <module> shuffle=True,callbacks=[early_stopping]) File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training.py", line 970, in fit batch_size=batch_size) File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training.py", line 787, in _standardize_user_data exception_prefix='target') File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training_utils.py", line 127, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (10800, 3, 3, 3)
該当のソースコード
import keras from keras.utils import np_utils from keras.models import Sequential from keras.layers.convolutional import MaxPooling2D from keras.layers import Conv2D, Flatten, Dense, Dropout from sklearn.model_selection import train_test_split from PIL import Image from keras.callbacks import EarlyStopping import numpy as np import glob import matplotlib.pyplot as plt from sklearn.model_selection import KFold from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix classes = ["犬","猫","兎" ] num_classes = len(classes) X = [] Y = [] for index, classlabel in enumerate(classes): dir = "./" + classlabel files = glob.glob(dir + "/*.jpg") for i, file in enumerate(files): image = Image.open(file) image = image.convert("RGB") image = image.resize((128, 128)) data = np.asarray(image) X.append(data) Y.append(index) X = np.array(X) Y = np.array(Y) X = X.astype('float32')/255.0 Y = np_utils.to_categorical(Y, num_classes) X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.1) kf = KFold(n_splits=10, shuffle=True) all_loss=[] all_val_loss=[] all_acc=[] all_val_acc=[] for train_index, val_index in kf.split(X_train, y_train): train_data=X_train[train_index] train_label=y_train[train_index] val_data=X_train[val_index] val_label=y_train[val_index] y_train = np_utils.to_categorical(y_train, num_classes) y_test = np_utils.to_categorical(y_train, num_classes) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same',input_shape=X_train.shape[1:], activation="relu")) model.add(Conv2D(32, (3, 3), padding='same', activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same', activation="relu")) model.add(Conv2D(64, (3, 3), padding='same', activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(128, (3, 3), padding='same', activation="relu")) model.add(Conv2D(128, (3, 3), padding='same', activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128 ,activation="relu")) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation="softmax")) # model.summary() opt = keras.optimizers.Adam(lr=0.0005, decay=1e-5) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # patience -> 10回学習して精度が上がらなかったとき、終了する early_stopping = EarlyStopping(monitor='val_acc', min_delta=0.8, patience=5, verbose=1) hist = model.fit(train_data, train_label, batch_size=10, verbose=1, epochs=10, validation_data=(val_data, y_test), shuffle=True,callbacks=[early_stopping]) score=model.evaluate(X_test, y_test) print('Test loss:', score[0]) print('test acc:', score[1]) model.save('./cnn.h5')
試したこと
様々なサイトを見たのですが、Flatten()が必要など、
すでに行っていたことしか出てこず、どのようにすればよいかわかりません。
あなたの回答
tips
プレビュー