calculate loss value over several tensors

Viewed 430

During the calculation of my CNN deep learning network, the loss on the training stage is calculated with a cross entropy function :

tf.nn.weighted_cross_entropy_with_logits(targets = labels_flat, logits = logits_flat, pos_weight=98.22)

But, it assume that the prediction and ground truth are very well located, which is not the case here. Actually, my ground truth can be shifted locally from 2-3 pixels and so, I would like to calculate the cross entropy using an uncertainty on the "pixelwise" comparison.

To do that, I process as the following: I defined the wanted uncertainty, here 2 pixels. I can then, crop 2 pixels at each side of the ground truth. The prediction is also cropped at different position, simulating a "shift" between prediction and ground truth. Next, I have to calculate the loss between this "shifted" prediction and ground truth, saved it and go to the next "shifted" prediction. Doing that, I have a loss calculated with different shift values.

If I understood correctly the cross entropy function from tensorflow, the pixelwise comparison between GT and prediction should be minimal if the prediction is good. So, I can calculate the average loss between the prediction and GT, using the minimal value for each component of the cross entropy tensors.

with tf.variable_scope('loss_layer'):
    Unc = 2
    labelsCropped = tf.image.crop_to_bounding_box(labels, Unc, Unc, 256-Unc*2, 256-Unc*2)
    labels_flat = tf.reshape(labelsCropped, [-1, depth])
    Allcross_entropy_loss = []
    for x in range(2*2):
        for y in range(2*2):
            logitsCropped = tf.image.crop_to_bounding_box(logits, x, y, 256-Unc*2, 256-Unc*2)
            logits_flat = tf.reshape(logitsCropped, [-1, depth])
            Allcross_entropy_loss += [tf.nn.weighted_cross_entropy_with_logits(targets = labels_flat, logits = logits_flat, pos_weight=98.22)]
            loss = tf.reduce_mean(tf.reduce_min(tf.stack(Allcross_entropy_loss), 0))

By the way, this produce a loss equal to 0 very fast (~100 iterations which is too much fast to be real, my previous model are calculated over 20 000 iterations (20 epochs and 1000 iterations/epoch, to have a loss of ~0.4). I expect something wrong here but I don't know how to display the output of each function, for example, I would like to know if the reduce_min function give a minimal tensors of 252x252 or just 1 value. If it return 1 value, it's wrong.

1 Answers

Mentioning the Solution below, for the benefit of the community.

Removing tf.stack from the below line resolves the issue.

loss = tf.reduce_mean(tf.reduce_min(tf.stack(Allcross_entropy_loss), 0))

Correct code is shown below:

loss = tf.reduce_mean(tf.reduce_min(Allcross_entropy_loss), 0)
Related