def build_losses(self, labels: Mapping[str, tf.Tensor], model_outputs: Union[Mapping[str, tf.Tensor], tf.Tensor], aux_losses: Optional[Any] = None): """Segmentation loss. Args: labels: labels. model_outputs: Output logits of the classifier. aux_losses: auxiliarly loss tensors, i.e. `losses` in keras.Model. Returns: The total loss tensor. """ loss_params = self._task_config.losses segmentation_loss_fn = segmentation_losses.SegmentationLoss( loss_params.label_smoothing, loss_params.class_weights, loss_params.ignore_label, use_groundtruth_dimension=loss_params.use_groundtruth_dimension, top_k_percent_pixels=loss_params.top_k_percent_pixels) total_loss = segmentation_loss_fn(model_outputs, labels['masks']) if aux_losses: total_loss += tf.add_n(aux_losses) return total_loss
def build_losses(self, outputs: Mapping[str, Any], labels: Mapping[str, Any], aux_losses: Optional[Any] = None) -> Dict[str, tf.Tensor]: """Build Panoptic Mask R-CNN losses.""" params = self.task_config.losses use_groundtruth_dimension = params.semantic_segmentation_use_groundtruth_dimension segmentation_loss_fn = segmentation_losses.SegmentationLoss( label_smoothing=params.semantic_segmentation_label_smoothing, class_weights=params.semantic_segmentation_class_weights, ignore_label=params.semantic_segmentation_ignore_label, use_groundtruth_dimension=use_groundtruth_dimension, top_k_percent_pixels=params. semantic_segmentation_top_k_percent_pixels) instance_segmentation_weight = params.instance_segmentation_weight semantic_segmentation_weight = params.semantic_segmentation_weight losses = super(PanopticMaskRCNNTask, self).build_losses(outputs=outputs, labels=labels, aux_losses=None) maskrcnn_loss = losses['model_loss'] segmentation_loss = segmentation_loss_fn( outputs['segmentation_outputs'], labels['gt_segmentation_mask']) model_loss = (instance_segmentation_weight * maskrcnn_loss + semantic_segmentation_weight * segmentation_loss) total_loss = model_loss if aux_losses: reg_loss = tf.reduce_sum(aux_losses) total_loss = model_loss + reg_loss losses.update({ 'total_loss': total_loss, 'maskrcnn_loss': maskrcnn_loss, 'segmentation_loss': segmentation_loss, 'model_loss': model_loss, }) return losses
def build_losses(self, labels, model_outputs, aux_losses=None): """Sparse categorical cross entropy loss. Args: labels: labels. model_outputs: Output logits of the classifier. aux_losses: auxiliarly loss tensors, i.e. `losses` in keras.Model. Returns: The total loss tensor. """ loss_params = self._task_config.losses segmentation_loss_fn = segmentation_losses.SegmentationLoss( loss_params.label_smoothing, loss_params.class_weights, loss_params.ignore_label, use_groundtruth_dimension=loss_params.use_groundtruth_dimension) total_loss = segmentation_loss_fn(model_outputs, labels['masks']) if aux_losses: total_loss += tf.add_n(aux_losses) return total_loss