def show_graph(t, threshold=0.9, pos_color="b", neg_color="r"):
    t = t.cpu()
    apos = torch.threshold(t, threshold, 0).numpy()
    gpos = nx.from_numpy_matrix(apos)

    aneg = torch.threshold(t.neg(), threshold, 0).numpy()
    gneg = nx.from_numpy_matrix(aneg)

    pos = nx.spring_layout(gpos)

    nx.draw_networkx(gpos, pos=pos, with_labels=False, node_size=2, edge_color=pos_color, node_color="black", alpha=0.2)
    nx.draw_networkx(gneg, pos=pos, with_labels=False, node_size=2, edge_color=neg_color, node_color="black", alpha=0.2)

    # nx.draw_networkx_edges(gneg,pos,
    #                            with_labels=False,
    #                            edge_color='r',
    #                            width=1.0,
    #                            alpha=0.1
    #                         )
    # nx.draw_networkx_edges(gpos,pos,
    #                            with_labels=False,
    #                            edge_color='b',
    #                            width=1.0,
    #                            alpha=0.2
    #                         )

    # plt.axis('off')
    plt.title('correlation network')
Esempio n. 2
0
def utc(softmax_out_target, t_feature, n_class, t_centroid, decay=0.3):

    n, d = t_feature.size()

    weight = softmax_out_target

    weight_sum = weight.sum(0)

    # [bs, nclass, d])
    features_target_sl = t_feature.repeat(1, n_class).reshape(-1, n_class, d)

    features_target_sl2 = softmax_out_target.repeat(1, n_class).reshape(
        -1, n_class, n_class)

    feature = torch.cat((features_target_sl, features_target_sl2), dim=2)

    weight = weight.reshape(-1, 1).expand(-1, d + n_class).reshape(
        n, n_class, d + n_class)

    t_sum_feature = (feature * weight).sum(0)
    t_n_classes = weight_sum + 1e-6

    current_t_centroid = torch.div(t_sum_feature, t_n_classes.view(n_class, 1))

    #set decay
    de = torch.threshold(current_t_centroid, 1e-6, -1)
    de = 1 - torch.threshold(-de, 0, 0)
    decay = decay * de

    # Moving Centroid
    t_centroid = (1 - decay) * t_centroid + decay * current_t_centroid
    return t_centroid
 def _iou(self, bbox, bboxes):
     inter_x1 = torch.max(bbox[0], bboxes[0])
     inter_y1 = torch.max(bbox[1], bboxes[1])
     inter_x2 = torch.min(bbox[2], bboxes[2])
     inter_y2 = torch.min(bbox[3], bboxes[3])
     inter_w = torch.threshold(inter_x2 - inter_x1 + 1, 0, 0)
     inter_h = torch.threshold(inter_y2 - inter_y1 + 1, 0, 0)
     inter_area = inter_w * inter_h
     bbox_area = (bbox[2] - bbox[0] + 1) * (bbox[3] - bbox[1] + 1)
     bboxes_area = (bboxes[2] - bboxes[0] + 1) * (bboxes[3] - bboxes[1] + 1)
     union_area = bbox_area + bboxes_area - inter_area
     return inter_area / union_area
Esempio n. 4
0
def sample_select(softmax_out_target, features_tar, ad_net, iter):
    # with torch.no_grad():
    ad_out = ad_net(features_tar, test=True).view(-1)
    entropy = losssntg.Entropy(softmax_out_target)
    entropy = 1.0 + torch.exp(-entropy)
    threshold = 1.6 - 0.1 * iter / 2000
    # w_e = torch.threshold(entropy,threshold,0)

    # return ad_out*w_e

    w_e = torch.threshold(entropy, threshold, -1)
    w_e = torch.threshold(-w_e, 0, 0)
    return 1 - w_e
Esempio n. 5
0
    def denoise(self, x, edge_index, edge_weight, with_attack):
        rate = self.denoise_rate if not with_attack else self.denoise_rate + self.attack_rate
        _, gcn_output = self.gcn_model(x, edge_index, edge_weight)
        gcn_output = gcn_output.detach()
        enc_output = self.enc_model(x).detach()

        edge_score = -self.scorer(
            torch.cat(
                [
                    gcn_output[edge_index[0]],
                    gcn_output[edge_index[1]],
                    # enc_output[edge_index[0]],
                    # enc_output[edge_index[1]],
                    edge_weight.unsqueeze(-1)
                ],
                dim=-1)).squeeze()
        edge_index, edge_score = self.symmetrization(edge_index, edge_score)
        quantile = torch.quantile(edge_score, 1 - rate).item()
        # if (not with_attack) and self.scorer.training:
        #     # quantile = quantile * 0
        #     quantile = min(quantile, 0)
        print(f"denoise threshold: {quantile:.4f}")
        masked_edge_score = torch.threshold(edge_score, quantile, value=-1e9)

        denoise_values = (1 - 2 * edge_weight) * \
            torch.sigmoid(masked_edge_score)

        return denoise_values, edge_score
Esempio n. 6
0
    def attack(self, x, edge_index, edge_weight):
        _, gcn_output = self.gcn_model(x, edge_index, edge_weight)
        gcn_output = gcn_output.detach()
        enc_output = self.enc_model(x).detach()

        edge_score: torch.Tensor = self.scorer(
            torch.cat(
                [
                    gcn_output[edge_index[0]],
                    gcn_output[edge_index[1]],
                    # enc_output[edge_index[0]],
                    # enc_output[edge_index[1]],
                    edge_weight.unsqueeze(-1)
                ],
                dim=-1)).squeeze()
        edge_index, edge_score = self.symmetrization(edge_index, edge_score)

        quantile = torch.quantile(edge_score, 1 - self.attack_rate).item()
        print(f"attack threshold: {quantile:.4f}")
        masked_edge_score = torch.threshold(edge_score, quantile, value=-1e9)
        # print(f"threshold: {quantile:.4f}")

        attack_values = (1 - 2 * edge_weight) * \
            torch.sigmoid(masked_edge_score)

        lower_bound = torch.quantile(edge_score, self.denoise_rate).item()
        pseudo_labels = ((edge_score > quantile) |
                         (edge_score < lower_bound)).to(torch.float)

        return attack_values, pseudo_labels
Esempio n. 7
0
def usc(y_s, s_feature, softmax_out_source, n_class, s_centroid, decay=0.3):

    n, d = s_feature.size()

    # get labels
    s_labels = y_s

    # image number in each class
    ones = torch.ones_like(s_labels, dtype=torch.float)
    zeros = torch.zeros(n_class).cuda()

    s_n_classes = zeros.scatter_add(0, s_labels, ones)

    # image number cannot be 0, when calculating centroids
    ones = torch.ones_like(s_n_classes)
    s_n_classes = torch.max(s_n_classes, ones)

    # calculating centroids, sum and divide
    zeros = torch.zeros(n_class, d).cuda()
    s_sum_feature = zeros.scatter_add(
        0, torch.transpose(s_labels.repeat(d, 1), 1, 0), s_feature)

    zeros = torch.zeros(n_class, n_class).cuda()
    s_sum_feature2 = zeros.scatter_add(
        0, torch.transpose(s_labels.repeat(n_class, 1), 1, 0),
        softmax_out_source)

    feature = torch.cat((s_sum_feature, s_sum_feature2), dim=1)

    current_s_centroid = torch.div(feature, s_n_classes.view(n_class, 1))

    #set decay
    de = torch.threshold(current_s_centroid, 1e-6, -1)
    de = 1 - torch.threshold(-de, 0, 0)
    decay = decay * de

    # Moving Centroid
    s_centroid = (1 - decay) * s_centroid + decay * current_s_centroid
    return s_centroid
Esempio n. 8
0
 def forward(self, x):
     n, c, t, h, w = x.size()
     if DROP:
         if DROP_3D:
             if TEMPORAL_DROP:
                 x = x.permute(0, 2, 1, 3, 4)
                 x = self.drop(x)
                 x = x.permute(0, 2, 1, 3, 4)
             else:
                 x = self.drop(x)
         else:
             x = x.view(n, -1, h, w)
             x = self.drop(x)
     else:
         weight = self.conv(x)
         if not self.use_relu:
             weight = sigmoid(weight)
             weight = threshold(weight, self.thres, 0.)
         if self.random:
             noise = torch.rand_like(weight)
             noise[:, :, ::2] = 1.
             weight = noise * weight.clone()
         x = weight[:, :, None] * x.view(n, self.group, -1, t, h, w)
     return self.net(x.view(n, c, t, h, w))
Esempio n. 9
0
 def f(x):
     return torch.threshold(x, 0, -10) + x + x + x
Esempio n. 10
0
 def threshold(x: torch.Tensor, th: float, val: float):
     o = torch.rand_like(x)
     o = x * torch.threshold(o, th, val)
     return o
Esempio n. 11
0
 def relu_bt(self, x):
     threshold = torch.norm(x, p=float("inf")).clone().detach()
     return -torch.threshold(-F.leaky_relu(x), -threshold, -threshold)
Esempio n. 12
0
 def forward(self, input):
     x = (input * self._slope) + 0.5
     x = torch.threshold(-x, -1, -1)
     x = torch.threshold(-x, 0, 0)
     return x
Esempio n. 13
0
 def forward(self, input):
     if self.inplace:
         result = torch.threshold_(input, self.delta, self.delta)
     else:
         result = torch.threshold(input, self.delta, self.delta)
     return result