def forward(self, input): """ Forward pass of the function. """ return ( (input > 2).float() + (input - F.pow(input, 2) / 4) * (input >= 0).float() * (input <= 2).float() + (input + F.pow(input, 2) / 4) * (input < 0).float() * (input >= -2).float() - (input < -2).float() )
def _abs_robust(diff, occ_mask=None, if_mask_=False): loss_diff = F.pow((F.abs(diff) + 0.01), 0.4) if not if_mask_: photo_loss = F.mean(loss_diff) else: photo_loss = F.sum(loss_diff * occ_mask) / (F.sum(occ_mask) + 1e-6) return photo_loss
def _charbonnier(diff, occ_mask=None, if_mask_=False): loss_diff = F.pow((diff**2 + 1e-6), 0.4) if not if_mask_: photo_loss = F.mean(loss_diff) else: photo_loss = F.sum(loss_diff * occ_mask) / (F.sum(occ_mask) + 1e-6) return photo_loss
def test_BetaRNG(): m1 = RNG(seed=111, device="xpu0") m2 = RNG(seed=111, device="xpu1") m3 = RNG(seed=222, device="xpu0") out1 = m1.beta(2, 1, size=(100, )) out1_ = m1.uniform(size=(100, )) out2 = m2.beta(2, 1, size=(100, )) out3 = m3.beta(2, 1, size=(100, )) np.testing.assert_allclose(out1.numpy(), out2.numpy(), atol=1e-6) assert out1.device == "xpu0" and out2.device == "xpu1" assert not (out1.numpy() == out3.numpy()).all() assert not (out1.numpy() == out1_.numpy()).all() alpha = Tensor([[2, 3, 4], [9, 10, 11]], dtype=np.float32, device="xpu0") beta = Tensor([0.5, 1, 1.5], dtype=np.float32, device="xpu0") expected_mean = (alpha / (alpha + beta)).numpy() expected_std = (F.sqrt(alpha * beta / (F.pow(alpha + beta, 2) * (alpha + beta + 1)))).numpy() out = m1.beta(alpha=alpha, beta=beta, size=(20, 30)) out_shp = out.shape if isinstance(out_shp, tuple): assert out_shp == (20, 30, 2, 3) else: assert all(out.shape.numpy() == np.array([20, 30, 2, 3])) assert (np.abs(out.mean(axis=(0, 1)).numpy() - expected_mean) / expected_std).mean() < 0.1 assert (np.abs(np.std(out.numpy(), axis=(0, 1)) - expected_std)).mean() < 0.1
def forward(self, x): B, C, _, _ = x.shape # avg_dims = tuple(range(2, len(x.shape))) # [2 ,3 ] nu2 = F.expand_dims(F.pow(x, 2).reshape(B, C, -1).mean(axis=-1, keepdims=True), axis=-1) # [B, C, 1, 1] x = x / F.sqrt(nu2 + F.abs(self.eps)) return F.maximum(self.gamma * x + self.beta, self.tau)
def SSIM(x, y, md=1): patch_size = 2 * md + 1 C1 = 0.01**2 C2 = 0.03**2 mu_x = nn.AvgPool2d(patch_size, 1, 0, mode="average")(x) mu_y = nn.AvgPool2d(patch_size, 1, 0, mode="average")(y) mu_x_mu_y = mu_x * mu_y mu_x_sq = F.pow(mu_x, 2) mu_y_sq = F.pow(mu_y, 2) sigma_x = nn.AvgPool2d(patch_size, 1, 0, mode="average")(x * x) - mu_x_sq sigma_y = nn.AvgPool2d(patch_size, 1, 0, mode="average")(y * y) - mu_y_sq sigma_xy = nn.AvgPool2d(patch_size, 1, 0, mode="average")( x * y) - mu_x_mu_y SSIM_n = (2 * mu_x_mu_y + C1) * (2 * sigma_xy + C2) SSIM_d = (mu_x_sq + mu_y_sq + C1) * (sigma_x + sigma_y + C2) SSIM = SSIM_n / SSIM_d dist = F.clip((1 - SSIM) / 2, 0, 1) return dist
def sigmoid_cross_entropy_retina(pred, label, ignore_label=-1, background=0, alpha=0.5, gamma=0): device = pred.device mask = 1 - F.equal(label, ignore_label).astype(np.float32) vlabel = label * mask n, m, c = pred.shape zero_mat = F.zeros([n, m, c + 1]).to(device) index = F.expand_dims(vlabel, 2).astype(np.int32) one_hot = F.scatter(zero_mat, 2, index, F.ones([n, m, 1])) onehot = one_hot[:, :, 1:] pos_part = F.pow(1 - pred, gamma) * onehot * F.log(pred) neg_part = F.pow(pred, gamma) * (1 - onehot) * F.log(1 - pred) loss = -(alpha * pos_part + (1 - alpha) * neg_part).sum(axis=2) * mask positive_mask = (label > 0) return loss.sum() / F.maximum(positive_mask.sum(), 1)
def forward(self, input): """ Forward pass of the function. """ return F.pow((1 + F.exp(-self.beta * input)), -self.alpha)
def isru(input, alpha): return input / (F.sqrt(1 + alpha * F.pow(input, 2)))
def _anchor_double_target(gt_boxes, im_info, all_anchors): gt_boxes, im_info = gt_boxes.detach(), im_info.detach() all_anchors = all_anchors.detach() gt_boxes = gt_boxes[:im_info[5].astype(np.int32), :] dummy = -F.ones([1, gt_boxes.shape[1]]).to(gt_boxes.device) gt_boxes = F.concat([gt_boxes, dummy], axis=0) valid_mask = 1 - (gt_boxes[:, 4] < 0).astype(np.float32) anchor_centers = _compute_center(all_anchors) gtboxes_centers = _compute_center(gt_boxes) # gtboxes_centers = gtboxes_centers * valid_mask.unsqueeze(1) gtboxes_centers = gtboxes_centers * F.expand_dims(valid_mask, axis=1) N, K = all_anchors.shape[0], gt_boxes.shape[0] an_centers = F.expand_dims(anchor_centers, axis=1) gt_centers = F.expand_dims(gtboxes_centers, axis=0) # an_centers = anchor_centers.unsqueeze(1).repeat(1, K, 1) # gt_centers = gtboxes_centers.unsqueeze(0).repeat(N, 1, 1) distance = F.abs(an_centers - gt_centers) distance = F.sqrt(F.pow(distance, 2).sum(axis=2)) start = 0 end = 5 overlaps = box_overlap_opr(all_anchors[:, :4], gt_boxes[:, :4]) overlaps *= F.expand_dims(valid_mask, axis=0) default_num = 16 ious_list = [] for l in range(start, end): _, index = F.cond_take(all_anchors[:, 4] == l, all_anchors[:, 4]) level_dist = distance[index, :].transpose(1, 0) ious = overlaps[index, :].transpose(1, 0) sorted_index = F.argsort(level_dist, descending=False) n = min(sorted_index.shape[1], default_num) ious = F.gather(ious, 1, sorted_index[:, :n]).transpose(1, 0) ious_list.append(ious) ious = F.concat(ious_list, axis=0) mean_var = F.mean(ious, axis=0) std_var = F.std(ious, 0) iou_thresh_per_gt = mean_var + std_var iou_thresh_per_gt = F.maximum(iou_thresh_per_gt, 0.2) # limits the anchor centers in the gtboxes N, K = all_anchors.shape[0], gt_boxes.shape[0] anchor_points = an_centers pos_area = _compute_pos_area(gt_boxes, 0.3) # pos_area = pos_area.unsqueeze(0).repeat(N, 1, 1) pos_area = F.broadcast_to(F.expand_dims(pos_area, axis=0), (N, K, pos_area.shape[-1])) l = anchor_points[:, :, 0] - pos_area[:, :, 0] r = pos_area[:, :, 2] - anchor_points[:, :, 0] t = anchor_points[:, :, 1] - pos_area[:, :, 1] b = pos_area[:, :, 3] - anchor_points[:, :, 1] is_in_gt = F.stack([l, r, t, b], axis=2) is_in_gt = is_in_gt.min(axis=2) > 0.1 valid_mask = (overlaps >= F.expand_dims( iou_thresh_per_gt, axis=0)) * is_in_gt.astype(np.float32) ious = overlaps * valid_mask sorted_index = F.argsort(ious, 1) sorted_overlaps = F.gather(ious, 1, sorted_index) max_overlaps = sorted_overlaps[:, :2].flatten() argmax_overlaps = sorted_index[:, :2].flatten() n, c = all_anchors.shape device = all_anchors.device labels = -F.ones(2 * n).to(device) positive_mask = (max_overlaps >= 0.2).to(device).astype(np.float32) negative_mask = (max_overlaps < 0.2).to(device).astype(np.float32) labels = positive_mask + labels * (1 - positive_mask) * (1 - negative_mask) bbox_targets = gt_boxes[argmax_overlaps, :4] all_anchors = F.broadcast_to(F.expand_dims(all_anchors, axis=1), (n, 2, c)).reshape(-1, c) bbox_targets = bbox_transform_opr(all_anchors[:, :4], bbox_targets) labels_cat = gt_boxes[argmax_overlaps, 4] labels_cat = labels_cat * (1 - F.equal(labels, -1).astype( np.float32)) - F.equal(labels, -1).astype(np.float32) return labels, bbox_targets, labels_cat
def _anchor_target(gt_boxes, im_info, all_anchors): gt_boxes, im_info = gt_boxes.detach(), im_info.detach() all_anchors = all_anchors.detach() gt_boxes = gt_boxes[:im_info[5], :] valid_mask = 1 - (gt_boxes[:, 4] < 0).astype(np.float32) anchor_centers = _compute_center(all_anchors) gtboxes_centers = _compute_center(gt_boxes) * F.expand_dims(valid_mask, axis=0) N, K = all_anchors.shape[0], gt_boxes.shape[0] # an_centers = anchor_centers.unsqueeze(1).repeat(1, K, 1) an_centers = F.expand_dims(anchor_centers, axis=1) gt_centers = F.expand_dims(gtboxes_centers, axis=0) # gt_centers = gtboxes_centers.unsqueeze(0).repeat(N, 1, 1) distance = F.abs(an_centers - gt_centers) distance = F.sqrt(F.pow(distance, 2).sum(axis=2)) start = 0 end = 5 overlaps = box_overlap_opr(all_anchors[:, :4], gt_boxes[:, :4]) overlaps = overlaps * valid_mask.unsqueeze(0) default_num = 9 ious_list = [] for l in range(start, end): index = torch.nonzero(all_anchors[:, 4].eq(l), as_tuple=False)[:, 0] level_dist = level_dist[index, :].transpose(1, 0) ious = distance[index, :].transpose(1, 0) sorted_index = torch.argsort(ious, 1, descending=False) n = min(default_num, sorted_index.shape[1]) ious = torch.gather(ious, 1, sorted_index[:, :n]).transpose(1, 0) ious_list.append(ious) ious = F.concat(ious_list, axis=0) mean_var = ious.mean(0) std_var = ious.std(0) iou_thresh_per_gt = mean_var + std_var iou_thresh_per_gt = torch.clamp(iou_thresh_per_gt, 0.35) n = iou_thresh_per_gt.shape[0] # limits the anchor centers in the gtboxes N, K = all_anchors.shape[0], gt_boxes.shape[0] anchor_points = an_centers proxies = gt_boxes.unsqueeze(0).repeat(N, 1, 1) l = anchor_points[:, :, 0] - proxies[:, :, 0] r = proxies[:, :, 2] - anchor_points[:, :, 0] t = anchor_points[:, :, 1] - proxies[:, :, 1] b = proxies[:, :, 3] - anchor_points[:, :, 1] is_in_gt = F.stack([l, r, t, b], axis=2) is_in_gt = is_in_gt.min(axis=2) > 0.1 valid_mask = (overlaps >= iou_thresh_per_gt.unsqueeze(0)) * is_in_gt ious = overlaps * valid_mask argmax_overlaps = torch.argmax(ious, axis=1) max_overlaps = torch.gather(ious, 1, argmax_overlaps.unsqueeze(1)) n = all_anchors.shape[0] labels = -F.ones(n) positive_mask = max_overlaps > 0 negative_mask = max_overlaps < config.rpn_negative_overlap labels = positive_mask + labels * (1 - positive_mask) * (1 - negative_mask) bbox_targets = gt_boxes[argmax_overlaps, :4] bbox_targets = bbox_transform_opr(all_anchors[:, :4], bbox_targets) labels_cat = gt_boxes[argmax_overlaps, 4] labels_cat = labels_cat * (1 - labels.eq(0).astype(np.float32)) labels_cat = labels_cat * (1 - labels.eq(-1).astype( np.float32)) - labels.eq(-1).astype(np.float32) return labels, bbox_targets, labels_cat