{"record":{"id":"eb315ef35ca29bde","repo":"tensorflow/models","slug":"build-loss-fn-must-be-called-after-build-model-eb315e","errorCode":null,"errorMessage":"build_loss_fn() must be called after build_model().","messagePattern":"build_loss_fn\\(\\) must be called after build_model\\(\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/detection/modeling/shapemask_model.py","lineNumber":156,"sourceCode":"        'coarse_mask_logits': coarse_mask_logits,\n        'prior_masks': prior_masks,\n    }\n\n    if not is_training:\n      model_outputs.update({\n          'num_detections': valid_detections,\n          'detection_boxes': valid_boxes,\n          'detection_outer_boxes': valid_outer_boxes,\n          'detection_masks': fine_mask_logits,\n          'detection_classes': valid_classes,\n          'detection_scores': valid_scores,\n      })\n\n    return model_outputs\n\n  def build_loss_fn(self):\n    if self._keras_model is None:\n      raise ValueError('build_loss_fn() must be called after build_model().')\n\n    filter_fn = self.make_filter_trainable_variables_fn()\n    trainable_variables = filter_fn(self._keras_model.trainable_variables)\n\n    def _total_loss_fn(labels, outputs):\n      cls_loss = self._cls_loss_fn(outputs['cls_outputs'],\n                                   labels['cls_targets'],\n                                   labels['num_positives'])\n      box_loss = self._box_loss_fn(outputs['box_outputs'],\n                                   labels['box_targets'],\n                                   labels['num_positives'])\n\n      # Adds Shapemask model losses.\n      shape_prior_loss = self._shapemask_prior_loss_fn(outputs['prior_masks'],\n                                                       labels['mask_targets'],\n                                                       labels['mask_is_valid'])\n      coarse_mask_loss = self._shapemask_loss_fn(outputs['coarse_mask_logits'],\n                                                 labels['mask_targets'],","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/detection/modeling/shapemask_model.py#L138-L174","documentation":"Error \"build_loss_fn() must be called after build_model().\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/detection/modeling/shapemask_model.py:156 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}