0

I want to visualize the layers in capsule networks. For that I need shape of intermediate layers. The code is as follows:

from keras.models import Model
model = Sequential()
conv1 = model.add(Conv2D(256, (9, 9), padding='valid', strides = 1, input_shape = (28, 28, 1), activation = 'relu', name = 'conv1'))
model.add(Conv2D(256, (9,9), padding='valid', strides = 2, name = 'primarycaps_conv2d'))
model.add(keras.layers.core.Reshape([-1,8]))
model.add(keras.layers.core.Lambda(squash, name = 'hello'))

layer_name = 'conv1'
intermediate_layer_model = Model(inputs=model.input,
                             outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)

Throws the following error:

NameError: name 'data' is not defined

How to overcome this?

Anusha Mehta
  • 99
  • 1
  • 1
  • 10

1 Answers1

0

Referring this, I got the solution as follows:

model = Sequential()
conv1 = model.add(Conv2D(256, (9, 9), padding='valid', strides = 1, input_shape = (28, 28, 1), activation = 'relu', name = 'conv1'))
model.add(Conv2D(256, (9,9), padding='valid', strides = 2, name = 'primarycaps_conv2d'))
model.add(keras.layers.core.Reshape([-1,8]))
model.add(keras.layers.core.Lambda(squash, name = 'hello'))

idx = 3  # index of desired layer
input_shape = model.layers[idx].get_output_shape_at(0)
print(input_shape)

get the output shape as follows:

(None, 1152, 8).

The problem of getting specific layer's output is solved.

Anusha Mehta
  • 99
  • 1
  • 1
  • 10