Exemplo n.º 1
0
def main(args):
    # get datasets
    source_train, source_test = chainer.datasets.get_svhn()
    target_train, target_test = chainer.datasets.get_mnist(ndim=3,
                                                           rgb_format=True)
    source = source_train, source_test

    # resize mnist to 32x32
    def transform(in_data):
        img, label = in_data
        img = resize(img, (32, 32))
        return img, label

    target_train = TransformDataset(target_train, transform)
    target_test = TransformDataset(target_test, transform)

    target = target_train, target_test

    # load pretrained source, or perform pretraining
    pretrained = os.path.join(args.output, args.pretrained_source)
    if not os.path.isfile(pretrained):
        source_cnn = pretrain_source_cnn(source, args)
    else:
        source_cnn = Loss(num_classes=10)
        serializers.load_npz(pretrained, source_cnn)

    # how well does this perform on target domain?
    test_pretrained_on_target(source_cnn, target, args)

    # initialize the target cnn (do not use source_cnn.copy)
    target_cnn = Loss(num_classes=10)
    # copy parameters from source cnn to target cnn
    target_cnn.copyparams(source_cnn)

    train_target_cnn(source, target, source_cnn, target_cnn, args)
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='Chainer example: MNIST')
    parser.add_argument('--resume',
                        '-r',
                        default='result/model_iter_9',
                        help='Resume the training from snapshot')
    parser.add_argument('--deformable',
                        '-d',
                        type=int,
                        default=1,
                        help='use deformable convolutions')
    args = parser.parse_args()

    if args.deformable == 1:
        model = DeformableConvnet(10)
    else:
        model = Convnet(10)
    chainer.serializers.load_npz(args.resume, model)

    train, test = chainer.datasets.get_mnist(ndim=3)
    test = TransformDataset(test, transform)

    test_iter = chainer.iterators.SerialIterator(test,
                                                 batch_size=1,
                                                 repeat=False,
                                                 shuffle=False)

    threshold = 1
    for i in range(1):
        batch = test_iter.next()
        in_arrays = concat_examples(batch, device=None)
        in_vars = tuple(chainer.Variable(x) for x in in_arrays)
        img, label = in_vars
        model(img)
        feat = model.feat
        H, W = feat.shape[2:]
        center = F.sum(feat[:, :, H / 2, W / 2])
        center.grad = np.ones_like(center.data)
        model.zerograds()
        img.zerograd()
        center.backward(retain_grad=True)

        img_grad = img.grad[0]  # (1, 28, 28)

        img_grad_abs = (np.abs(img_grad) / np.max(np.abs(img_grad)) *
                        255)[0]  # 28, 28
        img_grad_abs[np.isnan(img_grad_abs)] = 0
        y_indices, x_indices = np.where(img_grad_abs > threshold)
        plt.scatter(x_indices, y_indices, c='red')
        vis_img = transforms.chw_to_pil_image(255 * img.data[0])[:, :, 0]
        plt.imshow(vis_img, interpolation='nearest', cmap='gray')
        plt.show()
Exemplo n.º 3
0
    def test_transform_dataset(self):
        dataset = np.random.uniform(size=(10, 3, 32, 32))
        first_img = dataset[0]

        def transform(in_data):
            return in_data * 3

        transformed_dataset = TransformDataset(dataset, transform)
        out = transformed_dataset[0]
        np.testing.assert_equal(out, first_img * 3)

        outs = transformed_dataset[:2]
        np.testing.assert_equal([transform(elem) for elem in dataset[:2]],
                                outs)
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(
        description='ChainerCV training example: Faster R-CNN')
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--lr', '-l', type=float, default=1e-3)
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Output directory')
    parser.add_argument('--seed', '-s', type=int, default=0)
    parser.add_argument('--step_size', '-ss', type=int, default=50000)
    parser.add_argument('--iteration', '-i', type=int, default=70000)
    args = parser.parse_args()

    np.random.seed(args.seed)

    train_data = VOCDetectionDataset(split='trainval', year='2007')
    test_data = VOCDetectionDataset(split='test',
                                    year='2007',
                                    use_difficult=True,
                                    return_difficult=True)
    faster_rcnn = FasterRCNNVGG16(n_fg_class=len(voc_detection_label_names),
                                  pretrained_model='imagenet')
    faster_rcnn.use_preset('evaluate')
    model = FasterRCNNTrainChain(faster_rcnn)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)
        chainer.cuda.get_device(args.gpu).use()
    optimizer = chainer.optimizers.MomentumSGD(lr=args.lr, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005))

    def transform(in_data):
        img, bbox, label = in_data
        _, H, W = img.shape
        img = faster_rcnn.prepare(img)
        _, o_H, o_W = img.shape
        scale = o_H / H
        bbox = transforms.resize_bbox(bbox, (H, W), (o_H, o_W))

        # horizontally flip
        img, params = transforms.random_flip(img,
                                             x_random=True,
                                             return_param=True)
        bbox = transforms.flip_bbox(bbox, (o_H, o_W), x_flip=params['x_flip'])

        return img, bbox, label, scale

    train_data = TransformDataset(train_data, transform)

    train_iter = chainer.iterators.MultiprocessIterator(train_data,
                                                        batch_size=1,
                                                        n_processes=None,
                                                        shared_mem=100000000)
    test_iter = chainer.iterators.SerialIterator(test_data,
                                                 batch_size=1,
                                                 repeat=False,
                                                 shuffle=False)
    updater = chainer.training.updater.StandardUpdater(train_iter,
                                                       optimizer,
                                                       device=args.gpu)

    trainer = training.Trainer(updater, (args.iteration, 'iteration'),
                               out=args.out)

    trainer.extend(extensions.snapshot_object(model.faster_rcnn,
                                              'snapshot_model.npz'),
                   trigger=(args.iteration, 'iteration'))
    trainer.extend(extensions.ExponentialShift('lr', 0.1),
                   trigger=(args.step_size, 'iteration'))

    log_interval = 20, 'iteration'
    plot_interval = 3000, 'iteration'
    print_interval = 20, 'iteration'

    trainer.extend(chainer.training.extensions.observe_lr(),
                   trigger=log_interval)
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.PrintReport([
        'iteration',
        'epoch',
        'elapsed_time',
        'lr',
        'main/loss',
        'main/roi_loc_loss',
        'main/roi_cls_loss',
        'main/rpn_loc_loss',
        'main/rpn_cls_loss',
        'validation/main/map',
    ]),
                   trigger=print_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    if extensions.PlotReport.available():
        trainer.extend(extensions.PlotReport(['main/loss'],
                                             file_name='loss.png',
                                             trigger=plot_interval),
                       trigger=plot_interval)

    trainer.extend(
        DetectionVOCEvaluator(test_iter,
                              model.faster_rcnn,
                              use_07_metric=True,
                              label_names=voc_detection_label_names),
        trigger=ManualScheduleTrigger([args.step_size, args.iteration],
                                      'iteration'),
        invoke_before_training=False)

    trainer.extend(extensions.dump_graph('main/loss'))

    trainer.run()
Exemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser(description='Chainer CIFAR example:')
    parser.add_argument('--dataset', '-d', default='cifar10',
                        help='The dataset to use: cifar10 or cifar100')
    parser.add_argument('--batchsize', '-b', type=int, default=64,
                        help='Number of images in each mini-batch')
    parser.add_argument('--learnrate', '-l', type=float, default=0.05,
                        help='Learning rate for SGD')
    parser.add_argument('--epoch', '-e', type=int, default=300,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--gpu', '-g', type=int, default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out', '-o', default='result',
                        help='Directory to output the result')
    parser.add_argument('--resume', '-r', default='',
                        help='Resume the training from snapshot')
    parser.add_argument('--seed', '-s', type=int, default=123,
                        help='seed for split dataset into train & validation')
    parser.add_argument('--augment', '-a', type=bool, default=True,
                        help='whether augment dataset or not')
    parser.add_argument('--parallel', '-p', type=bool, default=True,
                        help='use multiprocess iterator or not')
    args = parser.parse_args()

    print('GPU: {}'.format(args.gpu))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('')

    # Set up a neural network to train.
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    if args.dataset == 'cifar10':
        print('Using CIFAR10 dataset.')
        class_labels = 10
        train, test = get_cifar10()
    elif args.dataset == 'cifar100':
        print('Using CIFAR100 dataset.')
        class_labels = 100
        train, test = get_cifar100()
    else:
        raise RuntimeError('Invalid dataset choice.')
    if args.augment:
        train = TransformDataset(train, transform)
    else:
        train, val = split_dataset_random(train, 45000, seed=args.seed)

    model = L.Classifier(DenseNetCifar(n_class=class_labels))
    if args.gpu >= 0:
        # Make a specified GPU current
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()  # Copy the model to the GPU

    optimizer = chainer.optimizers.NesterovAG(lr=args.learnrate, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(5e-4))

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                 repeat=False, shuffle=False)
    if args.parallel:
        train_iter = chainer.iterators.MultiprocessIterator(
            train, args.batchsize, n_processes=2)
        test_iter = chainer.iterators.MultiprocessIterator(
            test, args.batchsize, repeat=False, shuffle=False, n_processes=2)

    # Set up a trainer
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

    # Reduce the learning rate by half every 25 epochs.
    trainer.extend(extensions.ExponentialShift('lr', 0.5),
                   trigger=(25, 'epoch'))

    # Dump a computational graph from 'loss' variable at the first iteration
    # The "main" refers to the target link of the "main" optimizer.
    trainer.extend(extensions.dump_graph('main/loss'))

    # Take a snapshot at each epoch
    trainer.extend(extensions.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(extensions.snapshot_object(model.predictor,
                                              filename='densenet.model'), trigger=(args.epoch, 'epoch'))

    trainer.extend(extensions.observe_lr(), trigger=(1, 'epoch'))
    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport())

    # Print selected entries of the log to stdout
    # Here "main" refers to the target link of the "main" optimizer again, and
    # "validation" refers to the default name of the Evaluator extension.
    # Entries other than 'epoch' are reported by the Classifier link, called by
    # either the updater or the evaluator.
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'lr', 'elapsed_time']))

    trainer.extend(extensions.PlotReport(
        y_keys=['main/loss', 'validation/main/loss'], file_name='loss.png'))
    trainer.extend(extensions.PlotReport(
        y_keys=['main/accuracy', 'validation/main/accuracy'], file_name='accuracy.png'))

    # Print a progress bar to stdout
    trainer.extend(extensions.ProgressBar())

    if args.resume:
        # Resume from a snapshot
        chainer.serializers.load_npz(args.resume, trainer)

    # Run the training
    trainer.run()
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description="Vanilla_AE")
    parser.add_argument("--batchsize", "-b", type=int, default=128)
    parser.add_argument("--gpu", "-g", type=int, default=0)
    parser.add_argument("--snapshot", "-s", type=int, default=10)
    parser.add_argument("--n_dimz", "-z", type=int, default=16)
    parser.add_argument("--network", "-n", type=str, default='conv')

    args = parser.parse_args()
    if args.network == 'conv':
        import Network.mnist_conv as Network
    elif args.network == 'fl':
        import Network.mnist_fl as Network
    else:
        raise Exception('Error!')
    out = os.path.join('Pred', args.network, 'epoch_{}'.format(args.snapshot))
    os.makedirs(out, exist_ok=True)

    def transform(in_data):
        img, label = in_data
        img = resize(img, (32, 32))
        return img, label

    def plot_mnist_data(samples, label1, label2):
        for index, data in enumerate(samples):  #(配列番号,要素)
            plt.subplot(5, 13, index + 1)  #(行数, 列数, 何番目のプロットか)
            plt.axis('off')  #軸はoff
            plt.imshow(data.reshape(32, 32), cmap="gray")  #nearestで補完
            if index == 0:
                plt.title("in_1", color='red')
            elif index == 12:
                plt.title("in_2", color='red')
            elif index == 1:
                plt.title("out_1", color='red')
            elif index == 11:
                plt.title("out_2", color='red')
        pict = os.path.join(out, "{}_{}.png".format(label1, label2))
        plt.savefig(pict)
        plt.show()

    batchsize = args.batchsize
    gpu_id = args.gpu
    _, test = mnist.get_mnist(withlabel=True, ndim=3)
    test = TransformDataset(test, transform)
    xp = cupy
    Enc = Network.Encoder(n_dimz=args.n_dimz)
    Dec = Network.Decoder(n_dimz=args.n_dimz)
    Critic = Network.Critic()

    chainer.cuda.get_device(args.gpu).use()
    Enc.to_gpu(args.gpu)
    Dec.to_gpu(args.gpu)
    Critic.to_gpu(args.gpu)
    Enc_path = os.path.join('result', args.network,
                            'Enc_snapshot_epoch_{}.npz'.format(args.snapshot))
    Dec_path = os.path.join('result', args.network,
                            'Dec_snapshot_epoch_{}.npz'.format(args.snapshot))
    Critic_path = os.path.join(
        'result', args.network,
        'Critic_snapshot_epoch_{}.npz'.format(args.snapshot))
    chainer.serializers.load_npz(Enc_path, Enc)
    chainer.serializers.load_npz(Dec_path, Dec)
    chainer.serializers.load_npz(Critic_path, Critic)
    label1 = 1
    label2 = 5
    test1 = [i[0] for i in test if (i[1] == label1)]
    test2 = [i[0] for i in test if (i[1] == label2)]
    test1 = test1[0:5]
    test2 = test2[5:10]
    itp_list = []
    for i in range(0, 5):
        data1 = test1[i]
        data2 = test2[i]
        with chainer.using_config('train', False):
            z1 = Enc(xp.array([data1]).astype(np.float32))
            z2 = Enc(xp.array([data2]).astype(np.float32))
            y1 = Dec(z1)
            y2 = Dec(z2)
        in1 = (data1 * 255).astype(np.uint8).reshape(32, 32)
        in2 = (data2 * 255).astype(np.uint8).reshape(32, 32)
        z_diff = (z2 - z1).data
        z_itp = z1.data  #start point

        itp_list.append(in1)
        for j in range(1, 11):  #間の座標を出す
            z_itp = xp.vstack((z_itp, z1.data + z_diff / 10 * j))
        for k in range(0, 11):  #端から座標を移動して画像を出力
            with chainer.using_config('train', False):
                itp = Dec(xp.copy(z_itp[k][None, ...]))
            itp_out = (itp.data * 255).astype(np.uint8).reshape(32, 32)
            itp_list.append(chainer.cuda.to_cpu(itp_out))
        itp_list.append(in2)

    plot_mnist_data(itp_list, label1, label2)
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(
        description='ChainerCV training example: Faster R-CNN')
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--lr', '-l', type=float, default=1e-3)
    parser.add_argument('--out', '-o', default='result',
                        help='Output directory')
    parser.add_argument('--seed', '-s', type=int, default=0)
    parser.add_argument('--step_size', '-ss', type=int, default=50000)
    parser.add_argument('--iteration', '-i', type=int, default=70000)
    parser.add_argument('--train_data_dir', '-t', default=WIDER_TRAIN_DIR,
                        help='Training dataset (WIDER_train)')
    parser.add_argument('--train_annotation', '-ta', default=WIDER_TRAIN_ANNOTATION_MAT,
                        help='Annotation file (.mat) for training dataset')
    parser.add_argument('--val_data_dir', '-v', default=WIDER_VAL_DIR,
                        help='Validation dataset (WIDER_train)')
    parser.add_argument('--val_annotation', '-va', default=WIDER_VAL_ANNOTATION_MAT,
                        help='Annotation file (.mat) for validation dataset')
    args = parser.parse_args()

    np.random.seed(args.seed)

    # for logging pocessed files
    logger = logging.getLogger('logger')
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler(filename='filelog.log')
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    
    blacklist = []
    with open(BLACKLIST_FILE, 'r') as f:
        for line in f:
            l = line.strip()
            if l:
                blacklist.append(line.strip())
    
    # train_data = VOCDetectionDataset(split='trainval', year='2007')
    # test_data = VOCDetectionDataset(split='test', year='2007',
                                    # use_difficult=True, return_difficult=True)
    train_data = WIDERFACEDataset(args.train_data_dir, args.train_annotation, 
        logger=logger, exclude_file_list=blacklist)
    test_data = WIDERFACEDataset(args.val_data_dir, args.val_annotation)
    # faster_rcnn = FasterRCNNVGG16(n_fg_class=len(voc_detection_label_names),
                                  # pretrained_model='imagenet')
    faster_rcnn.use_preset('evaluate')
    model = FasterRCNNTrainChain(faster_rcnn)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)
        chainer.cuda.get_device(args.gpu).use()
    optimizer = chainer.optimizers.MomentumSGD(lr=args.lr, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005))


    train_data = TransformDataset(train_data, transform)
    #import pdb; pdb.set_trace()
    #train_iter = chainer.iterators.MultiprocessIterator(
    #    train_data, batch_size=1, n_processes=None, shared_mem=100000000)
    train_iter = chainer.iterators.SerialIterator(
        train_data, batch_size=1)
    test_iter = chainer.iterators.SerialIterator(
        test_data, batch_size=1, repeat=False, shuffle=False)
    updater = chainer.training.updater.StandardUpdater(
        train_iter, optimizer, device=args.gpu)

    trainer = training.Trainer(
        updater, (args.iteration, 'iteration'), out=args.out)

    trainer.extend(
        extensions.snapshot_object(model.faster_rcnn, 'snapshot_model.npz'),
        trigger=(args.iteration, 'iteration'))
    trainer.extend(extensions.ExponentialShift('lr', 0.1),
                   trigger=(args.step_size, 'iteration'))

    log_interval = 20, 'iteration'
    plot_interval = 3000, 'iteration'
    print_interval = 20, 'iteration'

    trainer.extend(chainer.training.extensions.observe_lr(),
                   trigger=log_interval)
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.PrintReport(
        ['iteration', 'epoch', 'elapsed_time', 'lr',
         'main/loss',
         'main/roi_loc_loss',
         'main/roi_cls_loss',
         'main/rpn_loc_loss',
         'main/rpn_cls_loss',
         'validation/main/map',
         ]), trigger=print_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(
                ['main/loss'],
                file_name='loss.png', trigger=plot_interval
            ),
            trigger=plot_interval
        )

    trainer.extend(
        DetectionVOCEvaluator(
            test_iter, model.faster_rcnn, use_07_metric=True,
            label_names=('face',)),
        trigger=ManualScheduleTrigger(
            [args.step_size, args.iteration], 'iteration'),
        invoke_before_training=False)

    trainer.extend(extensions.dump_graph('main/loss'))

    #try:
        # warnings.filterwarnings('error', category=RuntimeWarning)
    trainer.run()
Exemplo n.º 8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--batchsize', type=int, default=12)
    parser.add_argument('--class_weight', type=str, default='class_weight.npy')
    parser.add_argument('--out', type=str, default='result')
    args = parser.parse_args()

    # Triggers
    log_trigger = (50, 'iteration')
    validation_trigger = (2000, 'iteration')
    end_trigger = (16000, 'iteration')

    # Dataset
    train = CamVidDataset(split='train')
    train = TransformDataset(train, transform)
    val = CamVidDataset(split='val')

    # Iterator
    train_iter = iterators.MultiprocessIterator(train, args.batchsize)
    val_iter = iterators.MultiprocessIterator(val,
                                              args.batchsize,
                                              shuffle=False,
                                              repeat=False)

    # Model
    class_weight = np.load(args.class_weight)
    model = SegNetBasic(n_class=11)
    model = PixelwiseSoftmaxClassifier(model, class_weight=class_weight)
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()  # Make a specified GPU current
        model.to_gpu()  # Copy the model to the GPU

    # Optimizer
    optimizer = optimizers.MomentumSGD(lr=0.1, momentum=0.9)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005))

    # Updater
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)

    # Trainer
    trainer = training.Trainer(updater, end_trigger, out=args.out)

    trainer.extend(extensions.LogReport(trigger=log_trigger))
    trainer.extend(extensions.observe_lr(), trigger=log_trigger)
    trainer.extend(extensions.dump_graph('main/loss'))

    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(['main/loss'],
                                  x_key='iteration',
                                  file_name='loss.png'))
        trainer.extend(
            extensions.PlotReport(['validation/main/miou'],
                                  x_key='iteration',
                                  file_name='miou.png'))

    trainer.extend(extensions.snapshot_object(
        model.predictor, filename='model_iteration-{.updater.iteration}'),
                   trigger=end_trigger)
    trainer.extend(extensions.PrintReport([
        'epoch', 'iteration', 'elapsed_time', 'lr', 'main/loss',
        'validation/main/miou', 'validation/main/mean_class_accuracy',
        'validation/main/pixel_accuracy'
    ]),
                   trigger=log_trigger)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.extend(SemanticSegmentationEvaluator(val_iter, model.predictor,
                                                 camvid_label_names),
                   trigger=validation_trigger)

    trainer.run()
Exemplo n.º 9
0
def main():
    parser = argparse.ArgumentParser(
        description='ChainerCV Semantic Segmentation example with FCN')
    parser.add_argument('--batch_size',
                        '-b',
                        type=int,
                        default=1,
                        help='Number of images in each mini-batch')
    parser.add_argument('--iteration',
                        '-i',
                        type=int,
                        default=50000,
                        help='Number of iteration to carry out')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--lr',
                        '-l',
                        type=float,
                        default=1e-10,
                        help='Learning rate of the optimizer')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--resume',
                        '-r',
                        default='',
                        help='Resume the training from snapshot')
    args = parser.parse_args()

    print('GPU: {}'.format(args.gpu))
    print('# Minibatch-size: {}'.format(args.batch_size))
    print('# iteration: {}'.format(args.iteration))
    print('')
    batch_size = args.batch_size
    iteration = args.iteration
    gpu = args.gpu
    lr = args.lr
    out = args.out
    resume = args.resume

    # prepare datasets
    def transform(in_data):
        img, label = in_data
        vgg_subtract_bgr = np.array([103.939, 116.779, 123.68],
                                    np.float32)[:, None, None]
        img -= vgg_subtract_bgr
        img = transforms.pad(img, max_size=(512, 512), bg_value=0)
        label = transforms.pad(label, max_size=(512, 512), bg_value=-1)
        return img, label

    train_data = VOCSemanticSegmentationDataset(mode='train')
    test_data = VOCSemanticSegmentationDataset(mode='val')
    train_data = TransformDataset(train_data, transform)
    test_data = TransformDataset(test_data, transform)

    # set up FCN32s
    n_class = 21
    model = FCN32s(n_class=n_class)
    if gpu != -1:
        model.to_gpu(gpu)
        chainer.cuda.get_device(gpu).use()

    # prepare an optimizer
    optimizer = chainer.optimizers.MomentumSGD(lr=lr, momentum=0.99)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005))

    # prepare iterators
    train_iter = chainer.iterators.SerialIterator(train_data,
                                                  batch_size=batch_size)
    test_iter = chainer.iterators.SerialIterator(test_data,
                                                 batch_size=1,
                                                 repeat=False,
                                                 shuffle=False)

    updater = training.StandardUpdater(train_iter, optimizer, device=gpu)
    trainer = training.Trainer(updater, (iteration, 'iteration'), out=out)

    val_interval = 3000, 'iteration'
    log_interval = 100, 'iteration'

    trainer.extend(TestModeEvaluator(test_iter, model, device=gpu),
                   trigger=val_interval)

    # reporter related
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.PrintReport([
        'iteration', 'main/time', 'main/loss', 'validation/main/loss',
        'main/accuracy', 'validation/main/accuracy', 'main/accuracy_cls',
        'validation/main/accuracy_cls', 'main/iu', 'validation/main/iu',
        'main/fwavacc', 'validation/main/fwavacc'
    ]),
                   trigger=log_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    # visualize training
    trainer.extend(
        extensions.PlotReport(['main/loss', 'validation/main/loss'],
                              trigger=log_interval,
                              file_name='loss.png'))
    trainer.extend(
        extensions.PlotReport(['main/accuracy', 'validation/main/accuracy'],
                              trigger=log_interval,
                              file_name='accuracy.png'))
    trainer.extend(
        extensions.PlotReport(
            ['main/accuracy_cls', 'validation/main/accuracy_cls'],
            trigger=log_interval,
            file_name='accuracy_cls.png'))
    trainer.extend(
        extensions.PlotReport(['main/iu', 'validation/main/iu'],
                              trigger=log_interval,
                              file_name='iu.png'))
    trainer.extend(
        extensions.PlotReport(['main/fwavacc', 'validation/main/fwavacc'],
                              trigger=log_interval,
                              file_name='fwavacc.png'))

    def vis_transform(in_data):
        vgg_subtract_bgr = np.array([103.939, 116.779, 123.68],
                                    np.float32)[:, None, None]
        img, label = in_data
        img += vgg_subtract_bgr
        img, label = transforms.chw_to_pil_image_tuple((img, label),
                                                       indices=[0, 1])
        return img, label

    trainer.extend(
        SemanticSegmentationVisReport(
            range(10),  # visualize outputs for the first 10 data of test_data
            test_data,
            model,
            n_class=n_class,
            predict_func=model.predict,  # a function to predict output
            vis_transform=vis_transform),
        trigger=val_interval,
        invoke_before_training=True)

    trainer.extend(extensions.dump_graph('main/loss'))

    if resume:
        chainer.serializers.load_npz(osp.expanduser(resume), trainer)

    trainer.run()
Exemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser(description="Vanilla_AE")
    parser.add_argument("--batchsize", "-b", type=int, default=64)
    parser.add_argument("--epoch", "-e", type=int, default=100)
    parser.add_argument("--gpu", "-g", type=int, default=0)
    parser.add_argument("--snapshot", "-s", type=int, default=10)
    parser.add_argument("--n_dimz", "-z", type=int, default=16)
    parser.add_argument("--dataset", "-d", type=str, default='mnist')
    parser.add_argument("--network", "-n", type=str, default='conv')

    args = parser.parse_args()

    def transform(in_data):
        img = in_data
        img = resize(img, (32, 32))
        return img

    def transform2(in_data):
        img, label = in_data
        img = resize(img, (32, 32))
        return img, label

    #import program
    import Updater
    import Visualizer

    #print settings
    print("GPU:{}".format(args.gpu))
    print("epoch:{}".format(args.epoch))
    print("Minibatch_size:{}".format(args.batchsize))
    print('')
    out = os.path.join('result', args.network)
    batchsize = args.batchsize
    gpu_id = args.gpu
    max_epoch = args.epoch

    train_val, _ = mnist.get_mnist(withlabel=False, ndim=3)
    train_val = TransformDataset(train_val, transform)
    #for visualize
    _, test = mnist.get_mnist(withlabel=True, ndim=3)
    test = TransformDataset(test, transform2)
    label1 = 1
    label2 = 5
    test1 = [i[0] for i in test if (i[1] == label1)]
    test2 = [i[0] for i in test if (i[1] == label2)]
    test1 = test1[0:5]
    test2 = test2[5:10]

    if args.network == 'conv':
        import Network.mnist_conv as Network
    elif args.network == 'fl':
        import Network.mnist_fl as Network
    else:
        raise Exception('Error!')

    AE = Network.AE(n_dimz=args.n_dimz, batchsize=args.batchsize)
    train, valid = split_dataset_random(train_val, 50000, seed=0)

    #set iterator
    train_iter = iterators.SerialIterator(train, batchsize)
    valid_iter = iterators.SerialIterator(valid,
                                          batchsize,
                                          repeat=False,
                                          shuffle=False)

    #optimizer
    def make_optimizer(model, alpha=0.0002, beta1=0.5):
        optimizer = optimizers.Adam(alpha=alpha, beta1=beta1)
        optimizer.setup(model)
        optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001))
        return optimizer

    opt_AE = make_optimizer(AE)
    #trainer
    updater = Updater.AEUpdater(model=(AE),
                                iterator=train_iter,
                                optimizer={'AE': opt_AE},
                                device=args.gpu)

    trainer = training.Trainer(updater, (max_epoch, 'epoch'), out=out)
    trainer.extend(extensions.LogReport(log_name='log'))
    snapshot_interval = (args.snapshot, 'epoch')
    display_interval = (1, 'epoch')
    trainer.extend(extensions.snapshot_object(
        AE, filename='AE_snapshot_epoch_{.updater.epoch}.npz'),
                   trigger=snapshot_interval)
    trainer.extend(extensions.PrintReport(['epoch', 'AE_loss']),
                   trigger=display_interval)
    trainer.extend(extensions.ProgressBar())
    trainer.extend(Visualizer.out_generated_image(AE, test1, test2, out),
                   trigger=(1, 'epoch'))
    trainer.run()
    del trainer
    else:
        img_canvas = np.zeros(img_size, dtype=np.float32)
        ymin = np.random.randint(0, img_size[0] - h)
        xmin = np.random.randint(0, img_size[1] - w)
        img_canvas[ymin:ymin + h, xmin:xmin + w] = img
        img = img_canvas

    img = img[np.newaxis, :]  # Add the bach channel back
    return img, label


if __name__ == '__main__':
    args = parse_args()

    train, test = datasets.get_mnist(ndim=3)
    train = TransformDataset(train, transform_mnist_rts)
    train_iter = iterators.SerialIterator(train, batch_size=args.batch_size)

    model = L.Classifier(predictor=STCNN())
    optimizer = optimizers.Adam()
    optimizer.setup(model)

    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = Trainer(updater=updater,
                      stop_trigger=(args.max_iter, 'iteration'),
                      out=args.out)
    trainer.extend(extensions.LogReport(trigger=(1, 'iteration')))
    trainer.extend(extensions.PrintReport(
        ['epoch', 'iteration', 'main/loss', 'main/accuracy', 'elapsed_time']),
                   trigger=(1, 'iteration'))
    trainer.extend(
Exemplo n.º 12
0
def main():
    parser = argparse.ArgumentParser(description='Chainer example: MNIST')
    parser.add_argument('--batchsize', '-b', type=int, default=100,
                        help='Number of images in each mini-batch')
    parser.add_argument('--epoch', '-e', type=int, default=20,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--gpu', '-g', type=int, default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--deformable', '-d', type=int, default=1,
                        help='use deformable convolutions')
    args = parser.parse_args()

    print('GPU: {}'.format(args.gpu))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('deformable: {}'.format(args.deformable))
    print('')

    # Set up a neural network to train
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    # model = L.Classifier(Convnet(10))
    if args.deformable == 1:
        feat_extractor = DeformableConvnet(10)
    else:
        feat_extractor = Convnet(10)
    model = L.Classifier(feat_extractor)
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()  # Make a specified GPU current
        model.to_gpu()  # Copy the model to the GPU

    # Setup an optimizer
    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)

    # Load the MNIST dataset
    train, test = chainer.datasets.get_mnist(ndim=3)
    train = TransformDataset(train, transform)
    test = TransformDataset(test, transform)

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                 repeat=False, shuffle=False)

    # Set up a trainer
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'))

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

    # Dump a computational graph from 'loss' variable at the first iteration
    # The "main" refers to the target link of the "main" optimizer.
    trainer.extend(extensions.dump_graph('main/loss'))

    # Take a snapshot for each specified epoch
    if args.deformable:
        snapshot_fn = 'model_iter_{.updater.epoch}'
    else:
        snapshot_fn = 'non_deformable_{.updater.epoch}'
    trainer.extend(extensions.snapshot_object(feat_extractor, snapshot_fn),
                   trigger=(1, 'epoch'))

    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport())

    # Save two plot images to the result dir
    if extensions.PlotReport.available():
        trainer.extend(
            extensions.PlotReport(['main/loss', 'validation/main/loss'],
                                  'epoch', file_name='loss.png'))
        trainer.extend(
            extensions.PlotReport(
                ['main/accuracy', 'validation/main/accuracy'],
                'epoch', file_name='accuracy.png'))

    # Print selected entries of the log to stdout
    # Here "main" refers to the target link of the "main" optimizer again, and
    # "validation" refers to the default name of the Evaluator extension.
    # Entries other than 'epoch' are reported by the Classifier link, called by
    # either the updater or the evaluator.
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))

    # Print a progress bar to stdout
    trainer.extend(extensions.ProgressBar())

    # Run the training
    trainer.run()