Python torch.range() Examples
The following are 30
code examples of torch.range().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example #1
Source File: outils.py From ArtMiner with MIT License | 6 votes |
def ScoreRANSAC(match1, match2, matchSetT, sampleIndex, score, tolerance, nbSamplePoint, paramEstimate) : #All The Data nbMatch = len(matchSetT) sampleIndex = np.array(matchSetT)[sampleIndex] X = match1[sampleIndex] Y = match2[sampleIndex] H21 = paramEstimate(X, Y) error = Prediction(match1, match2, H21) isInlier = error < tolerance inlier = {} for i in range(len(match1)) : if isInlier[i] : score_i = score[i] * np.exp(-1 * error[i] ** 2 / tolerance ** 2) #score_i = score[i] key = (match1[i][0], match1[i][1]) if inlier.has_key(key) and inlier[key][1] < score_i : inlier[key] = [match2[i], score_i] elif not inlier.has_key(key) : inlier[key] = [match2[i], score_i] return H21, sum([item[1] for item in inlier.values()]), matchSetT
Example #2
Source File: test_meters.py From tnt with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testClassErrorMeter(self): mtr = meter.ClassErrorMeter(topk=[1]) output = torch.eye(3) if hasattr(torch, "arange"): target = torch.arange(0, 3) else: 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")
Example #3
Source File: test_oim.py From open-reid with MIT License | 6 votes |
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)
Example #4
Source File: adaptive_inference.py From MSDNet-PyTorch with MIT License | 6 votes |
def dynamic_eval_with_threshold(self, logits, targets, flops, T): n_stage, n_sample, _ = logits.size() max_preds, argmax_preds = logits.max(dim=2, keepdim=False) # take the max logits as confidence acc_rec, exp = torch.zeros(n_stage), torch.zeros(n_stage) acc, expected_flops = 0, 0 for i in range(n_sample): gold_label = targets[i] for k in range(n_stage): if max_preds[k][i].item() >= T[k]: # force to exit at k _g = int(gold_label.item()) _pred = int(argmax_preds[k][i].item()) if _g == _pred: acc += 1 acc_rec[k] += 1 exp[k] += 1 break acc_all, sample_all = 0, 0 for k in range(n_stage): _t = exp[k] * 1.0 / n_sample sample_all += exp[k] expected_flops += _t * flops[k] acc_all += acc_rec[k] return acc * 100.0 / n_sample, expected_flops
Example #5
Source File: adaptive_inference.py From MSDNet-PyTorch with MIT License | 6 votes |
def dynamic_evaluate(model, test_loader, val_loader, args): tester = Tester(model, args) if os.path.exists(os.path.join(args.save, 'logits_single.pth')): val_pred, val_target, test_pred, test_target = \ torch.load(os.path.join(args.save, 'logits_single.pth')) else: val_pred, val_target = tester.calc_logit(val_loader) test_pred, test_target = tester.calc_logit(test_loader) torch.save((val_pred, val_target, test_pred, test_target), os.path.join(args.save, 'logits_single.pth')) flops = torch.load(os.path.join(args.save, 'flops.pth')) with open(os.path.join(args.save, 'dynamic.txt'), 'w') as fout: for p in range(1, 40): print("*********************") _p = torch.FloatTensor(1).fill_(p * 1.0 / 20) probs = torch.exp(torch.log(_p) * torch.range(1, args.nBlocks)) probs /= probs.sum() acc_val, _, T = tester.dynamic_eval_find_threshold( val_pred, val_target, probs, flops) acc_test, exp_flops = tester.dynamic_eval_with_threshold( test_pred, test_target, flops, T) print('valid acc: {:.3f}, test acc: {:.3f}, test flops: {:.2f}M'.format(acc_val, acc_test, exp_flops / 1e6)) fout.write('{}\t{}\n'.format(acc_test, exp_flops.item()))
Example #6
Source File: polarmask_head.py From PolarMask with Apache License 2.0 | 6 votes |
def get_points(self, featmap_sizes, dtype, device): """Get points according to feature map sizes. Args: featmap_sizes (list[tuple]): Multi-level feature map sizes. dtype (torch.dtype): Type of points. device (torch.device): Device of points. Returns: tuple: points of each image. """ mlvl_points = [] for i in range(len(featmap_sizes)): mlvl_points.append( self.get_points_single(featmap_sizes[i], self.strides[i], dtype, device)) return mlvl_points
Example #7
Source File: asgcn.py From cogdl with MIT License | 6 votes |
def __init__(self, num_features, num_classes, hidden_size, num_layers, dropout, sample_size): super(ASGCN, self).__init__() self.num_features = num_features self.num_classes = num_classes self.hidden_size = hidden_size self.num_layers = num_layers self.dropout = dropout self.sample_size = sample_size self.w_s0 = Parameter(torch.FloatTensor(num_features)) self.w_s1 = Parameter(torch.FloatTensor(num_features)) shapes = [num_features] + [hidden_size] * (num_layers - 1) + [num_classes] self.convs = nn.ModuleList( [ GraphConvolution(shapes[layer], shapes[layer + 1]) for layer in range(num_layers) ] ) self.reset_parameters()
Example #8
Source File: outils.py From ArtMiner with MIT License | 6 votes |
def GetCC(match1, match2, mask1, mask2, score) : match1_arr = np.array(match1).astype(int) mask1[match1_arr[:, 0].flatten(), match1_arr[:, 1].flatten()] = 1 label1, nb_label1 = measure.label(mask1, connectivity=2, return_num=True) dict_match = dict(zip(match1, match2)) dict_score = dict(zip(match1, score)) CC = [] CC_score = np.zeros(nb_label1) for i in range(1, nb_label1 + 1) : CC.append({}) posx, posy = np.where(label1 == i) tmp_match1 = [(posx[j], posy[j]) for j in range(len(posx))] tmp_match2 = [dict_match[item] for item in tmp_match1] CC[i-1] = dict(zip(tmp_match1, tmp_match2)) CC[i-1]['mask2shape'] = mask2.shape CC_score[i -1 ] = sum(dict_score[item] for item in tmp_match1) return CC, CC_score
Example #9
Source File: masked_cross_entropy.py From tatk with Apache License 2.0 | 5 votes |
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 = torch.arange(0, max_len).long() # andy 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
Example #10
Source File: asgcn.py From cogdl with MIT License | 5 votes |
def sampling(self, x, v): all_support = [[] for _ in range(self.num_layers)] sampled = v x = torch.cat((x, torch.zeros(1, x.shape[1]).to(x.device)), dim=0) for i in range(self.num_layers - 1, -1, -1): cur_sampled, cur_support = self._sample_one_layer(x, self.adj[sampled], sampled, self.sample_size[i]) all_support[i] = cur_support.to(x.device) sampled = cur_sampled return x[sampled.to(x.device)], all_support, 0
Example #11
Source File: cluster_loss.py From reid_baseline_with_syncbn with MIT License | 5 votes |
def _shortest_dist(self, dist_mat): """Parallel version. Args: dist_mat: pytorch Variable, available shape: 1) [m, n] 2) [m, n, N], N is batch size 3) [m, n, *], * can be arbitrary additional dimensions Returns: dist: three cases corresponding to `dist_mat`: 1) scalar 2) pytorch Variable, with shape [N] 3) pytorch Variable, with shape [*] """ m, n = dist_mat.size()[:2] # Just offering some reference for accessing intermediate distance. dist = [[0 for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): if (i == 0) and (j == 0): dist[i][j] = dist_mat[i, j] elif (i == 0) and (j > 0): dist[i][j] = dist[i][j - 1] + dist_mat[i, j] elif (i > 0) and (j == 0): dist[i][j] = dist[i - 1][j] + dist_mat[i, j] else: dist[i][j] = torch.min(dist[i - 1][j], dist[i][j - 1]) + dist_mat[i, j] dist = dist[-1][-1] return dist
Example #12
Source File: asgcn.py From cogdl with MIT License | 5 votes |
def from_adjlist(self, adj): """Transfer adj-list format to sparsetensor""" u_sampled, index = torch.unique(torch.flatten(adj), return_inverse=True) row = (torch.range(0, index.shape[0]-1) / adj.shape[1]).long().to(adj.device) col = index values = torch.ones(index.shape[0]).float().to(adj.device) indices = torch.cat([row.unsqueeze(1), col.unsqueeze(1)], axis=1).t() dense_shape = (adj.shape[0], u_sampled.shape[0]) support = torch.sparse_coo_tensor(indices, values, dense_shape) return support, u_sampled.long()
Example #13
Source File: mydataset.py From ocr.pytorch with MIT License | 5 votes |
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)
Example #14
Source File: mydataset.py From ocr.pytorch with MIT License | 5 votes |
def randomGaussian(image, mean=0.2, sigma=0.3): """ 对图像进行高斯噪声处理 :param image: :return: """ def gaussianNoisy(im, mean=0.2, sigma=0.3): """ 对图像做高斯噪音处理 :param im: 单通道图像 :param mean: 偏移量 :param sigma: 标准差 :return: """ for _i in range( len( im ) ): im[_i] += random.gauss( mean, sigma ) return im # 将图像转化成数组 img = np.asarray( image ) img.flags.writeable = True # 将数组改为读写模式 width, height = img.shape[:2] img_r = gaussianNoisy( img[:, :, 0].flatten(), mean, sigma ) img_g = gaussianNoisy( img[:, :, 1].flatten(), mean, sigma ) img_b = gaussianNoisy( img[:, :, 2].flatten(), mean, sigma ) img[:, :, 0] = img_r.reshape( [width, height] ) img[:, :, 1] = img_g.reshape( [width, height] ) img[:, :, 2] = img_b.reshape( [width, height] ) return Image.fromarray( np.uint8( img ) )
Example #15
Source File: dataset.py From quantproject with Apache License 2.0 | 5 votes |
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)
Example #16
Source File: dataset.py From quantproject with Apache License 2.0 | 5 votes |
def __getitem__(self, index): assert index <= len(self), 'index range error' index += 1 with self.env.begin(write=False) as txn: img_key = 'image-%09d' % index imgbuf = txn.get(img_key) buf = six.BytesIO() buf.write(imgbuf) buf.seek(0) try: img = Image.open(buf).convert('L') except IOError: print('Corrupted image for %d' % index) return self[index + 1] if self.transform is not None: img = self.transform(img) label_key = 'label-%09d' % index label = str(txn.get(label_key)) if self.target_transform is not None: label = self.target_transform(label) return (img, label)
Example #17
Source File: cluster_loss.py From CVWC2019-Amur-Tiger-Re-ID with Apache License 2.0 | 5 votes |
def _shortest_dist(self, dist_mat): """Parallel version. Args: dist_mat: pytorch Variable, available shape: 1) [m, n] 2) [m, n, N], N is batch size 3) [m, n, *], * can be arbitrary additional dimensions Returns: dist: three cases corresponding to `dist_mat`: 1) scalar 2) pytorch Variable, with shape [N] 3) pytorch Variable, with shape [*] """ m, n = dist_mat.size()[:2] # Just offering some reference for accessing intermediate distance. dist = [[0 for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): if (i == 0) and (j == 0): dist[i][j] = dist_mat[i, j] elif (i == 0) and (j > 0): dist[i][j] = dist[i][j - 1] + dist_mat[i, j] elif (i > 0) and (j == 0): dist[i][j] = dist[i - 1][j] + dist_mat[i, j] else: dist[i][j] = torch.min(dist[i - 1][j], dist[i][j - 1]) + dist_mat[i, j] dist = dist[-1][-1] return dist
Example #18
Source File: asgcn.py From cogdl with MIT License | 5 votes |
def compute_adjlist(self, sp_adj, max_degree=32): """Transfer sparse adjacent matrix to adj-list format""" num_data = sp_adj.shape[0] adj = num_data + np.zeros((num_data+1, max_degree), dtype=np.int32) for v in range(num_data): neighbors = np.nonzero(sp_adj[v, :])[1] len_neighbors = len(neighbors) if len_neighbors > max_degree: neighbors = np.random.choice(neighbors, max_degree, replace=False) adj[v] = neighbors else: adj[v, :len_neighbors] = neighbors return adj
Example #19
Source File: chu_liu_edmonds_test.py From magnitude with MIT License | 5 votes |
def test_find_cycle(self): # No cycle parents = [0, 2, 3, 0, 3] current_nodes = [True for _ in range(5)] has_cycle, cycle = _find_cycle(parents, 5, current_nodes) assert not has_cycle assert not cycle # Cycle parents = [0, 2, 3, 1, 3] has_cycle, cycle = _find_cycle(parents, 5, current_nodes) assert has_cycle assert cycle == [1, 2, 3] # No cycle if ignored nodes are correctly ignored. parents = [-1, 0, 1, 4, 3] current_nodes = [True for _ in range(5)] current_nodes[4] = False current_nodes[3] = False has_cycle, cycle = _find_cycle(parents, 5, current_nodes) assert not has_cycle assert cycle == [] # Cycle, but excluding ignored nodes which form their own cycle. parents = [-1, 2, 1, 4, 3] current_nodes = [True for _ in range(5)] current_nodes[1] = False current_nodes[2] = False has_cycle, cycle = _find_cycle(parents, 5, current_nodes) assert has_cycle assert cycle == [3, 4]
Example #20
Source File: loss.py From SceneChangeDet with MIT License | 5 votes |
def __init__(self,num_steps,dist_flag='l2'): super(HistogramMaskLoss, self).__init__() self.step = 1.0 / (num_steps - 1) self.t = torch.range(0, 1, self.step).view(-1, 1) self.dist_flag = dist_flag self.distance = KLCoefficient()
Example #21
Source File: loss.py From SceneChangeDet with MIT License | 5 votes |
def __init__(self, num_steps): super(SampleHistogramLoss, self).__init__() self.step = 1.0 / (num_steps - 1) self.t = torch.range(0, 1, self.step).view(-1, 1).cuda() self.tsize = self.t.size()[0]
Example #22
Source File: magnet_loss.py From MagnetLoss-PyTorch with MIT License | 5 votes |
def expand_dims(var, dim=0): """ Is similar to [numpy.expand_dims](https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html). var = torch.range(0, 9).view(-1, 2) torch.expand_dims(var, 0).size() # (1, 5, 2) """ sizes = list(var.size()) sizes.insert(dim, 1) return var.view(*sizes)
Example #23
Source File: dataset.py From crnn-pytorch with MIT License | 5 votes |
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)
Example #24
Source File: dataset.py From crnn-pytorch with MIT License | 5 votes |
def __getitem__(self, index): assert index <= len(self), 'index range error' index += 1 with self.env.begin(write=False) as txn: img_key = 'image-%09d' % index imgbuf = txn.get(img_key.encode('utf-8')) buf = six.BytesIO() buf.write(imgbuf) buf.seek(0) try: img = Image.open(buf).convert('L') except IOError: print('Corrupted image for %d' % index) return self[index + 1] if self.transform is not None: img = self.transform(img) label_key = 'label-%09d' % index label = txn.get(label_key.encode('utf-8')) if self.target_transform is not None: label = self.target_transform(label) return (img, label)
Example #25
Source File: contactloss.py From obman_train with GNU General Public License v3.0 | 5 votes |
def batch_index_select(inp, dim, index): views = [inp.shape[0]] + [ 1 if i != dim else -1 for i in range(1, len(inp.shape)) ] expanse = list(inp.shape) expanse[0] = -1 expanse[dim] = -1 index = index.view(views).expand(expanse) return torch.gather(inp, dim, index)
Example #26
Source File: fcos_instance_head_miou_mskctness.py From PolarMask with Apache License 2.0 | 5 votes |
def get_bboxes(self, cls_scores, bbox_preds, centernesses, mask_preds, img_metas, cfg, rescale=None): assert len(cls_scores) == len(bbox_preds) num_levels = len(cls_scores) featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores] mlvl_points = self.get_points(featmap_sizes, bbox_preds[0].dtype, bbox_preds[0].device) result_list = [] for img_id in range(len(img_metas)): cls_score_list = [ cls_scores[i][img_id].detach() for i in range(num_levels) ] bbox_pred_list = [ bbox_preds[i][img_id].detach() for i in range(num_levels) ] centerness_pred_list = [ centernesses[i][img_id].detach() for i in range(num_levels) ] mask_pred_list = [ mask_preds[i][img_id].detach() for i in range(num_levels) ] img_shape = img_metas[img_id]['img_shape'] scale_factor = img_metas[img_id]['scale_factor'] det_bboxes = self.get_bboxes_single(cls_score_list, bbox_pred_list, mask_pred_list, centerness_pred_list, mlvl_points, img_shape, scale_factor, cfg, rescale) result_list.append(det_bboxes) return result_list
Example #27
Source File: fcos_instance_head_miou_mskctness.py From PolarMask with Apache License 2.0 | 5 votes |
def fcos_target(self, points, extra_data): assert len(points) == len(self.regress_ranges) num_levels = len(points) labels_list, bbox_targets_list, mask_targets_list = extra_data.values() # split to per img, per level num_points = [center.size(0) for center in points] labels_list = [labels.split(num_points, 0) for labels in labels_list] bbox_targets_list = [ bbox_targets.split(num_points, 0) for bbox_targets in bbox_targets_list ] mask_targets_list = [ mask_targets.split(num_points, 0) for mask_targets in mask_targets_list ] # concat per level image concat_lvl_labels = [] concat_lvl_bbox_targets = [] concat_lvl_mask_targets = [] for i in range(num_levels): concat_lvl_labels.append( torch.cat([labels[i] for labels in labels_list])) concat_lvl_bbox_targets.append( torch.cat( [bbox_targets[i] for bbox_targets in bbox_targets_list])) concat_lvl_mask_targets.append( torch.cat( [mask_targets[i] for mask_targets in mask_targets_list])) return concat_lvl_labels, concat_lvl_bbox_targets, concat_lvl_mask_targets
Example #28
Source File: polarmask_head.py From PolarMask with Apache License 2.0 | 5 votes |
def get_bboxes(self, cls_scores, bbox_preds, centernesses, mask_preds, img_metas, cfg, rescale=None): assert len(cls_scores) == len(bbox_preds) num_levels = len(cls_scores) featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores] mlvl_points = self.get_points(featmap_sizes, bbox_preds[0].dtype, bbox_preds[0].device) result_list = [] for img_id in range(len(img_metas)): cls_score_list = [ cls_scores[i][img_id].detach() for i in range(num_levels) ] bbox_pred_list = [ bbox_preds[i][img_id].detach() for i in range(num_levels) ] centerness_pred_list = [ centernesses[i][img_id].detach() for i in range(num_levels) ] mask_pred_list = [ mask_preds[i][img_id].detach() for i in range(num_levels) ] img_shape = img_metas[img_id]['img_shape'] scale_factor = img_metas[img_id]['scale_factor'] det_bboxes = self.get_bboxes_single(cls_score_list, bbox_pred_list, mask_pred_list, centerness_pred_list, mlvl_points, img_shape, scale_factor, cfg, rescale) result_list.append(det_bboxes) return result_list
Example #29
Source File: polarmask_head.py From PolarMask with Apache License 2.0 | 5 votes |
def polar_target(self, points, extra_data): assert len(points) == len(self.regress_ranges) num_levels = len(points) labels_list, bbox_targets_list, mask_targets_list = extra_data.values() # split to per img, per level num_points = [center.size(0) for center in points] labels_list = [labels.split(num_points, 0) for labels in labels_list] bbox_targets_list = [ bbox_targets.split(num_points, 0) for bbox_targets in bbox_targets_list ] mask_targets_list = [ mask_targets.split(num_points, 0) for mask_targets in mask_targets_list ] # concat per level image concat_lvl_labels = [] concat_lvl_bbox_targets = [] concat_lvl_mask_targets = [] for i in range(num_levels): concat_lvl_labels.append( torch.cat([labels[i] for labels in labels_list])) concat_lvl_bbox_targets.append( torch.cat( [bbox_targets[i] for bbox_targets in bbox_targets_list])) concat_lvl_mask_targets.append( torch.cat( [mask_targets[i] for mask_targets in mask_targets_list])) return concat_lvl_labels, concat_lvl_bbox_targets, concat_lvl_mask_targets
Example #30
Source File: outils.py From ArtMiner with MIT License | 5 votes |
def ScaleList(featScaleBase, nbOctave, scalePerOctave) : scaleList = np.array([featScaleBase * (2 ** nbOctave - 2**(float(scaleId) / scalePerOctave)) for scaleId in range(0, 1 + nbOctave * scalePerOctave)]).astype(int) + featScaleBase return scaleList