简体   繁体   中英

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?

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.

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM