试图在Keras中创建卷积神经自动编码器网络,但它不断崩溃

时间:2019-04-17 11:04:02

标签: python keras

我正在尝试使用keras创建自动编码器,并且我的数据形状如下所示: (62328, 1, 40, 40)

错误:

  

ValueError:负尺寸大小是由于输入形状为[?,1,40,40],[3,3,40,4]的'conv2d / Conv2D'(op:'Conv2D')从1中减去3引起的

,我不知道如何解决。我尝试将data_format更改为channels_lastchannels_first,但仍然无法正常工作。

请帮助

K.set_image_data_format('channels_last')
dense_layer = 0
layer_size = 4
conv_layer = 1
IMG_SIZE = 40

NAME = "AutoEncoder-{}-conv-{}-nodes-{}-dense-{}".format(conv_layer, layer_size, dense_layer, int(time.time()))

加载数据

pickle_in = open("X5.pickle","rb")
X = pickle.load(pickle_in)
pickle_in.close()
X=np.array(X)
print( X.shape)
X= X/255

pickle_in = open("y5.pickle","rb")
y = pickle.load(pickle_in)
pickle_in.close()
y=np.array(y)

从喀拉拉邦的模型开始

model = Sequential()
#encoding

这是我的问题发生的地方

shape=[1,IMG_SIZE,IMG_SIZE]
print (shape)
model.add(Conv2D(4, (3,3),input_shape = shape))

编码/解码数据

model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

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

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

#decoding
model.add(UpSampling2D((2,2)))
model.add(Conv2D(2, (3,3)))
model.add(Activation('relu'))

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

model.add(UpSampling2D((2,2)))
model.add(Conv2D(4, (3,3)))
model.add(Activation('relu'))

model.add(Conv2D(1,(3,3)))
model.add(Activation('sigmoid'))
tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'],
              )

model.summary()

model.fit(X,X,
          batch_size=32,
          epochs=10,
          validation_split=0.3,
          callbacks=[tensorboard])
model.save("64x3-CND.model")

1 个答案:

答案 0 :(得分:0)

这是因为您的输入形状不正确,并且data_format变量设置为channels_last,所以图像的输入形状应为(HEIGHT,WIDTH,CHANNELS_NUM)。

data_format更改为channels_first应该可以解决您的问题。

K.set_image_data_format('channels_first')