1
目標:評估一組5幅圖像並生成一幅圖像作爲輸出。Keras錯誤擬合模型:提供的元素過多
問題:我目前收到錯誤提供的元素太多。
聲明:我是新來的Keras和深度學習作爲一個整體,我完全相信,這種做法是錯誤的,但我想知道爲什麼我得到這個錯誤
輸出的形狀並輸入看起來正確的給我。我試過讓輸出只是一個形狀爲(無,6912)的密集層。
我試過讓輸出成爲Conv2d,但後來我得到以下錯誤,我不確定輸出是爲什麼(46,46,3),而不是(48,48,3)
Error when checking target: expected conv2d_1 to have shape (None, 46, 46, 3) but got array with shape (379, 48, 48, 3)
代碼:
width = 48
height = 48
png = []
for image_path in glob.glob(r"D:\temp\*.png"):
png.append(misc.imread(image_path))
im = np.asarray(png)
print ('dataset: ', im.shape)
window = 6
dataset = np.zeros([len(im) - window, window,width,height,3])
for i in range(len(dataset)):
dataset[i, :] = im[i:i + window]
x_train = dataset[:,:-1]
y_train = dataset[:,-1]
y_train1 = y_train.reshape(-1,width*height*3)
print("x_train: ", x_train.shape)
print("y_train:" ,y_train.shape)
print("y_train1:" ,y_train1.shape)
model = Sequential()
model.add(Conv3D(filters=40,
kernel_size=(5,10,10),
input_shape=(5,width,height,3),
padding='same',
activation='relu'))
model.add(Activation('relu'))
model.add(Conv3D(filters=40,
kernel_size=(3,3,3),
padding='same',
activation='relu'))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(width * height * 3, activation='softmax'))
model.add(Reshape((48,48,3)))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print("model Input: " ,model.input_shape)
print("model output:", model.output_shape)
model.fit(x_train, y_train, batch_size=10, epochs=300, validation_split=0.05)
輸出:
Using TensorFlow backend.
dataset: (385, 48, 48, 3)
x_train: (379, 5, 48, 48, 3)
y_train: (379, 48, 48, 3)
y_train1: (379, 6912)
model Input: (None, 5, 48, 48, 3)
model output: (None, 48, 48, 3)
Traceback (most recent call last):
....
File "D:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 424, in make_tensor_proto
(shape_size, nparray.size))
ValueError: Too many elements provided. Needed at most -1109917696, but received 1
模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv3d_1 (Conv3D) (None, 5, 48, 48, 40) 60040
_________________________________________________________________
activation_1 (Activation) (None, 5, 48, 48, 40) 0
_________________________________________________________________
conv3d_2 (Conv3D) (None, 5, 48, 48, 40) 43240
_________________________________________________________________
flatten_1 (Flatten) (None, 460800) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 460800) 0
_________________________________________________________________
dense_1 (Dense) (None, 6912) -110991078
_________________________________________________________________
reshape_1 (Reshape) (None, 48, 48, 3) 0
=================================================================
感謝先進。
在conv2D的情況下,您可能忘記使用'padding ='same''。使用(3,3)內核將丟棄2個像素。 ----現在,除了使用'categorical_crossentropy'之外,在代碼中看不到任何問題。你只在分類問題中使用它,爲了以防萬一,我建議你試試'mse'。 –
'model.summary()'的輸出是什麼? –
我已更新模型摘要。我確實發現了「-110991078」這個有趣的數字。關於conv2D你是對的,我沒有'padding ='same''。我也會嘗試'mse' –