前提・実現したいこと
CNNを用いて画像の分類をしています。
前処理した画像の情報は以下の通りです。
X_train.shape: (900, 32, 32, 3)
X_test.shape: (50, 32, 32, 3)
y_train.shape: (900, 1)
y_test.shape: (50, 1)
そのあと、Kerasが処理できる数値型に X_train と X_test を変換し、その後正規化を実行。
正解ラベル(y_trainとy_test)をOne-Hot表現
Y_train.shape: (900, 10)
Y_test.shape: (50, 10)
この後、画像の分類予測を行うようなmodelを構築するコードを完成→損失関数をimport→modelをコンパイル
まで実行したのですが、学習結果をhistに返すところでエラーが発生してしまいました。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-1fa3cc2074ec> in <module>() 5 validation_data=(X_test, Y_test), 6 verbose=1, ----> 7 batch_size=batch_size) ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 726 max_queue_size=max_queue_size, 727 workers=workers, --> 728 use_multiprocessing=use_multiprocessing) 729 730 def evaluate(self, ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs) 222 validation_data=validation_data, 223 validation_steps=validation_steps, --> 224 distribution_strategy=strategy) 225 226 total_samples = _get_total_number_of_samples(training_data_adapter) ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing) 545 max_queue_size=max_queue_size, 546 workers=workers, --> 547 use_multiprocessing=use_multiprocessing) 548 val_adapter = None 549 if validation_data: ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _process_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing) 592 batch_size=batch_size, 593 check_steps=False, --> 594 steps=steps) 595 adapter = adapter_cls( 596 x, ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset) 2536 # Additional checks to avoid users mistakenly using improper loss fns. 2537 training_utils.check_loss_and_target_compatibility( -> 2538 y, self._feed_loss_fns, feed_output_shapes) 2539 2540 # If sample weight mode has not been set and weights are None for all the ~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes) 741 raise ValueError('A target array with shape ' + str(y.shape) + 742 ' was passed for an output of shape ' + str(shape) + --> 743 ' while using as loss `' + loss_name + '`. ' 744 'This loss expects targets to have the same shape ' 745 'as the output.') ValueError: A target array with shape (900, 10) was passed for an output of shape (None, 14, 14, 10) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
該当のソースコード
Python
1# Kerasが処理できる数値型にX_trainとX_testを変換 2X_train = X_train.astype('f') 3X_test = X_test.astype('f') 4# 正規化 5X_train /= 255.0 6X_test /= 255.0 7# 正解ラベル(y_trainとy_test)をOne-Hot表現 8from keras.utils import np_utils 9Y_train = np_utils.to_categorical(y_train, num_classes=10).astype('i') 10Y_test = np_utils.to_categorical(y_test, num_classes=10).astype('i') 11print("Y_train.shape: ",Y_train.shape) 12print("Y_test.shape: ",Y_test.shape) 13 14model = Sequential() 15## 2次元畳み込み層1 16model.add(Conv2D(kernel_size=(3, 3),filters=32,activation='relu',input_shape=input_shape)) 17## 2次元畳み込み層2 18model.add(Conv2D( kernel_size=(3, 3),filters=64,activation='relu',)) 19## maxプーリング層 20model.add(MaxPooling2D(pool_size=(2, 2))) 21## 全結合層1 22model.add(Dropout(0.25)) 23model.add(Dense(units=128, activation='relu')) 24## 全結合層2 25model.add(Dropout(0.50)) 26model.add(Dense(units=10, activation='softmax')) 27model.summary() 28# コンパイル 29from tensorflow.keras.optimizers import SGD 30model.compile(loss='categorical_crossentropy', 31 optimizer=SGD(0.01), 32 metrics=['accuracy']) 33#学習を行う 34hist = model.fit(X_train, 35 Y_train, 36 epochs=1, 37 validation_data=(X_test, Y_test), 38 verbose=1, 39 batch_size=batch_size)
補足情報(FW/ツールのバージョンなど)
JupyterNotebook
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/25 02:33