Keras错误拟合模型:提供的元素太多

时间:2017-05-12 16:01:54

标签: python deep-learning keras

目标:评估一组5张图像并生成图像作为输出。

问题:我目前收到错误提供的元素太多。

免责声明:我对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         
=================================================================

先谢谢了。

1 个答案:

答案 0 :(得分:0)

Conv2D案例中,您可能忘记使用padding = 'same'。 (3,3)内核在每个维度中删除2个像素。

通过Dense图层中显示的负数参数,我相信你的模型比支持的更大。

可能单个图层的参数有最大限制。在这种情况下,我会减少卷积中的滤波器数量,或者在展平层之前对通道求和。