前提・実現したいこと
tensorflow,kerasでCNNを実装させたいと考えています
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-26-4e988137566f> in <module> 39 model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"]) 40 history = model.fit(X_train, Y_train, batch_size=5, nb_epoch=10, verbose=1, ---> 41 validation_split=0.4) C:\ProgramData\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. C:\ProgramData\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: C:\ProgramData\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_41_input to have 4 dimensions, but got array with shape (45, 250, 250)
該当のソースコード
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255.0 X_test /= 255.0 Y_train = np_utils.to_categorical(y_train, 3) Y_test = np_utils.to_categorical(y_test, 3) # モデルの定義 model = Sequential() model.add(Conv2D(16,(7,7),strides=(1,1),padding='valid',input_shape=(250,250,1))) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(30,(3,3),strides=(1,1),padding='valid')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(16,(3,3),strides=(1,1),padding='same')) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(10,(3,3),strides=(1,1),padding='same')) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(1,3),strides=(2,2))) model.add(Activation('relu')) model.add(Dense(10)) model.add(Activation('relu')) model.add(Dense(3, activation='softmax')) adam = Adam(lr=1e-8) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"]) history = model.fit(X_train, Y_train, batch_size=5, nb_epoch=10, verbose=1, validation_split=0.4)
試したこと
入力データなど確認しましたが、特に異常はありませんでした
補足情報(FW/ツールのバージョンなど)
tensorflow-gpu 2.2.0
Keras 2.3.1
いえ、「4次元の入力を期待しているが3次元しかない」ので、入力データの次元数が足りていません。
元の画像はグレースケールで大きさなどは揃えてあります
import os, glob
from PIL import Image
import numpy as np
folder = ['predata0','predata1','predata2']
X = []
y = []
for index, name in enumerate(folder):
dir = "./" + name
files = glob.glob(dir + "/*.bmp")
for i, file in enumerate(files):
image = Image.open(file)
data = np.asarray(image)
X.append(data)
y.append(index)
X = np.array(X)
y = np.array(y)
このように画像データからデータを作ったのですがここが間違えなんですかね
X_trainは以下の通りです
array([[[0.45490196, 0.40392157, 0.43529412, ..., 0.38431373,
0.34509805, 0.3372549 ],
[0.44313726, 0.4392157 , 0.43137255, ..., 0.3764706 ,
0.31764707, 0.33333334],
[0.4392157 , 0.40784314, 0.38431373, ..., 0.3647059 ,
0.3764706 , 0.38431373],
...,
[0.47058824, 0.4862745 , 0.4509804 , ..., 0.46666667,
0.4862745 , 0.50980395],
[0.46666667, 0.44705883, 0.47843137, ..., 0.42352942,
0.44313726, 0.49019608],
[0.47058824, 0.4509804 , 0.47058824, ..., 0.40392157,
0.40392157, 0.46666667]],
[[0.27058825, 0.33333334, 0.3882353 , ..., 0.45490196,
0.45490196, 0.54509807],
[0.30588236, 0.34117648, 0.4 , ..., 0.4392157 ,
0.44313726, 0.47058824],
[0.32156864, 0.3529412 , 0.3764706 , ..., 0.40392157,
0.43529412, 0.29411766],
...,
[0.4509804 , 0.41568628, 0.44705883, ..., 0.35686275,
0.48235294, 0.4117647 ],
[0.43529412, 0.42352942, 0.37254903, ..., 0.42745098,
0.4 , 0.47843137],
[0.47843137, 0.49411765, 0.3882353 , ..., 0.40784314,
0.45490196, 0.50980395]]], dtype=float32)
X.shapeは?おそらく、色データの次元がありません
X.shapeは(60,250,250)で、おっしゃられる通り色データの次元がありませんでした。
情けない話ですが、どのようにすれば色データの次元を追加できるのかも教えていただいてよろしいですか?
逆に、なぜ抜けるの?と聞きたい。
Image.open直後のimageと、いや、ここか。np.asarrayの引数が足りない事ないですか?参考にしたコード、numpyのリファレンスをよく確認してください。
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)
X_train.shape
(45, 250, 250, 1)
このように自分で追加することにしたんですが、それでもならず、、、
Error when checking target: expected dense_24 to have 4 dimensions, but got array with shape (45, 3)
回答1件
あなたの回答
tips
プレビュー