简体   繁体   English

`model.summary()` TensorFlow model 子类打印 output 形状为“多个”

[英]`model.summary()` with TensorFlow model subclassing print output shape as "multiple"

I tried to implement Vgg.network with following VggBlock.我尝试使用以下 VggBlock 实现 Vgg.network。

class VggBlock(tf.keras.Model):
  def __init__(self, filters, repetitions):
    super(VggBlock, self).__init__()
    self.repetitions = repetitions

    self.conv_layers = [Conv2D(filters=filters, kernel_size=(3, 3), padding='same', activation='relu') for _ in range(repetitions)]
    self.max_pool = MaxPool2D(pool_size=(2, 2))

  def call(self, inputs):
    x = inputs
    for layer in self.conv_layers:
      x = layer(x)
    return self.max_pool(x)

test_block = VggBlock(filters=64, repetitions=2)
temp_inputs = Input(shape=(224, 224, 3))
test_block(temp_inputs)
test_block.summary()

Then the above code prints:然后上面的代码打印:

Model: "vgg_block"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             multiple                  1792      
                                                                 
 conv2d_1 (Conv2D)           multiple                  36928     
                                                                 
 max_pooling2d (MaxPooling2D  multiple                 0         
 )                                                               
                                                                 
=================================================================
Total params: 38,720
Trainable params: 38,720
Non-trainable params: 0
_________________________________________________________________

And if I build Vgg with these blocks, its summary() also prints "multiple".如果我用这些块构建 Vgg,它的summary()也会打印“multiple”。

There are some questions similar to my problem, ex: https://github.com/keras-team/keras/issues/13782 , model.summary() can't print output shape while using subclass model有一些问题类似于我的问题,例如: https://github.com/keras-team/keras/issues/13782model.summary() can't print output shape while using subclass model

However, I can not extend the answers in the second link: in terms of varying input_shape .但是,我无法扩展第二个链接中的答案:根据不同的input_shape

How do I treat summary() in order to make "multiple" to be an appropriate shape.我如何对待summary()以使“多个”成为合适的形状。

You already linked some workarounds.您已经链接了一些解决方法。 You seem to be landing here , because the output shape of each layer cannot be determined.你好像是登陆这里,因为output各层的形状无法确定。 As stated here :如此所述:

You can do all these things (printing input / output shapes) in a Functional or Sequential model because these models are static graphs of layers.您可以在功能或顺序 model 中执行所有这些操作(打印输入/ output 形状),因为这些模型是 static 层图。

In contrast, a subclassed model is a piece of Python code (a call method).相比之下,子类 model 是一段 Python 代码(调用方法)。 There is no graph of layers here.这里没有图层图。 We cannot know how layers are connected to each other (because that's defined in the body of call, not as an explicit data structure), so we cannot infer input / output shapes.我们不知道层是如何相互连接的(因为这是在调用主体中定义的,而不是作为显式数据结构),所以我们无法推断输入 / output 形状。

You could also try something like this:你也可以尝试这样的事情:

import tensorflow as tf

class VggBlock(tf.keras.Model):

  def __init__(self, filters, repetitions, image_shape):
    super(VggBlock, self).__init__()
    self.repetitions = repetitions

    self.conv_layers = [tf.keras.layers.Conv2D(filters=filters, kernel_size=(3, 3), padding='same', activation='relu') for _ in range(repetitions)]
    self.max_pool = tf.keras.layers.MaxPool2D(pool_size=(2, 2))

    inputs = tf.keras.layers.Input(shape=image_shape)
    x = inputs
    for layer in self.conv_layers:
      x = layer(x)
    outputs = self.max_pool(x)
    self.model = tf.keras.Model(inputs, outputs)

  def call(self, inputs):
    return self.model(inputs)
  
  def summary(self):
    self.model.summary()

test_block = VggBlock(filters=64, repetitions=2, image_shape=(224, 224, 3))
test_block.summary()
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 224, 224, 3)]     0         
                                                                 
 conv2d (Conv2D)             (None, 224, 224, 64)      1792      
                                                                 
 conv2d_1 (Conv2D)           (None, 224, 224, 64)      36928     
                                                                 
 max_pooling2d (MaxPooling2D  (None, 112, 112, 64)     0         
 )                                                               
                                                                 
=================================================================
Total params: 38,720
Trainable params: 38,720
Non-trainable params: 0
_________________________________________________________________

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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