简体   繁体   English

ValueError:检查目标时出错:预期activation_7具有形状(154,154,1),但形状为(200,200,3)的数组

[英]ValueError: Error when checking target: expected activation_7 to have shape (154, 154, 1) but got array with shape (200, 200, 3)

I'm trying to build CNN Autoencoder. 我正在尝试构建CNN自动编码器。 Each training image(color) has size of 200*200. 每个训练图像(彩色)的尺寸为200 * 200。 I got the error while compiling: ValueError: Error when checking target: expected activation_7 to have shape (154, 154, 1) but got array with shape (200, 200, 3) with following code. 我在编译时收到错误: ValueError: Error when checking target: expected activation_7 to have shape (154, 154, 1) but got array with shape (200, 200, 3)具有以下代码的ValueError: Error when checking target: expected activation_7 to have shape (154, 154, 1) but got array with shape (200, 200, 3) How can the code can modified to work ? 怎样修改代码才能起作用?

autoencoder = Sequential()

autoencoder.add(Conv2D(64, (3, 3), input_shape=(200, 200, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))

autoencoder.add(Conv2D(32, (3, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))

autoencoder.add(Conv2D(32, (3, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2))) # encoded

autoencoder.add(Conv2D(32, (3, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(32, (3, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(64, (3, 3)))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(1, (3, 3)))
autoencoder.add(Activation('sigmoid'))

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

As i see it, there are two main issues with your code above. 如我所见,上面的代码存在两个主要问题。

First is dimension decrease, from (200,200) to (154,154) witch can easily be solved by padding='same' to every Conv2D layer like this: 首先是尺寸减小,从(200,200)到(154,154),可以通过对每个Conv2D层进行padding ='same'来轻松解决,例如:

autoencoder = Sequential()

autoencoder.add(Conv2D(64, (3, 3), input_shape=(200, 200, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))

autoencoder.add(Conv2D(32, (3, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))

autoencoder.add(Conv2D(32, (3, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2))) # encoded

autoencoder.add(Conv2D(32, (3, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(32, (3, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(64, (3, 3), padding='same'))
autoencoder.add(Activation('relu'))
autoencoder.add(UpSampling2D((2,2)))

autoencoder.add(Conv2D(1, (3, 3), padding='same'))
autoencoder.add(Activation('sigmoid'))

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

Second issue is with your target. 第二个问题是您的目标。 What you are trying to predict here with output (200,200,1) at your last Conv2D layer, with sigmoid activation and binary_crossentropy loss is a grayscale image. 您要在此处预测的最后一个Conv2D层的输出(200,200,1) ,具有sigmoid activationbinary_crossentropy loss是一个灰度图像。 If this is what you want you have to train your model with y_train being 1 channel images (200,200,1) otherwise you can change your output to (200,200,3) like this: 如果这是您想要的,则必须使用y_train为1通道图像(200,200,1)来训练模型,否则可以将输出更改为(200,200,3),如下所示:

change: 更改:

autoencoder.add(Conv2D(1, (3, 3), padding='same'))

to: 至:

autoencoder.add(Conv2D(3, (3, 3), padding='same'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查目标时出错:预期 dense_3 有 2 个维度,但得到了形状为 (5, 200, 200, 1) 的数组 - Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (5, 200, 200, 1) ValueError:检查目标时出错:预期activation_5 的形状为(1,),但数组的形状为(100,) - ValueError: Error when checking target: expected activation_5 to have shape (1,) but got array with shape (100,) ValueError:检查目标时出错:预期 activation_9 具有形状 (74, 6) 但得到形状为 (75, 6) 的数组 - ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6) ValueError:检查目标时出错:预期activation_1的形状为(158,),但数组的形状为(121,) - ValueError: Error when checking target: expected activation_1 to have shape (158,) but got array with shape (121,) ValueError:检查目标时出错:预期 activation_6 具有形状(70,)但得到形状为(71,)的数组 - ValueError: Error when checking target: expected activation_6 to have shape (70,) but got array with shape (71,) ValueError:检查目标时出错:预期激活具有形状 (1,) 但得到形状为 (2,) 的数组 - ValueError: Error when checking target: expected activation to have shape (1,) but got array with shape (2,) Keras - “ValueError:检查目标时出错:预期activation_1具有形状(无,9)但得到的数组具有形状(9,1) - Keras - "ValueError: Error when checking target: expected activation_1 to have shape (None, 9) but got array with shape (9,1) 检查目标时出错:预期 time_distributed_6 具有 3 个维度,但得到形状为 (200, 80) 的数组 - Error when checking target: expected time_distributed_6 to have 3 dimensions, but got array with shape (200, 80) ValueError:检查目标时出错:预期 activation_10 有 2 个维度,但得到了形状为 (118, 50, 1) 的数组 - ValueError: Error when checking target: expected activation_10 to have 2 dimensions, but got array with shape (118, 50, 1) keras:ValueError:检查模型目标时出错:预期activation_1具有形状(无,60),但数组的形状为(10,100) - keras: ValueError: Error when checking model target: expected activation_1 to have shape (None, 60) but got array with shape (10, 100)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM