简体   繁体   中英

Trying to create a convolutional neural Autoencoder network in Keras but it keeps crashing

I'm trying to create an autoencoder with keras and my data's shape is like this: (62328, 1, 40, 40)

The error:

ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d/Conv2D' (op: 'Conv2D') with input shapes: [?,1,40,40], [3,3,40,4]

and I don't know how to fix it. I've tried changing the data_format to channels_last or channels_first , but still it doesn't work.

Please help

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()))

loading the data

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)

starting with the model in keras

model = Sequential()
#encoding

this is where my problem happens

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

encoding / decoding the data

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")

This is because your input shape is incorrect, with the data_format variable set to channels_last , the input shape of the images is expected to be (HEIGHT, WIDTH, CHANNELS_NUM).

Changing the data_format to be channels_first should fix your problem.

K.set_image_data_format('channels_first')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM