Python ipdb.set_trace() Examples

The following are 30 code examples of ipdb.set_trace(). 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 ipdb , or try the search function .
Example #1
Source File: visualize_tfrecords.py    From human_dynamics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def visualize_tfrecords_test(fpaths):
    for fname in fpaths:
        print(fname)
        for serialized_ex in tf.python_io.tf_record_iterator(fname):
            results = read_from_example(serialized_ex)
            images = results['images']
            kps = results['kps']
            for i, (image, kp) in enumerate(zip(images, kps)):
                kp = kp.T
                import matplotlib.pyplot as plt
                plt.ion()
                plt.clf()
                plt.figure(1)
                skel_img = draw_skeleton(image, kp[:2, :], vis=kp[2, :])
                plt.imshow(skel_img)
                plt.title('%d' % i)
                plt.axis('off')
                plt.pause(1e-5)
                if i == 0:
                    ipdb.set_trace()
                if i > config.max_sequence_length:
                    break

            ipdb.set_trace() 
Example #2
Source File: Models.py    From SEASS with MIT License 6 votes vote down vote up
def forward(self, input):
        """
        input: (wrap(srcBatch), wrap(srcBioBatch), lengths), (wrap(tgtBatch), wrap(copySwitchBatch), wrap(copyTgtBatch))
        """
        # ipdb.set_trace()
        src = input[0]
        tgt = input[1][0][:-1]  # exclude last target from inputs
        src_pad_mask = Variable(src[0].data.eq(s2s.Constants.PAD).transpose(0, 1).float(), requires_grad=False,
                                volatile=False)
        enc_hidden, context = self.encoder(src)

        init_att = self.make_init_att(context)
        enc_hidden = self.decIniter(enc_hidden[1]).unsqueeze(0)  # [1] is the last backward hiden

        g_out, dec_hidden, _attn, _attention_vector = self.decoder(tgt, enc_hidden, context, src_pad_mask, init_att)

        return g_out 
Example #3
Source File: kitti_LidarImg_data_generator.py    From Attentional-PointNet with GNU General Public License v3.0 6 votes vote down vote up
def lidar_to_heightmap(points, img_size):
    # pdb.set_trace()
    lidar_data = np.array(points[:, :2])
    height_data = np.array(points[:,2])
    height_data *= 255/2
    height_data[height_data < 0] = 0
    height_data[height_data > 255] = 255
    height_data = np.fabs(height_data)
    height_data = height_data.astype(np.int32)


    lidar_data *= 9.9999
    lidar_data -= (0.5 * img_size, 0.5 * img_size)
    lidar_data = np.fabs(lidar_data)
    lidar_data = lidar_data.astype(np.int32)
    lidar_data = np.reshape(lidar_data, (-1, 2))
    lidar_img = np.zeros((img_size, img_size))
    lidar_img[tuple(lidar_data.T)] = height_data # TODO: sort the point wrt height first lex sort
    return lidar_img 
Example #4
Source File: Models.py    From NQG with GNU General Public License v3.0 6 votes vote down vote up
def forward(self, input):
        """
        (wrap(srcBatch), lengths), \
               (wrap(bioBatch), lengths), ((wrap(x) for x in featBatches), lengths), \
               (wrap(tgtBatch), wrap(copySwitchBatch), wrap(copyTgtBatch)), \
               indices
        """
        # ipdb.set_trace()
        src = input[0]
        tgt = input[3][0][:-1]  # exclude last target from inputs
        src_pad_mask = Variable(src[0].data.eq(s2s.Constants.PAD).transpose(0, 1).float(), requires_grad=False,
                                volatile=False)
        bio = input[1]
        feats = input[2]
        enc_hidden, context = self.encoder(src, bio, feats)

        init_att = self.make_init_att(context)
        enc_hidden = self.decIniter(enc_hidden[1]).unsqueeze(0)  # [1] is the last backward hiden

        g_out, c_out, c_gate_out, dec_hidden, _attn, _attention_vector = self.decoder(tgt, enc_hidden, context,
                                                                                      src_pad_mask, init_att)

        return g_out, c_out, c_gate_out 
Example #5
Source File: visualizer.py    From aerial_mtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def tensor2im(self, img, imtype=np.uint8, convert_value=255.0):
        # ToDo: improve this horrible function
        if img.shape[0] > 3:   # case of focalstack
            img = img[:, :3]
        
        if(type(img) != np.ndarray):
            image_numpy = img.cpu().float().numpy()
        else:
            image_numpy = img
        if img.shape[0] == 3:
            image_numpy = (image_numpy + 1) / 2.0 * convert_value
            # image_numpy = (image_numpy + mean/std) * std * 255.0
            image_numpy = image_numpy.astype(imtype)    # .transpose([2,0,1])
        else:
            # st()
            # image_numpy = image_numpy.astype(imtype)
            image_numpy = (image_numpy - image_numpy.min()) * (255 / self.opt.max_distance)
            # image_numpy = image_numpy - image_numpy.min()
            # image_numpy = (image_numpy / image_numpy.max()) * 255
            # image_numpy = (image_numpy / image_numpy.max()) * 255
            image_numpy = np.repeat(image_numpy, 3, axis=0)
        return image_numpy

    # visuals: dictionary of images to display or save 
Example #6
Source File: kitti_evaluation.py    From Attentional-PointNet with GNU General Public License v3.0 6 votes vote down vote up
def lidar_to_img(points, img_size):
    # pdb.set_trace()
    lidar_data = np.array(points[:, :2])
    lidar_data *= 9.9999
    lidar_data -= (0.5 * img_size, 0.5 * img_size)
    lidar_data = np.fabs(lidar_data)
    lidar_data = lidar_data.astype(np.int32)
    lidar_data = np.reshape(lidar_data, (-1, 2))
    lidar_img = np.zeros((img_size, img_size))
    lidar_img[tuple(lidar_data.T)] = 255
    return torch.tensor(lidar_img).cuda()


# def lidar_to_img(points, img_size):
#     # pdb.set_trace()
#     lidar_data = points[:, :2]
#     lidar_data *= 9.9999
#     lidar_data -= torch.tensor((0.5 * img_size, 0.5 * img_size)).cuda()
#     lidar_data = torch.abs(lidar_data)
#     lidar_data = torch.floor(lidar_data).long()
#     lidar_data = lidar_data.view(-1, 2)
#     lidar_img = torch.zeros((img_size, img_size)).cuda()
#     lidar_img[lidar_data.permute(1,0)] = 255
#     return lidar_img 
Example #7
Source File: mtl_test.py    From aerial_mtl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def initialize(self, opt):
        self.opt = opt
        self.opt.imageSize = self.opt.imageSize if len(self.opt.imageSize) == 2 else self.opt.imageSize * 2
        self.gpu_ids = ''
        self.batchSize = self.opt.batchSize
        self.checkpoints_path = os.path.join(self.opt.checkpoints, self.opt.name)
        self.create_save_folders()

        self.netG = self.load_network()
        # st()
        if 'vaihingen' not in self.opt.dataset_name:
            self.data_loader, _ = CreateDataLoader(opt)

        # visualizer
        self.visualizer = Visualizer(self.opt)
        if 'semantics' in self.opt.tasks:
            from util.util import get_color_palette
            self.opt.color_palette = np.array(get_color_palette(self.opt.dataset_name))
            self.opt.color_palette = list(self.opt.color_palette.reshape(-1)) 
Example #8
Source File: debug.py    From blueoil with Apache License 2.0 6 votes vote down vote up
def sample_kernel():
    ker_n = 16
    ker_h = 3
    ker_w = 3
    ker_d = 8
    ker_shape = [ker_n, ker_h, ker_w, ker_d]

    filter_h = 3
    filter_w = 3

    test_seq = np.array(np.arange(ker_d * ker_h * ker_w), ndmin=2)
    ones = np.array(np.ones(ker_n), ndmin=2).T
    test_mat = ones * test_seq

    #  out = im2col(x, [ker_h, ker_w])
    x = test_mat.astype(np.uint32)
    qm = QuantizedMatrix(x, 2)

    import ipdb
    ipdb.set_trace()

    return out 
Example #9
Source File: data_set_gen.py    From Multi-channel-speech-extraction-using-DNN with MIT License 6 votes vote down vote up
def transform(audio_data, save_image_path, nFFT=256, overlap=0.75):
    '''audio_data: signals to convert
    save_image_path: path to store the image file'''
    # spectrogram
    freq_data = stft(audio_data, nFFT, overlap)
    freq_data = np.maximum(np.abs(freq_data),
                           np.max(np.abs(freq_data)) / 10000)
    log_freq_data = 20. * np.log10(freq_data / 1e-4)
    N_samples = log_freq_data.shape[0]
    # log_freq_data = np.maximum(log_freq_data, max_m - 70)
    # print(np.max(np.max(log_freq_data)))
    # print(np.min(np.min(log_freq_data)))
    log_freq_data = np.round(log_freq_data)
    log_freq_data = np.transpose(log_freq_data)
    # ipdb.set_trace()

    assert np.max(np.max(log_freq_data)) < 256, 'spectrogram value too large'
    # save the image
    spec_imag = Image.fromarray(log_freq_data)
    spec_imag = spec_imag.convert('RGB')
    spec_imag.save(save_image_path)
    return N_samples 
Example #10
Source File: SENN.py    From Multi-channel-speech-extraction-using-DNN with MIT License 6 votes vote down vote up
def loss(self, inf_targets, inf_vads, targets, vads, mtl_fac):
        '''
        Loss definition
        Only speech inference loss is defined and work quite well
        Add VAD cross entropy loss if you want
        '''
        loss_v1 = tf.nn.l2_loss(inf_targets - targets) / self.batch_size
        loss_o = loss_v1
        reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        # ipdb.set_trace()
        loss_v = loss_o + tf.add_n(reg_loss)
        tf.scalar_summary('loss', loss_v)
        # loss_merge = tf.cond(
        #     is_val, lambda: tf.scalar_summary('val_loss_batch', loss_v),
        #     lambda: tf.scalar_summary('loss', loss_v))
        return loss_v, loss_o
        # return tf.reduce_mean(tf.nn.l2_loss(inf_targets - targets)) 
Example #11
Source File: selectImage_multiprocess.py    From MetaFGNet with MIT License 6 votes vote down vote up
def worker():
    global SHARE_Q
    while True :
        if not SHARE_Q.empty():
            item = SHARE_Q.get()
            # ipdb.set_trace()
            do_something(item)
            time.sleep(1)
            SHARE_Q.task_done()
        else:
            break
            
#parser = argparse.ArgumentParser(description='copy selected images from source dir. to target dir.')
#parser.add_argument('--rm_dset', default=False, action='store_true',
#                    help='true to remove existing selected data before reproducing it')
#parser.add_argument('--auxiliary-dataset', type=str, default='imagenet',
#                    help='choose auxiliary dataset between imagenet/l_bird')
#args = parser.parse_args() 
Example #12
Source File: DSHRED.py    From MultiTurnDialogZoo with MIT License 6 votes vote down vote up
def forward(self, inpt, lengths, hidden=None):
        # use pack_padded
        # inpt: [seq_len, batch], lengths: [batch_size]
        embedded = self.embed(inpt)    # [seq_len, batch, input_size]

        if not hidden:
            hidden = torch.randn(self.n_layer * 2, len(lengths), 
                                 self.hidden_size)
            if torch.cuda.is_available():
                hidden = hidden.cuda()

        embedded = nn.utils.rnn.pack_padded_sequence(embedded, lengths, enforce_sorted=False)
        _, hidden = self.gru(embedded, hidden)   
        hidden = hidden.sum(axis=0)
        # [n_layer * bidirection, batch, hidden_size]
        # hidden = hidden.reshape(hidden.shape[1], -1)
        # ipdb.set_trace()
        # hidden = hidden.permute(1, 0, 2)    # [batch, n_layer * bidirectional, hidden_size]
        # hidden = hidden.reshape(hidden.size(0), -1) # [batch, *]
        # hidden = self.bn(hidden)
        hidden = torch.tanh(hidden)   # [batch, hidden]
        return hidden 
Example #13
Source File: VHRED.py    From MultiTurnDialogZoo with MIT License 6 votes vote down vote up
def forward(self, inpt, lengths, hidden=None):
        # use pack_padded
        # inpt: [seq_len, batch], lengths: [batch_size]
        # embedded = self.embed(inpt)    # [seq_len, batch, input_size]

        if not hidden:
            hidden = torch.randn(self.n_layer * 2, len(lengths), 
                                 self.hidden_size)
            if torch.cuda.is_available():
                hidden = hidden.cuda()

        embedded = nn.utils.rnn.pack_padded_sequence(inpt, lengths,
                                                     enforce_sorted=False)
        _, hidden = self.gru(embedded, hidden)    
        # [n_layer * bidirection, batch, hidden_size]
        # hidden = hidden.reshape(hidden.shape[1], -1)
        # ipdb.set_trace()
        hidden = hidden.sum(axis=0)    # [4, batch, hidden] -> [batch, hidden]
        
        # hidden = hidden.permute(1, 0, 2)    # [batch, n_layer * bidirectional, hidden_size]
        # hidden = hidden.reshape(hidden.size(0), -1) # [batch, *]
        # hidden = self.bn(hidden)
        # hidden = self.hidden_proj(hidden)
        hidden = torch.tanh(hidden)   # [batch, hidden]
        return hidden 
Example #14
Source File: Loss.py    From reversible-rnn with MIT License 6 votes vote down vote up
def __init__(self, generator, tgt_vocab, label_smoothing=0.0):
        super(NMTLossCompute, self).__init__(generator, tgt_vocab)
        assert (label_smoothing >= 0.0 and label_smoothing <= 1.0)

        self.tgt_vocab_len = len(tgt_vocab)

        if label_smoothing > 0:
            # When label smoothing is turned on,
            # KL-divergence between q_{smoothed ground truth prob.}(w)
            # and p_{prob. computed by model}(w) is minimized.
            # If label smoothing value is set to zero, the loss
            # is equivalent to NLLLoss or CrossEntropyLoss.
            # All non-true labels are uniformly set to low-confidence.
            self.criterion = nn.KLDivLoss(size_average=False)
            one_hot = torch.randn(1, len(tgt_vocab))
            one_hot.fill_(label_smoothing / (len(tgt_vocab) - 2))
            one_hot[0][self.padding_idx] = 0
            self.register_buffer('one_hot', one_hot)
        else:
            weight = torch.ones(len(tgt_vocab))
            weight[self.padding_idx] = 0
            self.criterion = nn.NLLLoss(weight, size_average=False)  # IMPORTANT: NLLLoss is what we use. Interesting that size_average=False
            # ipdb.set_trace()
        self.confidence = 1.0 - label_smoothing 
Example #15
Source File: HRAN_ablation.py    From MultiTurnDialogZoo with MIT License 5 votes vote down vote up
def forward(self, inpt, lengths, hidden=None):
        # use pack_padded
        # inpt: [seq_len, batch], lengths: [batch_size]
        embedded = self.embed(inpt)    # [seq_len, batch, input_size]

        if not hidden:
            hidden = torch.randn(self.n_layer * 2, len(lengths), 
                                 self.hidden_size)
            if torch.cuda.is_available():
                hidden = hidden.cuda()

        embedded = nn.utils.rnn.pack_padded_sequence(embedded, lengths,
                                                     enforce_sorted=False)
        output, hidden = self.gru(embedded, hidden)    
        output, _ = nn.utils.rnn.pad_packed_sequence(output)
        output = output[:, :, :self.hidden_size] + output[:, :, self.hidden_size:]
        # [n_layer * bidirection, batch, hidden_size]
        # hidden = hidden.reshape(hidden.shape[1], -1)
        # ipdb.set_trace()
        hidden = hidden.sum(axis=0)    # [4, batch, hidden] -> [batch, hidden]
        
        # hidden = hidden.permute(1, 0, 2)    # [batch, n_layer * bidirectional, hidden_size]
        # hidden = hidden.reshape(hidden.size(0), -1) # [batch, *]
        # hidden = self.bn(hidden)
        # hidden = self.hidden_proj(hidden)
        hidden = torch.tanh(hidden)   # [batch, hidden]
        output = torch.tanh(output)   # [seq, batch, hidden]
        return output, hidden 
Example #16
Source File: WSeq_RA.py    From MultiTurnDialogZoo with MIT License 5 votes vote down vote up
def forward(self, inpt, last_hidden, encoder_outputs):
        # inpt: [batch_size], last_hidden: [2, batch, hidden_size]
        # encoder_outputs: [turn_len, seq, batch, hidden_size]
        embedded = self.embed(inpt).unsqueeze(0)    # [1, batch_size, embed_size]
        key = last_hidden.sum(axis=0)
        
        # word level attention
        context_output = []
        for turn in encoder_outputs:
            # ipdb.set_trace()
            word_attn_weights = self.word_level_attn(key, turn)
            context = word_attn_weights.bmm(turn.transpose(0, 1))
            context = context.transpose(0, 1).squeeze(0)    # [batch, hidden]
            context_output.append(context)
        context_output = torch.stack(context_output)    # [turn, batch, hidden]
        
        # output: [seq, batch, hidden], [2, batch, hidden]
        context_output, hidden = self.context_encoder(context_output)
        
        # [batch, hidden_size]
        if len(encoder_outputs) == 1:
            context = self.attn(context_output[0], context_output)
        else:
            context = self.attn(context_output[-1], context_output[:-1])
        context = context.unsqueeze(0)    # [1, batch, hidden]
        rnn_input = torch.cat([embedded, context], 2)   # [1, batch, 2 * hidden]

        # output: [1, batch, hidden_size], hidden: [1, batch, hidden_size]
        output, hidden = self.gru(rnn_input, last_hidden)
        output = output.squeeze(0)    # [batch, hidden_size]
        # context = context.squeeze(0)  # [batch, hidden]
        # output = torch.cat([output, context], 1)    # [batch, 2 * hidden]
        output = self.out(output)     # [batch, output_size]
        output = F.log_softmax(output, dim=1)
        return output, hidden 
Example #17
Source File: DSHRED.py    From MultiTurnDialogZoo with MIT License 5 votes vote down vote up
def predict(self, src, maxlen, lengths, loss=False):
        # predict for test dataset, return outputs: [maxlen, batch_size]
        # src: [turn, max_len, batch_size], lengths: [turn, batch_size]
        with torch.no_grad():
            turn_size, batch_size = len(src), src[0].size(1)
            outputs = torch.zeros(maxlen, batch_size)
            floss = torch.zeros(maxlen, batch_size, self.output_size)
            if torch.cuda.is_available():
                outputs = outputs.cuda()
                floss = floss.cuda()

            turns = []
            for i in range(turn_size):
                # sbatch = src[i].transpose(0, 1)
                hidden = self.utter_encoder(src[i], lengths[i])
                turns.append(hidden)
            turns = torch.stack(turns)

            # context encoding
            # output: [seq, batch, hidden], [batch, hidden]
            # static_attn: [1, batch, hidden]
            static_attn, context_output, hidden = self.context_encoder(turns)
            # hidden = hidden.unsqueeze(0)

            output = torch.zeros(batch_size, dtype=torch.long).fill_(self.sos)
            if torch.cuda.is_available():
                output = output.cuda()

            try:
                for i in range(1, maxlen):
                    output, hidden = self.decoder(output, hidden, context_output, static_attn)
                    floss[i] = output
                    output = output.max(1)[1]
                    outputs[i] = output
            except:
                ipdb.set_trace()

            if loss:
                return outputs, floss
            else:
                return outputs 
Example #18
Source File: utils.py    From MultiTurnDialogZoo with MIT License 5 votes vote down vote up
def sent2glove(vocab, sent):
    s = np.zeros(vocab['<unk>'].shape, dtype=np.float)
    for word in nltk.word_tokenize(sent):
        # ipdb.set_trace()
        vector = vocab.get(word, vocab['<unk>'])
        s += vector
    return s

# ================================================================================= # 
Example #19
Source File: load_data.py    From Azimuth with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read_V1_data(data_file, learn_options, AML_file=cur_dir + "/data/V1_suppl_data.txt"):
    if data_file is None:
        data_file = cur_dir + "/data/V1_data.xlsx"
    human_data = pandas.read_excel(data_file, sheetname=0, index_col=[0, 1])
    mouse_data = pandas.read_excel(data_file, sheetname=1, index_col=[0, 1])
    Xdf, Y = combine_organisms(human_data, mouse_data)

    # get position within each gene, then join and re-order
    # note that 11 missing guides we were told to ignore
    annotations = pandas.read_csv(AML_file, delimiter='\t', index_col=[0, 4])
    annotations.index.names = Xdf.index.names
    gene_position = pandas.merge(Xdf, annotations, how="inner", left_index=True, right_index=True)
    gene_position = util.impute_gene_position(gene_position)
    gene_position = gene_position[['Amino Acid Cut position', 'Nucleotide cut position', 'Percent Peptide']]
    Y = Y.loc[gene_position.index]
    Xdf = Xdf.loc[gene_position.index]

    Y['test'] = 1  # for bookeeping to keep consistent with V2 which uses this for "extra pairs"

    target_genes = Y['Target gene'].unique()

    Y.index.names = ['Sequence', 'Target gene']

    assert Xdf.index.equals(Y.index), "The index of Xdf is different from the index of Y (this can cause inconsistencies/random performance later on)"

    if learn_options is not None and learn_options["flipV1target"]:
        print "************************************************************************"
        print "*****************MATCHING DOENCH CODE (DEBUG MODE)**********************"
        print "************************************************************************"
        # normally it is: Y['average threshold'] = Y['average rank'] > 0.8, where 1s are good guides, 0s are not
        Y['average threshold'] = Y['average rank'] < 0.2  # 1s are bad guides
        print "press c to continue"
        import ipdb
        ipdb.set_trace()

    return annotations, gene_position, target_genes, Xdf, Y 
Example #20
Source File: read_upenn.py    From human_dynamics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def read_upenn(base_dir):
    seqs = glob(osp.join(base_dir, 'frames/*'))
    for seq_path in seqs[3:]:
        seq_name = osp.basename(seq_path)
        label_path = osp.join(base_dir, 'labels', '{}.mat'.format(seq_name))

        show_seq(seq_path, label_path)
        import ipdb
        ipdb.set_trace() 
Example #21
Source File: nmt_base.py    From DualLearning with MIT License 5 votes vote down vote up
def pred_probs(f_log_probs, prepare_data, options, iterator, verbose=True):
    probs = []

    n_done = 0

    for x, y in iterator:
        n_done += len(x)

        x, x_mask, y, y_mask = prepare_data(x, y,
                                            n_words_src=options['n_words_src'],
                                            n_words=options['n_words'])

        pprobs = f_log_probs(x, x_mask, y, y_mask)
        for pp in pprobs:
            probs.append(pp)

        if numpy.isnan(numpy.mean(probs)):
            ipdb.set_trace()

        if verbose:
            print >>sys.stderr, '%d samples computed' % (n_done)

    return numpy.array(probs)


# optimizers
# name(hyperp, tparams, grads, inputs (list), cost) = f_grad_shared, f_update 
Example #22
Source File: upenn_to_tfrecords_video.py    From human_dynamics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def clean_video(image_paths, gt2ds):
    """
    returns None if video is bad
    ow. returns cleaned/adjusted image_paths and gt2ds
    """
    for im_path in image_paths:
        if not osp.exists(im_path):
            print('!!--{} doesnt exist! Skipping..--!!'.format(im_path))
            ipdb.set_trace()
            return None, None

    # Cut the frame off at minimum # of visible points.
    num_vis = np.sum(gt2ds[:, :, 2] > 0, axis=1)
    if num_vis[0] == 0:
        # ugly,, but one video starts out with 0 kp
        num_vis = num_vis[1:]
        gt2ds = gt2ds[1:]
        image_paths = image_paths[1:]

    if np.any(num_vis <= MIN_VIS_PTS):
        cut_off = (num_vis > MIN_VIS_PTS).tolist().index(False)
        gt2ds = gt2ds[:cut_off]
        image_paths = image_paths[:cut_off]

    if len(image_paths) < MIN_NUM_FRAMES:
        print('Video too short! {}'.format(len(image_paths)))
        return None, None

    return image_paths, gt2ds 
Example #23
Source File: WSeq_RA.py    From MultiTurnDialogZoo with MIT License 5 votes vote down vote up
def forward(self, inpt, lengths, hidden=None):
        # use pack_padded
        # inpt: [seq_len, batch], lengths: [batch_size]
        embedded = self.embed(inpt)    # [seq_len, batch, input_size]

        if not hidden:
            hidden = torch.randn(self.n_layer * 2, len(lengths), 
                                 self.hidden_size)
            if torch.cuda.is_available():
                hidden = hidden.cuda()

        embedded = nn.utils.rnn.pack_padded_sequence(embedded, lengths, enforce_sorted=False)
        output, hidden = self.gru(embedded, hidden)    
        output, _ = nn.utils.rnn.pad_packed_sequence(output)
        output = output[:, :, :self.hidden_size] + output[:, :, self.hidden_size:]
        hidden = hidden.sum(axis=0)
        hidden = torch.tanh(hidden)
        output = torch.tanh(output)   # [seq, batch, hidden]
        # [n_layer * bidirection, batch, hidden_size]
        # hidden = hidden.reshape(hidden.shape[1], -1)
        # ipdb.set_trace()
        # hidden = hidden.permute(1, 0, 2)    # [batch, n_layer * bidirectional, hidden_size]
        # hidden = hidden.reshape(hidden.size(0), -1) # [batch, *]
        # hidden = self.bn(self.hidden_proj(hidden))
        # hidden = torch.tanh(hidden)   # [batch, hidden]
        return output, hidden 
Example #24
Source File: min_norm_solvers.py    From aerial_mtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _min_norm_2d(vecs, dps):
        """
        Find the minimum norm solution as combination of two points
        This is correct only in 2D
        ie. min_c |\sum c_i x_i|_2^2 st. \sum c_i = 1 , 1 >= c_1 >= 0 for all i, c_i + c_j = 1.0 for some i, j
        """
        dmin = 1e8
        for i in range(len(vecs)):
            for j in range(i+1,len(vecs)):
                if (i,j) not in dps:
                    dps[(i, j)] = 0.0
                    for k in range(len(vecs[i])):
                        dps[(i,j)] += torch.dot(vecs[i][k], vecs[j][k]).item()
                    dps[(j, i)] = dps[(i, j)]
                if (i,i) not in dps:
                    dps[(i, i)] = 0.0
                    for k in range(len(vecs[i])):
                        dps[(i,i)] += torch.dot(vecs[i][k], vecs[i][k]).item()
                if (j,j) not in dps:
                    dps[(j, j)] = 0.0   
                    for k in range(len(vecs[i])):
                        dps[(j, j)] += torch.dot(vecs[j][k], vecs[j][k]).item()
                c,d = MinNormSolver._min_norm_element_from2(dps[(i,i)], dps[(i,j)], dps[(j,j)])
                if d < dmin:
                    dmin = d
                    sol = [(i,j),c,d]
        return sol, dps 
Example #25
Source File: test_model_raster.py    From aerial_mtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def initialize(self, opt):
        # GenericTestModel.initialize(self, opt)
        self.opt = opt
        self.get_color_palette()
        self.opt.imageSize = self.opt.imageSize if len(self.opt.imageSize) == 2 else self.opt.imageSize * 2
        self.gpu_ids = ''
        self.batchSize = self.opt.batchSize
        self.checkpoints_path = os.path.join(self.opt.checkpoints, self.opt.name)
        self.create_save_folders()
        self.opt.use_semantics = (('multitask' in self.opt.model) or ('semantics' in self.opt.model))

        self.netG = self.load_network()
        # self.opt.dfc_preprocessing = 2
        # self.data_loader, _ = CreateDataLoader(opt, Dataset)

        # visualizer
        self.visualizer = Visualizer(self.opt)
        if 'semantics' in self.opt.tasks:
            from util.util import get_color_palette
            self.opt.color_palette = np.array(get_color_palette(self.opt.dataset_name))
            # self.opt.color_palette = list(self.opt.color_palette.reshape(-1))
            # st()

    # def initialize(self, opt):
    #     GenericTestModel.initialize(self, opt)
    #     self.get_color_palette() 
Example #26
Source File: networks.py    From aerial_mtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def define_G(input_nc, output_nc, ngf, net_architecture, opt, gpu_ids=[]):
    netG = None
    use_gpu = len(gpu_ids) > 0
    # norm_layer = get_norm_layer(norm_type=norm)
    use_dropout = opt.use_dropout
    pretrained = opt.pretrained
    init_method = opt.init_method
    use_skips = opt.use_skips
    d_block_type = opt.d_block_type
    n_classes = opt.n_classes # --> output_nc
    tasks = opt.tasks

    # from .dense_decoders_multitask_auto import denseUnet121
    from .dense_decoders_multitask_auto import denseUnet121
    netG = denseUnet121(pretrained=pretrained, input_nc=input_nc, outputs_nc=opt.outputs_nc, init_method=init_method, use_dropout=use_dropout, use_skips=use_skips, d_block_type=d_block_type, num_classes=n_classes, tasks=tasks, type_net=net_architecture, mtl_method=opt.mtl_method)
    
    # else:
    #     raise NotImplementedError('Generator model name [%s] is not recognized' % net_architecture)

    # print number of parameters of the network
    # print_network(netG)
    print_n_parameters_network(netG)
    # st()

    if len(gpu_ids) > 0:
        netG.cuda(device_id=gpu_ids[0])

    if not pretrained:
        w_init.init_weights(netG, init_method)
    return netG 
Example #27
Source File: vocoder_old2.py    From TTS-Cube with Apache License 2.0 5 votes vote down vote up
def forward(self, mgc, noise, tail=None):
        conditioning = self.conditioning(torch.Tensor(mgc).to(device).reshape(len(mgc), 1, 1, self.MGC_SIZE))
        conditioning = conditioning.reshape(len(mgc) * self.UPSAMPLE_SIZE, self.MGC_SIZE)

        prepend = torch.tensor(noise[:self.RECEPTIVE_FIELD]).to(device)
        prev_x = torch.tensor(noise).to(device)
        new_tail = []
        for iStack in range(self.NUM_STACKS):

            # prepare the input
            x_list = []
            for ii in range(len(prev_x) - self.RECEPTIVE_FIELD):
                x_list.append(prev_x[ii:ii + self.RECEPTIVE_FIELD])
            # from ipdb import set_trace
            # set_trace()
            x = torch.stack(x_list)
            x = x.reshape(len(mgc) * self.UPSAMPLE_SIZE, 1, x_list[0].shape[0])

            pre = self.convolutions[iStack](x, conditioning)
            pre = pre.reshape(pre.shape[0], pre.shape[1])
            pre = torch.relu(self.pre_output[iStack](pre))

            mean = self.mean_layer[iStack](pre)
            logvar = self.stdev_layer[iStack](pre)

            prev_x = self.reparameterize(mean, logvar,
                                         prev_x[self.RECEPTIVE_FIELD:].reshape(mean.shape[0], mean.shape[1]))
            new_tail.append(prev_x[-self.RECEPTIVE_FIELD:].detach())

            if iStack != self.NUM_STACKS - 1:
                if tail is None:
                    prev_x = torch.cat((prepend, prev_x.reshape((prev_x.shape[0]))))
                else:
                    prev_x = torch.cat((tail[iStack].reshape(tail[iStack].shape[0]), prev_x.reshape((prev_x.shape[0]))))

        return prev_x, mean, logvar, new_tail 
Example #28
Source File: ln_lstm2.py    From Multi-channel-speech-extraction-using-DNN with MIT License 5 votes vote down vote up
def __call__(self, inputs, state, scope=None):
        """Long short-term memory cell (LSTM)."""
        with tf.variable_scope(scope or type(self).__name__):
            c, h = state

            # change bias argument to False since LN will add bias via shift
            concat = tf.nn.rnn_cell._linear(
                [inputs, h], 4 * self._num_units, False)
            # ipdb.set_trace()

            i, j, f, o = tf.split(1, 4, concat)

            # add layer normalization to each gate
            i = ln(i, scope='i/')
            j = ln(j, scope='j/')
            f = ln(f, scope='f/')
            o = ln(o, scope='o/')

            new_c = (c * tf.nn.sigmoid(f + self._forget_bias) +
                     tf.nn.sigmoid(i) * self._activation(j))

            # add layer_normalization in calculation of new hidden state
            new_h = self._activation(
                ln(new_c, scope='new_h/')) * tf.nn.sigmoid(o)
            new_state = tf.nn.rnn_cell.LSTMStateTuple(new_c, new_h)
            return new_h, new_state 
Example #29
Source File: __init__.py    From attention-lvcsr with MIT License 5 votes vote down vote up
def ipdb_breakpoint(x):
    """A simple hook function for :func:`put_hook` that runs ipdb.

    Parameters
    ----------
    x : :class:`~numpy.ndarray`
        The value of the hooked variable.

    """
    import ipdb
    ipdb.set_trace() 
Example #30
Source File: Loss.py    From reversible-rnn with MIT License 5 votes vote down vote up
def _compute_loss(self, batch, output, target):
        # ipdb.set_trace()
        # scores = self.generator(self._bottle(output))
        scores = self.generator(output.view(-1, output.size(2)))  # Just doing _bottle w/o calling function
        gtruth = target.view(-1)

        # if self.confidence < 1:
        #     tdata = gtruth.data
        #     mask = torch.nonzero(tdata.eq(self.padding_idx)).squeeze()
        #     likelihood = torch.gather(scores.data, 1, tdata.unsqueeze(1))
        #     tmp_ = self.one_hot.repeat(gtruth.size(0), 1)
        #     tmp_.scatter_(1, tdata.unsqueeze(1), self.confidence)
        #     if mask.dim() > 0:
        #         likelihood.index_fill_(0, mask, 0)
        #         tmp_.index_fill_(0, mask, 0)
        #     gtruth = Variable(tmp_, requires_grad=False)

        # loss = self.criterion(scores, gtruth)

        weight = torch.ones(self.tgt_vocab_len).cuda()
        weight[self.padding_idx] = 0
        loss = F.nll_loss(scores, gtruth, weight=weight, size_average=False) # , ignore_index=-100, reduce=None, reduction='elementwise_mean')

        # if self.confidence < 1:
        #     loss_data = - likelihood.sum(0)
        # else:
        #     loss_data = loss.data.clone()

        loss_data = loss.data.clone()
        stats = self._stats(loss_data, scores.data, target.view(-1).data)

        return loss, stats