機械学習及びpython初心者です。
kerasでのモデル構築がうまくいきません。
ValueError Traceback (most recent call last)
<ipython-input-49-4638987c047f> in <module>
50 epochs=10,
51 batch_size=6,
---> 52 validation_data=(X_test,y_test))
~\Anaconda3\lib\site-packages\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)
1152 sample_weight=sample_weight,
1153 class_weight=class_weight,
-> 1154 batch_size=batch_size)
1155
1156 # Prepare validation data.
~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
133 ': expected ' + names[i] + ' to have ' +
134 str(len(shape)) + ' dimensions, but got array '
--> 135 'with shape ' + str(data_shape))
136 if not check_batch_axis:
137 data_shape = data_shape[1:]
ValueError: Error when checking input: expected conv2d_98_input to have 4 dimensions, but got array with shape (219, 150, 150)
該当のソースコード
import numpy as np
from keras import layers, models
from keras import optimizers
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D
from keras.layers import Activation,Dropout,Flatten,Dense
from keras.utils import np_utils
import matplotlib.pyplot as plt
model = models.Sequential()
model.add(layers.Conv2D(32,(3,3),activation="relu",input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64,(3,3),activation="relu"))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128,(3,3),activation="relu"))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128,(3,3),activation="relu"))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512,activation="relu"))
model.add(layers.Dense(10,activation="sigmoid")) #分類先の種類分設定
#モデル構成の確認
model.summary()
model.compile(loss="binary_crossentropy",
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=["acc"])
categories = ['a', 'b']
nb_classes = len(categories)
X_train, X_test, y_train, y_test = np.load('data/train.npy')
#データの正規化
X_train = X_train.astype("float") / 255
X_test = X_test.astype("float") / 255
#kerasで扱えるようにcategoriesをベクトルに変換
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
#モデルの学習
model = model.fit(X_train,
y_train,
epochs=10,
batch_size=6,
validation_data=(X_test,y_test))
input_shapeの書き方が違うのだと思いますがどうすれば解決できれば良いかわかりません。
ご指導いただけると幸いです。
参考にしているサイトのソースです
https://qiita.com/tomo_20180402/items/e8c55bdca648f4877188
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。