Issue
I've been following a convolutional autoencoder, derived from https://medium.com/analytics-vidhya/building-a-convolutional-autoencoder-using-keras-using-conv2dtranspose-ca403c8d144e using a data set of 72x72 greyscale images. I've been able to obtain a trainable model, but have been getting issues in applying it to my data.
Convolution/deconvolution:
input = Input(shape=(72,72,1))
e_input = Conv2D(32, (3, 3), activation='relu')(input) #70 70 32
e = MaxPooling2D((2, 2))(e_input) #35 35 32
e = Conv2D(64, (3, 3), activation='relu')(e) #33 33 64
e = MaxPooling2D((2, 2))(e) # 16 16 64
e = Conv2D(64, (3, 3), activation='relu')(e) #16 16 64
e = Flatten()(e) #1 1 12544
e_output = Dense(324, activation='softmax')(e) #1 1 324
d = Reshape((18,18,1))(e_output) #18 18 1, found to be generally N/4 units (so 7 for 28x28)
d = Conv2DTranspose(64,(3, 3), strides=2, activation='relu', padding='same')(d) #36 36 64
d = Conv2DTranspose(64,(3, 3), strides=2, activation='relu', padding='same')(d) #72 72 64
d = Conv2DTranspose(64,(3, 3), activation='relu', padding='same')(d) #72 72 64
d_output = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(d) #72 72 1
And here's the code for separate encoding and decoding, as well as defining the autoencoder model:
autoencoder = Model(input, d_output)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=["mae"])
encoder = Model(input, e_output)
encoded_input = Input(shape=(1,1))
decoder_layer = autoencoder.layers[-6](encoded_input)
l2 = autoencoder.layers[-5](decoder_layer)
decoder = Model(encoded_input, l2)
Following training, I inputted the following, wherein x_train is (2433, 72, 72).
encoded_imgs = encoder.predict(x_train)
decoded_imgs = decoder.predict(encoded_imgs)
And I obtain the error for decoded_imgs: "ValueError: Input 0 of layer "model_5" is incompatible with the layer: expected shape=(None, 1, 1), found shape=(None, 324)", but changing encoded_input's shape to (324) gives rise to another error (ValueError: Exception encountered when calling layer "dense_2" (type Dense).) in calling back layers in decoded_layer...
I speculate I'm not reshaping data at the correct time, or perhaps I'm unnecessarily using some models. Any help would be greatly appreciated.
Solution
The input of the decoder is the output of the encoder. In the model the shape of the encoder's output is (None, 324)
but you are passing an input of shape (1, 1)
. The shape of the latent layer is (None, 324)
. You can create a separate decoder model as follows:
decoder=Model(e_output, d_output)
Please find the working code here. Thank you!
Answered By - Tfer3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.