0

I have an autoencoder and I want to show the output of each layer as an image due to I want to know what each layer do on its input. my simple code is here. I used sth like this

from keras import backend as K
inputs = [K.learning_phase()] + autoencoder.inputs

_convout1_f = K.function(inputs, [autoencoder.layers[12].output])
def convout1_f(X):
    # The [0] is to disable the training phase flag
    return _convout1_f([0] + [X])
C1 = convout1_f(X)
C1 = np.squeeze(C1)

to access the output of each layer. for example the output of layer 12 is (28,28,16) this means I have 16 filter in cov layer. now how can I show the output of this layer as a separte images?

from __future__ import absolute_import
from __future__ import print_function
import theano
import warnings
import os 
import pylab as pl
import matplotlib.cm as cm
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.layers import Input
from keras.layers import Conv2D
from keras.models import Model
from keras.callbacks import TensorBoard
from keras import backend as K
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D,UpSampling2D
from keras.utils import np_utils



print(theano.config.device)
warnings.filterwarnings("ignore", category=DeprecationWarning) 
np.set_printoptions(precision=5, suppress=True)
nb_classes = 10

# the data, shuffled and split between tran and test sets
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
#
#X_train = X_train.reshape(X_train.shape[0],1,28, 28)
#X_test = X_test.reshape(X_test.shape[0], 1,28, 28)
#X_train = X_train.astype("float32")
#X_test = X_test.astype("float32")
#X_train /= 255
#X_test /= 255
#print('X_train shape:', X_train.shape)
#print(X_train.shape[0], 'train samples')
#print(X_test.shape[0], 'test samples')
#####################################################################
input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='conv1' )(input_img)
mxp1 = MaxPooling2D((2, 2), padding='same')(conv1)
conv2 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conv2')(mxp1)
mxp2 = MaxPooling2D((2, 2), padding='same')(conv2)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conv3')(mxp2)
encoded = MaxPooling2D((2, 2), padding='same')(conv3)

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

conv4 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conv4')(encoded)
us1 = UpSampling2D((2, 2))(conv4)
conv5 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conv5')(us1)
us2 = UpSampling2D((2, 2))(conv5)
conv6 = Conv2D(16, (3, 3), activation='relu', name='conv6')(us2)
up3 = UpSampling2D((2, 2))(conv6)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up3)

conv1_model=Model(input_img,conv1)

encoder=Model(input_img,encoded)
#decoder=Model(input_img,encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')


(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
autoencoder.fit(x_train, x_train,
                epochs=5,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
maedeh
  • 105
  • 2
  • 10
  • 1
    https://stackoverflow.com/questions/43305891/how-to-correctly-get-layer-weights-from-conv2d-in-keras – Luke DeLuccia Jan 21 '19 at 19:43
  • thank you. this code was for weight output if I want to show the output of layers what should I do? instead of using get_weight() I should use get_output()? another question is about showing the results. when I use this code, it shows the results in my console and it is very small. how can I show the output of plt.imshow in a new window instead of the console? sorry, but I am a beginner. – maedeh Jan 21 '19 at 20:10
  • Hi, could you please guide me for showing the output of intermediate layer? I used the mentioned code and this is my fit function w_extraction.fit([x_train,w_expand], [x_train,w_expand], epochs=200, batch_size=128, validation_data=([x_validation,wv_expand], [x_validation,wv_expand]), callbacks=[TensorBoard(log_dir='E:/tmp/AutewithW200', histogram_freq=0, write_graph=False)]) when I want to show my intermediate layer it produces error about tensorflow feeding. what should I do? – maedeh Jan 28 '19 at 18:03

0 Answers0