syntax = "proto3";

package second.protos;

// Message for configuring the localization loss, classification loss and hard
// example miner used for training object detection models. See core/losses.py
// for details
message Loss {
  // Localization loss to use.
  LocalizationLoss localization_loss = 1;

  // Classification loss to use.
  ClassificationLoss classification_loss = 2;

  // If not left to default, applies hard example mining.
  HardExampleMiner hard_example_miner = 3;

  // Classification loss weight.
  float classification_weight = 4;

  // Localization loss weight.
  float localization_weight = 5;
  
}

// Configuration for bounding box localization loss function.
message LocalizationLoss {
  oneof localization_loss {
    WeightedL2LocalizationLoss weighted_l2 = 1;
    WeightedSmoothL1LocalizationLoss weighted_smooth_l1 = 2;
  }
  bool encode_rad_error_by_sin = 3;
}

// L2 location loss: 0.5 * ||weight * (a - b)|| ^ 2
message WeightedL2LocalizationLoss {
  // DEPRECATED, do not use.
  // Output loss per anchor.
  bool anchorwise_output = 1;
  repeated float code_weight = 2;
}

// SmoothL1 (Huber) location loss: .5 * x ^ 2 if |x| < 1 else |x| - .5
message WeightedSmoothL1LocalizationLoss {
  // DEPRECATED, do not use.
  // Output loss per anchor.
  bool anchorwise_output = 1;
  float sigma = 2;
  repeated float code_weight = 3;
}


// Configuration for class prediction loss function.
message ClassificationLoss {
  oneof classification_loss {
    WeightedSigmoidClassificationLoss weighted_sigmoid = 1;
    WeightedSoftmaxClassificationLoss weighted_softmax = 2;
    BootstrappedSigmoidClassificationLoss bootstrapped_sigmoid = 3;
    SigmoidFocalClassificationLoss weighted_sigmoid_focal = 4;
    SoftmaxFocalClassificationLoss weighted_softmax_focal = 5;
  }
}

// Classification loss using a sigmoid function over class predictions.
message WeightedSigmoidClassificationLoss {
  // DEPRECATED, do not use.
  // Output loss per anchor.
  bool anchorwise_output = 1;
}

// Sigmoid Focal cross entropy loss as described in
// https://arxiv.org/abs/1708.02002
message SigmoidFocalClassificationLoss {
  // DEPRECATED, do not use.
  bool anchorwise_output = 1;
  // modulating factor for the loss.
  float gamma = 2;
  // alpha weighting factor for the loss.
  float alpha = 3;
}
// Sigmoid Focal cross entropy loss as described in
// https://arxiv.org/abs/1708.02002
message SoftmaxFocalClassificationLoss {
  // DEPRECATED, do not use.
  bool anchorwise_output = 1;
  // modulating factor for the loss.
  float gamma = 2;
  // alpha weighting factor for the loss.
  float alpha = 3;
}
// Classification loss using a softmax function over class predictions.
message WeightedSoftmaxClassificationLoss {
  // DEPRECATED, do not use.
  // Output loss per anchor.
  bool anchorwise_output = 1;
  // Scale logit (input) value before calculating softmax classification loss.
  // Typically used for softmax distillation.
  float logit_scale = 2;
}

// Classification loss using a sigmoid function over the class prediction with
// the highest prediction score.
message BootstrappedSigmoidClassificationLoss {
  // Interpolation weight between 0 and 1.
  float alpha = 1;

  // Whether hard boot strapping should be used or not. If true, will only use
  // one class favored by model. Othewise, will use all predicted class
  // probabilities.
  bool hard_bootstrap = 2;

  // DEPRECATED, do not use.
  // Output loss per anchor.
  bool anchorwise_output = 3;
}

// Configuation for hard example miner.
message HardExampleMiner {
  // Maximum number of hard examples to be selected per image (prior to
  // enforcing max negative to positive ratio constraint).  If set to 0,
  // all examples obtained after NMS are considered.
  int32 num_hard_examples = 1;

  // Minimum intersection over union for an example to be discarded during NMS.
  float iou_threshold = 2;

  // Whether to use classification losses ('cls', default), localization losses
  // ('loc') or both losses ('both'). In the case of 'both', cls_loss_weight and
  // loc_loss_weight are used to compute weighted sum of the two losses.
  enum LossType {
    BOTH = 0;
    CLASSIFICATION = 1;
    LOCALIZATION = 2;
  }
  LossType loss_type = 3;

  // Maximum number of negatives to retain for each positive anchor. If
  // num_negatives_per_positive is 0 no prespecified negative:positive ratio is
  // enforced.
  int32 max_negatives_per_positive = 4;

  // Minimum number of negative anchors to sample for a given image. Setting
  // this to a positive number samples negatives in an image without any
  // positive anchors and thus not bias the model towards having at least one
  // detection per image.
  int32 min_negatives_per_image = 5;
}
