Stanley
Stanley

Reputation: 2127

How to substract two tensor with mask in Tensorflow?

I am implementing YOLO network with a selfdefine loss.

Say there two tensor,GT and PD (ground truth and predicts).both are 2 dims matrix of 4x4.

Assume GT is:

0,0,0,0
0,1,0,0
0,0,1,0
0,0,0,0

PD has the same size with some random nums.

Here I need to calc Mean Squared Error separately.
calc MSE with ones in GT and calc MSE with zeros in GT seperately.
I prefer to use a mask to cover the unrelated elements, so the calculation with only calc the related elements. I already implemented this in numpy, but don't know how to do this with tf(v1.14)

import numpy as np
import numpy.ma as ma
conf = y_true[...,0]
conf = np.expand_dims(conf,-1)

conf_pred = y_pred[...,0]
conf_pred = np.expand_dims(conf_pred,-1)

noobj_conf = ma.masked_equal(conf,1)   #cover grid with objects
obj_conf = ma.masked_equal(conf,0)     #cover grid without objects

loss_obj = np.sum(np.square(obj_conf - conf_pred))
loss_noobj = np.sum(np.square(noobj_conf - conf_pred))

Any suggestions about how to implement this in tensorflow?

Upvotes: 3

Views: 267

Answers (1)

Anubhav Singh
Anubhav Singh

Reputation: 8699

If I understand you correctly, you want to calculate mean square errors of 0's and 1's separately.

You can do something like below:

y_true = tf.constant([[0,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,0]], dtype=tf.float32)
y_pred = tf.random.uniform([4, 4], minval=0, maxval=1)

# find indices where 0 is present in y_true
indices0 = tf.where(tf.equal(y_true, tf.zeros([1.]))) 
# find indices where 1 is present in y_true
indices1 = tf.where(tf.equal(y_true, tf.ones([1.]))) 

# find all values in y_pred which are present at indices0
y_pred_indices0 = tf.gather_nd(y_pred, indices0)
# find all values in y_pred which are present at indices1
y_pred_indices1 = tf.gather_nd(y_pred, indices1)

# mse loss calculations 
mse0 = tf.losses.mean_squared_error(labels=tf.gather_nd(y_true, indices0), predictions=y_pred_indices0)
mse1 = tf.losses.mean_squared_error(labels=tf.gather_nd(y_true, indices1), predictions=y_pred_indices1)

# mse0 = tf.reduce_sum(tf.squared_difference(tf.gather_nd(y_true, indices0), y_pred_indices0))
# mse1 = tf.reduce_sum(tf.squared_difference(tf.gather_nd(y_true, indices1), y_pred_indices1))

with tf.Session() as sess:
    y_, loss0, loss1 = sess.run([y_pred, mse0, mse1])
    print(y_)
    print(loss0, loss1)

output:

[[0.12770343 0.43467927 0.9362457  0.09105921]
 [0.46243036 0.8838414  0.92655015 0.9347118 ]
 [0.14018488 0.14527774 0.8395766  0.14391887]
 [0.1209656  0.7793218  0.70543754 0.749542  ]]
0.341359 0.019614244

Upvotes: 1

Related Questions