Tensorflow gradients missing

54 Views Asked by At

I am trying to train a model using InceptionResNetV2 as base model. I added a dense layer on top of it. But when I try to train the model I am getting this error-

ValueError: No gradients provided for any variable: (['dense_layer/kernel:0', 'dense_layer/bias:0'],). Provided `grads_and_vars` is ((None, <tf.Variable 'dense_layer/kernel:0' shape=(9216, 128) dtype=float32>), (None, <tf.Variable 'dense_layer/bias:0' shape=(128,) dtype=float32>)).

Here are the codes-

initial_base_model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(
    include_top=False,
    weights='imagenet',
    input_shape=(218,178,3)
)
initial_base_model.trainable = False
X_input = Input(shape=(218,178,3))
X = initial_base_model(X_input)
X = AveragePooling2D(pool_size=(3, 3), strides=(1, 1), data_format='channels_last')(X)
X = Flatten(data_format='channels_last')(X)
X = Dense(128, name='dense_layer')(X)
    
# L2 normalization
X = Lambda(lambda  x: tf.math.l2_normalize(x,axis=1))(X)

base_model = Model(inputs=[X_input], outputs=[X])
input_shape = (218,178,3)
A = Input(shape=input_shape, name = 'anchors')
P = Input(shape=input_shape, name = 'positives')
N = Input(shape=input_shape, name = 'negatives')
enc_A = base_model(A)
enc_P = base_model(P)
enc_N = base_model(N)
FRmodel = Model(inputs=[A, P, N], outputs=[enc_A, enc_P, enc_N])

I am using a generator function to train my model-

def data_generator(batch_size, name='train'):
    dummy_target = np.zeros((batch_size, 2, 1))
    anchors = np.zeros((batch_size, 218, 178, 3))
    positives = np.zeros((batch_size, 218, 178, 3))
    negatives = np.zeros((batch_size, 218, 178, 3))
    while True:
        included_files_in_batch = []
        for i in range(batch_size):
            file_num = random.randint(0, len(train_file_list))
            while file_num in included_files_in_batch:
                file_num = random.randint(0, len(train_file_list))
            if name == 'train':
                file = train_file_list[file_num]
            else:
                file = val_file_list[file_num]
            
            anchors[i] = load_file(file, 'anchor')
            positives[i] = load_file(file, 'positive')
            negatives[i] = load_file(file, 'negative')
            
            included_files_in_batch.append(file_num)
            
        data = {
                'anchors': anchors,
                'positives': positives,
                'negatives': negatives
            }
            
        yield (data, [dummy_target])

Here is my loss function-

def triplet_loss(y_pred, y_true, alpha=0.1):
    anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]

    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 0)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 0)
        
    basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
    loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0))
      
    return loss

I tried removing the callbacks, changing the optimizer, removing the l2_normalization at the end, doesn't work. When I tried to make the base model trainable I got all the trainable layer names in the error message. I am relatively new to computer vision. Any help would be a great help for me

0

There are 0 best solutions below