예제 #1
0
def run():
    start_num = 2
    end_num = 8
    dest = 'all_pairs_survey.png'

    next_x = 0
    for i, n in enumerate(range(start_num, end_num + 1)):
        im_dim = [76, 96][n > 5]
        next_x += im_dim + 1

    result_size = (2 * 96 + 1, next_x - 1)
    print(result_size)
    result = np.ones((2 * 96 + 1, next_x - 1))

    next_y = 0
    for target_label in [0, 1]:
        next_x = 0
        print('target_label', target_label)
        for i, n in enumerate(range(start_num, end_num + 1)):
            im_dim = [76, 96][n > 5]
            dy = [10, 0][n > 5]
            spec = SampleSpec(n, n, im_dim=im_dim, min_cell=15, max_cell=18)
            vis_input, vis_labels, stats = spec.blocking_generate_with_stats(
                200)
            sample_index = find_index(vis_labels, target_label)
            y = next_y + dy
            result[y:y + im_dim,
                   next_x:next_x + im_dim] = vis_input[sample_index, 0, :, :]
            next_x += im_dim + 1
            print(stats)
            # vis_input = 1.0 - vis_input
        next_y += 96 + 1

    scipy.misc.imsave(dest, result)
예제 #2
0
def run():
    # get the dataloader we want to work with
    data_loader = get_data_loader()

    # build main typenet object
    typenet = TypeNet(data_loader.img_shp,
                      data_loader.output_size,
                      args.num_top_features,
                      debug=args.debug,
                      activations=args.activations)

    # parallelize across multiple GPU's
    if args.ngpu > 1:
        typenet = nn.DataParallel(typenet)
        print('Devices:', typenet.device_ids)

    # push to cuda
    if args.cuda:
        typenet = typenet.cuda()

    # build optimizer
    print("training...")
    optimizer = optim.Adam(typenet.parameters(), lr=args.lr)

    # build the logger object
    logger = None
    if args.filelog != '':
        short_name = get_short_name()
        logger = FileLogger(
            {
                'train': ['epoch', 'loss', 'acc'],
                'test': ['epoch', 'loss', 'acc'],
            }, [
                __file__,
                'allpairs/grid_generator.py',
                'allpairs/symbol_drawing.py',
            ],
            path=args.filelog,
            file_prefix=short_name,
            args=args)
        logger.set_info('note', args.note)
        logger.set_info('uuid', logger.uuid)
        logger.set_info('args', str(args))

    # main training loop
    best_acc = 0.0
    for epoch in range(1, args.epochs + 1):
        train(epoch, typenet, optimizer, data_loader, logger)
        acc = test(epoch, typenet, data_loader, logger)
        best_acc = max(best_acc, acc)
        print('best acc: {}'.format(best_acc))

    # close all the generators, needed to deal with multi-process generators
    SampleSpec.close_generators()
예제 #3
0
def run():
    f = open(args.csv, 'w')
    spec = SampleSpec(args.num_pairs,
                      args.num_classes,
                      im_dim=args.pixels,
                      min_cell=15,
                      max_cell=18)
    for i in range(args.num):
        progress(i, args.num)
        images, labels, stats = spec.blocking_generate_with_stats(1)
        outpath = os.path.join(args.dest, '{}.png'.format(str(i).zfill(6)))
        scipy.misc.imsave(outpath, images[0, 0, :, :])
        f.write(str(labels[0]))
        f.write('\n')
예제 #4
0
def set_sample_spec(num_pairs, num_classes, reset_every=None, im_dim=76):
    global sample_spec
    assert sample_spec.generators is None, 'attempting to redefine spec after it has been used'
    sample_spec = SampleSpec(num_pairs=num_pairs,
                             num_classes=num_classes,
                             im_dim=im_dim,
                             min_cell=15,
                             max_cell=18,
                             reset_every=reset_every)
예제 #5
0
 def test_images_same(self):
     correct = ((
         ('e11b8020b9f8fabe', '18a3ae95b53eb2a3'),
         ('b1cbdc248365556e', '3a45220b17d7bf49'),
         ('7fbfff719fe71393', 'b436c33047a91227'),
         ('07d1403088b6c624', '6b0a099525d4b709'),
         ('deb6823a3285233a', 'c6c1a74630ba13ca'),
         ('e6433a7d3bfb7c5e', 'e7884540c7d4364b'),
         ('90228d0abfa1c73b', '7ce1c8786cc40475'),
         ('c4be817429e61d25', '6dcd05d7168be05e'),
         ('be5a2d302cae9f18', '89659712a3f2c7c7'),
         ('0a42707c9f1c3c32', '35b3d810a33533c8'),
     ), (
         ('d1a12878c42725a5', '1759c3b44205f0fb'),
         ('041d9ff827beba3e', 'bf9aa5fbcf176355'),
         ('0162327e25617782', '8ee558aac58bb032'),
         ('de3571b7bd364931', 'f439cfcbd075b6bf'),
         ('98ffaea06e268eda', 'ba5bad1a9b1676eb'),
         ('3c042e60e1ca15b6', '14d7b3a418f5510a'),
         ('367ca722d496bf33', '052a7ce6679df288'),
         ('f7321cb15555ce55', '231060e55e4e987d'),
         ('361d0c54a66fe7e2', '2dc6219f8ee96f1d'),
         ('f6e04ac9005b6d58', '64ec9871f8d58228'),
     ))
     np.random.seed(1234)
     im_dim = 96
     num = 10
     for which, n in enumerate([4, 8]):
         spec = SampleSpec(n, n, im_dim=im_dim, min_cell=15, max_cell=18)
         vis_input, vis_labels, stats = spec.blocking_generate_with_stats(
             num)
         for i in range(num):
             im = vis_input[i, 0, :, :]
             (h0, h1) = image_hash(im)
             h0 = h0[0:16]
             h1 = h1[0:16]
             # print(h0, h1)
             self.assertEqual(h0, correct[which][i][0])
             self.assertEqual(h1, correct[which][i][1])
         # print()
     self.assertTrue(True)
예제 #6
0
def run():
    data = get_data_loader()
    typenet = TypeNet(data.img_shp,
                      data.output_size,
                      args.num_top_features,
                      activations=args.activations)

    if args.ngpu > 1:  # parallelize across multiple GPU's
        typenet = nn.DataParallel(typenet)
    if args.cuda:  # push to cuda
        typenet = typenet.cuda()

    # main training loop
    optimizer = optim.Adam(typenet.parameters(), lr=args.lr)
    num_trained_on = 0
    while num_trained_on < args.max_train_samples:
        train(num_trained_on, typenet, optimizer, data)
        num_trained_on += args.batch_size
        test(typenet, data)

    # close all the generators, needed to deal with multi-process generators
    SampleSpec.close_generators()
예제 #7
0
import torch
from torch.utils.data import Dataset
import numpy as np
from allpairs.grid_generator import SampleSpec

DEFAULT_PAIRS = 4
DEFAULT_CLASSES = 4
sample_spec = SampleSpec(num_pairs=DEFAULT_PAIRS,
                         num_classes=DEFAULT_CLASSES,
                         im_dim=76,
                         min_cell=15,
                         max_cell=18)


def set_sample_spec(num_pairs, num_classes, reset_every=None, im_dim=76):
    global sample_spec
    assert sample_spec.generators is None, 'attempting to redefine spec after it has been used'
    sample_spec = SampleSpec(num_pairs=num_pairs,
                             num_classes=num_classes,
                             im_dim=im_dim,
                             min_cell=15,
                             max_cell=18,
                             reset_every=reset_every)


class ToTensor(object):
    """simple override to add context to ToTensor"""
    def __init__(self, numpy_base_type=np.float32):
        self.numpy_base_type = numpy_base_type

    def __call__(self, index, img, context):