AttributeError: Tensor.op is meaningless when eager execution is enabled

Viewed 3233

I am trying to implement RESNET 50 from scratch. After accumulating all the layers, I call tf.keras.Model. However, it gives an error:

AttributeError: Tensor.op is meaningless when eager execution is enabled.

For testing, I am inputting a 4-D tensor. conv_diff_size and conv_same_size are two custom blocks having con2d and batch-normalization layers. I am using TensorFlow 2.0 on Google Colab.

def ResNet50(inputs, classes):
  X = tf.keras.layers.Conv2D(64, kernel_size = (7,7), strides=2, padding='valid', data_format='channels_last', input_shape = inputs.shape)(inputs)
  X = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9)(X)
  X = tf.keras.layers.MaxPool2D(pool_size=(3, 3), strides=2)(X)

  X = conv_diff_size(X, [64, 64, 256])
  X = conv_same_size(X, [64, 64, 256])
  X = conv_same_size(X, [64, 64, 256])
  
  X = conv_diff_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])
  
  X = conv_diff_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  
  X = conv_diff_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
              
  X = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), name = 'avg_pool')(X)
  X = tf.keras.layers.Flatten()(X)
  X = tf.keras.layers.Dense(classes, activation='relu')(X)
  
  model = tf.keras.Model(inputs=X, outputs = X)
  return model
3 Answers

Sometimes this happens if you forgot to compile your model first. Make sure you're running model.compile(...) before you begin training.

If you are testing gradually your scripts in an interactive python environment (e.g. changing layers design after compilation or training), try to remove all the variables and start over.

the problem in your code is you are giving X as input as well as output.

Try this

import tensorflow as tf

def ResNet50(input_shape, classes):
  inputs = tf.keras.Input(shape=input_shape)#input_shape = (224,224,3)
  X = tf.keras.layers.Conv2D(64, kernel_size = (7,7), strides=2, padding='valid', data_format='channels_last', input_shape = inputs.shape)(inputs)
  X = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.9)(X)
  X = tf.keras.layers.MaxPool2D(pool_size=(3, 3), strides=2)(X)

  X = conv_diff_size(X, [64, 64, 256])
  X = conv_same_size(X, [64, 64, 256])
  X = conv_same_size(X, [64, 64, 256])

  X = conv_diff_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])
  X = conv_same_size(X, [128, 128, 512])

  X = conv_diff_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])
  X = conv_same_size(X, [256, 256, 1024])

  X = conv_diff_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
  X = conv_same_size(X, [512, 512, 2048])
            
  X = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), name = 'avg_pool')(X)
  X = tf.keras.layers.Flatten()(X)
  out = tf.keras.layers.Dense(classes, activation='relu')(X)

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

I have added a input tensor layer and assigned it to variable inputs and the final layer to variable out

Related