model.summary() - AttributeError: 'Tensor' object has no attribute 'summary'

model.summary() - AttributeError: 'Tensor' object has no attribute 'summary'

这是我的进口商品:

import tensorflow as tf
import keras
from keras.models import Sequential, Model
from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from keras.utils import Sequence

我定义了这个模型:

def create_ST_layer(input_shape = (64, 128, 3)):
    input_img = Input(shape=input_shape)
    model = Conv2D(48, kernel_size=(5, 5), input_shape = input_shape, strides = (1, 1), activation = "relu")(input_img)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Conv2D(32, kernel_size=(5, 5), strides = (1, 1), activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Dense(50, activation = "relu")(model)
    model = Dense(6)(model)

    return model

并通过以下方式创建模型:

model = create_ST_layer()

当我现在尝试获取模型的摘要时:

model.summary()

我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-5f15418b3570> in <module>()
----> 1 model.summary()

AttributeError: 'Tensor' object has no attribute 'summary'

我的导入有问题吗?

非常感谢!

我在 Google Colab 上的 tensorflow 2.2.0 上测试了这个。 我会改变一些事情来开始。使用新的 tensorflow 版本,而不是导入 keras,你应该导入 tensorflow.keras。 所以你的代码对于导入看起来像这样:

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from tensorflow.keras.utils import Sequence

您还需要调用以下行将层分组到具有训练和推理特征的对象中。 [型号 link] : https://www.tensorflow.org/api_docs/python/tf/keras/Model

因此您的完整代码将如下所示:

import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from tensorflow.keras.utils import Sequence

def create_ST_layer(input_shape = (64, 128, 3)):
    input_img = Input(shape=input_shape)
    model = Conv2D(48, kernel_size=(5, 5), input_shape = input_shape, strides = (1, 1), activation = "relu")(input_img)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Conv2D(32, kernel_size=(5, 5), strides = (1, 1), activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Dense(50, activation = "relu")(model)
    model = Dense(6)(model)
    model = tf.keras.Model(inputs=input_img, outputs= model)
    return model

model = create_ST_layer()
model.summary()

我使用您的模型得到以下输出: enter image description here

因为只需一层一层地添加就可以创建一个 Tensorflow 图。如果你想创建 Keras 模型,你应该-

  1. 使用 model.add() 添加图层。 [link]
  2. 通过 keras.models.Model() 创建 Tensorflow 图后创建 Keras 模型。

使用第二种方法,你可以这样做:

def create_ST_layer(input_shape = (64, 128, 3)):
    input_img = Input(shape=input_shape)
    model = Conv2D(48, kernel_size=(5, 5), input_shape = input_shape, strides = (1, 1), activation = "relu")(input_img)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Conv2D(32, kernel_size=(5, 5), strides = (1, 1), activation = "relu")(model)
    model = MaxPooling2D(pool_size=(2, 2), strides = (2, 2))(model)
    model = Dense(50, activation = "relu")(model)
    model = Dense(6)(model)
    myModel = Model(input_img, model)
    return myModel
model = create_ST_layer()
model.summary()