Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to tensorflow I was tring to use tf.concat so I used this layout instead of the regular Sequential layout. But the error I get is AttributeError: 'tuple' object has no attribute 'layer' The error exists in the 2nd line

inp = Input(shape=(1050,1050,3))
x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
x1= layers.MaxPooling2D(2,2)(x1)
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
x2= layers.Conv2D(64,(3,3), activation='relu')(x2)
x2= layers.MaxPooling2D(3,3)(x2)
x3= layers.Conv2D(64,(3,3), activation='relu')
x3= layers.Conv2D(64,(2,2), activation='relu')(x3)
x3= layers.Conv2D(64,(3,3), activation='relu')(x3)
x3= layers.Dropout(0.2)(x3)
x3= layers.MaxPooling2D(2,2)(x3)
x4= layers.Conv2D(64,(3,3), activation='relu')
x4= layers.MaxPooling2D(2,2)(x4)
x = layers.Dropout(0.2)(x4)
o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
y = layers.Flatten()(o)
y = layers.Dense(1024, activation='relu')(y)
y = layers.Dense(5, activation='softmax')(y) 

model = Model(inp, y)
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])

The imported files are

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import pandas as pd
import shutil
import csv
import tensorflow as tf
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import layers
from tensorflow.keras import Model
from keras.layers import Input

The error is

AttributeError                            Traceback (most recent call last)
<ipython-input-8-40840424e579> in <module>
      1 inp = Input(shape=(1050,1050,3))
----> 2 x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
      3 x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
      4 x1= layers.MaxPooling2D(2,2)(x1)
      5 x2= layers.Conv2D(32,(3,3), activation='relu')(x1)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    661               kwargs.pop('training')
    662             inputs, outputs = self._set_connectivity_metadata_(
--> 663                 inputs, outputs, args, kwargs)
    664           self._handle_activity_regularization(inputs, outputs)
    665           self._set_mask_metadata(inputs, outputs, previous_mask)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
   1706     kwargs.pop('mask', None)  # `mask` should not be serialized.
   1707     self._add_inbound_node(
-> 1708         input_tensors=inputs, output_tensors=outputs, arguments=kwargs)
   1709     return inputs, outputs
   1710 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
   1793     """
   1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
-> 1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,
   1797                                       input_tensors)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
    513 
    514   return pack_sequence_as(
--> 515       structure[0], [func(*x) for x in entries],
    516       expand_composites=expand_composites)
    517 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in <lambda>(t)
   1792             `call` method of the layer at the call that created the node.
   1793     """
-> 1794     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
   1795                                         input_tensors)
   1796     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,

AttributeError: 'tuple' object has no attribute 'layer'

Please Anyone tell me What to Do The Code is little changed than before Please take a look again

like image 415
Lawlesx Avatar asked Jan 01 '23 17:01

Lawlesx


2 Answers

You forgot to pass x2 an input argument in the fourth line, also same with x3 and x4. So instead of writing

x2= layers.Conv2D(32,(3,3), activation='relu')

You should have

x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
like image 154
Mohammad hp Avatar answered Jan 03 '23 06:01

Mohammad hp


You need to instance a Input layer to give the input to your first layer:

inp = Input(shape=(1050,1050,3))
x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
x1= layers.MaxPooling2D(2,2)(x1)
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
x2= layers.Conv2D(64,(3,3), activation='relu')(x2)
x2= layers.MaxPooling2D(3,3)(x2)
x3= layers.Conv2D(64,(3,3), activation='relu')(x2)
x3= layers.Conv2D(64,(2,2), activation='relu')(x3)
x3= layers.Conv2D(64,(3,3), activation='relu')(x3)
x3= layers.Dropout(0.2)(x3)
x3= layers.MaxPooling2D(2,2)(x3)
x4= layers.Conv2D(64,(3,3), activation='relu')(x3)
x4= layers.MaxPooling2D(2,2)(x4)
x = layers.Dropout(0.2)(x4)
o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
y = layers.Flatten()(o)
y = layers.Dense(1024, activation='relu')(y)
y = layers.Dense(5, activation='softmax')(y) 

model = Model(inp, y)
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])

As mentioned in the other answer, you also did not pass the correct input to one of the Conv2D layers. And you cannot use tf functions directly on Keras tensors, Keras already has a layer to perform concatenation.

like image 42
Dr. Snoopy Avatar answered Jan 03 '23 07:01

Dr. Snoopy