Python torch.cat() Examples
The following are 30 code examples for showing how to use torch.cat(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example 1
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: proposal_target_layer.py License: MIT License | 6 votes |
def _get_bbox_regression_labels(bbox_target_data, num_classes): """Bounding-box regression targets (bbox_target_data) are stored in a compact form N x (class, tx, ty, tw, th) This function expands those targets into the 4-of-4*K representation used by the network (i.e. only one class has non-zero targets). Returns: bbox_target (ndarray): N x 4K blob of regression targets bbox_inside_weights (ndarray): N x 4K blob of loss weights """ # Inputs are tensor clss = bbox_target_data[:, 0] bbox_targets = clss.new(clss.numel(), 4 * num_classes).zero_() bbox_inside_weights = clss.new(bbox_targets.shape).zero_() inds = (clss > 0).nonzero().view(-1) if inds.numel() > 0: clss = clss[inds].contiguous().view(-1,1) dim1_inds = inds.unsqueeze(1).expand(inds.size(0), 4) dim2_inds = torch.cat([4*clss, 4*clss+1, 4*clss+2, 4*clss+3], 1).long() bbox_targets[dim1_inds, dim2_inds] = bbox_target_data[inds][:, 1:] bbox_inside_weights[dim1_inds, dim2_inds] = bbox_targets.new(cfg.TRAIN.BBOX_INSIDE_WEIGHTS).view(-1, 4).expand_as(dim1_inds) return bbox_targets, bbox_inside_weights
Example 2
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
def greedy_decode(self, latent, max_len, start_id): ''' latent: (batch_size, max_src_seq, d_model) src_mask: (batch_size, 1, max_src_len) ''' batch_size = latent.size(0) ys = get_cuda(torch.ones(batch_size, 1).fill_(start_id).long()) # (batch_size, 1) for i in range(max_len - 1): # input("==========") # print("="*10, i) # print("ys", ys.size()) # (batch_size, i) # print("tgt_mask", subsequent_mask(ys.size(1)).size()) # (1, i, i) out = self.decode(latent.unsqueeze(1), to_var(ys), to_var(subsequent_mask(ys.size(1)).long())) prob = self.generator(out[:, -1]) # print("prob", prob.size()) # (batch_size, vocab_size) _, next_word = torch.max(prob, dim=1) # print("next_word", next_word.size()) # (batch_size) # print("next_word.unsqueeze(1)", next_word.unsqueeze(1).size()) ys = torch.cat([ys, next_word.unsqueeze(1)], dim=1) # print("ys", ys.size()) return ys[:, 1:]
Example 3
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
def forward(self, src, tgt, src_mask, tgt_mask): """ Take in and process masked src and target sequences. """ memory = self.encode(src, src_mask) # (batch_size, max_src_seq, d_model) # attented_mem=self.attention(memory,memory,memory,src_mask) # memory=attented_mem score = self.attention(memory, memory, src_mask) attent_memory = score.bmm(memory) # memory=self.linear(torch.cat([memory,attent_memory],dim=-1)) memory, _ = self.gru(attented_mem) ''' score=torch.sigmoid(self.linear(memory)) memory=memory*score ''' latent = torch.sum(memory, dim=1) # (batch_size, d_model) logit = self.decode(latent.unsqueeze(1), tgt, tgt_mask) # (batch_size, max_tgt_seq, d_model) # logit,_=self.gru_decoder(logit) prob = self.generator(logit) # (batch_size, max_seq, vocab_size) return latent, prob
Example 4
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
def greedy_decode(self, latent, max_len, start_id): ''' latent: (batch_size, max_src_seq, d_model) src_mask: (batch_size, 1, max_src_len) ''' batch_size = latent.size(0) ys = get_cuda(torch.ones(batch_size, 1).fill_(start_id).long()) # (batch_size, 1) for i in range(max_len - 1): # input("==========") # print("="*10, i) # print("ys", ys.size()) # (batch_size, i) # print("tgt_mask", subsequent_mask(ys.size(1)).size()) # (1, i, i) out = self.decode(latent.unsqueeze(1), to_var(ys), to_var(subsequent_mask(ys.size(1)).long())) prob = self.generator(out[:, -1]) # print("prob", prob.size()) # (batch_size, vocab_size) _, next_word = torch.max(prob, dim=1) # print("next_word", next_word.size()) # (batch_size) # print("next_word.unsqueeze(1)", next_word.unsqueeze(1).size()) ys = torch.cat([ys, next_word.unsqueeze(1)], dim=1) # print("ys", ys.size()) return ys[:, 1:]
Example 5
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
def forward(self, src, tgt, src_mask, tgt_mask): """ Take in and process masked src and target sequences. """ memory = self.encode(src, src_mask) # (batch_size, max_src_seq, d_model) # attented_mem=self.attention(memory,memory,memory,src_mask) # memory=attented_mem score = self.attention(memory, memory, src_mask) attent_memory = score.bmm(memory) # memory=self.linear(torch.cat([memory,attent_memory],dim=-1)) memory, _ = self.gru(attented_mem) ''' score=torch.sigmoid(self.linear(memory)) memory=memory*score ''' latent = torch.sum(memory, dim=1) # (batch_size, d_model) logit = self.decode(latent.unsqueeze(1), tgt, tgt_mask) # (batch_size, max_tgt_seq, d_model) # logit,_=self.gru_decoder(logit) prob = self.generator(logit) # (batch_size, max_seq, vocab_size) return latent, prob
Example 6
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 6 votes |
def greedy_decode(self, latent, max_len, start_id): ''' latent: (batch_size, max_src_seq, d_model) src_mask: (batch_size, 1, max_src_len) ''' batch_size = latent.size(0) ys = get_cuda(torch.ones(batch_size, 1).fill_(start_id).long()) # (batch_size, 1) for i in range(max_len - 1): # input("==========") # print("="*10, i) # print("ys", ys.size()) # (batch_size, i) # print("tgt_mask", subsequent_mask(ys.size(1)).size()) # (1, i, i) out = self.decode(latent.unsqueeze(1), to_var(ys), to_var(subsequent_mask(ys.size(1)).long())) prob = self.generator(out[:, -1]) # print("prob", prob.size()) # (batch_size, vocab_size) _, next_word = torch.max(prob, dim=1) # print("next_word", next_word.size()) # (batch_size) # print("next_word.unsqueeze(1)", next_word.unsqueeze(1).size()) ys = torch.cat([ys, next_word.unsqueeze(1)], dim=1) # print("ys", ys.size()) return ys[:, 1:]
Example 7
Project: DDPAE-video-prediction Author: jthsieh File: DDPAE_utils.py License: MIT License | 6 votes |
def pose_inv_full(pose): ''' param pose: N x 6 Inverse the 2x3 transformer matrix. ''' N, _ = pose.size() b = pose.view(N, 2, 3)[:, :, 2:] # A^{-1} # Calculate determinant determinant = (pose[:, 0] * pose[:, 4] - pose[:, 1] * pose[:, 3] + 1e-8).view(N, 1) indices = Variable(torch.LongTensor([4, 1, 3, 0]).cuda()) scale = Variable(torch.Tensor([1, -1, -1, 1]).cuda()) A_inv = torch.index_select(pose, 1, indices) * scale / determinant A_inv = A_inv.view(N, 2, 2) # b' = - A^{-1} b b_inv = - A_inv.matmul(b).view(N, 2, 1) transformer_inv = torch.cat([A_inv, b_inv], dim=2) return transformer_inv
Example 8
Project: DDPAE-video-prediction Author: jthsieh File: DDPAE.py License: MIT License | 6 votes |
def sample_content(self, content, sample): ''' Pass into content_lstm to get a final content. ''' content = content.view(-1, self.n_frames_input, self.total_components, self.content_latent_size) contents = [] for i in range(self.total_components): z = content[:, :, i, :] z = self.content_lstm(z).unsqueeze(1) # batch_size x 1 x (content_latent_size * 2) contents.append(z) content = torch.cat(contents, dim=1).view(-1, self.content_latent_size * 2) # Get mu and sigma, and sample. content_mu = content[:, :self.content_latent_size] content_sigma = F.softplus(content[:, self.content_latent_size:]) content = self.pyro_sample('content', dist.Normal, content_mu, content_sigma, sample) return content
Example 9
Project: DDPAE-video-prediction Author: jthsieh File: DDPAE.py License: MIT License | 6 votes |
def test(self, input, output): ''' Return decoded output. ''' input = Variable(input.cuda()) batch_size, _, _, H, W = input.size() output = Variable(output.cuda()) gt = torch.cat([input, output], dim=1) latent = self.encode(input, sample=False) decoded_output, components = self.decode(latent, input.size(0)) decoded_output = decoded_output.view(*gt.size()) components = components.view(batch_size, self.n_frames_total, self.total_components, self.n_channels, H, W) latent['components'] = components decoded_output = decoded_output.clamp(0, 1) self.save_visuals(gt, decoded_output, components, latent) return decoded_output.cpu(), latent
Example 10
Project: hgraph2graph Author: wengong-jin File: nnutils.py License: MIT License | 6 votes |
def hier_topk(cls_scores, icls_scores, vocab, topk): batch_size = len(cls_scores) cls_scores = F.log_softmax(cls_scores, dim=-1) cls_scores_topk, cls_topk = cls_scores.topk(topk, dim=-1) final_topk = [] for i in range(topk): clab = cls_topk[:, i] mask = vocab.get_mask(clab) masked_icls_scores = F.log_softmax(icls_scores + mask, dim=-1) icls_scores_topk, icls_topk = masked_icls_scores.topk(topk, dim=-1) topk_scores = cls_scores_topk[:, i].unsqueeze(-1) + icls_scores_topk final_topk.append( (topk_scores, clab.unsqueeze(-1).expand(-1, topk), icls_topk) ) topk_scores, cls_topk, icls_topk = zip(*final_topk) topk_scores = torch.cat(topk_scores, dim=-1) cls_topk = torch.cat(cls_topk, dim=-1) icls_topk = torch.cat(icls_topk, dim=-1) topk_scores, topk_index = topk_scores.topk(topk, dim=-1) batch_index = cls_topk.new_tensor([[i] * topk for i in range(batch_size)]) cls_topk = cls_topk[batch_index, topk_index] icls_topk = icls_topk[batch_index, topk_index] return topk_scores, cls_topk.tolist(), icls_topk.tolist()
Example 11
Project: hgraph2graph Author: wengong-jin File: hgnn.py License: MIT License | 6 votes |
def forward(self, x_graphs, x_tensors, y_graphs, y_tensors, y_orders, beta): x_tensors = make_cuda(x_tensors) y_tensors = make_cuda(y_tensors) x_root_vecs, x_tree_vecs, x_graph_vecs = self.encode(x_tensors) _, y_tree_vecs, y_graph_vecs = self.encode(y_tensors) diff_tree_vecs = y_tree_vecs.sum(dim=1) - x_tree_vecs.sum(dim=1) diff_graph_vecs = y_graph_vecs.sum(dim=1) - x_graph_vecs.sum(dim=1) diff_tree_vecs, tree_kl = self.rsample(diff_tree_vecs, self.T_mean, self.T_var) diff_graph_vecs, graph_kl = self.rsample(diff_graph_vecs, self.G_mean, self.G_var) kl_div = tree_kl + graph_kl diff_tree_vecs = diff_tree_vecs.unsqueeze(1).expand(-1, x_tree_vecs.size(1), -1) diff_graph_vecs = diff_graph_vecs.unsqueeze(1).expand(-1, x_graph_vecs.size(1), -1) x_tree_vecs = self.W_tree( torch.cat([x_tree_vecs, diff_tree_vecs], dim=-1) ) x_graph_vecs = self.W_graph( torch.cat([x_graph_vecs, diff_graph_vecs], dim=-1) ) loss, wacc, iacc, tacc, sacc = self.decoder((x_root_vecs, x_tree_vecs, x_graph_vecs), y_graphs, y_tensors, y_orders) return loss + beta * kl_div, kl_div.item(), wacc, iacc, tacc, sacc
Example 12
Project: hgraph2graph Author: wengong-jin File: hgnn.py License: MIT License | 6 votes |
def translate(self, tensors, cond, num_decode, enum_root): assert enum_root tensors = make_cuda(tensors) root_vecs, tree_vecs, graph_vecs = self.encode(tensors) cond = cond.view(1,1,-1) tree_cond = cond.expand(num_decode, tree_vecs.size(1), -1) graph_cond = cond.expand(num_decode, graph_vecs.size(1), -1) if enum_root: repeat = num_decode // len(root_vecs) modulo = num_decode % len(root_vecs) root_vecs = torch.cat([root_vecs] * repeat + [root_vecs[:modulo]], dim=0) tree_vecs = torch.cat([tree_vecs] * repeat + [tree_vecs[:modulo]], dim=0) graph_vecs = torch.cat([graph_vecs] * repeat + [graph_vecs[:modulo]], dim=0) z_tree = torch.randn(num_decode, 1, self.latent_size).expand(-1, tree_vecs.size(1), -1).cuda() z_graph = torch.randn(num_decode, 1, self.latent_size).expand(-1, graph_vecs.size(1), -1).cuda() z_tree_vecs = self.W_tree( torch.cat([tree_vecs, z_tree, tree_cond], dim=-1) ) z_graph_vecs = self.W_graph( torch.cat([graph_vecs, z_graph, graph_cond], dim=-1) ) return self.decoder.decode( (root_vecs, z_tree_vecs, z_graph_vecs) )
Example 13
Project: hgraph2graph Author: wengong-jin File: decoder.py License: MIT License | 6 votes |
def init_decoder_state(self, tree_batch, tree_tensors, src_root_vecs): batch_size = len(src_root_vecs) num_mess = len(tree_tensors[1]) agraph = tree_tensors[2].clone() bgraph = tree_tensors[3].clone() for i,tup in enumerate(tree_tensors[-1]): root = tup[0] assert agraph[root,-1].item() == 0 agraph[root,-1] = num_mess + i for v in tree_batch.successors(root): mess_idx = tree_batch[root][v]['mess_idx'] assert bgraph[mess_idx,-1].item() == 0 bgraph[mess_idx,-1] = num_mess + i new_tree_tensors = tree_tensors[:2] + [agraph, bgraph] + tree_tensors[4:] htree = HTuple() htree.mess = self.rnn_cell.get_init_state(tree_tensors[1], src_root_vecs) htree.emask = torch.cat( [bgraph.new_zeros(num_mess), bgraph.new_ones(batch_size)], dim=0 ) return htree, new_tree_tensors
Example 14
Project: hgraph2graph Author: wengong-jin File: encoder.py License: MIT License | 6 votes |
def embed_sub_tree(self, tree_tensors, hinput, subtree, is_inter_layer): subnode, submess = subtree num_nodes = tree_tensors[0].size(0) fnode, fmess, agraph, bgraph, cgraph, _ = self.get_sub_tensor(tree_tensors, subtree) if is_inter_layer: finput = self.E_i(fnode[:, 1]) hinput = index_select_ND(hinput, 0, cgraph).sum(dim=1) hnode = self.W_i( torch.cat([finput, hinput], dim=-1) ) else: finput = self.E_c(fnode[:, 0]) hinput = hinput.index_select(0, subnode) hnode = self.W_c( torch.cat([finput, hinput], dim=-1) ) if len(submess) == 0: hmess = fmess else: node_buf = torch.zeros(num_nodes, self.hidden_size, device=fmess.device) node_buf = index_scatter(hnode, node_buf, subnode) hmess = node_buf.index_select(index=fmess[:, 0], dim=0) pos_vecs = self.E_pos.index_select(0, fmess[:, 2]) hmess = torch.cat( [hmess, pos_vecs], dim=-1 ) return hnode, hmess, agraph, bgraph
Example 15
Project: hgraph2graph Author: wengong-jin File: nnutils.py License: MIT License | 6 votes |
def hier_topk(cls_scores, icls_scores, vocab, topk): batch_size = len(cls_scores) cls_scores = F.log_softmax(cls_scores, dim=-1) cls_scores_topk, cls_topk = cls_scores.topk(topk, dim=-1) final_topk = [] for i in range(topk): clab = cls_topk[:, i] mask = vocab.get_mask(clab) masked_icls_scores = F.log_softmax(icls_scores + mask, dim=-1) icls_scores_topk, icls_topk = masked_icls_scores.topk(topk, dim=-1) topk_scores = cls_scores_topk[:, i].unsqueeze(-1) + icls_scores_topk final_topk.append( (topk_scores, clab.unsqueeze(-1).expand(-1, topk), icls_topk) ) topk_scores, cls_topk, icls_topk = zip(*final_topk) topk_scores = torch.cat(topk_scores, dim=-1) cls_topk = torch.cat(cls_topk, dim=-1) icls_topk = torch.cat(icls_topk, dim=-1) topk_scores, topk_index = topk_scores.topk(topk, dim=-1) batch_index = cls_topk.new_tensor([[i] * topk for i in range(batch_size)]) cls_topk = cls_topk[batch_index, topk_index] icls_topk = icls_topk[batch_index, topk_index] return topk_scores, cls_topk.tolist(), icls_topk.tolist()
Example 16
Project: hgraph2graph Author: wengong-jin File: hgnn.py License: MIT License | 6 votes |
def translate(self, tensors, num_decode, enum_root, greedy=True): tensors = make_cuda(tensors) root_vecs, tree_vecs, graph_vecs = self.encode(tensors) all_smiles = [] if enum_root: repeat = num_decode // len(root_vecs) modulo = num_decode % len(root_vecs) root_vecs = torch.cat([root_vecs] * repeat + [root_vecs[:modulo]], dim=0) tree_vecs = torch.cat([tree_vecs] * repeat + [tree_vecs[:modulo]], dim=0) graph_vecs = torch.cat([graph_vecs] * repeat + [graph_vecs[:modulo]], dim=0) batch_size = len(root_vecs) z_tree = torch.randn(batch_size, 1, self.latent_size).expand(-1, tree_vecs.size(1), -1).cuda() z_graph = torch.randn(batch_size, 1, self.latent_size).expand(-1, graph_vecs.size(1), -1).cuda() z_tree_vecs = self.W_tree( torch.cat([tree_vecs, z_tree], dim=-1) ) z_graph_vecs = self.W_graph( torch.cat([graph_vecs, z_graph], dim=-1) ) return self.decoder.decode( (root_vecs, z_tree_vecs, z_graph_vecs), greedy=greedy)
Example 17
Project: hgraph2graph Author: wengong-jin File: hgnn.py License: MIT License | 6 votes |
def forward(self, x_graphs, x_tensors, y_graphs, y_tensors, y_orders, beta): x_tensors = make_cuda(x_tensors) y_tensors = make_cuda(y_tensors) x_root_vecs, x_tree_vecs, x_graph_vecs = self.encode(x_tensors) _, y_tree_vecs, y_graph_vecs = self.encode(y_tensors) diff_tree_vecs = y_tree_vecs.sum(dim=1) - x_tree_vecs.sum(dim=1) diff_graph_vecs = y_graph_vecs.sum(dim=1) - x_graph_vecs.sum(dim=1) diff_tree_vecs, tree_kl = self.rsample(diff_tree_vecs, self.T_mean, self.T_var) diff_graph_vecs, graph_kl = self.rsample(diff_graph_vecs, self.G_mean, self.G_var) kl_div = tree_kl + graph_kl diff_tree_vecs = diff_tree_vecs.unsqueeze(1).expand(-1, x_tree_vecs.size(1), -1) diff_graph_vecs = diff_graph_vecs.unsqueeze(1).expand(-1, x_graph_vecs.size(1), -1) x_tree_vecs = self.W_tree( torch.cat([x_tree_vecs, diff_tree_vecs], dim=-1) ) x_graph_vecs = self.W_graph( torch.cat([x_graph_vecs, diff_graph_vecs], dim=-1) ) loss, wacc, iacc, tacc, sacc = self.decoder((x_root_vecs, x_tree_vecs, x_graph_vecs), y_graphs, y_tensors, y_orders) return loss + beta * kl_div, kl_div.item(), wacc, iacc, tacc, sacc
Example 18
Project: hgraph2graph Author: wengong-jin File: decoder.py License: MIT License | 6 votes |
def init_decoder_state(self, tree_batch, tree_tensors, src_root_vecs): batch_size = len(src_root_vecs) num_mess = len(tree_tensors[1]) agraph = tree_tensors[2].clone() bgraph = tree_tensors[3].clone() for i,tup in enumerate(tree_tensors[-1]): root = tup[0] assert agraph[root,-1].item() == 0 agraph[root,-1] = num_mess + i for v in tree_batch.successors(root): mess_idx = tree_batch[root][v]['mess_idx'] assert bgraph[mess_idx,-1].item() == 0 bgraph[mess_idx,-1] = num_mess + i new_tree_tensors = tree_tensors[:2] + [agraph, bgraph] + tree_tensors[4:] htree = HTuple() htree.mess = self.rnn_cell.get_init_state(tree_tensors[1], src_root_vecs) htree.emask = torch.cat( [bgraph.new_zeros(num_mess), bgraph.new_ones(batch_size)], dim=0 ) return htree, new_tree_tensors
Example 19
Project: hgraph2graph Author: wengong-jin File: encoder.py License: MIT License | 6 votes |
def embed_sub_tree(self, tree_tensors, hinput, subtree, is_inter_layer): subnode, submess = subtree num_nodes = tree_tensors[0].size(0) fnode, fmess, agraph, bgraph, cgraph, _ = self.get_sub_tensor(tree_tensors, subtree) if is_inter_layer: finput = self.E_i(fnode[:, 1]) hinput = index_select_ND(hinput, 0, cgraph).sum(dim=1) hnode = self.W_i( torch.cat([finput, hinput], dim=-1) ) else: finput = self.E_c(fnode[:, 0]) hinput = hinput.index_select(0, subnode) hnode = self.W_c( torch.cat([finput, hinput], dim=-1) ) if len(submess) == 0: hmess = fmess else: node_buf = torch.zeros(num_nodes, self.hidden_size, device=fmess.device) node_buf = index_scatter(hnode, node_buf, subnode) hmess = node_buf.index_select(index=fmess[:, 0], dim=0) pos_vecs = self.E_pos.index_select(0, fmess[:, 2]) hmess = torch.cat( [hmess, pos_vecs], dim=-1 ) return hnode, hmess, agraph, bgraph
Example 20
Project: mmdetection Author: open-mmlab File: hrfpn.py License: Apache License 2.0 | 6 votes |
def forward(self, inputs): """Forward function.""" assert len(inputs) == self.num_ins outs = [inputs[0]] for i in range(1, self.num_ins): outs.append( F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) out = torch.cat(outs, dim=1) if out.requires_grad and self.with_cp: out = checkpoint(self.reduction_conv, out) else: out = self.reduction_conv(out) outs = [out] for i in range(1, self.num_outs): outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) outputs = [] for i in range(self.num_outs): if outs[i].requires_grad and self.with_cp: tmp_out = checkpoint(self.fpn_convs[i], outs[i]) else: tmp_out = self.fpn_convs[i](outs[i]) outputs.append(tmp_out) return tuple(outputs)
Example 21
Project: mmdetection Author: open-mmlab File: fovea_head.py License: Apache License 2.0 | 6 votes |
def get_targets(self, gt_bbox_list, gt_label_list, featmap_sizes, points): label_list, bbox_target_list = multi_apply( self._get_target_single, gt_bbox_list, gt_label_list, featmap_size_list=featmap_sizes, point_list=points) flatten_labels = [ torch.cat([ labels_level_img.flatten() for labels_level_img in labels_level ]) for labels_level in zip(*label_list) ] flatten_bbox_targets = [ torch.cat([ bbox_targets_level_img.reshape(-1, 4) for bbox_targets_level_img in bbox_targets_level ]) for bbox_targets_level in zip(*bbox_target_list) ] flatten_labels = torch.cat(flatten_labels) flatten_bbox_targets = torch.cat(flatten_bbox_targets) return flatten_labels, flatten_bbox_targets
Example 22
Project: mmdetection Author: open-mmlab File: reppoints_head.py License: Apache License 2.0 | 6 votes |
def centers_to_bboxes(self, point_list): """Get bboxes according to center points. Only used in :class:`MaxIoUAssigner`. """ bbox_list = [] for i_img, point in enumerate(point_list): bbox = [] for i_lvl in range(len(self.point_strides)): scale = self.point_base_scale * self.point_strides[i_lvl] * 0.5 bbox_shift = torch.Tensor([-scale, -scale, scale, scale]).view(1, 4).type_as(point[0]) bbox_center = torch.cat( [point[i_lvl][:, :2], point[i_lvl][:, :2]], dim=1) bbox.append(bbox_center + bbox_shift) bbox_list.append(bbox) return bbox_list
Example 23
Project: MomentumContrast.pytorch Author: peisuke File: train.py License: MIT License | 5 votes |
def queue_data(data, k): return torch.cat([data, k], dim=0)
Example 24
Project: MomentumContrast.pytorch Author: peisuke File: train.py License: MIT License | 5 votes |
def train(model_q, model_k, device, train_loader, queue, optimizer, epoch, temp=0.07): model_q.train() total_loss = 0 for batch_idx, (data, target) in enumerate(train_loader): x_q = data[0] x_k = data[1] x_q, x_k = x_q.to(device), x_k.to(device) q = model_q(x_q) k = model_k(x_k) k = k.detach() N = data[0].shape[0] K = queue.shape[0] l_pos = torch.bmm(q.view(N,1,-1), k.view(N,-1,1)) l_neg = torch.mm(q.view(N,-1), queue.T.view(-1,K)) logits = torch.cat([l_pos.view(N, 1), l_neg], dim=1) labels = torch.zeros(N, dtype=torch.long) labels = labels.to(device) cross_entropy_loss = nn.CrossEntropyLoss() loss = cross_entropy_loss(logits/temp, labels) optimizer.zero_grad() loss.backward() optimizer.step() total_loss += loss.item() momentum_update(model_q, model_k) queue = queue_data(queue, k) queue = dequeue_data(queue) total_loss /= len(train_loader.dataset) print('Train Epoch: {} \tLoss: {:.6f}'.format(epoch, total_loss))
Example 25
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: bbox_transform.py License: MIT License | 5 votes |
def bbox_transform_inv(boxes, deltas): # Input should be both tensor or both Variable and on the same device if len(boxes) == 0: return deltas.detach() * 0 widths = boxes[:, 2] - boxes[:, 0] + 1.0 heights = boxes[:, 3] - boxes[:, 1] + 1.0 ctr_x = boxes[:, 0] + 0.5 * widths ctr_y = boxes[:, 1] + 0.5 * heights dx = deltas[:, 0::4] dy = deltas[:, 1::4] dw = deltas[:, 2::4] dh = deltas[:, 3::4] pred_ctr_x = dx * widths.unsqueeze(1) + ctr_x.unsqueeze(1) pred_ctr_y = dy * heights.unsqueeze(1) + ctr_y.unsqueeze(1) pred_w = torch.exp(dw) * widths.unsqueeze(1) pred_h = torch.exp(dh) * heights.unsqueeze(1) pred_boxes = torch.cat(\ [_.unsqueeze(2) for _ in [pred_ctr_x - 0.5 * pred_w,\ pred_ctr_y - 0.5 * pred_h,\ pred_ctr_x + 0.5 * pred_w,\ pred_ctr_y + 0.5 * pred_h]], 2).view(len(boxes), -1) return pred_boxes
Example 26
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: proposal_target_layer.py License: MIT License | 5 votes |
def proposal_target_layer (rpn_rois, rpn_scores, gt_boxes, _num_classes, gt_weights): """ Assign object detection proposals to ground-truth targets. Produces proposal classification labels and bounding-box regression targets. """ # Proposal ROIs (0, x1, y1, x2, y2) coming from RPN # (i.e., rpn.proposal_layer.ProposalLayer), or any other source all_rois = rpn_rois all_scores = rpn_scores # Include ground-truth boxes in the set of candidate rois if cfg.TRAIN.USE_GT: zeros = rpn_rois.data.new(gt_boxes.shape[0], 1) all_rois = torch.cat( (all_rois, torch.cat((zeros, gt_boxes[:, :-1]), 1)) , 0) # not sure if it a wise appending, but anyway i am not using it all_scores = torch.cat((all_scores, zeros), 0) num_images = 1 rois_per_image = cfg.TRAIN.BATCH_SIZE / num_images fg_rois_per_image = int(round(cfg.TRAIN.FG_FRACTION * rois_per_image)) # Sample rois with classification labels and bounding box regression # targets labels, rois, roi_scores, bbox_targets, bbox_inside_weights, loss_weights = _sample_rois( all_rois, all_scores, gt_boxes, gt_weights, fg_rois_per_image, rois_per_image, _num_classes) rois = rois.view(-1, 5) roi_scores = roi_scores.view(-1) labels = labels.view(-1, 1) bbox_targets = bbox_targets.view(-1, _num_classes * 4) bbox_inside_weights = bbox_inside_weights.view(-1, _num_classes * 4) bbox_outside_weights = (bbox_inside_weights > 0).float() #print(bbox_outside_weights) bbox_outside_weights = bbox_outside_weights * loss_weights.view(-1,1) #print(bbox_outside_weights) return rois, roi_scores, labels, Variable(bbox_targets), Variable(bbox_inside_weights), Variable(bbox_outside_weights), Variable(loss_weights)
Example 27
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: proposal_target_layer.py License: MIT License | 5 votes |
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" # Inputs are tensor assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - targets.new(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / targets.new(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return torch.cat( [labels.unsqueeze(1), targets], 1)
Example 28
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: proposal_top_layer.py License: MIT License | 5 votes |
def proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, im_info, _feat_stride, anchors, num_anchors): """A layer that just selects the top region proposals without using non-maximal suppression, For details please see the technical report """ rpn_top_n = cfg.TEST.RPN_TOP_N scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.view(-1, 4) scores = scores.contiguous().view(-1, 1) length = scores.size(0) if length < rpn_top_n: # Random selection, maybe unnecessary and loses good proposals # But such case rarely happens top_inds = torch.from_numpy(npr.choice(length, size=rpn_top_n, replace=True)).long().cuda() else: top_inds = scores.sort(0, descending=True)[1] top_inds = top_inds[:rpn_top_n] top_inds = top_inds.view(rpn_top_n) # Do the selection here anchors = anchors[top_inds, :].contiguous() rpn_bbox_pred = rpn_bbox_pred[top_inds, :].contiguous() scores = scores[top_inds].contiguous() # Convert anchors into proposals via bbox transformations proposals = bbox_transform_inv(anchors, rpn_bbox_pred) # Clip predicted boxes to image proposals = clip_boxes(proposals, im_info[:2]) # Output rois blob # Our RPN implementation only supports a single input image, so all # batch inds are 0 batch_inds = proposals.data.new(proposals.size(0), 1).zero_() blob = torch.cat([batch_inds, proposals], 1) return blob, scores
Example 29
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: generate_pseudo_gtbox.py License: MIT License | 5 votes |
def generate_pseudo_gtbox(boxes, cls_prob, im_labels): """Get proposals from fuse_matrix inputs are all variables""" pre_nms_topN = 50 nms_Thresh = 0.1 num_images, num_classes = im_labels.size() boxes = boxes[:,1:] assert num_images == 1, 'batch size shoud be equal to 1' im_labels_tmp = im_labels[0, :] labelList = im_labels_tmp.data.nonzero().view(-1) gt_boxes = [] gt_classes = [] gt_scores = [] for i in labelList: scores, order = cls_prob[:,i].contiguous().view(-1).sort(descending=True) if pre_nms_topN > 0: order = order[:pre_nms_topN] scores = scores[:pre_nms_topN].view(-1, 1) proposals = boxes[order.data, :] keep = nms(torch.cat((proposals, scores), 1).data, nms_Thresh) proposals = proposals[keep, :] scores = scores[keep,] gt_boxes.append(proposals) gt_classes.append(torch.ones(keep.size(0),1)*(i+1)) # return idx=class+1 to include the background gt_scores.append(scores.view(-1,1)) gt_boxes = torch.cat(gt_boxes) gt_classes = torch.cat(gt_classes) gt_scores = torch.cat(gt_scores) proposals = {'gt_boxes' : gt_boxes, 'gt_classes': gt_classes, 'gt_scores': gt_scores} # print(gt_boxes.size()) # print(gt_classes.size()) # print(type(gt_boxes)) # print(type(gt_classes)) return torch.cat([gt_boxes,gt_classes],1),proposals
Example 30
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: choose_pseudo_gt.py License: MIT License | 5 votes |
def choose_pseudo_gt(boxes, cls_prob, im_labels): """Get proposals with highest score. inputs are all variables""" num_images, num_classes = im_labels.size() boxes = boxes[:,1:] assert num_images == 1, 'batch size shoud be equal to 1' im_labels_tmp = im_labels[0, :] gt_boxes = [] gt_classes = [] gt_scores = [] for i in range(num_classes): if im_labels_tmp[i].data.cpu().numpy() == 1: max_value,max_index = cls_prob[:, i].max(0) gt_boxes.append(boxes[max_index]) gt_classes.append(torch.ones(1,1)*(i+1)) # return idx=class+1 to include the background gt_scores.append(max_value.view(-1,1)) gt_boxes = torch.cat(gt_boxes) gt_classes = torch.cat(gt_classes) gt_scores = torch.cat(gt_scores) proposals = {'gt_boxes' : gt_boxes, 'gt_classes': gt_classes, 'gt_scores': gt_scores} return torch.cat([gt_boxes,gt_classes],1), proposals