"model.summary()",TensorFlow模型子类化打印输出形状作为"multiple"



我尝试用以下VggBlock实现Vgg网络。

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

那么上面的代码打印:

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
_________________________________________________________________

如果我用这些块构建Vgg,它的summary()也会打印"多重"。

有一些问题与我的问题类似,例如:https://github.com/keras-team/keras/issues/13782,model.summary()不能't在使用子类model

时输出形状然而,我不能扩展第二个链接的答案:在变化的input_shape方面。

我如何处理summary()以使"多重";是一个合适的形状。

您已经链接了一些变通方法。你好像是在这里着陆,因为无法确定每层的输出形状。如下所示:

你可以做所有这些事情(打印输入/输出形状)在一个功能或顺序模型,因为这些模型是静态图层。

相反,子类化的模型是一段Python代码(调用)方法)。这里没有图层图。我们不知道层是怎样的相互连接(因为这是在调用,而不是作为显式数据结构),因此我们不能推断输入/输出的形状。

你也可以这样做:

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
_________________________________________________________________

相关内容

  • 没有找到相关文章

最新更新