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

Viewed 8367

This are my imports:

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

I defined this model:

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

And created the model by:

model = create_ST_layer()

When I now try to get the summary of the model:

model.summary()

I get the following error:

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

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

Is there something wrong with my imports?

Thanks a lot!

2 Answers

I tested this on tensorflow 2.2.0 on Google Colab. I would change a couple of things to start with. With the new tensorflow version, rather than importing keras you should import tensorflow.keras. So your code would look like this for imports:

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

Also you need to call the following line to groups layers into an object with training and inference features. [Model link] : https://www.tensorflow.org/api_docs/python/tf/keras/Model

So your complete code would look something like this:

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

I get the following output with your model: enter image description here

Because just adding layers one by one will create a Tensorflow graph. If you want to create Keras model, you should either-

  1. add layers using model.add(). [link]
  2. create Keras model after creating a Tensorflow graph by keras.models.Model().

Using 2nd method, you could do:

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