-1

I have a simple autoencoder and I want to see the output of each layer especially the latent space. I know that after each layer some features like edges achieved but I want to show the output of each layer. I want to know what is the output of each layer and precisely I want to visualize the output of each layer in my autoencoder. what should I do for this and how can I do this? because I do not have a true imagination of my network and I want to know what happens in each layer of my network. I really need it. please help me with this issue. I am waiting to hear from you.

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.datasets import mnist
import numpy as np
input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
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)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
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')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

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

#train part


(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format
from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
maedeh
  • 105
  • 2
  • 10
  • Possible duplicate of [Visualizing output of convolutional layer in tensorflow](https://stackoverflow.com/questions/33802336/visualizing-output-of-convolutional-layer-in-tensorflow) – IanQ Jan 16 '19 at 22:45
  • I work with Keras and I am a beginner, so what should I do in Keras? – maedeh Jan 17 '19 at 03:07

1 Answers1

0

Use Keras Tutorial to visualize the output.

First layers you expect Haar-like Features (kind of lots of straight lines and so forth). All the best!

IanQ
  • 1,831
  • 5
  • 20
  • 29
  • I want to show the output of the intermediate layer as an image how can I do it? please give a sample code or a link that has sample code because I am not familiar with keras professionally. – maedeh Jan 20 '19 at 05:48
  • yes, I read it but it used VGG I want to have my network and show my results on mnist!? – maedeh Jan 20 '19 at 16:55
  • Hi, no one can help me? I also saw this link https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer that is about showing the intermediate layer. but I could not understand what is x and what should I put instead of it? please explain me what should I do now? I know this is simple, but I am a beginner;( – maedeh Jan 21 '19 at 02:10
  • X is your only. Did you read the link I sent? The concepts are the same but it goes in depth into the discussion – IanQ Jan 21 '19 at 14:13
  • If you changed the code you should update your question to reflect that. If the change is very large you should open a new, more specific question – IanQ Feb 05 '19 at 21:30
  • I did not change my code. I just the code in the mentioned address to visualize the intermediate layers. although it also does not work sometimes): finally I could not find an answer to my question. – maedeh Feb 05 '19 at 23:57