def convert_to_roi_format(self, boxes): concat_boxes = cat([b.bbox for b in boxes], dim=0) dtype = str(concat_boxes.dtype) ids = cat( [ jt.full((len(b), 1), i, dtype=dtype) for i, b in enumerate(boxes) ], dim=0, ) rois = jt.contrib.concat([ids, concat_boxes], dim=1) return rois
def test_scatter(self): src = jt.arange(1, 11).reshape((2, 5)) index = jt.array([[0, 1, 2, 0]]) x = jt.zeros((3, 5), dtype=src.dtype).scatter_(0, index, src) assert (x.data == [[1, 0, 0, 4, 0], [0, 2, 0, 0, 0], [0, 0, 3, 0, 0]]).all() index = jt.array([[0, 1, 2], [0, 1, 4]]) x = jt.zeros((3, 5), dtype=src.dtype).scatter_(1, index, src) assert (x.data == [[1, 2, 3, 0, 0], [6, 7, 0, 0, 8], [0, 0, 0, 0, 0]]).all() x = jt.full((2, 4), 2.).scatter_(1, jt.array([[2], [3]]), jt.array(1.23), reduce='multiply') assert np.allclose(x.data, [[2.0000, 2.0000, 2.4600, 2.0000], [2.0000, 2.0000, 2.0000, 2.4600]]), x x = jt.full((2, 4), 2.).scatter_(1, jt.array([[2], [3]]), jt.array(1.23), reduce='add') assert np.allclose(x.data, [[2.0000, 2.0000, 3.2300, 2.0000], [2.0000, 2.0000, 2.0000, 3.2300]])
def check_gpu_with_cpu(T, C, N, S, S_min): jt.set_global_seed(1) # Initialize random batch of input vectors, for *size = (T,N,C) input = jt.randn(T, N, C).log_softmax(2) # input = -jt.ones((T, N, C)) # input[0,0,1] += 0.01 # Initialize random batch of targets (0 = blank, 1:C = classes) target = jt.randint(low=1, high=C, shape=(N, S), dtype=jt.int) _input_jt = input input_lengths = jt.full((N, ), T, dtype=jt.int) target_lengths = jt.randint(low=S_min, high=S + 1, shape=(N, ), dtype=jt.int) # ctc_loss = nn.CTCLoss() loss = jt.ctc_loss(input, target, input_lengths, target_lengths, reduction='none') _loss_jt = loss loss_jt = loss.numpy() dinput_jt = jt.grad(_loss_jt, _input_jt) dinput_jt.sync() with jt.flag_scope(use_cuda=1): input = input.copy() target = target.copy() input_lengths = input_lengths.copy() target_lengths = target_lengths.copy() loss = jt.ctc_loss(input, target, input_lengths, target_lengths, reduction='none') grad = jt.grad(loss, input) np.testing.assert_allclose(_loss_jt.numpy(), loss.numpy(), atol=1e-5, rtol=1e-5) np.testing.assert_allclose(dinput_jt.numpy(), grad.numpy(), atol=1e-5, rtol=1e-5)
def check(T, C, N, S, S_min): jt.set_global_seed(0) # Initialize random batch of input vectors, for *size = (T,N,C) input = jt.randn(T, N, C).log_softmax(2) # input = -jt.ones((T, N, C)) # input[0,0,1] += 0.01 # Initialize random batch of targets (0 = blank, 1:C = classes) target = jt.randint(low=1, high=C, shape=(N, S), dtype=jt.int) _input_jt = input input_lengths = jt.full((N, ), T, dtype=jt.int) target_lengths = jt.randint(low=S_min, high=S + 1, shape=(N, ), dtype=jt.int) # ctc_loss = nn.CTCLoss() loss = jt.ctc_loss(input, target, input_lengths, target_lengths, reduction='none') _loss_jt = loss loss_jt = loss.numpy() input = torch.Tensor(input.numpy()).detach().requires_grad_() input_lengths = torch.full(size=(N, ), fill_value=T, dtype=torch.long) target_lengths = torch.LongTensor(target_lengths.numpy()) input_lengths = torch.LongTensor(input_lengths.numpy()) target = torch.LongTensor(target.numpy()) loss = tnn.CTCLoss(reduction='none')(input, target, input_lengths, target_lengths) np.testing.assert_allclose(loss.detach().numpy(), loss_jt, rtol=1e-5, atol=1e-5) dinput_jt = jt.grad(_loss_jt, _input_jt) dinput_jt.sync() loss.sum().backward()
def remove_isolated_nodes(edge_index, edge_attr=None, num_nodes=None): r"""Removes the isolated nodes from the graph given by :attr:`edge_index` with optional edge attributes :attr:`edge_attr`. In addition, returns a mask of shape :obj:`[num_nodes]` to manually filter out isolated node features later on. Self-loops are preserved for non-isolated nodes. Args: edge_index (Var int32): The edge indices. edge_attr (Var, optional): Edge weights or multi-dimensional edge features. (default: :obj:`None`) num_nodes (int, optional): The number of nodes, *i.e.* :obj:`max_val + 1` of :attr:`edge_index`. (default: :obj:`None`) :rtype: (Var int32, Var, Var bool) """ num_nodes = maybe_num_nodes(edge_index, num_nodes) out = segregate_self_loops(edge_index, edge_attr) edge_index, edge_attr, loop_edge_index, loop_edge_attr = out mask = jt.zeros((num_nodes), dtype=Var.bool) mask[edge_index.view(-1)] = 1 assoc = jt.full((num_nodes, ), -1, dtype=Var.int32) assoc[mask] = jt.arange(mask.sum()) edge_index = assoc[edge_index] loop_mask = jt.zeros_like(mask) loop_mask[loop_edge_index[0]] = 1 loop_mask = loop_mask & mask loop_assoc = jt.full_like(assoc, -1) loop_assoc[loop_edge_index[0]] = jt.arange(loop_edge_index.size(1)) loop_idx = loop_assoc[loop_mask] loop_edge_index = assoc[loop_edge_index[:, loop_idx]] edge_index = jt.concat([edge_index, loop_edge_index], dim=1) if edge_attr is not None: loop_edge_attr = loop_edge_attr[loop_idx] edge_attr = jt.concat([edge_attr, loop_edge_attr], dim=0) return edge_index, edge_attr, mask
def select_over_all_levels(self, boxlists): num_images = len(boxlists) results = [] for i in range(num_images): scores = boxlists[i].get_field("scores") labels = boxlists[i].get_field("labels") boxes = boxlists[i].bbox boxlist = boxlists[i] result = [] # skip the background for j in range(1, self.num_classes): inds = (labels == j).nonzero().view(-1) scores_j = scores[inds] boxes_j = boxes[inds, :].view(-1, 4) boxlist_for_class = BoxList(boxes_j, boxlist.size, mode="xyxy") boxlist_for_class.add_field("scores", scores_j) boxlist_for_class = boxlist_nms(boxlist_for_class, self.nms_thresh, score_field="scores") num_labels = len(boxlist_for_class) boxlist_for_class.add_field("labels", jt.full((num_labels, ), j).int32()) result.append(boxlist_for_class) result = cat_boxlist(result) number_of_detections = len(result) # Limit to max_per_image detections **over all classes** if number_of_detections > self.fpn_post_nms_top_n > 0: cls_scores = result.get_field("scores") image_thresh, _ = jt.kthvalue( cls_scores, number_of_detections - self.fpn_post_nms_top_n + 1) keep = cls_scores >= image_thresh keep = jt.nonzero(keep).squeeze(1) result = result[keep] results.append(result) return results
def filter_results(self, boxlist, num_classes): """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). """ # unwrap the boxlist to avoid additional overhead. # if we had multi-class NMS, we could perform this directly on the boxlist boxes = boxlist.bbox.reshape(-1, num_classes * 4) scores = boxlist.get_field("scores").reshape(-1, num_classes) result = [] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class # inds_all = (scores > self.score_thresh).int() inds_all = scores > self.score_thresh # print(self.score_thresh,num_classes) # print(inds_all.shape) # inds_all = inds_all.transpose(1,0) inds_nonzeros = [ inds_all[:,j].nonzero() for j in range(1, num_classes) ] jt.sync(inds_nonzeros) for j in range(1, num_classes): # with nvtx_scope("aa"): # inds = inds_all[:,j].nonzero().squeeze(1) # with nvtx_scope("bb"): # scores_j = scores[inds, j] # boxes_j = boxes[inds, j * 4 : (j + 1) * 4] # with nvtx_scope("cc"): # boxlist_for_class = BoxList(boxes_j, boxlist.size, mode="xyxy") # with nvtx_scope("cc2"): # boxlist_for_class.add_field("scores", scores_j) # with nvtx_scope("cc3"): # boxlist_for_class = boxlist_nms( # boxlist_for_class, self.nms # ) # with nvtx_scope("dd"): # num_labels = len(boxlist_for_class) # with nvtx_scope("dd2"): # boxlist_for_class.add_field( # "labels", jt.full((num_labels,), j).int32() # ) # result.append(boxlist_for_class) # inds = inds_all[:,j].nonzero().squeeze(1) inds = inds_nonzeros[j-1] if inds.shape[0] == 0: continue inds = inds.squeeze(1) scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4 : (j + 1) * 4] boxlist_for_class = BoxList(boxes_j, boxlist.size, mode="xyxy") boxlist_for_class.add_field("scores", scores_j) boxlist_for_class = boxlist_nms( boxlist_for_class, self.nms ) num_labels = len(boxlist_for_class) # print(j,num_labels) boxlist_for_class.add_field( "labels", jt.full((num_labels,), j).int32() ) result.append(boxlist_for_class) result = cat_boxlist(result) if not result.has_field('labels'): result.add_field('labels',jt.empty((0,))) if not result.has_field('scores'): result.add_field('scores',jt.empty((0,))) number_of_detections = len(result) #Limit to max_per_image detections **over all classes** if number_of_detections > self.detections_per_img > 0: cls_scores = result.get_field("scores") image_thresh, _ = jt.kthvalue( cls_scores, number_of_detections - self.detections_per_img + 1 ) keep = cls_scores >= image_thresh keep = jt.nonzero(keep).squeeze(1) result = result[keep] # # Absolute limit detection imgs # if number_of_detections > self.detections_per_img > 0: # cls_scores = result.get_field("scores") # scores, indices = jt.topk( # cls_scores, self.detections_per_img # ) # result = result[indices] return result