How to use the torch.cat function in torch

To help you get started, we’ve selected a few torch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github abhi4ssj / few-shot-segmentation / other_experiments / more / few_shot_segmentor_model9.py View on Github external
e_w2 = self.sigmoid(self.squeeze_conv_e2(e2))
        e3, out3, ind3 = self.encode3(e2)
        e3 = torch.cat((e3, F.avg_pool2d(mask, (8, 8), stride=8)), dim=1)
        e_w3 = self.sigmoid(self.squeeze_conv_e3(e3))

        bn = self.bottleneck(e3)
        bn_w = self.sigmoid(self.squeeze_conv_bn(bn))

        d3 = self.decode1(bn, out3, ind3)
        d3_1 = torch.cat((d3, F.avg_pool2d(mask, (4, 4), stride=4)), dim=1)
        d_w3 = self.sigmoid(self.squeeze_conv_d3(d3_1))
        d2 = self.decode2(d3, out2, ind2)
        d2_1 = torch.cat((d2, F.avg_pool2d(mask, (2, 2), stride=2)), dim=1)
        d_w2 = self.sigmoid(self.squeeze_conv_d2(d2_1))
        d1 = self.decode3(d2, out1, ind1)
        d1_1 = torch.cat((d1, mask), dim=1)
        d_w1 = self.sigmoid(self.squeeze_conv_d1(d1_1))
        logit = self.classifier.forward(d1)
        cls_w = self.sigmoid(logit)

        return e_w1, e_w2, e_w3, bn_w, d_w3, d_w2, d_w1, cls_w
github visionml / pytracking / pytracking / tracker / atom / atom.py View on Github external
# Extract features from the relevant scale
        iou_features = self.get_iou_features()
        iou_features = TensorList([x[scale_ind:scale_ind+1,...] for x in iou_features])

        init_boxes = init_box.view(1,4).clone()
        if self.params.num_init_random_boxes > 0:
            # Get random initial boxes
            square_box_sz = init_box[2:].prod().sqrt()
            rand_factor = square_box_sz * torch.cat([self.params.box_jitter_pos * torch.ones(2), self.params.box_jitter_sz * torch.ones(2)])
            minimal_edge_size = init_box[2:].min()/3
            rand_bb = (torch.rand(self.params.num_init_random_boxes, 4) - 0.5) * rand_factor
            new_sz = (init_box[2:] + rand_bb[:,2:]).clamp(minimal_edge_size)
            new_center = (init_box[:2] + init_box[2:]/2) + rand_bb[:,:2]
            init_boxes = torch.cat([new_center - new_sz/2, new_sz], 1)
            init_boxes = torch.cat([init_box.view(1,4), init_boxes])

        # Refine boxes by maximizing iou
        output_boxes, output_iou = self.optimize_boxes(iou_features, init_boxes)

        # Remove weird boxes with extreme aspect ratios
        output_boxes[:, 2:].clamp_(1)
        aspect_ratio = output_boxes[:,2] / output_boxes[:,3]
        keep_ind = (aspect_ratio < self.params.maximal_aspect_ratio) * (aspect_ratio > 1/self.params.maximal_aspect_ratio)
        output_boxes = output_boxes[keep_ind,:]
        output_iou = output_iou[keep_ind]

        # If no box found
        if output_boxes.shape[0] == 0:
            return

        # Take average of top k boxes
github BogiHsu / Voice-Conversion / model.py View on Github external
def forward(self, x):
        outs = []
        for l in self.conv1s:
            out = pad_layer(x, l)
            outs.append(out)
        out = torch.cat(outs + [x], dim=1)
        out = F.leaky_relu(out, negative_slope=self.ns)
        out = self.conv_block(out, [self.conv2], [self.ins_norm1, self.drop1], res=False)
        out = self.conv_block(out, [self.conv3, self.conv4], [self.ins_norm2, self.drop2])
        out = self.conv_block(out, [self.conv5, self.conv6], [self.ins_norm3, self.drop3])
        out = self.conv_block(out, [self.conv7, self.conv8], [self.ins_norm4, self.drop4])
        # dense layer
        out = self.dense_block(out, [self.dense1, self.dense2], [self.ins_norm5, self.drop5], res=True)
        out = self.dense_block(out, [self.dense3, self.dense4], [self.ins_norm6, self.drop6], res=True)
        out_rnn = RNN(out, self.RNN)
        out = torch.cat([out, out_rnn], dim=1)
        out = linear(out, self.linear)
        out = F.leaky_relu(out, negative_slope=self.ns)
        return out
github StanfordVL / GibsonEnvV2 / dev / filler_panorama_pc_full_res_avg_comp2_perceptual.py View on Github external
source_depth = torch.cat([source_depth, mask], 1)
                img.data.copy_(source)
                maskv.data.copy_(source_depth)
                img_original.data.copy_(target)
                imgc, maskvc, img_originalc = crop(img, maskv, img_original)
                comp.eval()
                recon = comp(imgc, maskvc)
                comp.train()
                
                if opt.cascade:
                    comp2.eval()
                    recon2 = comp2(torch.cat([recon, imgc[:,3:]], 1), maskvc)
                    comp2.train()
                    visual = torch.cat([imgc.data[:,:3,:,:], recon.data, recon2.data, img_originalc.data], 3)
                else:
                    visual = torch.cat([imgc.data[:,:3,:,:], recon.data, img_originalc.data], 3)
                
                
                visual = vutils.make_grid(visual, normalize=True)
                writer.add_image('image', visual, step)
                vutils.save_image(visual, '%s/compare%d_%d.png' % (opt.outf, epoch, i), nrow=1)

            if i%10 == 0:
                writer.add_scalar('MSEloss', loss.data[0], step)
                writer.add_scalar('G_loss', errG_data, step)
                writer.add_scalar('D_loss', errD_data, step)

            if i%10000 == 0:
                torch.save(comp.state_dict(), '%s/compG_epoch%d_%d.pth' % (opt.outf, epoch, i))
                torch.save(dis.state_dict(), '%s/compD_epoch%d_%d.pth' % (opt.outf, epoch, i))
github hirofumi0810 / neural_sp / models / pytorch / attention / nested_attention_seq2seq.py View on Github external
torch.zeros_like(att_weights_step_sub))
                # NOTE: remove 

                # Concatenate in time-dimension
                logits_i = torch.cat(logits_i, dim=1)
                logits_sub_i = torch.cat(logits_sub_i, dim=1)
                att_weights_i = torch.stack(att_weights_i, dim=1)
                att_weights_sub_i = torch.stack(att_weights_sub_i, dim=1)

                logits.append(logits_i)
                logits_sub.append(logits_sub_i)
                # att_weights.append(att_weights_i)
                # att_weights_sub.append(att_weights_sub_i)

            # Concatenate in batch-dimension
            logits = torch.cat(logits, dim=0)
            logits_sub = torch.cat(logits_sub, dim=0)
            # att_weights = torch.stack(att_weights, dim=0)
            # att_weights_sub = torch.stack(att_weights_sub, dim=0)
            att_weights = None
            att_weights_sub = None

        return logits, logits_sub, att_weights, att_weights_sub
github Linzaer / Ultra-Light-Fast-Generic-Face-Detector-1MB / vision / ssd / predictor.py View on Github external
scores = scores[0]
        if not prob_threshold:
            prob_threshold = self.filter_threshold
        # this version of nms is slower on GPU, so we move data to CPU.
        boxes = boxes.to(cpu_device)
        scores = scores.to(cpu_device)
        picked_box_probs = []
        picked_labels = []
        for class_index in range(1, scores.size(1)):
            probs = scores[:, class_index]
            mask = probs > prob_threshold
            probs = probs[mask]
            if probs.size(0) == 0:
                continue
            subset_boxes = boxes[mask, :]
            box_probs = torch.cat([subset_boxes, probs.reshape(-1, 1)], dim=1)
            box_probs = box_utils.nms(box_probs, self.nms_method,
                                      score_threshold=prob_threshold,
                                      iou_threshold=self.iou_threshold,
                                      sigma=self.sigma,
                                      top_k=top_k,
                                      candidate_size=self.candidate_size)
            picked_box_probs.append(box_probs)
            picked_labels.extend([class_index] * box_probs.size(0))
        if not picked_box_probs:
            return torch.tensor([]), torch.tensor([]), torch.tensor([])
        picked_box_probs = torch.cat(picked_box_probs)
        picked_box_probs[:, 0] *= width
        picked_box_probs[:, 1] *= height
        picked_box_probs[:, 2] *= width
        picked_box_probs[:, 3] *= height
        return picked_box_probs[:, :4], torch.tensor(picked_labels), picked_box_probs[:, 4]
github GraphNAS / GraphNAS / variants / micro_graphnas / micro_gnn.py View on Github external
def forward(self, s0, s1, edge_index, drop_prob):
        s0 = self.preprocess0(s0)
        s1 = self.preprocess1(s1)

        states = [s0, s1]
        for i in range(self._steps):
            h1 = states[self._indices[i]]
            op1 = self._gnn[i]
            s = op1(h1, edge_index)
            # s = F.dropout(s, p=drop_prob, training=self.training)
            states += [s]
        if self._concat == "concat":
            return self._act(torch.cat(states[2:], dim=1))
        else:
            tmp = states[2]
            for i in range(2,len(states)):
                if self._concat == "add":
                    tmp = torch.add(tmp, states[i])
                elif self._concat == "product":
                    tmp = torch.mul(tmp, states[i])
            return tmp
github jwyang / graph-rcnn.pytorch / lib / scene_parser / rcnn / modeling / relation_heads / imp / imp.py View on Github external
hx_edge = [x_pred]

        for t in range(self.update_step):
            sub_vert = hx_obj[t][rel_inds[:, 0]]  #
            obj_vert = hx_obj[t][rel_inds[:, 1]]

            '''update object features'''
            message_pred_to_subj = self.subj_node_gate(torch.cat([sub_vert, hx_edge[t]], 1)) * hx_edge[t]  # nrel x d
            message_pred_to_obj = self.obj_node_gate(torch.cat([obj_vert, hx_edge[t]], 1)) * hx_edge[t]    # nrel x d
            node_message = (torch.mm(subj_pred_map, message_pred_to_subj) / (subj_pred_map.sum(1, keepdim=True) + 1e-5) \
                          + torch.mm(obj_pred_map, message_pred_to_obj) / (obj_pred_map.sum(1, keepdim=True) + 1e-5)) / 2.
            hx_obj.append(self.node_gru(node_message, hx_obj[t]))
            # hx_obj.append(F.relu(node_message + hx_obj[t]))

            '''update predicat features'''
            message_subj_to_pred = self.subj_edge_gate(torch.cat([sub_vert, hx_edge[t]], 1)) * sub_vert  # nrel x d
            message_obj_to_pred = self.obj_edge_gate(torch.cat([obj_vert, hx_edge[t]], 1)) * obj_vert    # nrel x d
            edge_message = (message_subj_to_pred + message_obj_to_pred) / 2.
            hx_edge.append(self.edge_gru(edge_message, hx_edge[t]))
            # hx_edge.append(F.relu(edge_message + hx_edge[t]))

        '''compute results and losses'''
        # final classifier that converts the features into predictions
        # for object prediction, we do not do bbox regression again
        obj_class_logits = self.obj_predictor(hx_obj[-1].unsqueeze(2).unsqueeze(3))
        pred_class_logits = self.pred_predictor(hx_edge[-1].unsqueeze(2).unsqueeze(3))

        if obj_class_logits is None:
            logits = torch.cat([proposal.get_field("logits") for proposal in proposals], 0)
            obj_class_labels = logits[:, 1:].max(1)[1] + 1
        else:
            obj_class_labels = obj_class_logits[:, 1:].max(1)[1] + 1
github zuoxingdong / VIN_PyTorch_Visdom / VIN.py View on Github external
if record_images: # TODO: Currently only support single input image
            # Save grid image in Numpy array
            self.grid_image = X.data[0].cpu().numpy() # cpu() works both GPU/CPU mode
            # Save reward image in Numpy array
            self.reward_image = r.data[0].cpu().numpy() # cpu() works both GPU/CPU mode
        
        # Initialize value map (zero everywhere)
        v = torch.zeros(r.size())
        # Move to GPU if necessary
        v = v.cuda() if X.is_cuda else v
        # Wrap to autograd.Variable
        v = Variable(v)
        
        # K-iterations of Value Iteration module
        for _ in range(args.k):
            rv = torch.cat([r, v], 1) # [batch_size, 2, imsize, imsize]
            q = self.conv_q(rv)
            v, _ = torch.max(q, 1) # torch.max returns (values, indices)
            
            if record_images: 
                # Save single value image in Numpy array for each VI step
                self.value_images.append(v.data[0].cpu().numpy()) # cpu() works both GPU/CPU mode
        
        # Do one last convolution
        rv = torch.cat([r, v], 1) # [batch_size, 2, imsize, imsize]
        q = self.conv_q(rv)
        
        # Attention model
        q_out = attention(q, [S1.long(), S2.long(), args])
        
        # Final Fully Connected layer
        logits = self.fc1(q_out)