print out the shape of each layer in the net architecture

亡梦爱人 提交于 2019-12-22 09:06:19

问题


In Keras, we can define the network as follows. Are there any way to output the shape after each layer. For instance, I want to print out the shape of inputs after the line defining inputs, then print out the shape of conv1 after the line defining conv1, etc.

inputs = Input((1, img_rows, img_cols))
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(inputs)
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(pool1)
conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

回答1:


If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via: layer.input_shape

from keras.utils.layer_utils import layer_from_config

config = layer.get_config()
layer = layer_from_config(config)

Source: https://keras.io/layers/about-keras-layers/

May be this the easiest way to do:

model.layers[layer_of_interest_index].output_shape



回答2:


Just using model.summary(), which gives you pretty print.




回答3:


To print the complete model and all of its dependencies you can also look here: https://keras.io/visualization/

I used this command to save my model visualization as a png:

from keras.utils.visualize_util import plot
plot(model, to_file='model.png')

If you only want to print the layer shape you can do something like this:

layer = model.layers[-1]
print(layer.output._keras_shape)

Prints: (None, 1, 224, 224) # Nr. Filters, Channels, x_dim, y_dim



来源:https://stackoverflow.com/questions/40733984/print-out-the-shape-of-each-layer-in-the-net-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!