Eka
Eka

Reputation: 15010

How to view the summary of neural networks with keras functional api

I have a very large neural network which I am making using keras functional api. I want to monitor the parameters and shape of layers added in a model which is not defined yet something like model.summary().

If I have a model like this

input_img = Input(shape=(256, 256, 3))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
#stage1

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
#stage2

tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
#stage3

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

I want the summary() of this progressing model at these various stages. I know we can do model.summary() by defining the model=Model(input,output) but can we do this as we are progressing through the layers?

Upvotes: 4

Views: 1389

Answers (3)

Dr. Snoopy
Dr. Snoopy

Reputation: 56367

You can easily get the compile-time shape of any Keras tensor with the _keras_shape member variable, like:

input_img = Input(shape=(256, 256, 3))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
#stage1

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
#stage2

tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
#stage3

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

print("Output shape is: {}".format(output._keras_shape))

You can do this at any point in your computation as long as you have a TensorVariable (the output of a layer). Its not the same as the full summary but it helps a lot for debugging.

Upvotes: 2

Vipin Makde
Vipin Makde

Reputation: 86

Didn't have much reputation points to comment.

You must refer TENSORBOARD for get various kind of visualization related to Deep Learning and is easy to use too.

Upvotes: 1

Daniel Möller
Daniel Möller

Reputation: 86600

An ugly answer (since I don't have a pretty one)

You can define as many models as you like and take their summaries:

input_img = Input(shape=(256, 256, 3))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)

model = Model(input_img, [tower1,tower2,tower3])
model.summary()


tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

model = Model(input_img, [tower1,tower2,tower3])
model.summary()

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

model = Model(input_img, output)
model.summary()

Upvotes: 2

Related Questions