def forward(self, boxes, pred_maskiou, labels): num_masks = pred_maskiou.shape[0] index = torch.arange(num_masks, device=labels.device) maskious = pred_maskiou[index, labels] maskious = [maskious] results = [] for maskiou, box in zip(maskious, boxes): bbox = RBoxList(box.bbox, box.size, mode="xywha") for field in box.fields(): bbox.add_field(field, box.get_field(field)) bbox_scores = bbox.get_field("scores") mask_scores = bbox_scores * maskiou bbox.add_field("mask_scores", mask_scores) results.append(bbox) return results
def forward(self, x, boxes): """ Arguments: x (Tensor): the mask logits boxes (list[BoxList]): bounding boxes that are used as reference, one for ech image Returns: results (list[BoxList]): one BoxList for each image, containing the extra field mask """ mask_prob = x.sigmoid() # select masks coresponding to the predicted classes num_masks = x.shape[0] labels = [bbox.get_field("labels") for bbox in boxes] labels = torch.cat(labels) index = torch.arange(num_masks, device=labels.device) mask_prob = mask_prob[index, labels][:, None] boxes_per_image = [len(box) for box in boxes] mask_prob = mask_prob.split(boxes_per_image, dim=0) if self.masker: mask_prob = self.masker(mask_prob, boxes) results = [] for prob, box in zip(mask_prob, boxes): bbox = RBoxList(box.bbox, box.size, mode="xywha") for field in box.fields(): bbox.add_field(field, box.get_field(field)) # print("box prob:", prob.shape, np.unique(prob[0].data.cpu().numpy())[-10:]) scores = bbox.get_field("scores") # print("box_0:", scores[0], scores.shape) bbox.add_field("mask", prob) results.append(bbox) return results