model.summary() output is not consistent with model definition

Viewed 1019

I'm using the subclassing API to build a simple conv net and I want to use the summary method to get an idea of what the architecture looks like for my model. However, when I call model.summary(), the layers are out of order and the output shape is not shown either. Is there a clean way of getting around this? or do I need to override the model.summary() method in the model class.

Here are the layers in question:

class thing(keras.Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.conv1 = keras.layers.convolutional.Conv2D(96, 
                                                       kernel_size= (11, 11), 
                                                       strides= 4, 
                                                       activation = "relu",
                                                       data_format="channels_last",
                                                       input_shape= (277,277, 3))

        self.flatten = keras.layers.Flatten(data_format="channels_last")
        
        self.dense = keras.layers.Dense(4096, activation= "relu")

        self.pool = keras.layers.pooling.MaxPooling2D(pool_size= (3,3), strides = 2,
                                                      data_format="channels_last")
    def call(self, inputs):
        conv1 = self.conv1(inputs)
        pool1 = self.pool(conv1)
        flatten_conv = self.flatten(pool1)
        ff_1 = self.dense(flatten_conv)

        return ff_1


a = thing()

a.build(input_shape=(None, 277, 277, 3))

a.summary()



OUTPUT: 
Model: "thing_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_9 (Conv2D)            multiple                  34944     
_________________________________________________________________
flatten_9 (Flatten)          multiple                  0         
_________________________________________________________________
dense_14 (Dense)             multiple                  415240192 
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 multiple                  0         
=================================================================
Total params: 415,275,136
Trainable params: 415,275,136
Non-trainable params: 0
_________________________________________________________________

1 Answers

The model.summary function using tensorflow.python.keras.utils.layer_utils.print_summary function to print the info of the model structure, it loop over model.layer to print all the layers info, and the model.layer is a list contain all the layers you defined within the model (i.e with self.), the order of layers inside this list is determined by the order you define the layers.

So you could define the layers with same order as you call the layers (will not give you the info about layers connection and layers outputs shape though), or you could getting around this by define a simple summary function inside your custom model class:

def summary_model(self):
    inputs = keras.Input(shape=(277, 277, 3))
    outputs = self.call(inputs)
    keras.Model(inputs=inputs, outputs=outputs, name="thing").summary()

and call it using:

a.summary_model()

which outputs:

Model: "thing"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(None, 277, 277, 3)]     0
_________________________________________________________________
conv2d (Conv2D)              (None, 67, 67, 96)        34944
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 33, 33, 96)        0
_________________________________________________________________
flatten (Flatten)            (None, 104544)            0
_________________________________________________________________
dense (Dense)                (None, 4096)              428216320
=================================================================
Total params: 428,251,264
Trainable params: 428,251,264
Non-trainable params: 0
_________________________________________________________________
Related