예제 #1
0
def multiply_karatsuba(x, y):
    """
    Multiplies two numbers represented as arrays
    using the Karatsuba algorithm, falling back
    on grade school algorithm for the base case

    :param x: []int
    :param y: []int
    :rtype []int
    """
    x, y = match_padding(x, y)
    a, b = split(x)
    c, d = split(y)

    if len(x) == 1:
        return multiply_simple(x, y)

    res_1 = multiply_karatsuba(a, c)
    res_2 = multiply_karatsuba(b, d)

    partial = multiply_karatsuba(add(a, b), add(c, d))
    # res_3 is partial - res_1 - res_2.
    # To simplify, just add res_1 and res_2 then subtract that sum from partial
    res_3 = subtract(partial, add(res_1, res_2))

    res = add(pad(res_1, len(x), 'right'), res_2,
              pad(res_3, (len(x) + 1) // 2, 'right'))

    return res
예제 #2
0
def compute_validation_outputs(model, val_iter, field, optional_names=[]):
    loss, predictions, answers = [], [], []
    outputs = [[] for _ in range(len(optional_names))]
    for batch_idx, batch in enumerate(val_iter):
        l, p = model(batch)
        loss.append(l)
        predictions.append(pad(p, 150, dim=-1, val=field.vocab.stoi['<pad>']))
        a = None
        if hasattr(batch, 'wikisql_id'):
            a = batch.wikisql_id.data.cpu()
        elif hasattr(batch, 'squad_id'):
            a = batch.squad_id.data.cpu()
        elif hasattr(batch, 'woz_id'):
            a = batch.woz_id.data.cpu()
        else:
            a = pad(batch.answer.data.cpu(),
                    150,
                    dim=-1,
                    val=field.vocab.stoi['<pad>'])
        answers.append(a)
        for opt_idx, optional_name in enumerate(optional_names):
            outputs[opt_idx].append(getattr(batch, optional_name).data.cpu())
    loss = torch.cat(loss, 0) if loss[0] is not None else None
    predictions = torch.cat(predictions, 0)
    answers = torch.cat(answers, 0)
    return loss, predictions, answers, [
        torch.cat([
            pad(x, 150, dim=-1, val=field.vocab.stoi['<pad>']) for x in output
        ], 0) for output in outputs
    ]
def multiply_karatsuba_parallel(x, y, key=None):
    """
    Multiplies two numbers represented as arrays
    using the Karatsuba algorithm, falling back
    on grade school algorithm for the base case

    :param x: []int
    :param y: []int
    :rtype []int
    """
    x, y = match_padding(x, y)
    a, b = split(x)
    c, d = split(y)

    # for base case, go simple
    if len(x) == 1:
        return multiply_simple(x, y)

    # for big numbers, go parallel
    if len(x) > 300:
        # generate random ids for the subprocess outputs
        r1 = random.random()
        r2 = random.random()
        r3 = random.random()

        # run the sub-multiplications in parallel
        p1 = Process(target=multiply_karatsuba_parallel, args=[a, c, r1])
        p2 = Process(target=multiply_karatsuba_parallel, args=[b, d, r2])
        p3 = Process(target=multiply_karatsuba_parallel,
                     args=[add(a, b), add(c, d), r3])

        p1.start()
        p2.start()
        p3.start()
        p1.join()
        p2.join()
        p3.join()

        # get the results
        res_1 = return_dict[r1]
        res_2 = return_dict[r2]
        partial = return_dict[r3]

    # for smaller numbers, don't bother parallelizing
    else:
        res_1 = multiply_karatsuba_parallel(a, c)
        res_2 = multiply_karatsuba_parallel(b, d)
        partial = multiply_karatsuba_parallel(add(a, b), add(c, d))

    # do the karatsuba shuffle
    res_3 = subtract(partial, add(res_1, res_2))
    res = add(pad(res_1, len(x), 'right'), res_2,
              pad(res_3, (len(x) + 1) // 2, 'right'))

    # if we are in parallel mode, write the result to the global dict
    if key is not None:
        return_dict[key] = res

    return res
예제 #4
0
 def __build_do87(self,data):
     # encode data
     enc = self.des3enc(pad(data))
     # encode length of: encrypted data + one more for
     #                   padding content indicator
     l = ber_tlv_len(len(enc)+1)
     return [0x87] + l + [0x01] + enc
예제 #5
0
    def counter(self):
        ctr = self.iv[:]

        offsetstr = hex(self.ivoffset)[2:]
        if len(offsetstr) > 8:
            # if ivoffset gets VERY large, need to pad to ... 16, 24
            raise Exception('ivoffset to large... need to fix')
        offset = pad(offsetstr, 8, '0')

        l = len(offset)

        i=0
        offsets = []
        while i<l:
            offsets.append( int( offset[i] + offset[i + 1], 16 ) )
            i+=2
        offsets.reverse()
        l = len(offsets)

        carryover = 0
        i=0
        while i<l:
            offsets[i] += carryover
            ctr[i] += offsets[i]
            carryover = 1 if ctr[i] >= 256 else 0
            ctr[i] %= 256
            i += 1
        
        self.ivoffset += 1
        toreturn = ''.join(map(chr, ctr))
        return toreturn
예제 #6
0
    def encrypt_bytes(self, f_bytes, key, iv):
        """ Encrypts f_bytes using AES in CBC mode """

        AES_cip = AES.new(key, AES.MODE_CBC, iv)
        f_bytes = util.pad(f_bytes, AES.block_size)
        cipher = AES_cip.encrypt(f_bytes)

        return cipher
예제 #7
0
    def encrypt_filename(self, name, key):
        """ Encrypts filename using AES in ECB mode """

        AES_cipher = AES.new(key, AES.MODE_ECB)
        name = util.pad(name.encode(), AES.block_size)
        cipher = AES_cipher.encrypt(name)

        return cipher.hex()
예제 #8
0
 def setup_validation_data(self, filename):
     """Read a validation set from a file."""
     _, src_seqs, tgt_indices = read_validation_file(
         path.join(self.data_dir, filename), self.src_indexer,
         self.tgt_indexer)
     src_seqs, tgt_indices = drop_oov_from_validation_set(
         src_seqs, tgt_indices, self.src_n_vocab, self.tgt_n_vocab)
     self.src_seqs = torch.LongTensor(pad(src_seqs))
     self.tgt_indices = torch.LongTensor(tgt_indices)
예제 #9
0
파일: runner.py 프로젝트: brhoades/light-up
def prnBase( cfg, gen=False ):
    evals="~"
    avg="~"
    std="~"
    skew="~"
    best="~"
    genn="~"
    diversity=0
    if gen != False:
        avg = round( gen.average( ), 4 )
        genn = gen.num
        evals = gen.fitEvals
        diversity = len(gen.fitTable.data)
        std=round( gen.stdev( ), 4 )
        skew=round( gen.skew( ), 1 )
        best=round( gen.max( ), 4 )
    out = ""
    if int(cfg[MAIN][TOTAL_RUNS]) >= 10:
        out += "\t"
    out += util.pad(genn, cfg[TERMINATION][GENERATION_LIMIT])
    out += "\t"
    if cfg[TERMINATION][TYPE] == GENERATIONAL_LIMIT:
        out += "\t"
        if math.log(int(cfg[TERMINATION][GENERATION_LIMIT]), 10) >= 12:
            out += "\t"
    out += util.pad(evals, cfg[TERMINATION][EVALUATION_LIMIT])
    if cfg[TERMINATION][TYPE] == FITNESS_EVALUATION_LIMIT and math.log(int(cfg[TERMINATION][EVALUATION_LIMIT]), 10) >= 3:
        out += "\t"
    out += "\t"
    out += str(avg)
    out += "\t"
    out += str(skew)
    out += "\t"
    out += str(std)
    out += "\t"
    out += str(best)
    out += "\t"
    out += str(diversity)
    out += "\t"
    
    util.delprn(out, 1)
예제 #10
0
    def encrypt(self, m):
        plaintext = pad(m, self._block_size)
        # plaintext = m

        assert len(plaintext) % self._block_size == 0
        blocks = []
        for i in progressbar.progressbar(range(0, len(plaintext), self._block_size)):
            plaintext_block = plaintext[i:i + self._block_size]
            block = self._encrypt_block(plaintext_block)
            blocks.append(block)

        return b"".join(blocks)
예제 #11
0
파일: blocks.py 프로젝트: vlicaret/segprop
 def __init__(self, size=5, pad=None, device='cuda'):
     super(MedianFilterWeighted3d, self).__init__()
     try:
         size[0]
     except TypeError:
         size = (size, size, size)
     kernel = torch.ones((size[0], size[1], size[2]),
                         device=device) / (size[0] * size[1] * size[2])
     kernel = kernel.view(1, 1, size[0], size[1], size[2])
     if pad is None:
         pad = util.pad(size)
     self.pad = pad[::-1]
     self.weights = kernel
예제 #12
0
파일: blocks.py 프로젝트: vlicaret/segprop
 def __init__(self, kernel_size=5, sigma=2, pad=None, device='cuda'):
     super(GaussBlur3d, self).__init__()
     try:
         kernel_size[0]
     except TypeError:
         kernel_size = (kernel_size, kernel_size, kernel_size)
     kernel = util.gaussian(kernel_size, sigma, device=device)
     kernel = kernel.view(1, 1, kernel_size[0], kernel_size[1],
                          kernel_size[2])
     if pad is None:
         pad = util.pad(kernel_size)
     self.pad = pad[::-1]
     self.weights = kernel
예제 #13
0
파일: validate.py 프로젝트: shaogx/decaNLP
def compute_validation_outputs(model, val_iter, field, optional_names=[]):
    loss, predictions, answers = [], [], []
    outputs = [[] for _ in range(len(optional_names))]
    for batch_idx, batch in enumerate(val_iter):
        l, p = model(batch)
        loss.append(l)
        predictions.append(pad(p, 150, dim=-1, val=field.vocab.stoi['<pad>']))
        a = None
        if hasattr(batch, 'wikisql_id'):
            a = batch.wikisql_id.data.cpu()
        elif hasattr(batch, 'squad_id'):
            a = batch.squad_id.data.cpu()
        elif hasattr(batch, 'woz_id'):
            a = batch.woz_id.data.cpu()
        else:
            a =  pad(batch.answer.data.cpu(), 150, dim=-1, val=field.vocab.stoi['<pad>'])
        answers.append(a)
        for opt_idx, optional_name in enumerate(optional_names):
            outputs[opt_idx].append(getattr(batch, optional_name).data.cpu()) 
    loss = torch.cat(loss, 0) if loss[0] is not None else None
    predictions = torch.cat(predictions, 0)
    answers = torch.cat(answers, 0)
    return loss, predictions, answers, [torch.cat([pad(x, 150, dim=-1, val=field.vocab.stoi['<pad>']) for x in output], 0) for output in outputs]
예제 #14
0
파일: blocks.py 프로젝트: vlicaret/segprop
 def __init__(self, size=5, pad=None, device='cuda'):
     super(MedianFilter3d, self).__init__()
     try:
         size[0]
     except TypeError:
         size = (size, size, size)
     kernel = torch.zeros(
         (size[0] * size[1] * size[2], size[0], size[1], size[2]),
         device=device)
     for i in range(size[0]):
         for j in range(size[1]):
             for k in range(size[2]):
                 kernel[i + j * size[0] + k * size[0] * size[1], i, j,
                        k] = 1
     if pad is None:
         pad = util.pad(size)
     self.pad = pad[::-1]
     self.weights = kernel.unsqueeze(1)
예제 #15
0
    def setup_validation_data(self, filename):
        """Read a validation set from a file."""
        src_indices, src_seqs, tgt_indices = read_validation_file(
            path.join(self.data_dir, filename), self.src_indexer,
            self.tgt_indexer)
        # Drop pairs that contain OOV words (subwords)
        src_seqs, tgt_indices = drop_oov_from_validation_set(
            src_seqs, tgt_indices, self.src_n_vocab, self.tgt_n_vocab)

        valid = {}
        valid['valid_src_subword_ids'] = torch.LongTensor(pad(src_seqs))
        # if not self.params.disable_cuda and torch.cuda.is_available():  # TODO
        #     valid['valid_src_subword_ids'] = valid['valid_src_subword_ids'].cuda()

        valid['valid_src_word_ids'] = src_indices
        valid['valid_tgt_word_ids'] = tgt_indices
        valid['gold'] = defaultdict(list)
        for src, tgt in zip(src_indices, tgt_indices):
            valid['gold'][src].append(tgt)

        return valid
예제 #16
0
def go(arg):

    tbw = SummaryWriter(log_dir=arg.tb_dir)

    with open(arg.data, 'r') as file:
        lines = file.readlines()

    print('Creating word indices')

    dist = Counter()

    for line in lines:
        dist.update(util.tokenize(line))

    vocab = dist.most_common(arg.max_vocab - len(EXTRA_SYMBOLS))

    i2w = EXTRA_SYMBOLS + [w[0] for w in vocab]
    w2i = {word:ix for ix, word in enumerate(i2w)}

    data = []
    suff = [EOS] if arg.add_eos else []

    for line in lines:
        words = util.tokenize(line)
        indices = []
        for word in words:
            if word in w2i:
                indices.append(w2i[word])
            else:
                indices.add(UNK)

        if len(indices) > 0:
            data.append(indices + suff)

    data.sort(key= lambda x : len(x))

    vocab_size = len(i2w)
    print('vocabulary size', vocab_size)
    print('top 100 words:', i2w[:100])
    print('sentence lengths ', [len(s) for s in data])

    def decode(indices):

        sentence = ''
        for id in indices:
                if id == PAD:
                    break
                sentence += i2w[id] + ' '

        return sentence

    s = random.choice(data)
    print('random sentence', s)
    print('               ', decode(s))

    ## Set up the model

    embedding = torch.nn.Embedding(num_embeddings=vocab_size, embedding_dim=arg.embedding_size)

    seq_enc = models.SeqEncoder(vocab_size=vocab_size, embedding=embedding, zsize=arg.latent_size)
    seq_dec = models.SeqDecoder(vocab_size=vocab_size, embedding=embedding, zsize=arg.latent_size)

    mods = [seq_enc, seq_dec]

    if torch.cuda.is_available():
        for model in mods:
            model.cuda()

    params = []
    for model in mods:
        params.extend(model.parameters())
    optimizer = Adam(params, lr=arg.lr)

    instances_seen = 0

    for e in range(arg.epochs):
        if arg.annealing_mode == None or arg.annealing_mode == 'none':
            weight = 1
        elif arg.annealing_mode == 'linear':
            weight = util.lin_anneal(e, arg.epochs)
        elif arg.annealing_mode == 'logistic':
            weight = util.log_anneal(e, arg.epochs)
        else:
            raise Exception('Annea;ing mode {} not recognized'.format(arg.annealing_mode))

        print('Epoch {}, setting KL weight to {}'.format(e, weight))
        for fr in tqdm.trange(0, len(data), arg.batch_size):
            if arg.instance_limit is not None and fr > arg.instance_limit:
                break

            to = min(len(data), fr + arg.batch_size)

            batch = data[fr:to]
            batch, lengths = util.pad(batch)

            # Created shifted versions
            b, s = batch.size()

            # Input for the decoder
            batch_teacher = torch.cat([torch.ones(b, 1, dtype=torch.long), batch], dim=1)
            batch_out     = torch.cat([batch, torch.zeros(b, 1, dtype=torch.long)], dim=1)

            lengths = torch.LongTensor(lengths)

            if torch.cuda.is_available():

                batch = batch.cuda()
                batch_teacher = batch_teacher.cuda()
                batch_out = batch_out.cuda()

                lengths = lengths.cuda()

            batch = Variable(batch)
            batch_teacher = Variable(batch_teacher)
            batch_out = Variable(batch_out)
            lengths = Variable(lengths)

            z = seq_enc(batch, lengths)

            kl = util.kl_loss(*z)

            rec = seq_dec(util.sample(*z), batch_teacher, lengths + 1)
            rec = rec.transpose(1, 2)
            rl  = nll_loss(rec, batch_out, reduce=False).view(b, -1)

            rl = rl.sum(dim=1)

            loss = (rl + weight * kl).mean()

            #- backward pass
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            instances_seen += b

            tbw.add_scalar('lang/kl', float(kl.mean()), instances_seen)
            tbw.add_scalar('lang/rec', float(rl.mean()), instances_seen)
            tbw.add_scalar('lang', float(loss), instances_seen)

        # Interpolate
        for r in range(REP):

            print('Interpolation, repeat', r)

            z1, z2 = torch.randn(2, arg.latent_size)
            if torch.cuda.is_available():
                z1, z2 = z1.cuda(), z2.cuda()

            zs = util.slerp(z1, z2, 10)

            if arg.original_sampling:
                print('== sentences==')
                sentences = seq_dec.sample_old(z=zs)
            else:
                print('== sentences (temp={}) =='.format(TEMPS[r]))
                sentences = seq_dec.sample(z=zs, temperature=TEMPS[r])


            for s in sentences:
                print('   ', decode(s))
예제 #17
0
def main(args):
    global verbose
    verbose = args.verbose

    # Read subword sequences and embeddings
    if verbose:
        logger.info('Load subwords from ' + args.path_subwords)
    data = dict(np.load(args.path_subwords))
    N = data['seqs'].shape[0]
    D = data['W'].shape[1]
    W = np.r_[np.zeros((1, D)), data['W']]
    seqs = np.array([[v + 1 for v in seq] for seq in data['seqs']])  # 0=PAD

    # Load a trained transformer
    if verbose:
        logger.info('Load a transformer from ' + args.path_transformer)
    transformer = SubwordEmbedding(D, n_layers=args.n_layers)
    transformer.load(args.path_transformer)

    # Vocabularies (words)
    vocab = []
    with open(args.path_original) as f:
        next(f)  # skip a header
        for i, line in enumerate(f):
            if i == N:  # reached the vocabulary size
                break
            vocab.append(line.split(' ', 1)[0])

    batch_size = args.batch_size
    n_batches = N // batch_size + int(N % batch_size > 0)  # number of batches
    batches = [
        np.arange(n * batch_size, min(N, (n + 1) * batch_size))
        for n in range(n_batches)
    ]

    # Transform subword embeddings and aggregate them to word embeddings
    def embedding_layer(idx_seqs):  # Imitating embeddings.Embedding class
        return torch.FloatTensor([[W[idx] for idx in idx_seq]
                                  for idx_seq in idx_seqs])

    if verbose:
        logger.info('Converting...')
    vecs = []
    for batch in tqdm(batches, total=n_batches):
        batch_seqs = torch.LongTensor(pad(seqs[batch]))
        batch_seqs.requires_grad = False
        vecs += [
            vec.detach().numpy()
            for vec in transformer(batch_seqs, embedding_layer)
        ]

    # Output to file in text word2vec format
    if verbose:
        logger.info('Output to ' + args.path_output)
    with open(args.path_output, 'w') as f:
        f.write('{N} {D}\n'.format(N=N, D=D))
        for word, vec in zip(vocab, vecs):
            f.write('{word} {vec}\n'.format(word=word,
                                            vec=' '.join(str(v) for v in vec)))

    return 0
예제 #18
0
def encryptData(key, data):
 """Encrypts the apk data using the specified AES key"""
 aes = AES.new(key, AES.MODE_ECB)
 return ''.join(aes.encrypt(util.pad(c, constants.paddingSize)) for c in util.chunk(data, constants.blockSize))
예제 #19
0
    logger.info(f"Finding {N} farthest away from the mean")
    oddities = nlargest(N, zip(dist, util.image_ids()))

    logger.info(f"Finished histogram mean outliers")
    return oddities



if __name__ == "__main__":
    import time, timeit
    if True:

        a1, a2, a3 = time.time(), time.process_time(), timeit.default_timer()
        result = frequency_mean(10)
        b1, b2, b3 = time.time(), time.process_time(), timeit.default_timer()

        #print(b1-a1, b2-a2, b3-a3)

        #print(result)

        with Figure(1, 1) as fig:
            ax = fig.ax[0, 0]

            for error, key in result:
                img = util.pad(util.image(key), 100, 100)
                ax.imshow(img, cmap="gray")

                fig.update(2)
                input("Press any key to continue")
예제 #20
0
            img = center(img)

            ax.imshow(img, cmap="gray")

            fig.update(1)

    #--- Fixed shift

    if False:
        with Figure(1, 1) as fig:
            ax = fig.ax[0, 0]

            key = ["3337", "1590", "1377"][0]
            for img in util.images():
                img = util.pad(img, 100, 100)

                # angle = PCA_angle(img) / tau * 360
                # img = rotate(img, angle=angle)

                img = shift(img, 20, 50)

                ax.clear()
                ax.imshow(img, cmap="gray")

                fig.update()

    #--- PCA angles

    if False:
        for img in util.progress(util.images()):
예제 #21
0
    def forward(self, input_sentences, input_sentence_length,
                input_conversation_length, input_masks):
        """
        Args:
            input_sentences: (Variable, LongTensor) [num_sentences, seq_len]
            target_sentences: (Variable, LongTensor) [num_sentences, seq_len]
        Return:
            decoder_outputs: (Variable, FloatTensor)
                - train: [batch_size, seq_len, vocab_size]
                - eval: [batch_size, seq_len]
        """
        num_sentences = input_sentences.size(0)
        max_len = input_conversation_length.max().item()

        # encoder_outputs: [num_sentences, max_source_length, hidden_size * direction]
        # encoder_hidden: [num_layers * direction, num_sentences, hidden_size]
        # encoder_outputs, encoder_hidden = self.encoder(input_sentences,
        #                                                input_sentence_length)
        all_encoder_layers, _ = self.encoder(input_sentences,
                                             token_type_ids=None,
                                             attention_mask=input_masks)

        bert_output = []
        for idx in range(self.config.num_bert_layers):
            layer = all_encoder_layers[idx]
            bert_output.append(layer[:, 0, :])
        bert_output = torch.stack(bert_output, dim=1)
        bert_output = torch.mean(bert_output, dim=1, keepdim=False)

        # encoder_hidden: [num_sentences, num_layers * direction * hidden_size]
        encoder_hidden = bert_output

        # pad and pack encoder_hidden
        start = torch.cumsum(
            torch.cat((to_var(input_conversation_length.data.new(1).zero_()),
                       input_conversation_length[:-1])), 0)

        # encoder_hidden: [batch_size, max_len, num_layers * direction * hidden_size]
        encoder_hidden = torch.stack([
            pad(encoder_hidden.narrow(0, s, l), max_len) for s, l in zip(
                start.data.tolist(), input_conversation_length.data.tolist())
        ], 0)

        # context_outputs: [batch_size, max_len, context_size]
        context_outputs, context_last_hidden = self.context_encoder(
            encoder_hidden, input_conversation_length)

        # flatten outputs
        # context_outputs: [num_sentences, context_size]
        context_outputs = torch.cat([
            context_outputs[i, :l, :]
            for i, l in enumerate(input_conversation_length.data)
        ])

        context_outputs = self.dropoutLayer(context_outputs)

        # project context_outputs to decoder init state
        decoder_init = self.context2decoder(context_outputs)

        output = self.decoder2output(decoder_init)

        return output
예제 #22
0
def denoise(flow_h5_path, pv_path, output_path, pv_series=None, surface_size=7, method='frames', interpolate=False,
            vector_mult=1, frame_filter=None, start_from=None, stop_at=None, device='cuda', overwrite=True):
    """
    _filters labels with a 3_d gaussian.
    :param flow_h5_path: String tuple. Tuple containing paths to (flow_forward.h5, flow_backward.h5)
    :param pv_path: String. Path to previous iteration folder.
    :param output_path: String. Output folder.
    :param pv_series: Int array. Frames to consider when constructing th 3_d volume to be filtered. Ex [0, 5] = 0 - current frame, 5 - frames at -5,+5 distance.
    :param surface_size: Float. Filter size on the YX axes, in pixels.
    :param method: String. Method for 3D volume construction. Possible values: 'self' - project current frame along flow vectors, 'frames' - project series frame.
    :param interpolate: Bool. Bilinearly interpolate coordinates during flow projection. Might be useful for low resolution data and long propagation distances.
    :param vector_mult: Float. Multiplies default flow vectors by the specified amount.
    :param frame_filter: Function. Receives frame number and returns 'True' for any frame that should be skipped. Useful for evaluation.
    :param start_from: Int. Start from frame number.
    :param stop_at: Int. Stop on frame number.
    :param device: py_torch device to run on.
    :param overwrite: Bool. Overwrite previous results.
    :return: 0 if completed successfully.
    """
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    if pv_series is None:
        pv_series = [0]

    # meta
    pv_maps = sorted(os.listdir(pv_path))
    pv_no = [_frame_no(name) for name in pv_maps]
    noy, nox, noc = toio.load(os.path.join(pv_path, pv_maps[0]), device=device)['map'].shape
    basename = _basename(pv_maps[0])
    pv_series = sorted(pv_series)
    depth = pv_series[-1]
    nof = pv_no[-1]
    cur_times = sum([i == 0 for i in pv_series])
    pv_series = [i for i in pv_series if i > 0]
    depth_size = len(pv_series) * 2 + cur_times
    depth_sigma = depth_size / 2.5
    surface_sigma = surface_size / 2.5
    if start_from is None:
        start_from = pv_no[0]
    if stop_at is None or stop_at > pv_no[-1]:
        stop_at = pv_no[-1]

    # configure filter
    pad = util.pad((0, surface_size, surface_size))
    denoiser = blocks.GaussBlur3d((depth_size, surface_size, surface_size), (depth_sigma, surface_sigma, surface_sigma), pad=pad, device=device)

    # process
    for i in range(max(depth + pv_no[0], start_from), min(nof - depth + 1, stop_at)):
        path = os.path.join(output_path, basename + '_{:06d}'.format(i) + '.npz')

        # only compute frames that aren't filtered
        if frame_filter is not None and frame_filter(i):
            continue

        # overwrite
        if not overwrite and os.path.exists(path):
            continue

        # verbose
        print('{:06d}'.format(i))

        # load flows
        flow_data_fwd = h5py.File(flow_h5_path[0], 'r')
        flow_data_fwd = flow_data_fwd['flow'][i:i + depth]
        flow_data_fwd = torch.from_numpy(flow_data_fwd).to(device).flip(3)
        flow_data_bkw = h5py.File(flow_h5_path[1], 'r')
        flow_data_bkw = flow_data_bkw['flow'][i - depth:i]
        flow_data_bkw = torch.from_numpy(flow_data_bkw).to(device).flip(0).flip(3)

        # load map
        vote_map = toio.load(os.path.join(pv_path, pv_maps[i - pv_no[0]]), device=device)['votes'].type(torch.float)

        # 3d frame-time voxel votes
        if method == 'self':
            fw_maps = _flow_align(flow_data_fwd, vote_map, pv_series, vector_mult=vector_mult, interpolate=interpolate)
            bk_maps = _flow_align(flow_data_bkw, vote_map, pv_series, vector_mult=vector_mult, interpolate=interpolate)
        elif method == 'frames':
            fw_maps = torch.zeros((noy, nox, noc, len(pv_series)), device=device, dtype=torch.float)
            bk_maps = torch.zeros((noy, nox, noc, len(pv_series)), device=device, dtype=torch.float)
            for j, k in enumerate(pv_series):
                pre_map = toio.load(os.path.join(pv_path, pv_maps[i - k - pv_no[0]]), device=device)['votes']
                nxt_map = toio.load(os.path.join(pv_path, pv_maps[i + k - pv_no[0]]), device=device)['votes']
                bk_maps[..., j] = _flow_end_map(flow_data_fwd[depth - k:depth], pre_map, vector_mult=vector_mult, interpolate=interpolate)
                fw_maps[..., j] = _flow_end_map(flow_data_bkw[depth - k:depth], nxt_map, vector_mult=vector_mult, interpolate=interpolate)
            del pre_map, nxt_map
        elif method == 'noalign':
            fw_maps = torch.zeros((noy, nox, noc, len(pv_series)), device=device, dtype=torch.float)
            bk_maps = torch.zeros((noy, nox, noc, len(pv_series)), device=device, dtype=torch.float)
            for j, k in enumerate(pv_series):
                bk_maps[..., j] = toio.load(os.path.join(pv_path, pv_maps[i - k - pv_no[0]]), device=device)['votes']
                fw_maps[..., j] = toio.load(os.path.join(pv_path, pv_maps[i + k - pv_no[0]]), device=device)['votes']
        else:
            raise NotImplementedError
        votes = torch.cat((bk_maps.flip(3), vote_map.unsqueeze(-1).expand(-1, -1, -1, cur_times), fw_maps), dim=3)

        # denoise
        votes = votes.permute((2, 3, 0, 1))
        votes = votes.unsqueeze(0)
        votes = denoiser(votes).squeeze()
        votes = votes.permute((1, 2, 0))

        # extract final map
        map = torch.argmax(votes, 2)
        map = classmap.logical(map, noc)

        # save
        np.savez_compressed(path, map=map.cpu().numpy().astype(bool), votes=votes.cpu().numpy())

    # copy the rest
    if frame_filter is None:
        for i in list(range(depth)) + list(range(len(pv_maps) - depth, len(pv_maps))):
            copyfile(os.path.join(pv_path, pv_maps[i]), os.path.join(output_path, pv_maps[i]))

    # verbose
    # print('done!')

    return 0
예제 #23
0
 def sendMoveToKafra(self, gamesocket, index, quantity):
     packet='\x94\x00' + util.pad(3) + util.packWord(index) + util.pad(12) +util.packWord(quantity)+ util.pad(2)
     gamesocket.send(packet)
예제 #24
0
with Figure(2, 5) as fig:
    axes = get_axes(2, 5, fig)

    arrow(axes[..., 2])

    for col_index in [0, 1, 3, 4]:
        clean(axes[..., col_index])
        square(axes[..., col_index])

    angles = lambda: (util.images().pad().map(transform.PCA_angle))

    # block 1
    img_mean = util.mean(util.images().pad(100, 100))
    img_3 = [
        util.pad(util.image(i), 100, 100) for i in ["1377", "1590", "9728"]
    ]

    samples = [img_mean] + img_3

    block_imshow(1, axes, samples)

    # block 2
    angle_mean = util.angle_mean(angles())
    angle_3 = list(map(transform.PCA_angle, img_3))

    transformed_samples = [angle_mean] + angle_3

    block_circle(2, axes, transformed_samples)

    fig.save("figure/example.pdf")
예제 #25
0
 def sendAreaSkill(self, gamesocket, skillNum, level, x, y):
     #packet=struct.pack('2s9xH5xH2x4s', '\x72\x00', level, skillNum, targetID)
     packet='\x13\x01'+util.pad(3)+util.packWord(level) + util.pad(8) + util.packWord(skillNum) + util.pad(12) + util.packWord(x) + util.pad(7) + util.packWord(y)
     
     gamesocket.send(packet)
예제 #26
0
def encryptData(key, data):
    """Encrypts the apk data using the specified AES key"""
    aes = AES.new(key, AES.MODE_ECB)
    return ''.join(
        aes.encrypt(util.pad(c, constants.paddingSize))
        for c in util.chunk(data, constants.blockSize))
예제 #27
0
def go(arg):

    tbw = SummaryWriter(log_dir=arg.tb_dir)

    transform = Compose([
        Lambda(lambda x: CenterCrop(min(x.size))(x)),
        Resize(size=(arg.img_size, arg.img_size)),
        ToTensor()
    ])

    imdir = arg.data_dir + os.sep + 'val2017'
    anfile = arg.data_dir + os.sep + 'annotations' + os.sep + 'captions_val2017.json'

    coco_data = coco.CocoCaptions(root=imdir,
                                  annFile=anfile,
                                  transform=transform)

    ## Make a dictionary

    util.ensure(arg.cache_dir)
    if os.path.isfile(arg.cache_dir + os.sep + 'i2w.pkl'):
        with open(arg.cache_dir + os.sep + 'i2w.pkl', 'rb') as file:
            i2w = pickle.load(file)
        with open(arg.cache_dir + os.sep + 'w2i.pkl', 'rb') as file:
            w2i = pickle.load(file)
        print('Word indices loaded.')
    else:
        print('Creating word indices')  # Why is this so slow?

        dist = Counter()
        for i in tqdm.trange(len(coco_data)):
            for caption in coco_data[i][1]:
                dist.update(util.tokenize(caption))

        vocab = dist.most_common(arg.max_vocab - len(EXTRA_SYMBOLS))

        i2w = EXTRA_SYMBOLS + [w[0] for w in vocab]
        w2i = {word: ix for ix, word in enumerate(i2w)}

        with open(arg.cache_dir + os.sep + 'i2w.pkl', 'wb') as file:
            pickle.dump(i2w, file)
        with open(arg.cache_dir + os.sep + 'w2i.pkl', 'wb') as file:
            pickle.dump(w2i, file)

    vocab_size = len(i2w)
    print('vocabulary size', vocab_size)
    print('top 100 words:', i2w[:100])

    def decode(indices):

        sentence = ''
        for id in indices:
            # if id == PAD:
            #     break
            sentence += i2w[id] + ' '

        return sentence

    ## Set up the models
    embedding = torch.nn.Embedding(num_embeddings=vocab_size,
                                   embedding_dim=arg.embedding_size)

    if arg.mode != Mode.style:
        img_enc = models.ImEncoder(in_size=(arg.img_size, arg.img_size),
                                   zsize=arg.latent_size)
        img_dec = models.ImDecoder(in_size=(arg.img_size, arg.img_size),
                                   zsize=arg.latent_size)

        seq_enc = models.SeqEncoder(vocab_size=vocab_size,
                                    embedding=embedding,
                                    zsize=arg.latent_size)
        seq_dec = models.SeqDecoder(vocab_size=vocab_size,
                                    embedding=embedding,
                                    zsize=arg.latent_size)

        mods = [img_enc, img_dec, seq_enc, seq_dec]
    else:
        img_enc = models.ImEncoder(in_size=(arg.img_size, arg.img_size),
                                   zsize=arg.latent_size)
        img_sty = models.ImEncoder(in_size=(arg.img_size, arg.img_size),
                                   zsize=arg.latent_size)
        img_dec = models.ImDecoder(in_size=(arg.img_size, arg.img_size),
                                   zsize=arg.latent_size * 2)

        seq_enc = models.SeqEncoder(vocab_size=vocab_size,
                                    embedding=embedding,
                                    zsize=arg.latent_size)
        seq_sty = models.SeqEncoder(vocab_size=vocab_size,
                                    embedding=embedding,
                                    zsize=arg.latent_size)
        seq_dec = models.SeqDecoder(vocab_size=vocab_size,
                                    embedding=embedding,
                                    zsize=arg.latent_size * 2)

        mods = [img_enc, img_dec, img_sty, seq_enc, seq_dec, seq_sty]

    if torch.cuda.is_available():
        for model in mods:
            model.cuda()

    #- The standard dataloader approach doesn't seem to work with the captions, so we'll do our own batching.
    #  It's a little slower, probably, but it won't be the bottleneck
    params = []
    for model in mods:
        params.extend(model.parameters())
    optimizer = Adam(params, lr=arg.lr)

    instances_seen = 0

    for e in range(arg.epochs):
        print('epoch', e)
        for fr in tqdm.trange(0, len(coco_data), arg.batch_size):
            if arg.instance_limit is not None and fr > arg.instance_limit:
                break

            to = min(len(coco_data), fr + arg.batch_size)

            images = []
            captions = []

            for i in range(fr, to):
                images.append(coco_data[i][0].unsqueeze(0))
                captions.append(random.choice(
                    coco_data[i]
                    [1]))  # we choose one of the available captions at random

            imbatch = torch.cat(images, dim=0)
            b, c, w, h = imbatch.size()

            capbatch = []  # to integer sequence
            for caption in captions:
                capbatch.append(util.intseq(util.tokenize(caption), w2i))

            capbatch, lengths = util.pad(capbatch)

            # Created shifted versions
            b, s = capbatch.size()

            # Input for the decoder
            cap_teacher = torch.cat(
                [torch.ones(b, 1, dtype=torch.long), capbatch], dim=1)
            cap_out = torch.cat(
                [capbatch, torch.zeros(b, 1, dtype=torch.long)], dim=1)

            lengths = torch.LongTensor(lengths)

            if torch.cuda.is_available():
                imbatch = imbatch.cuda()

                capbatch = capbatch.cuda()
                cap_teacher = cap_teacher.cuda()
                cap_out = cap_out.cuda()

                lengths = lengths.cuda()

            imbatch = Variable(imbatch)
            capbatch = Variable(capbatch)
            cap_teacher = Variable(cap_teacher)
            cap_out = Variable(cap_out)
            lengths = Variable(lengths)

            zimg = img_enc(imbatch)
            zcap = seq_enc(capbatch, lengths)

            kl_img = util.kl_loss(*zimg)
            kl_cap = util.kl_loss(*zcap)

            zimg_sample = util.sample(*zimg)
            zcap_sample = util.sample(*zcap)

            if arg.mode == Mode.style:
                zimg_sty = img_sty(imbatch)
                zcap_sty = seq_sty(capbatch, lengths)

                kl_img_sty = util.kl_loss(*zimg_sty)
                kl_cap_sty = util.kl_loss(*zcap_sty)

                zimg_sample_sty = util.sample(*zimg_sty)
                zcap_sample_sty = util.sample(*zcap_sty)

                zimg_sample = torch.cat([zimg_sample, zimg_sample_sty], dim=1)
                zcap_sample = torch.cat([zcap_sample, zcap_sample_sty], dim=1)

            rec_imgimg = img_dec(zimg_sample)
            rl_imgimg = binary_cross_entropy(rec_imgimg, imbatch,
                                             reduce=False).view(b,
                                                                -1).sum(dim=1)

            rec_capcap = seq_dec(zcap_sample, cap_teacher,
                                 lengths + 1).transpose(1, 2)
            rl_capcap = nll_loss(rec_capcap, cap_out,
                                 reduce=False).view(b, -1).sum(dim=1)

            if arg.mode != Mode.independent:
                rec_capimg = img_dec(zcap_sample)
                rl_capimg = binary_cross_entropy(rec_capimg,
                                                 imbatch,
                                                 reduce=False).view(
                                                     b, -1).sum(dim=1)

                rec_imgcap = seq_dec(zimg_sample, cap_teacher,
                                     lengths + 1).transpose(1, 2)
                rl_imgcap = nll_loss(rec_imgcap, cap_out,
                                     reduce=False).view(b, -1).sum(dim=1)

            loss_img = rl_imgimg + kl_img
            loss_cap = rl_capcap + kl_cap

            if arg.mode == Mode.coupled:
                loss_img = loss_img + rl_capimg + kl_img
                loss_cap = loss_cap + rl_imgcap + kl_cap

            if arg.mode == Mode.style:
                loss_img = loss_img + kl_img_sty
                loss_cap = loss_cap + kl_cap_sty

            loss = loss_img.mean() + loss_cap.mean()

            #- backward pass
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            instances_seen += b

            tbw.add_scalar('score/img/kl', float(kl_img.mean()),
                           instances_seen)
            tbw.add_scalar('score/imgimg/rec', float(rl_imgimg.mean()),
                           instances_seen)
            tbw.add_scalar('score/cap/kl', float(kl_cap.mean()),
                           instances_seen)
            tbw.add_scalar('score/capcap/rec', float(rl_capcap.mean()),
                           instances_seen)
            tbw.add_scalar('score/loss', float(loss), instances_seen)

            if arg.mode != Mode.independent:
                tbw.add_scalar('score/capimg/rec', float(rl_capimg.mean()),
                               instances_seen)
                tbw.add_scalar('score/imgcap/rec', float(rl_imgcap.mean()),
                               instances_seen)

        # Interpolate
        zpairs = []
        for r in range(REP):

            print('Interpolation, repeat', r)

            l = arg.latent_size if arg.mode != Mode.style else arg.latent_size * 2
            z1, z2 = torch.randn(2, l)
            if torch.cuda.is_available():
                z1, z2 = z1.cuda(), z2.cuda()

            zpairs.append((z1, z2))

            zs = util.slerp(z1, z2, 10)

            print('== sentences (temp={}) =='.format(TEMPS[r]))
            sentences = seq_dec.sample(z=zs, temperature=TEMPS[r])

            for s in sentences:
                print('   ', decode(s))

        print('== images ==')

        util.interpolate(zpairs, img_dec, name='interpolate.{}'.format(e))