def loss(self, score, delta_per_class, target_delta_for_sample_roi, bbox_bg_label_for_sample_roi): assert isinstance(score, Variable) assert isinstance(delta_per_class, Variable) assert isinstance(target_delta_for_sample_roi, np.ndarray) assert isinstance(bbox_bg_label_for_sample_roi, np.ndarray) #---------- debug target_delta_for_sample_roi = Variable( torch.FloatTensor(target_delta_for_sample_roi)) bbox_bg_label_for_sample_roi = Variable( torch.LongTensor(bbox_bg_label_for_sample_roi)) if torch.cuda.is_available(): target_delta_for_sample_roi = target_delta_for_sample_roi.cuda() bbox_bg_label_for_sample_roi = bbox_bg_label_for_sample_roi.cuda() n_sample = score.shape[0] delta_per_class = delta_per_class.view(n_sample, -1, 4) # get delta for roi w.r.t its corresponding bbox label index = torch.arange(0, n_sample).long() if torch.cuda.is_available(): index = index.cuda() delta = delta_per_class[index, bbox_bg_label_for_sample_roi.data] head_delta_loss = delta_loss(delta, target_delta_for_sample_roi, bbox_bg_label_for_sample_roi, 1) head_class_loss = F.cross_entropy(score, bbox_bg_label_for_sample_roi) return head_delta_loss + head_class_loss
def loss(self, delta, score, anchor, gt_bbox, image_size): #---------- debug assert isinstance(delta, Variable)#this is a good way to check the conditions assert isinstance(score, Variable) assert isinstance(anchor, np.ndarray) assert isinstance(gt_bbox, np.ndarray) #---------- debug target_delta, anchor_label = self.anchor_target_creator.make_anchor_target(anchor, gt_bbox, image_size) target_delta = Variable(torch.FloatTensor(target_delta)) anchor_label = Variable(torch.LongTensor(anchor_label)) if torch.cuda.is_available(): target_delta, anchor_label = target_delta.cuda(), anchor_label.cuda() rpn_delta_loss = delta_loss(delta, target_delta, anchor_label, 3) rpn_class_loss = F.cross_entropy(score, anchor_label, ignore_index=-1) # ignore loss for label value -1 return rpn_delta_loss + rpn_class_loss