No gradient on ResNetv2 101 with TensorFlow

Viewed 85

With TensorFlow 2.3, on Ubuntu 18.04, in Python, I want to train a residual network to generate heatmaps. I am new to TF. I could solve all my issues so far until I get the exception No gradients provided for any variable. I am able to reproduce the exception with the following elementary code.

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        #input_tensor=x1,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

model.compile(optimizer='adam', loss="mean_squared_error", run_eagerly=True)

train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
model.fit(train_ds, epochs=1)

This code ends with "ValueError: No gradients provided for any variable".

I could find related errors on the Internet. I think I would be able to write my own learning loop using tf.GradientTape but I would expect the above code to work without the need of a custom learning loop. Does anyone know why the above code fails ?


After some digging, here is what I came up with.

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
opt = tf.keras.optimizers.Adam()
loss_fn = lambda: tf.keras.losses.mse(model(input), output)
for input, output in train_ds:
  opt.minimize(loss_fn, model.trainable_weights)

I still don't know why the original code generates a No gradient exception (so I don't consider this edit as an answer).

I found the basic training loops not helpful for my specific problem. I followed this example on the Optimizer doc page.

1 Answers

I tried to replicate and resolve your code in TF 2.6 version ,by changing only last part of code in model.fit() operation. Here is the Edited code:

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        #input_tensor=x1,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

model.compile(optimizer='adam', loss="mean_squared_error", run_eagerly=True)

#train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
model.fit(dummy_image_float(224,224), dummy_result(7,7,2048), epochs=2)

Reference:

Related