Keras autoencoder outputting wrong shape


Question: 

I'm trying to build a deep convolutional autoencoder in Keras, but it keeps outputting the wrong shape.

Code:

def build_network(input_shape):
    input_input =  Input(shape=input_shape)

    #Encode
    x = Conv2D(16, (3, 3), activation='relu', padding = 'same')(input_input)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)

    #Decode
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(x) 
    x = UpSampling2D((2, 2))(x)
    decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
    autoencoder = Model(input_input, decoded)
    return autoencoder


if __name__ == "__main__":
    print(build_network((1, 32, 32)).layers[-1].output)

I expect the output shape to be the same as the input shape, but it is instead (8, 32, 1) for (1, 32, 32)




1 Answer: 

Try to use print(build_network((32,32,1)).layers[-1].output). Or if you want to use channel first than you need to change model like this,

def build_network(input_shape):
    input_input =  Input(shape=input_shape)

    #Encode
    x = Conv2D(16, (3, 3), activation='relu', padding = 'same')(input_input)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = MaxPooling2D((2, 2), padding='same')(x)

    #Decode
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
    x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
    x = Conv2D(16, (3, 3), activation='relu', padding='same')(x) 
    decoded = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
    # decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
    autoencoder = Model(input_input, decoded)
    return autoencoder

if __name__ == "__main__":
    print(build_network((1, 32, 32)).layers[-1].output)

Because in UpSampling2D, default is "channels_last".

 

More Articles


How to disable "Project Files Changes" alert in phpstorm?

How to disable this alert box in phpstorm.

wpf - Adding NAudio controls to toolbox

I am trying to use the controls from NAudio library such as WaveViewer, VolumeMeter, VolumeSlider, Fader, WaveformPainter etc. I am using Visual Studio 2012 and have net 4.5 installed. I have also downloaded and referenced NAudio 1.7 and its .dll to my project. When I right click on toolbox and clic

configuration - PhpStorm configure to work remotely

Hi is there a way for PhpStorm to work directly on a remote server? No local files. Because as of the moment PhpStorm has local files wherein it just automatically uploads all files during save on remote.My problem is if someone changes something remotely I need to manually download it first before


c# - WindowsForm app with wpf windows is not working on other computers

I'm new to WPF and c# programming,i have built a winform app with a database and some wpf forms.wpf forms contain some background images and componentone tab controls.To add wpf forms:i added a wpf user control and modified its code as a window.in that case i m unable to put background images from u

android - How to get a progress bar under the ActionBar in Material Design?

The Material Design specifications for Progress and activity specifically show in several areas a horizontal progress indicator of only a few pixels (dips) thick below whatever it is we are calling the ActionBar these days (probably Toolbar).However, I don't see any way to actually implement that po

phpStorm show changed/modififed files in project view

After moving from Zend Studio (ZS) to phpStorm I am starting to miss the way that ZS used to show changed files and display more than one project at once. E.g: notice how the IndexController.php shows that this has been modified. Is there anyway to do this with phpStorm?


AttributeError: 'NoneType' object has no attribute '_inbound_nodes' in Autoencoders in Keras

I am not able to solve the following error, please accept my apologies if it sounds naive, I am very new to Keras.The output of the encoder is actually a complex value, so each output is real and imaginary part, input_h1 is also a complex value with real and imaginary parts represented as a vector.

c# - MVVM Bind to Page.DataContext from TabControl

I'm structuring my WPF application using MVVM Light and am creating the ViewModel using the IOC.The page initializes its DataContext like this:DataContext="{Binding Main, Source={StaticResource Locator}}"A TabControl has its content bound to another ViewModel, so binding from within the TabControl w

PHPstorm project navigation

How to select opened file in projects list in PHPstorm? I mean, have a PHPstorm "select in projects" Netbeans function analog.

python - Keras Model not learning using Model API

I am running following code to make keras model for classifaction and I can not figure out what is wrong as all the layers seem to be fine.The model is not learningDataset used: Cifar-10Envoirment: Google colabCode:# Dataset Propsinput_shape = (32, 32, 3)total_cats = 10#Code for making modelinputs =