text: 
  from __future__ import division
  import tensorflow as tf
  
  class SSD_fcLoss:
  
      def __init__(self,
                   alpha=1.0):
  
          self.alpha = alpha
  
      def smooth_L1_loss(self, y_true, y_pred):
  
          absolute_loss = tf.abs(y_true - y_pred)
          square_loss = 0.5 * (y_true - y_pred)**2
          l1_loss = tf.where(tf.less(absolute_loss, 1.0), square_loss, absolute_loss - 0.5)
          
          return tf.reduce_sum(l1_loss, axis=-1)
  
      def log_loss(self, y_true, y_pred):
  
          y_pred = tf.maximum(y_pred, 1e-15)
          log_loss = -tf.reduce_sum(tf.pow(1-y_pred, 2) * y_true * tf.log(y_pred), axis=-1) # focal loss square term
          
          return log_loss
  
      def compute_loss(self, y_true, y_pred):
          
          self.alpha = tf.constant(self.alpha)
  
          batch_size = tf.shape(y_pred)[0] # Output dtype: tf.int32
          n_boxes = tf.shape(y_pred)[1] # Output dtype: tf.int32, note that `n_boxes` in this context denotes the total number of boxes per image, not the number of boxes per cell.
  
          # 1: Compute the losses for class and box predictions for every box.
          classification_loss = tf.to_float(self.log_loss(y_true[:,:,:-12], y_pred[:,:,:-12])) # Output shape: (batch_size, n_boxes)
          localization_loss = tf.to_float(self.smooth_L1_loss(y_true[:,:,-12:-8], y_pred[:,:,-12:-8])) # Output shape: (batch_size, n_boxes)
  
          # 2: Compute the classification losses for the positive and negative targets.
          negatives = y_true[:,:,0] # Tensor of shape (batch_size, n_boxes)
          positives = tf.to_float(tf.reduce_max(y_true[:,:,1:-12], axis=-1)) # Tensor of shape (batch_size, n_boxes)
  
          # Count the number of positive boxes (classes 1 to n) in y_true across the whole batch.
          n_positive = tf.reduce_sum(positives)
          pos_class_loss = tf.reduce_sum(classification_loss * positives, axis=-1) # Tensor of shape (batch_size,)
  
          # Compute the classification loss for the negative default boxes (if there are any).
          neg_class_loss_all = classification_loss * negatives # Tensor of shape (batch_size, n_boxes)
          n_neg_losses = tf.count_nonzero(neg_class_loss_all, dtype=tf.int32) # The number of non-zero loss entries in `neg_class_loss_all`        
          n_negative_keep =  n_neg_losses  ## keep all negative boxes with non-zero losses ##
  
          # In the unlikely case when either (1) there are no negative ground truth boxes at all
          # or (2) the classification loss for all negative boxes is zero, return zero as the `neg_class_loss`.
          def f1():
              return tf.zeros([batch_size])
          # Otherwise compute the negative loss.
          def f2():
              # reshape `neg_class_loss_all` to 1D
              neg_class_loss_all_1D = tf.reshape(neg_class_loss_all, [-1]) # Tensor of shape (batch_size * n_boxes,)
              # ...and then we get the indices for the `n_negative_keep` boxes with the highest loss out of those...
              values, indices = tf.nn.top_k(neg_class_loss_all_1D,
                                            k=n_negative_keep,
                                            sorted=False) # We don't need them sorted.
              # ...and with these indices we'll create a mask...
              negatives_keep = tf.scatter_nd(indices=tf.expand_dims(indices, axis=1),
                                             updates=tf.ones_like(indices, dtype=tf.int32),
                                             shape=tf.shape(neg_class_loss_all_1D)) # Tensor of shape (batch_size * n_boxes,)
              negatives_keep = tf.to_float(tf.reshape(negatives_keep, [batch_size, n_boxes])) # Tensor of shape (batch_size, n_boxes)
              # ...and use it to keep only those boxes and mask all other classification losses
              neg_class_loss = tf.reduce_sum(classification_loss * negatives_keep, axis=-1) # Tensor of shape (batch_size,)
              return neg_class_loss
  
          neg_class_loss = tf.cond(tf.equal(n_neg_losses, tf.constant(0)), f1, f2)
  
          class_loss = pos_class_loss + neg_class_loss # Tensor of shape (batch_size,)
  
          # 3: Compute the localization loss for the positive targets.
          loc_loss = tf.reduce_sum(localization_loss * positives, axis=-1) # Tensor of shape (batch_size,)
  
          # 4: Compute the total loss.
          total_loss = (class_loss + self.alpha * loc_loss) / tf.maximum(1.0, n_positive) # In case `n_positive == 0`
  
          total_loss = total_loss * tf.to_float(batch_size)
  
          return total_loss
  