Python torch.cat() Examples
The following are 30
code examples of torch.cat().
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
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: hrfpn.py From mmdetection with Apache License 2.0 | 7 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 #2
Source File: reppoints_head.py From mmdetection with 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 #3
Source File: model2.py From controllable-text-attribute-transfer with 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 #4
Source File: model2.py From controllable-text-attribute-transfer with 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 #5
Source File: proposal_target_layer.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with 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 #6
Source File: model2.py From controllable-text-attribute-transfer with 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
Source File: model2.py From controllable-text-attribute-transfer with 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 #8
Source File: model2.py From controllable-text-attribute-transfer with 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 #9
Source File: DDPAE_utils.py From DDPAE-video-prediction with 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 #10
Source File: DDPAE.py From DDPAE-video-prediction with 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 #11
Source File: DDPAE.py From DDPAE-video-prediction with 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 #12
Source File: nnutils.py From hgraph2graph with 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 #13
Source File: decoder.py From hgraph2graph with 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
Source File: hgnn.py From hgraph2graph with 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 #15
Source File: fovea_head.py From mmdetection with 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 #16
Source File: encoder.py From hgraph2graph with 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 #17
Source File: decoder.py From hgraph2graph with 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 #18
Source File: hgnn.py From hgraph2graph with 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 #19
Source File: hgnn.py From hgraph2graph with 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 #20
Source File: nnutils.py From hgraph2graph with 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 #21
Source File: encoder.py From hgraph2graph with 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 #22
Source File: hgnn.py From hgraph2graph with 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 #23
Source File: hgnn.py From hgraph2graph with MIT License | 5 votes |
def forward(self, x_graphs, x_tensors, y_graphs, y_tensors, y_orders, cond, beta): x_tensors = make_cuda(x_tensors) y_tensors = make_cuda(y_tensors) cond = torch.tensor(cond).float().cuda() 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 = self.U_tree( torch.cat([diff_tree_vecs, cond], dim=-1) ) #combine condition for posterior diff_graph_vecs = self.U_graph( torch.cat([diff_graph_vecs, cond], dim=-1) ) #combine condition for posterior 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 = torch.cat([diff_tree_vecs, cond], dim=-1) #combine condition for posterior diff_graph_vecs = torch.cat([diff_graph_vecs, cond], dim=-1) #combine condition for posterior 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 #24
Source File: proposal_top_layer.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with 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 #25
Source File: rnn.py From hgraph2graph with MIT License | 5 votes |
def get_init_state(self, fmess, init_state=None): h = torch.zeros(len(fmess), self.hidden_size, device=fmess.device) c = torch.zeros(len(fmess), self.hidden_size, device=fmess.device) if init_state is not None: h = torch.cat( (h, init_state), dim=0) c = torch.cat( (c, torch.zeros_like(init_state)), dim=0) return h,c
Example #26
Source File: generate_pseudo_gtbox.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with 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 #27
Source File: hgnn.py From hgraph2graph with MIT License | 5 votes |
def translate(self, tensors, num_decode, enum_root, greedy): tensors = make_cuda(tensors) root_vecs, tree_vecs, graph_vecs = self.encode(tensors) 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) else: root_vecs = root_vecs.unsqueeze(0).expand(num_decode, -1) tree_vecs = tree_vecs.unsqueeze(0).expand(num_decode, -1, -1) graph_vecs = graph_vecs.unsqueeze(0).expand(num_decode, -1, -1) return self.decoder.decode( (root_vecs, tree_vecs, graph_vecs), greedy=greedy)
Example #28
Source File: encoder.py From hgraph2graph with MIT License | 5 votes |
def embed_root(self, hmess, tree_tensors, roots): roots = tree_tensors[2].new_tensor(roots) fnode = tree_tensors[0].index_select(0, roots) agraph = tree_tensors[2].index_select(0, roots) nei_message = index_select_ND(hmess, 0, agraph) nei_message = nei_message.sum(dim=1) node_hiddens = torch.cat([fnode, nei_message], dim=1) return self.W_root(node_hiddens)
Example #29
Source File: encoder.py From hgraph2graph with MIT License | 5 votes |
def forward(self, tensors, h, num_nodes, subset): fnode, fmess, agraph, bgraph = tensors subnode, submess = subset if len(submess) > 0: h = self.rnn.sparse_forward(h, fmess, submess, bgraph) nei_message = index_select_ND(self.rnn.get_hidden_state(h), 0, agraph) nei_message = nei_message.sum(dim=1) node_hiddens = torch.cat([fnode, nei_message], dim=1) node_hiddens = self.W_o(node_hiddens) node_buf = torch.zeros(num_nodes, self.hidden_size, device=fmess.device) node_hiddens = index_scatter(node_hiddens, node_buf, subnode) return node_hiddens, h
Example #30
Source File: model2.py From controllable-text-attribute-transfer with Apache License 2.0 | 5 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