如何使用tf.keras.Model.summary查看父模型中子模型的层?

我有一个tf.keras.Model的子类模型,代码如下:

import tensorflow as tfclass Mymodel(tf.keras.Model):    def __init__(self, classes, backbone_model, *args, **kwargs):        super(Mymodel, self).__init__(self, args, kwargs)        self.backbone = backbone_model        self.classify_layer = tf.keras.layers.Dense(classes,activation='sigmoid')    def call(self, inputs):        x = self.backbone(inputs)        x = self.classify_layer(x)        return xinputs = tf.keras.Input(shape=(224, 224, 3))model = Mymodel(inputs=inputs, classes=61,                 backbone_model=tf.keras.applications.MobileNet())model.build(input_shape=(20, 224, 224, 3))model.summary()

结果是:

_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================mobilenet_1.00_224 (Model)   (None, 1000)              4253864   _________________________________________________________________dense (Dense)                multiple                  61061     =================================================================Total params: 4,314,925Trainable params: 4,293,037Non-trainable params: 21,888_________________________________________________________________

但我想查看mobilenet的所有层,于是我尝试提取mobilenet的所有层并放入模型中:

import tensorflow as tfclass Mymodel(tf.keras.Model):    def __init__(self, classes, backbone_model, *args, **kwargs):        super(Mymodel, self).__init__(self, args, kwargs)        self.backbone = backbone_model        self.classify_layer = tf.keras.layers.Dense(classes,activation='sigmoid')    def my_process_layers(self,inputs):        layers = self.backbone.layers        tmp_x = inputs        for i in range(1,len(layers)):            tmp_x = layers[i](tmp_x)        return tmp_x    def call(self, inputs):        x = self.my_process_layers(inputs)        x = self.classify_layer(x)        return xinputs = tf.keras.Input(shape=(224, 224, 3))model = Mymodel(inputs=inputs, classes=61,                 backbone_model=tf.keras.applications.MobileNet())model.build(input_shape=(20, 224, 224, 3))model.summary()

结果没有变化。

    _________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================mobilenet_1.00_224 (Model)   (None, 1000)              4253864   _________________________________________________________________dense (Dense)                multiple                  61061     =================================================================Total params: 4,314,925Trainable params: 4,293,037Non-trainable params: 21,888_________________________________________________________________

然后我尝试提取一层插入到模型中:

结果也没有变化。我很困惑。

_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================mobilenet_1.00_224 (Model)   (None, 1000)              4253864   _________________________________________________________________dense (Dense)                multiple                  244       =================================================================Total params: 4,254,108Trainable params: 4,232,220Non-trainable params: 21,888_________________________________________________________________

但我发现Dense层的参数变了,我不知道发生了什么。


回答:

为了能够查看backbone的层,你需要使用backbone.inputbackbone.output来构建你的新模型

from tensorflow.keras.models import Modeldef  Mymodel(backbone_model, classes):    backbone = backbone_model    x = backbone.output    x = tf.keras.layers.Dense(classes,activation='sigmoid')(x)    model = Model(inputs=backbone.input, outputs=x)    return modelinput_shape = (224, 224, 3)model = Mymodel(backbone_model=tf.keras.applications.MobileNet(input_shape=input_shape, include_top=False, pooling='avg'),                classes=61)model.summary()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注