victor zhao
victor zhao

Reputation: 73

'Model' object has no attribute '_name'

I'm making a CNN in Keras. But I have a problem in making Keras model. Here's my code:

x = Input(shape=(256,256,1))
for i in range(16):
    u = int(16 * 2 ** (i//4))
    x = BatchNormalization()(x)
    x1 = Conv2D(u, kernel_size=(1,1), strides=(1,1), activation='relu')(x)
    x1 = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x1)
    x2 = Conv2D(u, kernel_size=(2,2), strides=(1,1), activation='relu')(x)
    x2 = MaxPooling2D(pool_size=(2,2), strides=(1,1))(x2)
    x3 = Conv2D(u, kernel_size=(3,3), strides=(1,1), activation='relu')(x)
    x3 = MaxPooling2D(pool_size=(1,1), strides=(1,1))(x3)
    x = multiply([x1, x2, x3])
    #x = Dropout(0.45)(x)
    x = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x)
out = BatchNormalization()(x)
model = tf.keras.models.Model(inputs=x, outputs=out)

and I got the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-99-630b3ef0b15f> in <module>()
     13     x = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x)
     14 out = BatchNormalization()(x)
---> 15 model = tf.keras.models.Model(inputs=x, outputs=out)
...

AttributeError: 'Model' object has no attribute '_name'

Upvotes: 0

Views: 2399

Answers (1)

today
today

Reputation: 33410

The problem is that you are assigning other Tensors to x after defining it as Input tensor. Therefore, it can't be used as the input of the model, i.e. inputs=x. To resolve this with minimal modifications simply store x in another variable after defining it as Input tensor:

x = Input(shape=(256,256,1))
inp = x

# the rest is the same...

model = tf.keras.models.Model(inputs=inp, outputs=out) # pass `inp` as inputs

Upvotes: 1

Related Questions