Reputation: 55
I am a student studying Object Detection. I implemented a simple object finding model with FCN. The accuracy is 95% but the recall is 0. In short, the model has determined that there are no objects for all areas. I used softmax_cross_entropy as the error function. However, if the model judges that there is no object for the part where the object exists, I want to make a large error. I think I have to design the loss function myself, what should I do?
def model(X, P):
x = tf.cast(X, tf.float32)
x = x / 255.0
net = slim.conv2d(x, 32, kernel_size =(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 128 160
net = slim.conv2d(net, 64, kernel_size =(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 64 80
net = slim.conv2d(net, 128, kernel_size =(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 32 40
net = slim.conv2d(net, 256, kernel_size =(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 16 20
net = slim.conv2d(net, 512, kernel_size=(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 8 10
net = tf.nn.dropout(net, keep_prob=P)
net = slim.conv2d(net, 2, kernel_size=(3, 3))
net = slim.max_pool2d(net, (2, 2)) # 4 5
X = tf.placeholder(tf.uint8 ,[None, 320, 256, 3])
Y = tf.placeholder(tf.int64, [None, 20])
P = tf.placeholder(tf.float32)
y = tf.reshape(Y, (-1, 5, 4))
logits = model(X,P)
arg = tf.argmax(logits, -1)
with tf.name_scope('Optimizer'):
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.AdadeltaOptimizer(0.001).minimize(cost)
Upvotes: 2
Views: 202
Reputation: 30236
You are using sparse_softmax_cross_entropy
, this already allows you to add weights to your loss:
tf.losses.sparse_softmax_cross_entropy(
labels,
logits,
weights=1.0,
...
weights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If weights is a tensor of shape [batch_size], then the loss weights apply to each corresponding sample.
If your y is binary (1 for object, 0 for empty), you could simply multiply this with a desired factor and pass it in as weights:
penalty_empty = 0.5
penalty_object = 1
weights = penalty_empty + y * (penalty_object - penalty_empty)
Upvotes: 2