Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: When subclassing the `Model` class, you should implement a `call` method. on tensorflow custom model

I am trying to train my custom model on Cifar 10 dataset. My model's code is below: -

class cifar10Model(keras.Model):
  def __init__(self):
    super(cifar10Model, self).__init__()
    self.conv1 = keras.layers.Conv2D(32, 3, activation='relu', input_shape=(32, 32, 3))
    self.pool1 = keras.layers.MaxPool2D((3, 3))
    self.batch_norm1 = keras.layers.BatchNormalization()
    self.dropout1 = keras.layers.Dropout(0.1)

    self.conv2 = keras.layers.Conv2D(64, 3, activation='relu')
    self.pool2 = keras.layers.MaxPool2D((3, 3))
    self.batch_norm2 = keras.layers.BatchNormalization()
    self.dropout2 = keras.layers.Dropout(0.2)

    self.conv3 = keras.layers.Conv2D(128, 3, activation='relu')
    self.pool3 = keras.layers.MaxPool2D((3, 3))
    self.batch_norm3 = keras.layers.BatchNormalization()
    self.dropout3 = keras.layers.Dropout(0.3)

    self.flatten = keras.layers.Flatten()
    self.dense1 = keras.layers.Dense(128, activation='relu')
    self.dense2 = keras.layers.Dense(10)

    def call(self, x):
      x = self.conv1(x)
      x = self.pool1(x)
      x = self.batch_norm1(X)
      x = self.dropout1(x)

      x = self.conv2(x)
      x = self.pool2(x)
      x = self.batch_norm2(X)
      x = self.dropout2(x)

      x = self.conv3(x)
      x = self.pool3(x)
      x = self.batch_norm3(x)
      x = self.dropout3(x)

      x = self.flatten(x)
      x = self.dense1(x)
      return self.dense2(x)

model = cifar10Model()

When i run this code this gives me no error.

Then i defined my training loop

loss_object = keras.losses.SparseCategoricalCrossentropy(from_logits=True)

optimizer = keras.optimizers.Adam()

train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')

test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')

@tf.function
def train_step(images, labels):
  with tf.GradientTape() as tape:
    predictions = model(images, training=True)
    loss = loss_object(labels, predictions)
  grad = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(zip(grad, model.trainable_variables))
  train_loss(loss)
  train_accuracy(labels, predictions)

@tf.function
def test_step(images, labels):
  predictions = model(images)
  t_loss = loss_object(labels, predictions)

  test_loss(t_loss)
  test_accuracy(labels, predictions)

epochs = 10

for epoch in range(epochs):
  train_loss.reset_states()
  train_accuracy.reset_states()
  test_loss.reset_states()
  test_accuracy.reset_states()

  for images, labels in train_dataset:
    train_step(images, labels)

  for images, labels in test_dataset:
    test_step(images, labels)

  template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
  print(template.format(epoch + 1,
                        train_loss.result(),
                        train_accuracy.result() * 100,
                        test_loss.result(),
                        test_accuracy.result() * 100))

When i run this code, i get the following error

NotImplementedError: When subclassing the `Model` class, you should implement a `call` method.

I am currently running my code on google colab.

My colab link is https://colab.research.google.com/drive/1sOlbRpPRdyOCJI0zRFfIA-Trj1vrIbWY?usp=sharing

My tensorflow version on colab is 2.2.0

Also, when i tried to predict labels from untrained model by this code :-

print(model(train_images))

This also gives me the same error. The error is saying that i have not implemented the call method on model. but, i have defined the call method.

I also tried by changing the call method to __call__ method.

But still, it gives me the same error.

Thanks in advance :-

like image 488
programmer pro Avatar asked Jan 01 '23 01:01

programmer pro


1 Answers

The problem is with indentation. You've defined call method inside __init__. Try defining it outside the __init__ method as follows:

class cifar10Model(keras.Model):
  def __init__(self):
    super(cifar10Model, self).__init__()
    self.conv1 = keras.layers.Conv3D(32, 3, activation='relu', input_shape=(32, 32, 3))
    self.pool1 = keras.layers.MaxPool3D((3, 3, 3))
    self.batch_norm1 = keras.layers.BatchNormalization()
    self.dropout1 = keras.layers.Dropout(0.1)

    self.conv2 = keras.layers.Conv3D(64, 3, activation='relu')
    self.pool2 = keras.layers.MaxPool3D((3, 3, 3))
    self.batch_norm2 = keras.layers.BatchNormalization()
    self.dropout2 = keras.layers.Dropout(0.2)

    self.conv3 = keras.layers.Conv3D(128, 3, activation='relu')
    self.pool3 = keras.layers.MaxPool3D((3, 3, 3))
    self.batch_norm3 = keras.layers.BatchNormalization()
    self.dropout3 = keras.layers.Dropout(0.3)

    self.flatten = keras.layers.Flatten()
    self.dense1 = keras.layers.Dense(128, activation='relu')
    self.dense2 = keras.layers.Dense(10)

  def call(self, x):
    x = self.conv1(x)
    x = self.pool1(x)
    x = self.batch_norm1(X)
    x = self.dropout1(x)

    x = self.conv2(x)
    x = self.pool2(x)
    x = self.batch_norm2(X)
    x = self.dropout2(x)

    x = self.conv3(x)
    x = self.pool3(x)
    x = self.batch_norm3(X)
    x = self.dropout3(x)

    x = self.flatten(x)
    x = self.dense1(x)
    return self.dense2(x)

model = cifar10Model()

Hope this helps.

like image 128
Abhinav Goyal Avatar answered Jan 02 '23 13:01

Abhinav Goyal