def calc_loss_class(true_box_conf,CLASS_SCALE, true_box_class,pred_box_class): ''' == input == true_box_conf : tensor of shape (N batch, N grid h, N grid w, N anchor) true_box_class : tensor of shape (N batch, N grid h, N grid w, N anchor), containing class index pred_box_class : tensor of shape (N batch, N grid h, N grid w, N anchor, N class) CLASS_SCALE : 1.0 == output == class_mask if object exists in this (grid_cell, anchor) pair and the class object receive nonzero weight class_mask[iframe,igridy,igridx,ianchor] = 1 else: 0 ''' class_mask = true_box_conf * CLASS_SCALE ## L_{i,j}^obj * lambda_class nb_class_box = tf.reduce_sum(tf.cast(class_mask > 0.0, tf.float32)) loss_class = tf.nn.sparse_softmax_cross_entropy_with_logits(labels = true_box_class, logits = pred_box_class) loss_class = tf.reduce_sum(loss_class * class_mask) / (nb_class_box + 1e-6) return(loss_class)