def test_rnn_bidirectional():
    hidden_size = 20
    input_size = 10
    seq_len = 5
    batch_size = 7

    for bias in (True, False):
        for batch_first in (True, False):
            input = var(T.randn(batch_size, seq_len,
                                input_size)) if batch_first else var(
                                    T.randn(seq_len, batch_size, input_size))
            hx = None
            rnn = SubLSTM(input_size,
                          hidden_size,
                          num_layers=3,
                          bias=bias,
                          batch_first=batch_first,
                          bidirectional=True)

            outputs = []
            for i in range(6):
                output, hx = rnn(input, hx)
                outputs.append(output)

            T.stack(outputs).sum().backward()

            assert hx[-1][-1][0].size() == T.Size([batch_size, hidden_size])
            assert hx[-1][-1][1].size() == T.Size([batch_size, hidden_size])
Ejemplo n.º 2
0
def fit(model, train_loader, test_loader, learning_rate, epochs):
    criterion = nn.BCELoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    for e in range(epochs):
        correct = 0

        for batch_idx, (X_batch, y_batch) in enumerate(train_loader):
            var_X_batch = var(X_batch).float()
            var_y_batch = var(y_batch).float()

            output = model(var_X_batch)
            loss = criterion(output, var_y_batch)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            pred = [1 if i > 0.5 else 0 for i in output]
            for i in range(len(pred)):
                if int(pred[i]) == int(var_y_batch[i]): correct += 1
            if batch_idx % 20 == 0:
                print(
                    'Epoch : {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\t Accuracy:{:.3f}%'
                    .format(
                        e, batch_idx * len(X_batch), len(train_loader.dataset),
                        100. * batch_idx / len(train_loader), loss.data[0],
                        float(correct * 100) / float(var_y_batch.shape[0] *
                                                     (batch_idx + 1))))
        print()
        evaluate(model, test_loader, 32)
Ejemplo n.º 3
0
def generate_data(batch_size, bits=7, cuda=-1):
    max_N = 2**(bits - 1) + 1
    N = np.random.randint(1, max_N)
    k = np.random.randint(1, N + 1)

    input_data = np.zeros((batch_size, N + 3 + k + 1, bits + 2),
                          dtype=np.float32)
    target_output = np.zeros((batch_size, N + 3 + k + 1, bits + 2),
                             dtype=np.float32)

    for i in range(batch_size):
        inp_sequence, sorted_top_k = get_example(N, k, bits)
        input_data[i, :N, :bits] = inp_sequence
        target_output[i, N + 3:N + 3 + k, :bits] = sorted_top_k
        input_data[i, N + 1, :bits] = get_bit_sequence(np.array([k]), bits)

    input_data[:, N, -2] = 1
    input_data[:, N + 2, -1] = 1
    target_output[:, N + 3 + k, -1] = 1

    input_data = T.from_numpy(input_data)
    target_output = T.from_numpy(target_output)

    if cuda != -1:
        input_data = input_data.cuda(cuda)
        target_output = target_output.cuda(cuda)

    return var(input_data), var(target_output)
Ejemplo n.º 4
0
def generate_data(
    batch_size, total_length, input_length, npy_idx,
    base_path='/home/kaji/data/frames_cleanpass/np_out/',
    cuda=-1, loop_mode=False):
    if loop_mode:
        flag_list = [2] * total_length
    else:
        flag_list = [1] * total_length
    for i in range(input_length):
        flag_list[i] = 0
    if input_length < total_length:
        flag_list[input_length] = 1
    npy_path = base_path + str(npy_idx) + ".npy"
    npy_values = np.load(npy_path)
    input_data = []
    target_output = []
    for i in range(batch_size):
        start_idx = random.randrange(0, len(npy_values)-total_length-3)
        in_data = npy_values[start_idx:start_idx+total_length]
        out_data = npy_values[(start_idx+1):(start_idx+1)+total_length]
        input_data.append(in_data)
        target_output.append(out_data)
    input_data = np.stack(input_data, axis=0).astype("float32")
    target_output = np.stack(target_output, axis=0).astype("float32")
    input_data = T.from_numpy(input_data)
    target_output = T.from_numpy(target_output)
    if cuda != -1:
        input_data = input_data.cuda()
        target_output = target_output.cuda()
    return var(input_data), var(target_output), flag_list
Ejemplo n.º 5
0
    def init_params(self, bs=None):
        """
        if self.cell_type == "LSTM":
            self.hidden_c = (self.init_hidden(bs), self.init_hidden(bs))
            self.hidden_q = (self.init_hidden(bs), self.init_hidden(bs))
            self.hidden_coatt = (self.init_hidden_coatt(
                bs), self.init_hidden_coatt(bs))
            self.hidden_bmod1 = (self.init_hidden_bmod(bs),
                                 self.init_hidden_bmod(bs))
        """
        if self.cell_type == "GRU":
            self.hidden_c, self.hidden_q = self.init_hidden(
                bs), self.init_hidden(bs)
            self.hidden_coatt = self.init_hidden_coatt(bs)
            self.hidden_bmod1 = self.init_hidden_bmod(bs)
        else:
            raise TypeError("Invalid Cell Type - use LSTM or GRU")

        if torch.cuda.is_available():
            self.beta = var(
                torch.ones((
                    bs,
                    self.c_size,
                )) * (1 / self.c_size)).cuda()
        else:
            self.beta = var(
                torch.ones((
                    bs,
                    self.c_size,
                )) * (1 / self.c_size))
Ejemplo n.º 6
0
def train(net, trainloader, trainset, criterian):
    '''Training the network
    '''
    learning_rate = 1e-3
    epoches = 200

    #optimizer = optim.SGD(net.parameters(), lr=learning_rate);
    optimizer = optim.Adam(net.parameters())

    for iteration in range(epoches):
        running_loss = 0
        running_acc = 0

        for (img, label) in trainloader:
            img = var(img).cuda()
            label = var(label).cuda()
            # forward pass
            optimizer.zero_grad()
            output = net(img)
            loss = criterian(output, label)
            # backward pass and update the net
            loss.backward()
            optimizer.step()
            # compute the training loss and training Accuracy
            running_loss += loss.data[0]
            _, predict = torch.max(output, 1)
            correct_num = (predict == label).sum()
            running_acc += correct_num.data[0]
        #end_for

        running_loss /= len(trainset)
        running_acc /= len(trainset)
        print('Train[%d / %d] loss: %.5f, Acc: %.2f' %
              (iteration + 1, epoches, running_loss, 100 * running_acc))
Ejemplo n.º 7
0
    def __getitem__(self, idx):
        imgstr = self.imgstrs[idx]
        img1str = os.path.join(self.readdir, imgstr)
        img2str = os.path.join(self.readdir, imgstr[:-6] + '_R.bmp')

        corder = 0
        if corder == 0 or self.transform is None:
            img1 = cv2.imread(img1str)
            img2 = cv2.imread(img2str)
        else:
            img1 = cv2.imread(img2str)
            img2 = cv2.imread(img1str)

        img3 = cv2.imread(os.path.join(self.readdir, imgstr[:-6] + '_M.bmp'))

        img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB).astype(np.float32)
        img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB).astype(np.float32)
        img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB).astype(np.float32)
        if self.transform is not None:
            # random shift and crop
            cropx = random.randint(2, 20)
            cropy = random.randint(2, 20)

            shift = random.randint(0, 2)
            ifx = random.randint(0, 1)
            shiftx = 0
            shifty = 0

            img3 = img3[cropy:cropy + 128, cropx:cropx + 128, :]
            img1 = img1[cropy - shifty:cropy + 128 - shifty,
                        cropx - shiftx:cropx + 128 - shiftx, :]
            img2 = img2[cropy + shifty:cropy + 128 + shifty,
                        cropx + shiftx:cropx + 128 + shiftx, :]

            flipH = random.randint(0, 1)
            flipV = random.randint(0, 1)
            if flipH == 1:
                img1 = np.flip(img1, 0)
                img2 = np.flip(img2, 0)
                img3 = np.flip(img3, 0)
            if flipV == 1:
                img1 = np.flip(img1, 1)
                img2 = np.flip(img2, 1)
                img3 = np.flip(img3, 1)

        # Then prepare for the RGB2GRAY
        img1 = (
            cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY).astype(np.float32) -
            cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY).astype(np.float32)).reshape(
                [img2.shape[0], img2.shape[1], 1])
        # IPython.embed()
        # exit()
        # Finish te transfer from RGB to GRAY, and img1 is no longer needed
        # I have to note that for PIL,  what we get is RGB, but in cv2, what we get is BGR
        return var(torch.from_numpy(img1.transpose(2,0,1).astype(np.float32) / 255.0)), var(torch.from_numpy(img2.transpose(2,0,1).astype(np.float32) / 255.0)), \
               var(torch.from_numpy(img3.transpose(2,0,1).astype(np.float32) / 255.0))
Ejemplo n.º 8
0
def evaluate(model, test_loader, batch_size):
    correct = 0
    for test_imgs, test_labels in test_loader:
        test_imgs = var(test_imgs).float()
        var_y_batch = var(test_labels).float()
        output = model(test_imgs)
        pred = [1 if i > 0.5 else 0 for i in output]
        for i in range(len(pred)):
            if int(pred[i]) == int(var_y_batch[i]): correct += 1
    print("Test accuracy:{:.3f}% ".format(float(correct) * 100 / 5000))
    def __getitem__(self, idx):
        imgstr = self.imgstrs[idx]

        imgs = []
        for i in range(7):
            fname = os.path.join(self.readdir, imgstr, 'im%d.png' % (i + 1))
            imgs.append(np.array(pilImg.open(fname)))

        if self.transform is not None:
            # random shift and crop
            cropx = random.randint(2, 100)
            cropy = random.randint(2, 100)

            for i in range(7):
                imgs[i] = imgs[i][cropy:cropy + 128, cropx:cropx + 128, :]

            flipH = random.randint(0, 1)
            flipV = random.randint(0, 1)
            if flipH == 1:
                for i in range(7):
                    imgs[i] = np.flip(imgs[i], 0)

            if flipV == 1:
                for i in range(7):
                    imgs[i] = np.flip(imgs[i], 1)
        return [
            var(
                torch.from_numpy(
                    tmpimg.transpose(2, 0, 1).astype(np.float32) / 255.0))
            for tmpimg in imgs
        ]
Ejemplo n.º 10
0
    def forward(self, word_index_list):
        assert len(word_index_list) == self.batch_size

        lookup_block = torch.LongTensor(word_index_list)
        lookup_block = var(lookup_block, requires_grad=False)
        if use_cuda: lookup_block = lookup_block.cuda()
        assert lookup_block.size() == (self.batch_size, max_words)

        sent_block = self.W_wrd(lookup_block).transpose(1,2)
        assert sent_block.size() == (self.batch_size, self.d_wrd, max_words)
        sent_block = sent_block.contiguous()

        input_to_conv = sent_block.view(self.batch_size, 1, self.d_wrd, max_words)
        x1 = F.relu(self.word_level_conv(input_to_conv))
        assert x1.size() == (self.batch_size, self.clu1, 1, max_words)
        y1 = x1.view(self.batch_size, self.clu1, max_words)

        y2 = self.word_level_maxpool(y1)
        assert y2.size() == (self.batch_size,self.clu1,1)

        y2 = y2.view(self.batch_size, self.clu1)
        if self.use_dropout:
            y2 = self.dropout(y2)
        if self.hlu == 0:
            y2 = self.fc1(y2)
        else:
            y2 = F.relu(self.fc1(y2))
            y2 = self.fc2(y2)
        assert y2.size() == (self.batch_size, self.num_classes)
        return y2
Ejemplo n.º 11
0
def RunSingleTest(net):
    img = im.open(args.single)
    if args.tencrop:
        img = img.resize((256, 256), im.LANCZOS)
        img = tv.transforms.TenCrop((224, 224))(img)
        img = torch.stack(
            [normalize(tv.transforms.ToTensor()(crop)) for crop in img])
    else:
        img = img.resize((224, 224), im.LANCZOS)
        img = normalize(tv.transforms.ToTensor()(img))

    img = var(img.view(1, *img.shape))

    if not args.cpu:
        img = img.cuda()

    if args.tencrop:
        bs, ncrops, c, h, w = img.size()
        img = img.view(-1, c, h, w)

    ret = net(img)

    if args.tencrop:
        ret = ret.view(bs, ncrops, -1).mean(1)

    utils.WriteSinglePred(ret, sys.stdout, args.csv)

    if args.output_file is not None:
        with open(args.output_file, 'w') as f:
            utils.WriteSinglePred(ret, f, args.csv)
Ejemplo n.º 12
0
    def forward(self, source, target, source_lengths, target_lengths):
        attentions = []
        encoded, hidden = self.encoder(source, source_lengths)
        hidden = tuple([h[:self.decoder.n_layers] for h in hidden])
        batch_size = len(source)

        outputs = cuda(T.zeros(batch_size, max(target_lengths),
                               self.decoder.output_size),
                       gpu_id=self.gpu_id)
        # todo: use tensor instead of numpy
        input = cudavec(np.array([SOS] * batch_size, dtype=np.long),
                        gpu_id=self.gpu_id).unsqueeze(1)

        # manually unrolled
        for x in range(max(target_lengths)):
            o, hidden, att = self.decoder(input, encoded, hidden)
            outputs[:, x, :] = o
            attentions.append(att.data.cpu().numpy())

            if self._teacher_force():
                input = target[:, x].unsqueeze(1).long()
            else:
                input = var(o.data.topk(1)[0].squeeze(1).long())

        return outputs, np.array(attentions)
Ejemplo n.º 13
0
    def forward(self,
                source,
                target,
                source_lengths,
                target_lengths,
                hidden=None):
        attentions = []

        encoded, controller_hidden = self.controller(source, source_lengths,
                                                     hidden)

        # the encoder LSTM's last hidden layer
        hidden = tuple(
            [h[:self.decoder.n_layers] for h in controller_hidden[0]])
        batch_size = len(source)

        outputs = cuda(T.zeros(batch_size, max(target_lengths),
                               self.decoder.output_size),
                       gpu_id=self.gpu_id)
        input = cuda(T.LongTensor([SOS] * batch_size),
                     gpu_id=self.gpu_id).unsqueeze(1)

        # manually unrolled
        for x in range(max(target_lengths)):
            o, hidden, att = self.decoder(input, encoded, hidden)
            outputs[:, x, :] = o
            # attentions.append(att.data.cpu().numpy())

            if self._teacher_force():
                input = target[:, x].unsqueeze(1).long()
            else:
                input = var(o.data.topk(1)[0].squeeze(1).long())

        return outputs, np.array(attentions), controller_hidden
Ejemplo n.º 14
0
def test_dnc_rnn():
    input = var(T.randn(10, 50, 64))
    model = DNC('RNN', 64, num_layers=1, mem_size=5, read_heads=2, gpu_id=-1)

    o, h = model(input)

    assert o.size() == input.size()
Ejemplo n.º 15
0
  def read_from_sparse_memory(self, memory, indexes, keys, least_used_mem, usage):
    b = keys.size(0)
    read_positions = []

    # we search for k cells per read head
    for batch in range(b):
      distances, positions = indexes[batch].search(keys[batch])
      read_positions.append(positions)
    read_positions = T.stack(read_positions, 0)

    # add least used mem to read positions
    # TODO: explore possibility of reading co-locations or ranges and such
    (b, r, k) = read_positions.size()
    read_positions = var(read_positions).squeeze(1).view(b, -1)

    # no gradient here
    # temporal reads
    (b, m, w) = memory.size()
    # get the top KL entries
    max_length = int(least_used_mem[0, 0].data.cpu().numpy()) if not self.mem_limit_reached else (m-1)

    # differentiable ops
    # append forward and backward read positions, might lead to duplicates
    read_positions = T.cat([read_positions, least_used_mem], 1)
    read_positions = T.clamp(read_positions, 0, max_length)

    visible_memory = memory.gather(1, read_positions.unsqueeze(2).expand(b, self.c, w))

    read_weights = σ(θ(visible_memory, keys), 2)
    read_vectors = T.bmm(read_weights, visible_memory)
    read_weights = T.prod(read_weights, 1)

    return read_vectors, read_positions, read_weights, visible_memory
Ejemplo n.º 16
0
def WritePred(net, t_loader, filename, cuda=True, tencrop=False, out_arr=None):
    assert t_loader.batch_size == 1
    with open(filename, 'w') as f:
        tl = len(t_loader)
        idx = 0
        for data in tqdm(t_loader, file=sys.stdout):
            in_img = var(data)
            if cuda:
                in_img = in_img.cuda()
            if tencrop:
                bs, ncrops, c, h, w = in_img.size()
                in_img = in_img.view(-1, c, h, w)
            ret = net(in_img)
            if tencrop:
                ret = ret.view(bs, ncrops, -1).mean(1)
            _, p3 = torch.topk(ret, 3)
            p3 = p3.view(-1).cpu().data.numpy()
            print('test/' + t_loader.dataset.getName(idx),
                  p3[0],
                  p3[1],
                  p3[2],
                  file=f)
            if out_arr is not None:
                out_arr.append([p3[0], p3[1], p3[2]])
            idx += 1
Ejemplo n.º 17
0
def generate_data(batch_size, bits=6, cuda=-1):
    n = np.random.randint(10, 25 + 1)
    sample_edges = []
    sample_queries = []
    sample_shortest_paths = []
    sample_lengths = []

    for i in range(batch_size):
        edges, query, shortest_paths, length_of_shortest_path = construct_example(
            n, bits)
        sample_edges.append(edges)
        sample_queries.append(query)
        sample_shortest_paths.append(shortest_paths)
        sample_lengths.append(length_of_shortest_path)

    m = sample_edges[0].shape[0] // 2
    # print("m",m)
    length = m + 1 + 1 + 1 + np.max(
        sample_lengths) + 1  # edges + eoe + query + eoq + output + eoo
    size = 2 * bits + 2
    input_data = np.zeros((batch_size, length, size), dtype=np.float32)

    for i in range(batch_size):
        input_data[i, :m, :bits] = sample_edges[i][::2]
        input_data[i, :m, bits:2 * bits] = sample_edges[i][1::2]
        input_data[i, m + 1, :bits] = sample_queries[i][0]
        input_data[i, m + 1, bits:2 * bits] = sample_queries[i][1]

    input_data[:, m, -2] = 1
    input_data[:, m + 2, -1] = 1

    def target_output(actual_output):
        actual_output = actual_output.cpu().detach().numpy()
        target_array = np.zeros((batch_size, length, size), dtype=np.float32)
        for i in range(batch_size):
            # find closest target output
            curr_output = actual_output[i,
                                        m + 3:m + 3 + sample_lengths[i], :-2]
            # print("sample_lengths[i]",sample_lengths[i])
            curr_targets = sample_shortest_paths[i].reshape(
                (len(sample_shortest_paths[i]), -1, 2 * bits))
            # print("curr_output",curr_output)
            # print("curr_targets",curr_targets)
            best_target = np.argmin(
                np.sum(np.abs(curr_targets - curr_output), axis=(1, 2)))
            best_target = curr_targets[best_target]
            # constrct target array
            target_array[i, m + 3:m + 3 + sample_lengths[i], :-2] = best_target
            target_array[i, m + 3 + sample_lengths[i]:, -1] = 1

        target_array = T.from_numpy(target_array)
        if cuda != -1:
            target_array = target_array.cuda(cuda)
        return var(target_array)

    input_data = T.from_numpy(input_data)
    if cuda != -1:
        input_data = input_data.cuda(cuda)
    return var(input_data), target_output
Ejemplo n.º 18
0
 def init_hidden_coatt(self, bs=None):  # for context_attention GRU
     weight_scale = 0.01
     if bs is None:
         bs = self.batch_size
     weight = next(self.parameters()).data
     return var(
         weight.new(self.n_layers * self.dirs, bs,
                    self.hidden_size * 2 * self.dirs)).zero_()
Ejemplo n.º 19
0
 def init_hidden_bmod(self, bs=None):
     weight_scale = 0.01
     if bs is None:
         bs = self.batch_size
     weight = next(self.parameters()).data
     return var(weight.new(self.n_layers, bs,
                           self.hidden_size)) \
         .uniform_(-weight_scale, weight_scale)
Ejemplo n.º 20
0
def test_vision():
    vgg = SimpleVGG(128)
    res = SimpleResNet()

    x = var(torch.rand(8, 3, 256, 256))

    assert vgg(x).size() == (8, 128, 32, 32)
    assert res(x).size() == (8, 128, 32, 32)
Ejemplo n.º 21
0
def test_multilayer_dnc_lstm():
    input = var(T.randn(10, 50, 64))

    model = DNC('LSTM', 64, num_layers=4, mem_size=5, read_heads=4, gpu_id=-1)

    o, h = model(input)

    assert o.size() == input.size()
Ejemplo n.º 22
0
def generate_data(batch_size, length, size, device):
    input_data = np.zeros((batch_size, 2 * length + 1, size), dtype=np.float32)
    target_output = np.zeros(
        (batch_size, 2 * length + 1, size), dtype=np.float32)

    sequence = np.random.binomial(1, 0.5, (batch_size, length, size - 1))

    input_data[:, :length, :size - 1] = sequence
    input_data[:, length, -1] = 1  # the end symbol
    target_output[:, length + 1:, :size - 1] = sequence

    input_data = T.from_numpy(input_data)
    target_output = T.from_numpy(target_output)
    input_data = input_data.to(device)
    target_output = target_output.to(device)

    return var(input_data), var(target_output)
Ejemplo n.º 23
0
def test_cell():
  hidden_size = 20
  input_size = 10

  for bias in (True, False):
    input = var(T.randn(3, input_size))
    hx = var(T.randn(3, hidden_size))
    cx = var(T.randn(3, hidden_size))

    cell = SubLSTMCell(input_size, hidden_size, bias=bias)

    for i in range(6):
      (hx, cx) = cell(input, (hx, cx))

    hx.sum().backward()
    assert hx.size() == T.Size([3, hidden_size])
    assert cx.size() == T.Size([3, hidden_size])
Ejemplo n.º 24
0
def test_cell():
    hidden_size = 20
    input_size = 10

    for bias in (True, False):
        input = var(T.randn(3, input_size))
        hx = var(T.randn(3, hidden_size))
        cx = var(T.randn(3, hidden_size))

        cell = SubLSTMCell(input_size, hidden_size, bias=bias)

        for i in range(6):
            (hx, cx) = cell(input, (hx, cx))

        hx.sum().backward()
        assert hx.size() == T.Size([3, hidden_size])
        assert cx.size() == T.Size([3, hidden_size])
Ejemplo n.º 25
0
    def __getitem__(self, idx):
        imgstr = self.imgstrs[idx]
        img1str = os.path.join(self.readdir, imgstr)
        img2str = os.path.join(self.readdir, imgstr[:-6] + '_R.bmp')

        corder = 0
        if corder == 0 or self.transform is None:
            img1 = np.array(pilImg.open(img1str))
            img2 = np.array(pilImg.open(img2str))
        else:
            img1 = np.array(pilImg.open(img2str))
            img2 = np.array(pilImg.open(img1str))

        img3 = np.array(
            pilImg.open(os.path.join(self.readdir, imgstr[:-6] + '_M.bmp')))

        if self.transform is not None:
            # random shift and crop
            cropx = random.randint(2, 20)
            cropy = random.randint(2, 20)

            shift = random.randint(0, 2)
            ifx = random.randint(0, 1)
            shiftx = 0
            shifty = 0

            img3 = img3[cropy:cropy + 128, cropx:cropx + 128, :]
            img1 = img1[cropy - shifty:cropy + 128 - shifty,
                        cropx - shiftx:cropx + 128 - shiftx, :]
            img2 = img2[cropy + shifty:cropy + 128 + shifty,
                        cropx + shiftx:cropx + 128 + shiftx, :]

            flipH = random.randint(0, 1)
            flipV = random.randint(0, 1)
            if flipH == 1:
                img1 = np.flip(img1, 0)
                img2 = np.flip(img2, 0)
                img3 = np.flip(img3, 0)
            if flipV == 1:
                img1 = np.flip(img1, 1)
                img2 = np.flip(img2, 1)
                img3 = np.flip(img3, 1)

        return var(torch.from_numpy(img1.transpose(2,0,1).astype(np.float32) / 255.0)), var(torch.from_numpy(img2.transpose(2,0,1).astype(np.float32) / 255.0)), \
               var(torch.from_numpy(img3.transpose(2,0,1).astype(np.float32) / 255.0))
Ejemplo n.º 26
0
def test(net, testset, testloader, criterian, batch_size, n_class, log_file):
    '''Testing the network
    '''
    net.eval()

    testloss, testacc = 0., 0.
    class_correct = list(0. for i in range(10))
    class_total = list(0. for i in range(10))

    for (img, label) in testloader:
        img = var(img).cuda()
        label = var(label).cuda()
        #forward pass
        output = net(img)
        #loss
        loss = criterian(output, label)
        testloss += loss.data[0]
        #prediction
        _, predict = torch.max(output, 1)
        num_correct = (predict == label).sum()
        testacc += num_correct.data[0]
        #
        c = (predict == label).squeeze()
        for i in range(batch_size):
            l = label[i].data[0]
            class_correct[l] += c[i].data[0]
            class_total[l] += 1
        #end_for
    #end_for
    testloss /= len(testset)
    testacc /= len(testset)

    f = open(log_file, 'a')
    f.write('\n-------------------\n')

    print("Test: Loss: %.5f, Acc: %.2f %%" % (testloss, 100 * testacc))
    f.write("Test: Loss: %.5f, Acc: %.2f %%\n" % (testloss, 100 * testacc))
    for i in range(10):
        print('Accuracy of %5d : %2d %%' %
              (i, 100 * class_correct[i] / class_total[i]))
        f.write('Accuracy of %5d : %2d %%\n' %
                (i, 100 * class_correct[i] / class_total[i]))
    #end_for

    f.close()
Ejemplo n.º 27
0
def pim(im):
    val_tfm = Compose([
        tsfms.Resize(256),
        tsfms.CenterCrop(256),
        tsfms.Normalize((0.48, 0.225), mode='meanstd'),
        tsfms.ToTensor()
    ])
    imt = val_tfm(im)
    imb = imt.unsqueeze(0)
    imb = var(imb.cuda(), requires_grad=True)
    return imb
Ejemplo n.º 28
0
def test_memory():
    ξ = var(T.randn(64, 32))
    # ξ = var(T.randn(64, (32 * 4) + (3 * 32) + (5 * 4) + 3))
    n = Memory(32)
    hx = n.reset(64)
    v, hx = n(ξ, hx)

    assert v.size() == T.Size([64, 32, 4])
    v1, hx = n(ξ, hx)

    assert v1.size() == T.Size([64, 32, 4])
Ejemplo n.º 29
0
def ig_grad(im, model, base, steps=50):

    try:
        print(pim(im).size)
        score = F.softmax(model.forward(pim(im)))
        print(score[0][1].data.cpu())
        sc = score[0][1].data.cpu()[0]
        if sc < 1:
            sample_batch, step = gen_ten(im, base, steps)
            sample_batch = var(sample_batch.data, requires_grad=True)
            sample_out = F.softmax(model.forward(sample_batch))
            crit = get_crit()
            sample_loss = crit(sample_out,
                               var(torch.ones((steps)).long().cuda()))
            sample_loss.backward()
            sample_grad = sample_batch.grad * step
            return (True, sample_grad.sum(0))
        else:
            return (False, None)
    except Exception as e:
        print(e)
Ejemplo n.º 30
0
def validate(model,data):
  # To get validation accuracy = (correct/total)*100.
  total = 0
  correct = 0
  for i,(images,labels) in enumerate(data):
    images = var(images.cuda())
    x = model(images)
    value,pred = torch.max(x,1)
    pred = pred.data.cpu()
    total += x.size(0)
    correct += torch.sum(pred == labels)
  return correct*100./total
Ejemplo n.º 31
0
def test_function():
  hidden_size = 20
  input_size = 10

  for bias in (True, False):
    weight_ih = T.nn.Parameter(T.Tensor(4 * hidden_size, input_size))
    weight_hh = T.nn.Parameter(T.Tensor(4 * hidden_size, hidden_size))
    bias_ih = T.nn.Parameter(T.Tensor(4 * hidden_size)) if bias else None
    bias_hh = T.nn.Parameter(T.Tensor(4 * hidden_size)) if bias else None

    input = var(T.randn(3, input_size))
    hx = var(T.randn(3, hidden_size))
    cx = var(T.randn(3, hidden_size))
    cell = SubLSTMCellF
    for i in range(6):
      hx, cx = cell(input, (hx, cx), weight_ih, weight_hh, bias_ih, bias_hh)

    hx.sum().backward()

    assert hx.size() == T.Size([3, hidden_size])
    assert cx.size() == T.Size([3, hidden_size])
Ejemplo n.º 32
0
def test_rnn_bidirectional():
  hidden_size = 20
  input_size = 10
  seq_len = 5
  batch_size = 7

  for bias in (True, False):
    for batch_first in (True, False):
      input = var(T.randn(batch_size, seq_len, input_size)) if batch_first else var(T.randn(seq_len, batch_size, input_size))
      hx = None
      rnn = SubLSTM(input_size, hidden_size, num_layers=3, bias=bias, batch_first=batch_first, bidirectional=True)

      outputs = []
      for i in range(6):
        output, hx = rnn(input, hx)
        outputs.append(output)

      T.stack(outputs).sum().backward()

      assert hx[-1][-1][0].size() == T.Size([batch_size, hidden_size])
      assert hx[-1][-1][1].size() == T.Size([batch_size, hidden_size])
Ejemplo n.º 33
0
    def __getitem__(self, idx):
        imgstr = self.imgstrs[idx]
        img1str = os.path.join(self.readdir, imgstr, 'im1.png')
        img2str = os.path.join(self.readdir, imgstr, 'im2.png')

        img1 = np.array(pilImg.open(img2str))
        img2 = np.array(pilImg.open(img1str))
        img3 = np.array(
            pilImg.open(os.path.join(self.readdir, imgstr, 'im3.png')))

        img3 = img3[:256, :256, :]
        img1 = img1[:256, :256, :]
        img2 = img2[:256, :256, :]
        if self.transform is not None:
            # random shift and crop
            # cropx = random.randint(2, 20)
            # cropy = random.randint(2, 20)

            # shift = random.randint(0, 2)
            # ifx = random.randint(0, 1)
            # shiftx = 0
            # shifty = 0

            # img3 = img3[cropy:cropy + 128, cropx:cropx + 128, :]
            # img1 = img1[cropy - shifty:cropy + 128 - shifty, cropx - shiftx : cropx + 128 - shiftx,:]
            # img2 = img2[cropy + shifty:cropy + 128 + shifty, cropx + shiftx : cropx + 128 + shiftx,:]

            flipH = random.randint(0, 1)
            flipV = random.randint(0, 1)
            if flipH == 1:
                img1 = np.flip(img1, 0)
                img2 = np.flip(img2, 0)
                img3 = np.flip(img3, 0)
            if flipV == 1:
                img1 = np.flip(img1, 1)
                img2 = np.flip(img2, 1)
                img3 = np.flip(img3, 1)

        return var(torch.from_numpy(img1.transpose(2,0,1).astype(np.float32) / 255.0)), var(torch.from_numpy(img2.transpose(2,0,1).astype(np.float32) / 255.0)), \
               var(torch.from_numpy(img3.transpose(2,0,1).astype(np.float32) / 255.0))