Args: batched_inputs: a list, batched outputs of :class:`DatasetMapper` . Each item in the list contains the inputs for one image. For now, each item in the list is a dict that contains: * image: Tensor, image in (C, H, W) format.
(self, batched_inputs, do_postprocess=True)
| 99 | |
| 100 | |
| 101 | def forward(self, batched_inputs, do_postprocess=True): |
| 102 | """ |
| 103 | Args: |
| 104 | batched_inputs: a list, batched outputs of :class:`DatasetMapper` . |
| 105 | Each item in the list contains the inputs for one image. |
| 106 | For now, each item in the list is a dict that contains: |
| 107 | |
| 108 | * image: Tensor, image in (C, H, W) format. |
| 109 | * instances: Instances |
| 110 | |
| 111 | Other information that's included in the original dicts, such as: |
| 112 | |
| 113 | * "height", "width" (int): the output resolution of the model, used in inference. |
| 114 | See :meth:`postprocess` for details. |
| 115 | """ |
| 116 | images, images_whwh = self.preprocess_image(batched_inputs) |
| 117 | if isinstance(images, (list, torch.Tensor)): |
| 118 | images = nested_tensor_from_tensor_list(images) |
| 119 | |
| 120 | # Feature Extraction. |
| 121 | src = self.backbone(images.tensor) |
| 122 | features = list() |
| 123 | for f in self.in_features: |
| 124 | feature = src[f] |
| 125 | features.append(feature) |
| 126 | |
| 127 | # Prepare Proposals. |
| 128 | proposal_boxes = self.init_proposal_boxes.weight.clone() |
| 129 | proposal_boxes = box_cxcywh_to_xyxy(proposal_boxes) |
| 130 | proposal_boxes = proposal_boxes[None] * images_whwh[:, None, :] |
| 131 | |
| 132 | # Prediction. |
| 133 | outputs_class, outputs_coord = self.head(features, proposal_boxes, self.init_proposal_features.weight) |
| 134 | output = {'pred_logits': outputs_class[-1], 'pred_boxes': outputs_coord[-1]} |
| 135 | |
| 136 | if self.training: |
| 137 | gt_instances = [x["instances"].to(self.device) for x in batched_inputs] |
| 138 | targets = self.prepare_targets(gt_instances) |
| 139 | if self.deep_supervision: |
| 140 | output['aux_outputs'] = [{'pred_logits': a, 'pred_boxes': b} |
| 141 | for a, b in zip(outputs_class[:-1], outputs_coord[:-1])] |
| 142 | |
| 143 | loss_dict = self.criterion(output, targets) |
| 144 | weight_dict = self.criterion.weight_dict |
| 145 | for k in loss_dict.keys(): |
| 146 | if k in weight_dict: |
| 147 | loss_dict[k] *= weight_dict[k] |
| 148 | return loss_dict |
| 149 | |
| 150 | else: |
| 151 | box_cls = output["pred_logits"] |
| 152 | box_pred = output["pred_boxes"] |
| 153 | results = self.inference(box_cls, box_pred, images.image_sizes) |
| 154 | |
| 155 | if do_postprocess: |
| 156 | processed_results = [] |
| 157 | for results_per_image, input_per_image, image_size in zip(results, batched_inputs, images.image_sizes): |
| 158 | height = input_per_image.get("height", image_size[0]) |
nothing calls this directly
no test coverage detected