Home > OS >  AttributeError: 'Flatten' object has no attribute 'shape'
AttributeError: 'Flatten' object has no attribute 'shape'

Time:12-11

I am new to TensorFlow and was trying to implement a CNN model using tf.keras.layers API. This is the code that I am trying to implement.

def convolutional_model(input_shape):
    input_img = tf.keras.Input(shape=input_shape)
    Z1 = tf.keras.layers.Conv2D(filters = 16 , kernel_size= (4,4), strides = (1,1), padding='same')(input_img)
    A1 = tf.keras.layers.ReLU()
    P1 = tf.keras.layers.MaxPool2D(pool_size=(8,8), strides=(8, 8), padding='same')
    Z2 = tf.keras.layers.Conv2D(filters = 16 , kernel_size= (2,2), strides = (1,1), padding='same')(input_img)
    A2 = tf.keras.layers.ReLU()
    P2 = tf.keras.layers.MaxPool2D(pool_size=(4,4), strides=(4, 4), padding='valid')
    F = tf.keras.layers.Flatten()
    outputs = tf.keras.layers.Dense(units=6, activation='softmax')(F)

    model = tf.keras.Model(inputs=input_img, outputs=outputs)
    return model

When I try to run this I get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-66-12f400853748> in convolutional_model(input_shape)
     43     P2 = tf.keras.layers.MaxPool2D(pool_size=(4,4), strides=(4, 4), padding='valid')
     44     F = tf.keras.layers.Flatten()
---> 45     outputs = tf.keras.layers.Dense(units=6, activation='softmax')(F)
     46 
     47     # YOUR CODE ENDS HERE

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    980       with ops.name_scope_v2(name_scope):
    981         if not self.built:
--> 982           self._maybe_build(inputs)
    983 
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2616     if not self.built:
   2617       input_spec.assert_input_compatibility(
-> 2618           self.input_spec, inputs, self.name)
   2619       input_list = nest.flatten(inputs)
   2620       if input_list and self._dtype_policy.compute_dtype is None:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    164         spec.min_ndim is not None or
    165         spec.max_ndim is not None):
--> 166       if x.shape.ndims is None:
    167         raise ValueError('Input '   str(input_index)   ' of layer '  
    168                          layer_name   ' is incompatible with the layer: '

AttributeError: 'Flatten' object has no attribute 'shape'

The function runs without any error when I replace F with input_img but that is not the output I want. Can someone help me with how to correct this?

CodePudding user response:

The Keras functional API is designed so you pass in the previous layers of the model as input to the next layer. You didn't really do that for most of the layers you've defined, and instead made them standalone layers that aren't connected to each other. To pass the results of one layer to another, you need to call the layer by passing in the previous layer. For instance, you might've meant something like this.

Z1 = tf.keras.layers.Conv2D(filters = 16 , kernel_size= (4,4), strides = (1,1), padding='same')(input_img)
A1 = tf.keras.layers.ReLU()(Z1)
  • Related