コード例 #1
0
ファイル: dataset.py プロジェクト: dcrmg/chinese-ocr
    def __iter__(self):
        n_batch = len(self) // self.batch_size
        tail = len(self) % self.batch_size
        index = torch.LongTensor(len(self)).fill_(0)
        for i in range(n_batch):
            random_start = random.randint(0, len(self) - self.batch_size)
            batch_index = random_start + torch.range(0, self.batch_size - 1)
            index[i * self.batch_size:(i + 1) * self.batch_size] = batch_index
        # deal with tail
        if tail:
            random_start = random.randint(0, len(self) - self.batch_size)
            tail_index = random_start + torch.range(0, tail - 1)
            index[(i + 1) * self.batch_size:] = tail_index

        return iter(index)
コード例 #2
0
ファイル: DCN.py プロジェクト: ParsonsZeng/DiCoNet
 def eliminate_rows(self, prob_sc, ind, phis):
     """ eliminate rows of phis and prob_matrix scale """
     length = prob_sc.size()[1]
     mask = (prob_sc[:, :, 0] > 0.85).type(dtype)
     rang = (Variable(torch.range(0, length - 1).unsqueeze(0)
             .expand_as(mask)).
             type(dtype))
     ind_sc = torch.sort(rang * (1-mask) + length * mask, 1)[1]
     # permute prob_sc
     m = mask.unsqueeze(2).expand_as(prob_sc)
     mm = m.clone()
     mm[:, :, 1:] = 0
     prob_sc = (torch.gather(prob_sc * (1 - m) + mm, 1,
                ind_sc.unsqueeze(2).expand_as(prob_sc)))
     # compose permutations
     ind = torch.gather(ind, 1, ind_sc)
     active = torch.gather(1-mask, 1, ind_sc)
     # permute phis
     active1 = active.unsqueeze(2).expand_as(phis)
     ind1 = ind.unsqueeze(2).expand_as(phis)
     active2 = active.unsqueeze(1).expand_as(phis)
     ind2 = ind.unsqueeze(1).expand_as(phis)
     phis_out = torch.gather(phis, 1, ind1) * active1
     phis_out = torch.gather(phis_out, 2, ind2) * active2
     return prob_sc, ind, phis_out, active
コード例 #3
0
ファイル: test_meters.py プロジェクト: elanmart/tnt
    def testConfusionMeter(self):
        mtr = meter.ConfusionMeter(k=3)

        output = torch.Tensor([[.8, 0.1, 0.1], [10, 11, 10], [0.2, 0.2, .3]])
        target = torch.range(0, 2)
        mtr.add(output, target)

        conf_mtrx = mtr.value()
        self.assertEqual(conf_mtrx.sum(), 3, "All should be correct")
        self.assertEqual(conf_mtrx.diagonal().sum(),
                         3, "All should be correct")

        target = torch.Tensor([1, 0, 0])
        mtr.add(output, target)

        self.assertEqual(conf_mtrx.sum(), 6,
                         "Six tests should give six values")
        self.assertEqual(conf_mtrx.diagonal().sum(), 3,
                         "Shouldn't have changed since all new values were false")
        self.assertEqual(conf_mtrx[0].sum(), 3,
                         "All top have gotten one guess")
        self.assertEqual(conf_mtrx[1].sum(), 2,
                         "Two first at the 2nd row have a guess")
        self.assertEqual(conf_mtrx[1][2], 0,
                         "The last one should be empty")
        self.assertEqual(conf_mtrx[2].sum(), 1,
                         "Bottom row has only the first test correct")
        self.assertEqual(conf_mtrx[2][2], 1,
                         "Bottom row has only the first test correct")

        mtr = meter.ConfusionMeter(k=4, normalized=True)
        output = torch.Tensor([
            [.8, 0.1, 0.1, 0],
            [10, 11, 10, 0],
            [0.2, 0.2, .3, 0],
            [0, 0, 0, 1],
        ])

        target = torch.Tensor([0, 1, 2, 3])
        mtr.add(output, target)
        conf_mtrx = mtr.value()

        self.assertEqual(conf_mtrx.sum(), output.size(1),
                         "All should be correct")
        self.assertEqual(conf_mtrx.diagonal().sum(), output.size(1),
                         "All should be correct")

        target[0] = 1
        target[1] = 0
        target[2] = 0

        mtr.add(output, target)
        conf_mtrx = mtr.value()

        self.assertEqual(conf_mtrx.sum(), output.size(1),
                         "The normalization should sum all values to 1")
        for i, row in enumerate(conf_mtrx):
            self.assertEqual(row.sum(), 1,
                             "Row no " + str(i) + " fails to sum to one in normalized mode")
コード例 #4
0
ファイル: test_datasets.py プロジェクト: elanmart/tnt
 def testBatchDataset(self):
     t = torch.range(0,15).long()
     batchsize = 8
     d = dataset.ListDataset(t, lambda x: {'input': x})
     d = dataset.BatchDataset(d, batchsize)
     ex = d[0]['input']
     self.assertEqual(len(ex), batchsize)
     self.assertEqual(ex[-1], batchsize - 1)
コード例 #5
0
def get_one_hot(labels, num_classes):

    one_hot = Variable(torch.range(0, num_classes-1)).unsqueeze(0).expand(labels.size(0), num_classes)
    if (type(labels.data) is torch.cuda.FloatTensor) or (type(labels.data) is torch.cuda.LongTensor):
        one_hot = one_hot.cuda()


    one_hot = one_hot.eq(labels.unsqueeze(1).expand_as(one_hot).float()).float()
    return one_hot
コード例 #6
0
ファイル: DCN.py プロジェクト: ParsonsZeng/DiCoNet
 def fwd_merge(self, Inputs_N, target, Phis, Bs, lp,
               batch, depth, mode='train', epoch=0):
     # Flow backwards
     Phis, Bs, Inputs_N = Phis[::-1], Bs[::-1], Inputs_N[::-1]
     length = self.merge.n
     perm = (torch.range(0.0, length)
             .unsqueeze(0).expand(self.batch_size, length + 1))
     perm = Variable(perm, requires_grad=False).type(dtype_l)
     ind = perm[:, :-1].clone()
     prob_matrix = Variable(torch.eye(length + 1)).type(dtype)
     prob_matrix = prob_matrix.unsqueeze(0).expand(self.batch_size,
                                                   length + 1, length + 1)
     # concatenate pad_token to input
     pad_token = (self.merge.pad_token[:-1].unsqueeze(0)
                  .expand(self.batch_size, 1, self.input_size))
     input = torch.cat((pad_token, Inputs_N[0]), 1)
     phis = Phis[0]
     input_target = torch.cat((pad_token, Inputs_N[-1]), 1)
     input_scale = input
     input_norm = input_scale
     Perms = [perm]
     Points = [input_scale]
     for i, scale in enumerate(range(depth)):
         if scale < depth - 1:
             # fine scales
             prob_sc = self.merge(input_scale, phis)
             input_norm = torch.cat((pad_token, Inputs_N[scale + 1]), 1)
             phis = Phis[scale + 1]
             prob_sc, ind, phis, _ = self.eliminate_rows(prob_sc, ind, phis)
             comb = self.combine_matrices(prob_matrix, prob_sc, perm,
                                          last=False)
             prob_matrix, _, perm = comb
             # postprocess before feeding to next scale
             hard_out, soft_out = self.outputs(input_norm,
                                               prob_matrix, perm)
             input_scale = hard_out
         else:
             # coarsest scale
             if mode == 'test':
                 prob_sc = self.merge(input_scale, phis,
                                      input_target=None,
                                      target=None)
             else:
                 prob_sc = self.merge(input_scale, phis,
                                      input_target=input_target,
                                      target=target)
             comb = self.combine_matrices(prob_matrix, prob_sc, perm,
                                          last=True)
             prob_matrix, prob_sc, perm = comb
             hard_out, soft_out = self.outputs(input, prob_matrix, perm)
             loss, pg_loss = self.merge.compute_loss(prob_matrix, target,
                                                     lp=lp)
         Perms.append(perm)
         Points.append(input_norm)
     return loss, pg_loss, Perms
コード例 #7
0
def sequence_mask(sequence_length, max_len=None):
    if max_len is None:
        max_len = sequence_length.data.max()
    batch_size = sequence_length.size(0)
    seq_range = torch.range(0, max_len - 1).long()
    seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
    seq_range_expand = Variable(seq_range_expand)
    if sequence_length.is_cuda:
        seq_range_expand = seq_range_expand.cuda()
    seq_length_expand = (sequence_length.unsqueeze(1)
                         .expand_as(seq_range_expand))
    return seq_range_expand < seq_length_expand
コード例 #8
0
ファイル: test_meters.py プロジェクト: elanmart/tnt
    def testClassErrorMeter(self):
        mtr = meter.ClassErrorMeter(topk=[1])
        output = torch.eye(3)
        target = torch.range(0, 2)
        mtr.add(output, target)
        err = mtr.value()

        self.assertEqual(err, [0], "All should be correct")

        target[0] = 1
        target[1] = 0
        target[2] = 0
        mtr.add(output, target)
        err = mtr.value()
        self.assertEqual(err, [50.0], "Half should be correct")
コード例 #9
0
ファイル: test_oim.py プロジェクト: DianJin2018/open-reid
 def test_forward_backward(self):
     import torch
     import torch.nn.functional as F
     from torch.autograd import Variable
     from reid.loss import OIMLoss
     criterion = OIMLoss(3, 3, scalar=1.0, size_average=False)
     criterion.lut = torch.eye(3)
     x = Variable(torch.randn(3, 3), requires_grad=True)
     y = Variable(torch.range(0, 2).long())
     loss = criterion(x, y)
     loss.backward()
     probs = F.softmax(x)
     grads = probs.data - torch.eye(3)
     abs_diff = torch.abs(grads - x.grad.data)
     self.assertEquals(torch.log(probs).diag().sum(), -loss)
     self.assertTrue(torch.max(abs_diff) < 1e-6)
コード例 #10
0
ファイル: apmeter.py プロジェクト: elanmart/tnt
    def value(self):
        """Returns the model's average precision for each class

        Return:
            ap (FloatTensor): 1xK tensor, with avg precision for each class k
        """

        if self.scores.numel() == 0:
            return 0
        ap = torch.zeros(self.scores.size(1))
        rg = torch.range(1, self.scores.size(0)).float()
        if self.weights.numel() > 0:
            weight = self.weights.new(self.weights.size())
            weighted_truth = self.weights.new(self.weights.size())

        # compute average precision for each class
        for k in range(self.scores.size(1)):
            # sort scores
            scores = self.scores[:, k]
            targets = self.targets[:, k]
            _, sortind = torch.sort(scores, 0, True)
            truth = targets[sortind]
            if self.weights.numel() > 0:
                weight = self.weights[sortind]
                weighted_truth = truth.float() * weight
                rg = weight.cumsum(0)

            # compute true positive sums
            if self.weights.numel() > 0:
                tp = weighted_truth.cumsum(0)
            else:
                tp = truth.float().cumsum(0)

            # compute precision curve
            precision = tp.div(rg)

            # compute average precision
            ap[k] = precision[truth.byte()].sum() / max(truth.sum(), 1)
        return ap
コード例 #11
0
ファイル: trainer.py プロジェクト: Jarvx/web-classification
    def test(self):
        
        self.model.eval()
        
        loss = 0
        predictions = torch.zeros(len(self.val_data))
        predictions = predictions
        indices = torch.range(1,self.val_data.num_classes)
        correct = 0
        total = 0
        nb_samples=len(self.val_data)
        for idx in tqdm(range(len(self.val_data)),desc='Testing epoch  '+str(self.epoch)+'',ascii=True, file=sys.stdout):
            text, label = self.val_data[idx]
            tree=None
            if len(text)<3:
                nb_samples-=1
                continue

            target = map_label_to_target(label,self.num_classes)
            
            outputs = self.model(tree, text) # size(1,5)
            if outputs is None:
                continue
                nb_samples-=1
            
            _, predicted = torch.max(outputs.data, 1)
            
            total += target.size(0)
           # print(type(predicted))
           # print(type(target))
            correct += (predicted == target.data).sum()
            err = self.criterion(outputs, target)
            loss += err.data[0]
        loss=loss/nb_samples
        acc=correct/total
            
        #val_loss=loss/len(self.val_data)
        print("Val loss:{} Acc:{}".format(loss,acc))
コード例 #12
0
    def forward(self, output, target):

        batch_size = output.data.size(0)
        height = output.data.size(2)
        width = output.data.size(3)

        # Get x,y,w,h,conf,cls
        output = output.view(batch_size, self.num_anchors, -1, height * width)
        coord = torch.zeros_like(output[:, :, :4, :])
        coord[:, :, :2, :] = output[:, :, :2, :].sigmoid()
        coord[:, :, 2:4, :] = output[:, :, 2:4, :]
        conf = output[:, :, 4, :].sigmoid()
        cls = output[:, :, 5:, :].contiguous().view(
            batch_size * self.num_anchors, self.num_classes,
            height * width).transpose(1, 2).contiguous().view(
                -1, self.num_classes)

        # Create prediction boxes
        pred_boxes = torch.FloatTensor(
            batch_size * self.num_anchors * height * width, 4)
        lin_x = torch.range(0, width - 1).repeat(height,
                                                 1).view(height * width)
        lin_y = torch.range(0, height - 1).repeat(
            width, 1).t().contiguous().view(height * width)
        anchor_w = self.anchors[:, 0].contiguous().view(self.num_anchors, 1)
        anchor_h = self.anchors[:, 1].contiguous().view(self.num_anchors, 1)

        if torch.cuda.is_available():
            pred_boxes = pred_boxes.cuda()
            lin_x = lin_x.cuda()
            lin_y = lin_y.cuda()
            anchor_w = anchor_w.cuda()
            anchor_h = anchor_h.cuda()

        pred_boxes[:, 0] = (coord[:, :, 0].detach() + lin_x).view(-1)
        pred_boxes[:, 1] = (coord[:, :, 1].detach() + lin_y).view(-1)
        pred_boxes[:, 2] = (coord[:, :, 2].detach().exp() * anchor_w).view(-1)
        pred_boxes[:, 3] = (coord[:, :, 3].detach().exp() * anchor_h).view(-1)
        pred_boxes = pred_boxes.cpu()

        # Get target values
        coord_mask, conf_mask, cls_mask, tcoord, tconf, tcls = self.build_targets(
            pred_boxes, target, height, width)
        coord_mask = coord_mask.expand_as(tcoord)
        tcls = tcls[cls_mask].view(-1).long()
        cls_mask = cls_mask.view(-1, 1).repeat(1, self.num_classes)

        if torch.cuda.is_available():
            tcoord = tcoord.cuda()
            tconf = tconf.cuda()
            coord_mask = coord_mask.cuda()
            conf_mask = conf_mask.cuda()
            tcls = tcls.cuda()
            cls_mask = cls_mask.cuda()

        conf_mask = conf_mask.sqrt()
        cls = cls[cls_mask].view(-1, self.num_classes)

        # Compute losses
        mse = nn.MSELoss(size_average=False)
        ce = nn.CrossEntropyLoss(size_average=False)
        self.loss_coord = self.coord_scale * mse(
            coord * coord_mask, tcoord * coord_mask) / batch_size
        self.loss_conf = mse(conf * conf_mask, tconf * conf_mask) / batch_size
        self.loss_cls = self.class_scale * 2 * ce(cls, tcls) / batch_size
        self.loss_tot = self.loss_coord + self.loss_conf + self.loss_cls

        #         return self.loss_tot, self.loss_coord, self.loss_conf, self.loss_cls
        return self.loss_tot
コード例 #13
0
def MatchPair(minNet, strideNet, model, transform, scales, feat1, feat1W, feat1H, I2, listW, listH, featChannel, tolerance, vote):

	match1 = []
	match2 = []
	similarity = []
	nbFeat = feat1W * feat1H
	bestScore = 0
	matchSetT = [] # We estimate the transformation from matchSetT
	
	for i in range(len(scales)) : 
		# Normalized I2 feature
		tmp_I2 = imgFeat(minNet, strideNet, I2, model, transform, scales[i])
		tmp_I2 = tmp_I2.data
		tmp_norm = torch.sum(tmp_I2 ** 2, dim = 1, keepdim=True) ** 0.5 + 1e-7
		tmp_I2 = tmp_I2 / tmp_norm.expand(tmp_I2.size())
		
		# Hough Transformation Grid, only for the current scale
		tmp_feat_w, tmp_feat_h = tmp_I2.shape[2], tmp_I2.shape[3]
		tmp_transformation = np.zeros((feat1W + tmp_feat_w, feat1H + tmp_feat_h))
		
		# I2 spatial information
		tmp_nbFeat = tmp_feat_w * tmp_feat_h
		tmp_I2 = tmp_I2.view(featChannel, -1).contiguous().transpose(0, 1)
		tmp_w = (torch.range(0, tmp_feat_w-1,1)).unsqueeze(1).expand(tmp_feat_w, tmp_feat_h).contiguous().view(-1).type(torch.LongTensor)
		tmp_h = (torch.range(0, tmp_feat_h-1, 1)).unsqueeze(0).expand(tmp_feat_w, tmp_feat_h).contiguous().view(-1).type(torch.LongTensor)

		# Feature Similarity
		score = torch.mm(tmp_I2, feat1)
		
		# Top1 match for both images
		topk0_score, topk0_index = score.topk(k=1, dim = 0)
		topk1_score, topk1_index = score.topk(k=1, dim = 1)
		
		index0 = torch.cuda.FloatTensor(tmp_nbFeat, nbFeat).fill_(0).scatter_(0, topk0_index, topk0_score)
		index1 = torch.cuda.FloatTensor(tmp_nbFeat, nbFeat).fill_(0).scatter_(1, topk1_index, topk1_score)
		intersectionScore = index0 * index1
		intersection = intersectionScore.nonzero()
		
		for item in intersection : 
			i2, i1 = item[0], item[1]
			w1, h1, w2, h2 = listW[i1], listH[i1], tmp_w[i2], tmp_h[i2]
			
			# Store all the top1 matches
			match1.append([(w1 + 0.49) / feat1W, (h1 + 0.49) / feat1H])
			match2.append([(w2 + 0.49) / tmp_feat_w, (h2 + 0.49) / tmp_feat_h])
			
			shift_w = int(w1 - w2  + tmp_feat_w - 2)
			shift_h = int(h1 - h2 + tmp_feat_h - 2)
			
			# Update the current hough transformation grid
			tmp_transformation[shift_w, shift_h] = tmp_transformation[shift_w, shift_h] + 1
			
			similarity.append(intersectionScore[i2, i1] ** 0.5)
		
		# Update the hough transformation grid with gaussian voting
		tmp_transformation = convolve2d(tmp_transformation, vote, mode='same')
		
		# Find the best hough transformation, and store the correspondant matches on matchSetT
		mode_non_zero = np.where((tmp_transformation == tmp_transformation.max()))
		score = tmp_transformation[mode_non_zero[0][0], mode_non_zero[1][0]]
		
		if score > bestScore :
			tmp_match1_ransac = []
			tmp_match2_ransac = []
			begin = len(match1) - len(intersection)  
			tmp_index = []
			for j, item in enumerate(intersection) : 
				i2, i1 = item[0], item[1]
				w1, h1, w2, h2 = listW[i1], listH[i1], tmp_w[i2], tmp_h[i2]
				shift_w = int(w1 - w2  + tmp_feat_w -2)
				shift_h = int(h1 - h2 + tmp_feat_h -2)
				diff_w, diff_h = np.abs(shift_w - mode_non_zero[0][0]), np.abs(shift_h - mode_non_zero[1][0])
				if diff_w <= tolerance and diff_h <= tolerance : 
					tmp_index.append(j+begin)
				
			matchSetT = tmp_index
			bestScore = score
			
	
	match1, match2, similarity = np.array(match1), np.array(match2), np.array(similarity)
	if len(matchSetT) == 0 :
		return [], [] , [], []
		
	return np.hstack((match1, np.ones((match1.shape[0], 1)))), np.hstack((match2, np.ones((match2.shape[0], 1)))), similarity, matchSetT
コード例 #14
0
ファイル: save.py プロジェクト: KoryBurns/Pytorch
def _seq_to_encoded(seq, letter_encoder, alphabet_length, sequence_length):
  b = torch.zeros((alphabet_length, sequence_length))
  seq_ints = [letter_encoder[s] for s in seq]
  b[seq_ints, torch.range(sequence_length)] = 1

  return b
コード例 #15
0
    def compute_rank_late_ranks_fusion(self, txt_feats, img_feats, img_masks,
                                       gt_inds):
        src_bsize, nturns, fsize = txt_feats.size()
        tgt_bsize, nregions, fsize = img_feats.size()

        # computing the rank of each query in one step is infeasible
        bsize = self.cfg.rank_batch_size
        nstep = tgt_bsize // bsize
        if tgt_bsize % bsize > 0:
            nstep += 1

        all_ranks = []
        # compute the rank of each query
        for txt_id in range(src_bsize):
            tmp_txt_feats = txt_feats[txt_id].unsqueeze(0)
            sim_list = []
            for j in range(nstep):
                curr_img_feats = img_feats[j * bsize:min((j + 1) *
                                                         bsize, tgt_bsize)]
                curr_img_masks = img_masks[j * bsize:min((j + 1) *
                                                         bsize, tgt_bsize)]
                csize = curr_img_feats.size(0)
                curr_txt_feats = tmp_txt_feats.expand(csize, nturns,
                                                      fsize).contiguous()
                curr_step_sims = self.criterion.compute_pairwise_similarity(
                    curr_img_feats, curr_img_masks, curr_txt_feats)
                curr_step_scores = self.criterion.pairwise_similarity_to_score(
                    curr_step_sims, curr_img_masks)
                curr_step_sims = torch.sum(curr_step_sims * curr_step_scores,
                                           -1)
                sim_list.append(curr_step_sims)
            sim_list = torch.cat(sim_list, 0)
            # sim_list: (tgt_size, nturns)

            rank_scores = []
            for turn in range(nturns):
                sim = sim_list[:, turn]
                rank_inds = torch.argsort(sim, dim=-1, descending=True)
                cur_scores = sim.new_zeros((tgt_bsize, ))
                order_inds = torch.range(start=0, end=(tgt_bsize - 1)).to(
                    cur_scores.device) + 1
                cur_scores[rank_inds] = order_inds
                # for j in range(tgt_bsize):
                #     cur_scores[rank_inds[j]] = j+1
                rank_scores.append(cur_scores)
            rank_scores = torch.stack(rank_scores, 0)
            # print('rank_scores', rank_scores.size())

            rank_per_turn = []
            for turn in range(nturns):
                if turn > 0:
                    accu_scores = torch.mean(rank_scores[:(turn + 1), :],
                                             0).view(-1)
                else:
                    accu_scores = rank_scores[0, :].view(-1)
                inds = torch.argsort(accu_scores, dim=-1, descending=False)
                r = torch.eq(inds, gt_inds[txt_id]).nonzero()[0]
                rank_per_turn.append(r)
            rank_per_turn = torch.cat(rank_per_turn, 0)
            all_ranks.append(rank_per_turn)
        all_ranks = torch.stack(all_ranks, 0)
        print('all_ranks', all_ranks.size())
        return all_ranks
コード例 #16
0
def main(args):
    # load graph data
    import warnings
    warnings.filterwarnings('ignore')
    global pool
    node_indice, indice_node, start_indice, triples, rel_set = init_triples(
        'data/graph/author_community.pkl')
    edges = numpy.array(triples)
    num_to_generate = edges.shape[0]
    choices = np.random.uniform(size=num_to_generate)

    train_p = 0.4
    valid_p = 0.1
    test_p = 0.5

    train_flag = choices <= train_p
    test_flag = (choices > train_p) & (choices <= train_p + test_p)
    validation_flag = choices > (train_p + test_p)

    test = edges[test_flag]
    train = edges[train_flag]
    valid = edges[validation_flag]

    train_data = train
    valid_data = valid
    test_data = test

    num_rels = len(rel_set)
    num_nodes = len(node_indice.items())

    print("# entities: {}".format(num_nodes))
    print("# relations: {}".format(num_rels))
    print("# training edges: {}".format(train.shape[0]))
    print("# validation edges: {}".format(valid.shape[0]))
    print("# testing edges: {}".format(test.shape[0]))

    embedding_dict = init_embedding('data/graph/author_w2v_embedding.pkl',
                                    node_indice)

    valid_author = read_valid_author(
        'data/classification/test_author_text_corpus.txt', node_indice)

    node_li = []
    label_li = []
    for k, v in valid_author.items():
        node_li.append(k)
        label_li.append(v)

    eval_node = torch.from_numpy(np.asarray(node_li))
    eval_label = np.asarray(label_li)

    for k, v in embedding_dict.items():
        print(v.shape)
        break
    # check cuda
    use_cuda = args.gpu >= 0 and torch.cuda.is_available()
    if use_cuda:
        torch.cuda.set_device(args.gpu)
        eval_node = eval_node.cuda()
    # create model
    if args.pretrain is False:
        embedding_dict = None

    model = LinkPredict(
        num_nodes,
        args.n_hidden,
        num_rels,
        num_bases=args.n_bases,
        num_hidden_layers=args.n_layers,
        dropout=args.dropout,
        use_cuda=use_cuda,
        reg_param=args.regularization,
        # embed_dict=embedding_dict,
        embed_dict=embedding_dict,
        freeze_embedding=args.freeze)

    # validation and testing triplets
    valid_data = torch.LongTensor(valid_data)
    test_data = torch.LongTensor(test_data)

    # build test graph
    test_graph, test_rel, test_norm = build_test_graph(num_nodes, num_rels,
                                                       train_data)
    with open('graph_dump.pkl', 'wb') as f:
        pickle.dump(test_graph, f)
    test_deg = test_graph.in_degrees(range(
        test_graph.number_of_nodes())).float().view(-1, 1)
    test_node_id = torch.arange(0, num_nodes, dtype=torch.long).view(-1, 1)
    test_rel = torch.from_numpy(test_rel)
    test_norm = node_norm_to_edge_norm(test_graph,
                                       torch.from_numpy(test_norm).view(-1, 1))

    if use_cuda:
        model.cuda()

        # test_graph = test_graph.to('cuda:' + str(args.gpu))
        # test_node_id = test_node_id.cuda()
        # test_rel = test_rel.cuda()
        # test_norm = test_norm.cuda()

        # test_data = test_data.cuda()
        # valid_data = valid_data.cuda()

    # build adj list and calculate degrees for sampling
    adj_list, degrees = get_adj_and_degrees(num_nodes, train_data)

    # optimizer
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)

    model_state_file = 'model_state.pth'
    forward_time = []
    backward_time = []
    if args.edge_sampler == "neighbor":
        pool = MultiThreadSamplerPool(train_data,
                                      args.graph_batch_size,
                                      args.graph_split_size,
                                      num_rels,
                                      adj_list,
                                      degrees,
                                      args.negative_sample,
                                      args.edge_sampler,
                                      max_runtime=args.n_epochs,
                                      max_worker=64)
    print('init sampler thread pool')
    # training loop
    print("start training loop...")

    epoch = 0
    best_nmi = 0
    best_ari = 0
    best_mif1 = 0
    best_maf1 = 0

    while True:
        if epoch % 1 == 0:
            # perform validation on CPU because full graph is too large
            model.eval()
            print("start eval")
            model.cpu()
            embed = model(test_graph, test_node_id, test_rel, test_norm)
            embed = embed.detach()
            eval_embed = embed[eval_node]
            eval_embed = eval_embed.numpy()
            NMI, ARI, MICRO_F1, MACRO_F1 = evaluator(eval_embed, eval_label)

            # if NMI < best_nmi or ARI < best_ari or MICRO_F1 < best_mif1 or MACRO_F1 < best_maf1:
            if MICRO_F1 < best_mif1 or MACRO_F1 < best_maf1:
                if epoch >= args.n_epochs:
                    break
            else:
                best_epoch = epoch
                best_nmi = NMI
                best_ari = ARI
                best_mif1 = MICRO_F1
                best_maf1 = MACRO_F1
                # best_mrr = mrr
                # torch.save({'state_dict': model.state_dict(), 'epoch': epoch},
                #            model_state_file)
            test_dict = embed_opt(
                embed,
                torch.range(0, num_nodes - 1, dtype=torch.long).cuda(),
                indice_node)

            with open(
                    '/home/xiaomeng/jupyter_base/author_embedding/codes/GCN/emb/rgcn_embed_{}.pkl'
                    .format(epoch), 'wb') as f:
                pickle.dump(test_dict, f)
                # pickle.dump(test_dict, 'rgcn_embed.pkl')
            with open(
                    '/home/xiaomeng/jupyter_base/author_embedding/codes/GCN/mds/rgcn_model_{}.pkl'
                    .format(epoch), 'wb') as f:
                torch.save({
                    'state_dict': model.state_dict(),
                    'epoch': epoch
                }, f)
            if use_cuda:
                model.cuda()
        model.train()
        if args.edge_sampler == "neighbor":
            # 防止队列为空
            while pool.q.empty():
                pass
            # perform edge neighborhood sampling to generate training graph and data
            g, node_id, edge_type, node_norm, data, labels = pool.get()
        # perform edge neighborhood sampling to generate training graph and data
        else:
            g, node_id, edge_type, node_norm, data, labels = \
                generate_sampled_graph_and_labels(
                    train_data, args.graph_batch_size, args.graph_split_size,
                    num_rels, adj_list, degrees, args.negative_sample,
                    args.edge_sampler)
            print("Done edge sampling")
        # set node/edge feature
        node_id = torch.from_numpy(node_id).view(-1, 1).long()
        edge_type = torch.from_numpy(edge_type)
        edge_norm = node_norm_to_edge_norm(
            g,
            torch.from_numpy(node_norm).view(-1, 1))
        data, labels = torch.from_numpy(data), torch.from_numpy(labels)
        deg = g.in_degrees(range(g.number_of_nodes())).float().view(-1, 1)
        if use_cuda:
            node_id, deg = node_id.cuda(), deg.cuda()
            edge_type, edge_norm = edge_type.cuda(), edge_norm.cuda()
            data, labels = data.cuda(), labels.cuda()
            g = g.to(args.gpu)

        t0 = time.time()
        embed = model(g, node_id, edge_type, edge_norm)
        loss = model.get_loss(g, embed, data, labels)
        t1 = time.time()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(),
                                       args.grad_norm)  # clip gradients
        optimizer.step()
        t2 = time.time()

        forward_time.append(t1 - t0)
        backward_time.append(t2 - t1)
        print(
            "Epoch {:04d} | Loss {:.4f} | Best NMI {:.4f} | Best ARI {:.4f} | Best MICRO-F1 {:.4f} | Best MACRO-F1 {:.4f} | Forward {:.4f}s | Backward {:.4f}s | Best Epoch {}"
            .format(epoch, loss.item(), best_nmi, best_ari, best_mif1,
                    best_maf1, forward_time[-1], backward_time[-1],
                    best_epoch))

        optimizer.zero_grad()

        # validation

        # if use_cuda:
        #     model.cuda()
        epoch += 1
        # if epoch % args.evaluate_every == 0:
        #     # perform validation on CPU because full graph is too large
        #
        #     #     model.cpu()
        #     model.eval()
        #     print("start eval")
        #     mrr = calc_mrr(embed, model.w_relation, torch.LongTensor(train_data),
        #                          test_data, valid_data, hits=[1, 3, 10], eval_bz=args.eval_batch_size,
        #                          eval_p=args.eval_protocol)
        #     # save best model
        #     if mrr < best_mrr:
        #         if epoch >= args.n_epochs:
        #             break
        #     else:
        #         best_mrr = mrr
        #         torch.save({'state_dict': model.state_dict(), 'epoch': epoch},
        #                    model_state_file)

    print("training done")
    print("Mean forward time: {:4f}s".format(np.mean(forward_time)))
    print("Mean Backward time: {:4f}s".format(np.mean(backward_time)))

    print("\nstart testing:")

    # use best model checkpoint
    checkpoint = torch.load(model_state_file)
    # if use_cuda:
    #     model.cpu() # test on CPU
    model.eval()
    model.load_state_dict(checkpoint['state_dict'])
    print("Using best epoch: {}".format(checkpoint['epoch']))
    embed = model(test_graph, test_node_id, test_rel, test_norm)
    # calc_mrr(embed, model.w_relation, torch.LongTensor(train_data), valid_data,
    #                test_data, hits=[1, 3, 10], eval_bz=args.eval_batch_size, eval_p=args.eval_protocol)
    eval_embed = embed[eval_node]
    eval_embed = eval_embed.detach().cpu().numpy()
    evaluator(eval_embed, eval_label)
コード例 #17
0
import PixelUnShuffle
import torch
import torch.nn as nn
import torch.nn.functional as F

x = torch.range(start=0, end=31).reshape([1, 8, 2, 2])
print('x:')
print(x.shape)
print(x)

y = F.pixel_shuffle(x, 2)
print('y:')
print(y.shape)
print(y)

x_ = PixelUnShuffle.pixel_unshuffle(y, 2)
print('x_:')
print(x_.shape)
print(x_)

s = torch.randn(1, 3, 256, 256)
ss = PixelUnShuffle.pixel_unshuffle(s, 2)
ssss = PixelUnShuffle.pixel_unshuffle(s, 4)
print(ss.shape, ssss.shape)
コード例 #18
0
ファイル: test_meters.py プロジェクト: Pandinosaurus/tnt
    def testConfusionMeter(self):
        mtr = meter.ConfusionMeter(k=3)

        output = torch.Tensor([[0.8, 0.1, 0.1], [10, 11, 10], [0.2, 0.2, 0.3]])
        if hasattr(torch, "arange"):
            target = torch.arange(0, 3)
        else:
            target = torch.range(0, 2)
        mtr.add(output, target)

        conf_mtrx = mtr.value()
        self.assertEqual(conf_mtrx.sum(), 3, "All should be correct")
        self.assertEqual(conf_mtrx.diagonal().sum(), 3,
                         "All should be correct")

        target = torch.Tensor([1, 0, 0])
        mtr.add(output, target)

        self.assertEqual(conf_mtrx.sum(), 6,
                         "Six tests should give six values")
        self.assertEqual(
            conf_mtrx.diagonal().sum(),
            3,
            "Shouldn't have changed since all new values were false",
        )
        self.assertEqual(conf_mtrx[0].sum(), 3,
                         "All top have gotten one guess")
        self.assertEqual(conf_mtrx[1].sum(), 2,
                         "Two first at the 2nd row have a guess")
        self.assertEqual(conf_mtrx[1][2], 0, "The last one should be empty")
        self.assertEqual(conf_mtrx[2].sum(), 1,
                         "Bottom row has only the first test correct")
        self.assertEqual(conf_mtrx[2][2], 1,
                         "Bottom row has only the first test correct")

        mtr = meter.ConfusionMeter(k=4, normalized=True)
        output = torch.Tensor([
            [0.8, 0.1, 0.1, 0],
            [10, 11, 10, 0],
            [0.2, 0.2, 0.3, 0],
            [0, 0, 0, 1],
        ])

        target = torch.Tensor([0, 1, 2, 3])
        mtr.add(output, target)
        conf_mtrx = mtr.value()

        self.assertEqual(conf_mtrx.sum(), output.size(1),
                         "All should be correct")
        self.assertEqual(conf_mtrx.diagonal().sum(), output.size(1),
                         "All should be correct")

        target[0] = 1
        target[1] = 0
        target[2] = 0

        mtr.add(output, target)
        conf_mtrx = mtr.value()

        self.assertEqual(
            conf_mtrx.sum(),
            output.size(1),
            "The normalization should sum all values to 1",
        )
        for i, row in enumerate(conf_mtrx):
            self.assertEqual(
                row.sum(),
                1,
                "Row no " + str(i) + " fails to sum to one in normalized mode",
            )
コード例 #19
0
def plot_theta_tilde_NN_image_v3(theta_tilde_log, x_per_case, y_per_case,
                                 numb_dct_basis, y_limit):

    theta_tilde = theta_tilde_log.clone().detach()
    input_d = theta_tilde.shape[1]
    hidden_d = theta_tilde.shape[0]
    iteration = theta_tilde.shape[2]
    case = theta_tilde.shape[3]

    fig = plt.figure(figsize=[20, 10])
    fig.patch.set_facecolor('white')
    gs = fig.add_gridspec(case * y_per_case, x_per_case)

    xrange = np.arange(iteration)

    dct_basis_freq = input_d // numb_dct_basis  # theta_tilde(dct_basis_freq * j)
    dct_basis = torch.range(start=0, end=input_d, step=dct_basis_freq).tolist()

    random_theta_m_index = torch.randint(low=0,
                                         high=hidden_d,
                                         size=(x_per_case * y_per_case, ))

    for j in range(case):
        for i in range(y_per_case):
            for k in range(x_per_case):

                theta_m_index = random_theta_m_index[i * x_per_case + k]
                select_theta_tilde = theta_tilde[theta_m_index, :, :, j]

                plot_theta_tilde = select_theta_tilde
                plot_theta_tilde_shifted_by_1 = plot_theta_tilde[:, 1:]
                plot_theta_tilde_remove_last = plot_theta_tilde[:, :-1]
                plot_theta_tilde_rate = torch.abs(
                    plot_theta_tilde_shifted_by_1 -
                    plot_theta_tilde_remove_last)

                gs_y_axis = j * y_per_case + i
                p1 = fig.add_subplot(gs[gs_y_axis, k]).imshow(
                    plot_theta_tilde_rate.detach().cpu().numpy(),
                    aspect="auto",
                    cmap="hot")
                fig.colorbar(p1)
                title = r"$|\Delta\tilde{\theta}_{" + str(
                    theta_m_index.item()) + "}(k)|$ for case " + str(j + 1)
                fig.add_subplot(gs[gs_y_axis, k]).set_title(title)
                yticklabel = [
                    r"$\tilde{\theta}$(" + str(int(dct_basis[i])) + ")"
                    for i in range(len(dct_basis))
                ]

                fig.add_subplot(gs[gs_y_axis, k]).set_yticks(np.arange(
                    start=0, stop=input_d, step=dct_basis_freq),
                                                             minor=False)
                fig.add_subplot(gs[gs_y_axis, k]).set_ylabel("Frequency [k]",
                                                             fontsize=10)
                fig.add_subplot(gs[gs_y_axis, k]).set_xlabel("Iteration",
                                                             fontsize=10)

                if j == 0 and case != 1:
                    fig.add_subplot(gs[gs_y_axis, k]).set_ylim([5, 0])
                    fig.add_subplot(gs[gs_y_axis,
                                       k]).set_yticks(np.arange(start=0,
                                                                stop=10,
                                                                step=3),
                                                      minor=False)
                else:
                    fig.add_subplot(gs[gs_y_axis, k]).set_ylim([y_limit, 0])
コード例 #20
0
import torch
x1 = torch.empty(5, 3)
x2 = torch.range(1, 3)
print(x2)
'''
均匀分布:torch.rand()

标准正态分布:torch.randn()

离散正态分布:torch.normal()

线性间距向量:torch.linespace()
'''
x3 = torch.zeros(5, 3)
x4 = torch.tensor([1, 2])
x1.size()
x5 = torch.add(x1, x2)
x5 = x5.view(15)
x6 = x5.numpy()
x7 = torch.from_numpy(x6)