MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / inference

Method inference

projects/SparseRCNN/sparsercnn/detector.py:187–235  ·  view source on GitHub ↗

Arguments: box_cls (Tensor): tensor of shape (batch_size, num_proposals, K). The tensor predicts the classification probability for each proposal. box_pred (Tensor): tensors of shape (batch_size, num_proposals, 4). The tensor predicts

(self, box_cls, box_pred, image_sizes)

Source from the content-addressed store, hash-verified

185 return new_targets
186
187 def inference(self, box_cls, box_pred, image_sizes):
188 """
189 Arguments:
190 box_cls (Tensor): tensor of shape (batch_size, num_proposals, K).
191 The tensor predicts the classification probability for each proposal.
192 box_pred (Tensor): tensors of shape (batch_size, num_proposals, 4).
193 The tensor predicts 4-vector (x,y,w,h) box
194 regression values for every proposal
195 image_sizes (List[torch.Size]): the input image sizes
196
197 Returns:
198 results (List[Instances]): a list of #images elements.
199 """
200 assert len(box_cls) == len(image_sizes)
201 results = []
202
203 if self.use_focal:
204 scores = torch.sigmoid(box_cls)
205 labels = torch.arange(self.num_classes, device=self.device).\
206 unsqueeze(0).repeat(self.num_proposals, 1).flatten(0, 1)
207
208 for i, (scores_per_image, box_pred_per_image, image_size) in enumerate(zip(
209 scores, box_pred, image_sizes
210 )):
211 result = Instances(image_size)
212 scores_per_image, topk_indices = scores_per_image.flatten(0, 1).topk(self.num_proposals, sorted=False)
213 labels_per_image = labels[topk_indices]
214 box_pred_per_image = box_pred_per_image.view(-1, 1, 4).repeat(1, self.num_classes, 1).view(-1, 4)
215 box_pred_per_image = box_pred_per_image[topk_indices]
216
217 result.pred_boxes = Boxes(box_pred_per_image)
218 result.scores = scores_per_image
219 result.pred_classes = labels_per_image
220 results.append(result)
221
222 else:
223 # For each box we assign the best class or the second best if the best on is `no_object`.
224 scores, labels = F.softmax(box_cls, dim=-1)[:, :, :-1].max(-1)
225
226 for i, (scores_per_image, labels_per_image, box_pred_per_image, image_size) in enumerate(zip(
227 scores, labels, box_pred, image_sizes
228 )):
229 result = Instances(image_size)
230 result.pred_boxes = Boxes(box_pred_per_image)
231 result.scores = scores_per_image
232 result.pred_classes = labels_per_image
233 results.append(result)
234
235 return results
236
237 def preprocess_image(self, batched_inputs):
238 """

Callers 1

forwardMethod · 0.95

Calls 4

InstancesClass · 0.90
BoxesClass · 0.90
flattenMethod · 0.80
maxMethod · 0.80

Tested by

no test coverage detected