Esempio n. 1
0
def train(idx):
    print('train by fold: {}'.format(idx))
    df = pd.read_feather('./data/features.normalized.feather')
    fold = pd.read_feather('./data/fold.{}.feather'.format(idx))

    df['valid'] = df['SK_ID_CURR'].isin(fold['SK_ID_CURR'])
    model = TemporalEnsembling(df)

    train_dset = create_dataset(df, 'TARGET', 'valid')
    train_iter = SerialIterator(train_dset, 512)

    optimizer = Adam()
    optimizer.setup(model)
    updater = StandardUpdater(train_iter, optimizer, device=0)
    trainer = Trainer(updater, (20, 'epoch'), out='tempem_result')
    trainer.extend(
        make_extension((1, 'epoch'))(UpdateExtention(
            train_dset, './data/nn.fold.{}.feather'.format(idx))))
    trainer.extend(extensions.LogReport())
    filename = 'fold_%d_snapshot_epoch-{.updater.epoch}' % (idx)
    trainer.extend(extensions.snapshot(filename=filename))
    trainer.extend(
        extensions.PrintReport(
            ['epoch', 'main/loss', 'main/val/auc', 'elapsed_time']))
    trainer.run()
Esempio n. 2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--batchsize', '-b', type=int, default=128)
    parser.add_argument('--epoch', '-e', type=int, default=20)
    parser.add_argument('--out', '-o', default='result')
    args = parser.parse_args()

    model = L.Classifier(MLP(128, 10))

    optimizer = optimizers.Adam()
    optimizer.setup(model)

    train, test = datasets.get_mnist()

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

    updater = StandardUpdater(train_iter, optimizer, device=-1)
    trainer = Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    trainer.extend(extensions.Evaluator(test_iter, model, device=-1))

    trainer.extend(JsonlReport())
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'elapsed_time'],
        log_report='JsonlReport',
    ))
    trainer.extend(extensions.ProgressBar())

    trainer.run()
Esempio n. 3
0
def main():
    # input_size: 299
    #model = InceptionV4(dim_out=17)
    #model = InceptionV4(dim_out=17, base_filter_num=6, ablocks=2, bblocks=1, cblocks=1)
    #model = InceptionResNetV2(dim_out=17)
    #model = InceptionResNetV2(dim_out=17, base_filter_num=8, ablocks=1, bblocks=2, cblocks=1)

    # input_size: 224
    #model = VGGNetBN(17)  # VGGNet original size
    #model = VGGNetBN(17, 16)  # VGGNet 1/4 of filter num
    #model = GoogLeNetBN(17)  # GoogLeNet original size
    #model = GoogLeNetBN(17, 16)  # GoogleNet 1/2 filter num
    #model = GoogLeNetBN(17, 8)  # GoogleNet 1/4 filter num
    #model = ResNet50(17)  # ResNet50 original size
    #model = ResNet50(17, 32)  # ResNet50 1/2 size
    #model = ResNet50(17, 16)  # ResNet50 1/4 size
    #model = SqueezeNet(17)  #SqueezeNet original size
    #model = SqueezeNet(17, 8)  #SqueezeNet 1/2 filter num
    #model = MobileNet(17)  # MobileNet original size
    #model = MobileNet(17, 16)  # MobileNet 1/2 filter num
    #model = MobileNet(17, 8)  # MobileNet 1/4 filter num

    # input_size: 100
    #model = FaceClassifier100x100V2(n_classes=17)
    model = FaceClassifier100x100V(n_classes=17)

    optimizer = Adam()
    optimizer.setup(model)

    train_dataset = load_dataset('train.tsv', True)
    test_dataset = load_dataset('test.tsv')

    train_iter = SerialIterator(train_dataset, batch_size=BATCH_SIZE)
    test_iter = SerialIterator(test_dataset, batch_size=BATCH_SIZE, shuffle=False, repeat=False)
    updater = StandardUpdater(train_iter, optimizer, device=DEVICE)
    trainer = Trainer(updater, (N_EPOCHS, 'epoch'), out='result')

    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(extensions.LogReport())
    trainer.extend(extensions.ProgressBar(update_interval=10))
    trainer.extend(extensions.Evaluator(test_iter, model, device=DEVICE))
    trainer.extend(extensions.PrintReport(['main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy']))
    #trainer.extend(extensions.snapshot_object(model, 'snapshot_{.updater.epoch}.model'))

    trainer.run()

    chainer.serializers.save_npz('result/model.npz', model.to_cpu())
Esempio n. 4
0
def main(args):
    chainer.config.user_gpu = args.g
    if args.g >= 0:
        chainer.backends.cuda.get_device_from_id(args.g).use()
        print("GPU mode")

    mnist_3 = get_mnist_num(args.neg_numbers)
    mnist_8 = get_mnist_num(args.pos_numbers)

    # iteratorを作成
    kwds = {"batch_size": args.batchsize, "shuffle": True, "repeat": True}
    mnist_3_iter = iterators.SerialIterator(mnist_3, **kwds)
    mnist_8_iter = iterators.SerialIterator(mnist_8, **kwds)

    generator = Generator()
    critic = Critic()
    if args.g >= 0:
        generator.to_gpu()
        critic.to_gpu()

    adam_args = args.alpha, args.beta1, args.beta2
    opt_g = optimizers.Adam(*adam_args)
    opt_g.setup(generator)
    opt_c = optimizers.Adam(*adam_args)
    opt_c.setup(critic)

    updater = WGANUpdater(mnist_3_iter, mnist_8_iter, opt_g, opt_c,
                          args.n_cri1, args.n_cri2, args.gp_lam, args.l1_lam)
    trainer = Trainer(updater, (args.epoch, "epoch"), out=args.result_dir)
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport(["epoch", "generator/loss", "critic/loss"]))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(
        extensions.PlotReport(("generator/loss", "critic/loss"),
                              "epoch",
                              file_name="loss_plot.eps"))
    trainer.extend(
        ext_save_img(generator, mnist_8, args.result_dir + "/out_images"))

    trainer.run()
Esempio n. 5
0
def train():
    # train_txt = "/media/common-ns/New Volume/reseach/Dataset/OU-ISIR_by_Setoguchi/CV01.txt"
    train_dir = "/media/common-ns/New Volume/reseach/Dataset/OU-ISIR_by_Setoguchi/Gallery/CV01(Gallery)/*"
    train = load_OULP(path_dir=train_dir)

    # print(train[0])

    # 教師データ
    # train = train[0:1000]
    train = [i[0] for i in train]  # dataのパスとラベルのうち、dataだけ抜き出す
    train = datasets.TupleDataset(train, train)  # 同じパス画像のペアから、dataに変換してタプルにする

    batch_size = 195
    train_iter = chainer.iterators.SerialIterator(train, batch_size=batch_size)

    #model = L.Classifier(Autoencoder(), lossfun=F.mean_squared_error)
    model = L.Classifier(CAE(), lossfun=sce_loss)
    model.compute_accuracy = False
    optimizer = optimizers.Adam()
    optimizer.setup(model)

    updater = StandardUpdater(train_iter, optimizer, device=0)
    trainer = Trainer(
        updater,
        (1000, 'epoch'),
        out="result",
    )
    trainer.extend(extensions.LogReport())
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss']))
    trainer.extend(extensions.snapshot(), trigger=(200, 'epoch'))
    trainer.extend(extensions.snapshot_object(
        target=model, filename='model_snapshot_{.updater.iteration}'),
                   trigger=(250, 'epoch'))
    trainer.extend(extensions.ProgressBar())

    trainer.run()
    serializers.save_npz(
        "/home/common-ns/setoguchi/chainer_files/Convolutional_Auto_Encoder/CAE_FC_model",
        model)
Esempio n. 6
0
def run_linear_network(loss_fn, alpha=0.3, batch_size=2):

    # Get data
    np.random.seed(42)
    dataset = get_dataset()
    iterator = SerialIterator(dataset, batch_size, repeat=True, shuffle=True)

    # Set up network and loss
    predictor = L.Linear(None, 1)
    ranker = Ranker(predictor)
    loss = Loss(ranker, loss_fn)

    # Optimizer
    optimizer = Adam(alpha=alpha)
    optimizer.setup(loss)
    updater = StandardUpdater(iterator, optimizer, converter=zeropad_concat)
    trainer = Trainer(updater, (100, 'epoch'))
    log_report = extensions.LogReport(log_name=None)
    trainer.extend(log_report)
    np.random.seed(42)
    trainer.run()
    last_ndcg = log_report.log[-1]['ndcg']
    return last_ndcg
    def chainer_model_pipe(self, nn, train, valid, params):
        epoch = params['epoch']
        batch_size = params['batch_size']
        use_gpu = params['use_gpu']

        if 'fixed_base_w' in params.keys():
            fixed_base_w = params['fixed_base_w']
        else:
            fixed_base_w = False

        # Model Instance
        model = L.Classifier(nn)

        if use_gpu:
            device = 0
            model.to_gpu(device)
        else:
            device = -1

        # ミニバッチのインスタンスを作成
        train_iter = SerialIterator(train, batch_size)
        valid_iter = SerialIterator(valid,
                                    batch_size,
                                    repeat=False,
                                    shuffle=False)

        # Set Lerning
        optimizer = Adam()
        optimizer.setup(model)

        if fixed_base_w:
            model.predictor.base.disable_update()

        updater = StandardUpdater(train_iter, optimizer, device=device)

        trainer = Trainer(updater, (epoch, 'epoch'), out='result/cat_dog')
        trainer.extend(Evaluator(valid_iter, model, device=device))
        trainer.extend(LogReport(trigger=(1, 'epoch')))
        trainer.extend(PrintReport([
            'epoch', 'main/accuracy', 'validation/main/accuracy', 'main/loss',
            'validation/main/loss', 'elapsed_time'
        ]),
                       trigger=(1, 'epoch'))

        trainer.run()

        if use_gpu:
            model.to_cpu()

        return model
Esempio n. 8
0
def main(args):
    chainer.config.user_gpu = args.g
    if args.g >= 0:
        chainer.backends.cuda.get_device_from_id(args.g).use()
        print("GPU mode")

    mnist_train = chainer.datasets.get_mnist()[0]  # MNISTデータ取得
    mnist_train = chainer.dataset.concat_examples(mnist_train)[
        0]  # 画像だけ(ラベルは不要)
    mnist_train = mnist_train.reshape((-1, 1, 28, 28))  # 画像形式(N,C,H,W)に整形
    mnist_iter = iterators.SerialIterator(mnist_train,
                                          args.batchsize,
                                          shuffle=True,
                                          repeat=True)  # iteratorを作成

    generator = Generator(Z_DIM)
    critic = Critic()
    if args.g >= 0:
        generator.to_gpu()
        critic.to_gpu()

    opt_g = optimizers.Adam(args.alpha, args.beta1, args.beta2)
    opt_g.setup(generator)
    opt_c = optimizers.Adam(args.alpha, args.beta1, args.beta2)
    opt_c.setup(critic)

    updater = WGANUpdater(mnist_iter, opt_g, opt_c, args.n_cri, args.gp_lam)
    trainer = Trainer(updater, (args.epoch, "epoch"), out=args.result_dir)
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport(["epoch", "generator/loss", "critic/loss"]))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(
        extensions.PlotReport(("generator/loss", "main/wdist"),
                              "epoch",
                              file_name="loss_plot.eps"))
    trainer.extend(extensions.snapshot_object(
        generator, "model_epoch_{.updater.epoch}.model"),
                   trigger=(10, "epoch"))
    trainer.extend(ext_save_img(generator, args.result_dir + "/out_images"))

    trainer.run()
Esempio n. 9
0
def main():
    args = parse_args()
    with open(args.config_path) as f:
        config = json.load(f)
    with open(args.app_config) as f:
        app_config = json.load(f)
    app_train_config = app_config.get('train', {})
    command_config = {'gpu': args.gpu}
    if args.output_dir is not None:
        command_config['output_dir'] = args.output_dir
    config.update(command_config)

    device_id = config['gpu']
    batch_size = config['batch_size']

    network_params = config['network']
    nets = {k: util.create_network(v) for k, v in network_params.items()}
    optimizers = {
        k: util.create_optimizer(v['optimizer'], nets[k])
        for k, v in network_params.items()
    }
    if len(optimizers) == 1:
        key, target_optimizer = list(optimizers.items())[0]
        target = nets[key]
    else:
        target = nets
        target_optimizer = optimizers

    if device_id >= 0:
        chainer.cuda.get_device_from_id(device_id).use()
        for net in nets.values():
            net.to_gpu()

    datasets = dataset.get_dataset()
    iterators = {}
    if isinstance(datasets, dict):
        for name, data in datasets.items():
            if name == 'train':
                train_iterator = chainer.iterators.SerialIterator(
                    data, batch_size)
            else:
                iterators[name] = chainer.iterators.SerialIterator(
                    data, batch_size, repeat=False, shuffle=False)
    else:
        train_iterator = chainer.iterators.SerialIterator(datasets, batch_size)
    updater = TrainingStep(train_iterator,
                           target_optimizer,
                           model.calculate_metrics,
                           device=device_id)
    trainer = Trainer(updater, (config['epoch'], 'epoch'),
                      out=config['output_dir'])
    if hasattr(model, 'make_eval_func'):
        for name, iterator in iterators.items():
            evaluator = extensions.Evaluator(
                iterator,
                target,
                eval_func=model.make_eval_func(target),
                device=device_id)
            trainer.extend(evaluator, name=name)

    dump_graph_node = app_train_config.get('dump_graph', None)
    if dump_graph_node is not None:
        trainer.extend(extensions.dump_graph(dump_graph_node))

    trainer.extend(extensions.snapshot(filename='snapshot.state'),
                   trigger=(1, 'epoch'))
    for k, net in nets.items():
        file_name = 'latest.{}.model'.format(k)
        trainer.extend(extensions.snapshot_object(net, filename=file_name),
                       trigger=(1, 'epoch'))
    max_value_trigger_key = app_train_config.get('max_value_trigger', None)
    min_value_trigger_key = app_train_config.get('min_value_trigger', None)
    if max_value_trigger_key is not None:
        trigger = triggers.MaxValueTrigger(max_value_trigger_key)
        for key, net in nets.items():
            file_name = 'best.{}.model'.format(key)
            trainer.extend(extensions.snapshot_object(net, filename=file_name),
                           trigger=trigger)
    elif min_value_trigger_key is not None:
        trigger = triggers.MinValueTrigger(min_value_trigger_key)
        for key, net in nets.items():
            file_name = 'best.{}.model'.format(key)
            trainer.extend(extensions.snapshot_object(net, file_name),
                           trigger=trigger)
    trainer.extend(extensions.LogReport())
    if len(optimizers) == 1:
        for name, opt in optimizers.items():
            if not hasattr(opt, 'lr'):
                continue
            trainer.extend(extensions.observe_lr(name))
    else:
        for name, opt in optimizers.items():
            if not hasattr(opt, 'lr'):
                continue
            key = '{}/lr'.format(name)
            trainer.extend(extensions.observe_lr(name, key))

    if extensions.PlotReport.available():
        plot_targets = app_train_config.get('plot_report', {})
        for name, targets in plot_targets.items():
            file_name = '{}.png'.format(name)
            trainer.extend(
                extensions.PlotReport(targets, 'epoch', file_name=file_name))

    if not args.silent:
        print_targets = app_train_config.get('print_report', [])
        if print_targets is not None and print_targets != []:
            trainer.extend(extensions.PrintReport(print_targets))
        trainer.extend(extensions.ProgressBar())

    trainer.extend(generate_image(nets['gen'], 10, 10,
                                  config['output_image_dir']),
                   trigger=(1, 'epoch'))

    if args.resume:
        chainer.serializers.load_npz(args.resume, trainer)

    trainer.run()
Esempio n. 10
0
def training(configuration, i, verbose=False):
    #setting parameters
    batch_size = configuration["n_batchsize"]
    epochs = configuration["n_epochs"]
    n_z = configuration["n_z"]
    n_categorical = configuration["n_categorical"]
    n_continuous = configuration["n_continuous"]
    experiment = configuration["experiment"]
    n_neurons_gen = configuration["n_neurons_gen"]
    n_neurons_dis = configuration["n_neurons_dis"]
    n_neurons_cri = configuration["n_neurons_cri"]
    gpu = configuration["gpu"]
    output_name = configuration["output_name"]

    #import the training data

    if experiment == "random_left_right":
        x_dim = 12  #problem
        xi_dim = 6  #trajectory
    else:
        x_dim = 14
        xi_dim = 7

    if verbose:
        print("Loading data")
    train = import_dataset.import_data(configuration, x_dim, xi_dim)
    train_iter = iterators.SerialIterator(train, batch_size)

    z_iter = iterators.RandomNoiseIterator(
        UniformNoiseGenerator(-1, 1, n_z + n_continuous), batch_size)

    # Creating the Neural Networks models
    gen = Generator(n_z + n_continuous, x_dim, xi_dim, n_neurons_gen)
    dis = Discriminator(x_dim,
                        xi_dim,
                        n_neurons_dis,
                        cinfogan=configuration["cinfogan"],
                        n_continuous=n_continuous)
    critic = Critic(x_dim, xi_dim, n_neurons_cri)

    if (configuration["cinfogan"]):
        saving_directory = "results/models/cinfogan_models_" + str(i)
    else:
        saving_directory = "results/models/cgan_models_" + str(i)

    if configuration["wasserstein"]:
        if verbose:
            print("Using Wasserstein")
        optimizer_generator = optimizers.RMSprop(lr=0.00005)
        optimizer_critic = optimizers.RMSprop(lr=0.00005)

        a = xp.zeros((1, xi_dim)).astype(xp.float32)
        b = xp.zeros((1, x_dim)).astype(xp.float32)
        critic(Variable(a), Variable(b))

        optimizer_generator.setup(gen)
        optimizer_critic.setup(critic)

        updater = WassersteinGANUpdater(
            iterator=train_iter,
            noise_iterator=z_iter,
            noise_dim=n_z,
            x_dim=x_dim,
            xi_dim=xi_dim,
            experiment=configuration["experiment"],
            optimizer_generator=optimizer_generator,
            optimizer_critic=optimizer_critic,
            device=gpu,
        )
    else:
        if verbose:
            print("Not using Wasserstein")
        optimizer_generator = optimizers.Adam()
        optimizer_discriminator = optimizers.SGD()

        optimizer_generator.setup(gen)
        optimizer_discriminator.setup(dis)

        if configuration["cinfogan"]:
            updater = GANUpdater(
                iterator=train_iter,
                noise_iterator=z_iter,
                noise_dim=n_z,
                continuous_dim=n_continuous,
                x_dim=x_dim,
                xi_dim=xi_dim,
                experiment=configuration["experiment"],
                optimizer_generator=optimizer_generator,
                optimizer_discriminator=optimizer_discriminator,
                collision_measure=configuration["collision_measure"],
                saving_directory=saving_directory,
                device=gpu)
        else:
            updater = Cgan_GANUpdater(
                iterator=train_iter,
                noise_iterator=z_iter,
                noise_dim=n_z,
                x_dim=x_dim,
                xi_dim=xi_dim,
                experiment=configuration["experiment"],
                optimizer_generator=optimizer_generator,
                optimizer_discriminator=optimizer_discriminator,
                collision_measure=configuration["collision_measure"],
                saving_directory=saving_directory,
                device=gpu)

    if verbose:
        print("setup trainer...")
    trainer = Trainer(updater, stop_trigger=(epochs, 'epoch'))

    # changing the name because we do multiple experiments
    if configuration["cinfogan"]:
        trainer.out = "results/models/cinfogan_models_" + str(i)
    else:
        trainer.out = "results/models/cgan_models_" + str(i)

    trainer.extend(extensions.LogReport())

    if configuration["wasserstein"]:
        print_report_args = [
            'epoch', 'gen/loss', 'cri/loss', 'lin_ratio', 'cgan_ratio',
            'diff_ratio'
        ]
    else:
        print_report_args = [
            'epoch', 'gen/loss', 'dis/loss', 'lin_ratio', 'cgan_ratio',
            'diff_ratio'
        ]

    if verbose:
        trainer.extend(extensions.PrintReport(print_report_args))
        trainer.extend(extensions.ProgressBar())
    if configuration["collision_measure"] != 0:
        trainer.extend(extensions.GeneratorSample(configuration, x_dim, xi_dim,
                                                  n_continuous, n_z, train),
                       trigger=(1, 'epoch'))

    # We delete the f_metric.dat file to be sure we do not mixed multiple experiment data.
    cmd = "touch results/f_metric.dat && rm results/f_metric.dat"
    os.system(cmd)

    if verbose:
        print("START TRAINING!!")
    trainer.run()

    if configuration["output_name"] != "":
        output_name = configuration["output_name"]
    else:
        output_name = str(configuration["experiment"])
Esempio n. 11
0
def main(args):
    result_dir_path = Path(args.result_dir)
    try:
        result_dir_path.mkdir(parents=True)
    except FileExistsError:
        pass

    with Path(args.setting).open("r") as f:
        setting = json.load(f)
    pprint.pprint(setting)

    chainer.config.user_gpu_mode = (args.g >= 0)
    if chainer.config.user_gpu_mode:
        chainer.backends.cuda.get_device_from_id(args.g).use()

    # 訓練用正常データ
    mnist_neg = get_mnist_num(setting["label"]["neg"])

    # iteratorを作成
    kwds = {
        "batch_size": setting["iterator"]["batch_size"],
        "shuffle": True,
        "repeat": True
    }
    neg_iter = iterators.SerialIterator(mnist_neg, **kwds)

    generator = model.Generator()
    discriminator = model.Discriminator()
    if chainer.config.user_gpu_mode:
        generator.to_gpu()
        discriminator.to_gpu()

    opt_g = optimizers.Adam(**setting["optimizer"])
    opt_g.setup(generator)
    opt_d = optimizers.Adam(**setting["optimizer"])
    opt_d.setup(discriminator)
    if setting["regularization"]["weight_decay"] > 0.0:
        opt_g.add_hook(
            chainer.optimizer.WeightDecay(
                setting["regularization"]["weight_decay"]))
        opt_d.add_hook(
            chainer.optimizer.WeightDecay(
                setting["regularization"]["weight_decay"]))

    updater = GANUpdater(neg_iter, opt_g, opt_d, **setting["updater"])
    trainer = Trainer(updater, (setting["iteration"], "iteration"),
                      out=args.result_dir)

    # テストデータを取得
    test_neg = get_mnist_num(setting["label"]["neg"], train=False)
    test_pos = get_mnist_num(setting["label"]["pos"], train=False)
    # 正常にラベル0,異常にラベル1を付与
    test_neg = chainer.datasets.TupleDataset(
        test_neg, np.zeros(len(test_neg), dtype=np.int32))
    test_pos = chainer.datasets.TupleDataset(
        test_pos, np.ones(len(test_pos), dtype=np.int32))
    test_ds = chainer.datasets.ConcatenatedDataset(test_neg, test_pos)
    test_iter = iterators.SerialIterator(test_ds,
                                         repeat=False,
                                         shuffle=True,
                                         batch_size=500)

    ev_target = model.EvalModel(generator, discriminator,
                                setting["updater"]["noise_std"])
    ev_target = model.ExtendedClassifier(ev_target)
    if chainer.config.user_gpu_mode:
        ev_target.to_gpu()
    evaluator = extensions.Evaluator(
        test_iter,
        ev_target,
        device=args.g if chainer.config.user_gpu_mode else None)
    trainer.extend(evaluator)

    # 訓練経過の表示などの設定
    trigger = (5000, "iteration")
    trainer.extend(extensions.LogReport(trigger=trigger))
    trainer.extend(extensions.PrintReport(
        ["iteration", "generator/loss", "generator/l2", "discriminator/loss"]),
                   trigger=trigger)
    trainer.extend(extensions.ProgressBar())
    trainer.extend(
        extensions.PlotReport(("generator/loss", "discriminator/loss"),
                              "iteration",
                              file_name="loss_plot.eps",
                              trigger=trigger))
    trainer.extend(
        extensions.PlotReport(["generator/l2"],
                              "iteration",
                              file_name="gen_l2_plot.eps",
                              trigger=trigger))
    trainer.extend(
        extensions.PlotReport(
            ("validation/main/F", "validation/main/accuracy"),
            "iteration",
            file_name="acc_plot.eps",
            trigger=trigger))
    trainer.extend(ext_save_img(generator, test_pos, test_neg,
                                result_dir_path / "out_images",
                                setting["updater"]["noise_std"]),
                   trigger=trigger)
    trainer.extend(extensions.snapshot_object(
        generator, "gen_iter_{.updater.iteration:06d}.model"),
                   trigger=trigger)
    trainer.extend(extensions.snapshot_object(
        discriminator, "dis_iter_{.updater.iteration:06d}.model"),
                   trigger=trigger)

    # 訓練開始
    trainer.run()
Esempio n. 12
0
v_iter = MultithreadIterator(validation,
                             batch_size=batch,
                             repeat=True,
                             shuffle=True,
                             n_threads=batch)

model = Network(channels, blocks, ksize)
if device >= 0: model.to_gpu()
optimizer = optimizers.Adam().setup(model)
updater = CustomUpdater({
    "main": t_iter,
    "test": v_iter
}, optimizer, (patch, patch))

trainer = Trainer(updater, (epoch, "epoch"), out=out)
log = extensions.LogReport()
trainer.extend(log)
trainer.extend(
    extensions.PrintReport(["epoch", "iteration", "loss", "test"], log))
trainer.extend(extensions.ProgressBar(update_interval=1))
trainer.extend(lambda trainer: save_hdf5(
    f"{out}/m{trainer.updater.iteration}.hdf5", model),
               trigger=(5, "epoch"))
trainer.extend(lambda trainer: save_hdf5(
    f"{out}/o{trainer.updater.iteration}.hdf5", optimizer),
               trigger=(5, "epoch"))

trainer.run()
save_hdf5(f"{out}/model.hdf5", model)
save_hdf5(f"{out}/optimizer.hdf5", optimizer)
Esempio n. 13
0
def main():
    parser = argparse.ArgumentParser(description='training mnist')
    parser.add_argument('--gpu',
                        '-g',
                        default=-1,
                        type=int,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=300,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=100,
                        help='Number of images in each mini-batch')
    parser.add_argument('--seed',
                        '-s',
                        type=int,
                        default=0,
                        help='Random seed')
    parser.add_argument('--report_trigger',
                        '-rt',
                        type=str,
                        default='1e',
                        help='Interval for reporting (Ex.100i/1e)')
    parser.add_argument('--save_trigger',
                        '-st',
                        type=str,
                        default='1e',
                        help='Interval for saving the model (Ex.100i/1e)')
    parser.add_argument('--load_model',
                        '-lm',
                        type=str,
                        default=None,
                        help='Path of the model object to load')
    parser.add_argument('--load_optimizer',
                        '-lo',
                        type=str,
                        default=None,
                        help='Path of the optimizer object to load')
    args = parser.parse_args()

    if not Path('output').exists():
        Path('output').mkdir()
    start_time = datetime.now()
    save_dir = Path('output/{}'.format(start_time.strftime('%Y%m%d_%H%M')))

    random.seed(args.seed)
    np.random.seed(args.seed)
    cupy.random.seed(args.seed)
    chainer.config.cudnn_deterministic = True

    model = L.Classifier(SEResNet50(n_class=101))
    # model = L.Classifier(SERes2Net50(n_class=101))
    # model = L.Classifier(GCResNet50(n_class=101))
    # model = L.Classifier(AAResNet50(n_class=101))

    if args.load_model is not None:
        serializers.load_npz(args.load_model, model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    optimizer = optimizers.Adam(alpha=1e-3,
                                weight_decay_rate=1e-4,
                                amsgrad=True)
    optimizer.setup(model)
    if args.load_optimizer is not None:
        serializers.load_npz(args.load_optimizer, optimizer)

    augmentation = {
        'HorizontalFlip': {
            'p': 0.5
        },
        'PadIfNeeded': {
            'p': 1.0,
            'min_height': 512,
            'min_width': 512
        },
        'Rotate': {
            'p': 1.0,
            'limit': 15,
            'interpolation': 1
        },
        'Resize': {
            'p': 1.0,
            'height': 248,
            'width': 248,
            'interpolation': 2
        },
        'RandomScale': {
            'p': 1.0,
            'scale_limit': 0.09,
            'interpolation': 2
        },
        'RandomCrop': {
            'p': 1.0,
            'height': 224,
            'width': 224
        },
    }
    resize = {
        'PadIfNeeded': {
            'p': 1.0,
            'min_height': 512,
            'min_width': 512
        },
        'Resize': {
            'p': 1.0,
            'height': 224,
            'width': 224,
            'interpolation': 2
        }
    }

    sl = slice(0, None, 5)
    train_data = Food101Dataset(augmentation=augmentation, drop_index=sl)
    valid_data = Food101Dataset(augmentation=resize, index=sl)

    train_iter = iterators.SerialIterator(train_data, args.batchsize)
    valid_iter = iterators.SerialIterator(valid_data,
                                          args.batchsize,
                                          repeat=False,
                                          shuffle=False)

    updater = StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = Trainer(updater, (args.epoch, 'epoch'), out=save_dir)

    report_trigger = (int(args.report_trigger[:-1]), 'iteration'
                      if args.report_trigger[-1] == 'i' else 'epoch')
    trainer.extend(extensions.LogReport(trigger=report_trigger))
    trainer.extend(extensions.Evaluator(valid_iter, model, device=args.gpu),
                   name='val',
                   trigger=report_trigger)
    trainer.extend(extensions.PrintReport([
        'epoch', 'iteration', 'main/loss', 'main/accuracy', 'val/main/loss',
        'val/main/accuracy', 'elapsed_time'
    ]),
                   trigger=report_trigger)
    trainer.extend(
        extensions.PlotReport(['main/loss', 'val/main/loss'],
                              x_key=report_trigger[1],
                              marker='.',
                              file_name='loss.png',
                              trigger=report_trigger))
    trainer.extend(
        extensions.PlotReport(['main/accuracy', 'val/main/accuracy'],
                              x_key=report_trigger[1],
                              marker='.',
                              file_name='accuracy.png',
                              trigger=report_trigger))

    save_trigger = (int(args.save_trigger[:-1]),
                    'iteration' if args.save_trigger[-1] == 'i' else 'epoch')
    trainer.extend(extensions.snapshot_object(
        model,
        filename='model_{0}-{{.updater.{0}}}.npz'.format(save_trigger[1])),
                   trigger=save_trigger)
    trainer.extend(extensions.snapshot_object(
        optimizer,
        filename='optimizer_{0}-{{.updater.{0}}}.npz'.format(save_trigger[1])),
                   trigger=save_trigger)
    trainer.extend(extensions.ProgressBar())

    if save_dir.exists():
        shutil.rmtree(save_dir)
    save_dir.mkdir()

    # Write parameters text
    with open(save_dir / 'train_params.txt', 'w') as f:
        f.write('model: {}\n'.format(model.predictor.__class__.__name__))
        f.write('n_epoch: {}\n'.format(args.epoch))
        f.write('batch_size: {}\n'.format(args.batchsize))
        f.write('seed: {}\n'.format(args.seed))
        f.write('n_data_train: {}\n'.format(len(train_data)))
        f.write('n_data_val: {}\n'.format(len(valid_data)))
        f.write('augmentation: \n')
        for k, v in augmentation.items():
            f.write('  {}: {}\n'.format(k, v))

    trainer.run()
Esempio n. 14
0
    def __call__(self, x):
        return F.sum(x)

    def to_gpu(self, device=None):
        super(Model, self).to_gpu(device)


class Dataset(DatasetMixin):
    def __init__(self):
        super(Dataset, self).__init__()

    def __len__(self):
        return 1024

    def get_example(self, i):
        return np.array([[1, 2], [3, 4]], dtype=np.float32)


dataset = Dataset()
iterator = SerialIterator(dataset, 2, False, False)
model_training = Model()
model_training.to_gpu()
optimizer = Adam()
optimizer.setup(model_training)
updater = StandardUpdater(iterator, optimizer, device=0)
trainer = Trainer(updater, stop_trigger=[1, "iteration"])
trainer.extend(snapshot_object(model_training, "model_iter_{.updater.iteration}"), trigger=[1, "iteration"])
trainer.run()
model_test = Model()
load_npz("result/model_iter_1", model_test)
model_test.to_gpu()
Esempio n. 15
0
def main(epochs=257*8, lr=0.38, seq_len=120, pred_len=39, out="result", device=0):
    
    # CHOOSE ONE:
    # get the training dataset but keep a slice for validation
    dataset = get_dataset(182, -39 -39)
    # get the entire dataset
    #dataset = get_dataset(182, -39)
    
    iter = ParallelSequentialIterator(dataset, pred_len=1, repeat=True)

    model = Model(pred_len=pred_len, dropout=0.1)
    if device >= 0:
        model.to_gpu()

    # Try some different optimizers
    #optimizer = optimizers.Adam(alpha=lr)
    #optimizer = optimizers.MomentumSGD(lr=lr, momentum=0.9)
    optimizer = optimizers.RMSpropGraves(lr=lr, alpha=0.95, momentum=0.2)
    #optimizer = optimizers.RMSprop(lr=lr, alpha=0.5)

    optimizer.setup(model)

    #optimizer.add_hook(chainer.optimizer.GradientClipping(5))#grad_clip))
    #optimizer.add_hook(chainer.optimizer.WeightDecay(1.E-7))

    updater = BPTTUpdater(iter, optimizer, seq_len=seq_len, pred_len=pred_len, data_len=len(dataset), device=device)
    trainer = Trainer(updater, (epochs, 'epoch'), out=out)

    interval = 10

    # Try some learning-rate decay
    #trainer.extend(extensions.ExponentialShift('lr', 0.995)) #0.1, lr, lr * 0.1), trigger=(10, 'epoch'))

    trainer.extend(extensions.observe_lr(), trigger=(interval, "iteration"))
    
    trainer.extend(extensions.LogReport(trigger=(interval, 'iteration')))
    trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'loss', 'lr']),
            trigger=(interval, 'iteration'))
    trainer.extend(extensions.ProgressBar(update_interval=interval))

    # export snapshots to resume training
    trainer.extend(extensions.snapshot(filename='snapshot_epoch_{.updater.epoch}'), trigger=(257*6, "epoch"))
    trainer.extend(extensions.snapshot_object(model, "model_epoch_{.updater.epoch}"), trigger=(257*2, "epoch"))

    # change to True to resume from file
    if False:
        chainer.serializers.load_npz('result/snapshot_epoch_1030', trainer)
        
    trainer.run()


    # save model
    from chainer import serializers
    serializers.save_npz('restaurant.model', model)
Esempio n. 16
0
def main():
    args = create_args('train')
    targs = get_params_from_target(args.target)
    targs['A'] = args.anchors_per_position
    targs['T'] = args.max_points_per_voxel
    targs['K'] = args.max_voxels
    result_dir = create_result_dir(args.model_name)
    dump_setting(targs, result_dir)

    # Prepare devices
    devices = {}
    for gid in [int(i) for i in args.gpus.split(',')]:
        if 'main' not in devices:
            devices['main'] = gid
        else:
            devices['gpu{}'.format(gid)] = gid

    # Instantiate a model
    model = CRMapDetector(VoxelNet, args.loss_alpha, args.loss_beta, **targs)

    # Instantiate a optimizer
    optimizer = get_optimizer(model, **vars(args))

    # Setting up datasets
    prep = VoxelRPNPreprocessor(**targs)
    train = TransformDataset(
        KITTI(args.kitti_path, 'train',
              train_proportion=args.train_proportion), prep)
    valid = TransformDataset(
        KITTI(args.kitti_path, 'val', valid_proportion=args.valid_proportion),
        prep)
    print('train: {}, valid: {}'.format(len(train), len(valid)))

    # Iterator
    train_iter = MultiprocessIterator(train, args.batchsize)
    valid_iter = MultiprocessIterator(valid,
                                      args.valid_batchsize,
                                      repeat=False,
                                      shuffle=False)

    # Updater
    updater = ParallelUpdater(train_iter, optimizer, devices=devices)
    trainer = Trainer(updater, (args.epoch, 'epoch'), out=result_dir)

    # Extentions
    trainer.extend(extensions.Evaluator(valid_iter,
                                        model,
                                        device=devices['main']),
                   trigger=(args.valid_freq, 'epoch'))
    trainer.extend(extensions.snapshot(),
                   trigger=(args.snapshot_iter, 'iteration'))
    trainer.extend(extensions.LogReport(),
                   trigger=(args.show_log_iter, 'iteration'))
    trainer.extend(extensions.ProgressBar(update_interval=20))
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'iteration', 'main/conf_loss', 'main/reg_loss',
            'validation/main/conf_loss', 'validation/main/reg_loss'
        ]))

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

    # Train and save
    trainer.run()
    model.save(create_result_file(args.model_name))
Esempio n. 17
0
def train(opt):
    if opt.use_cpu:
        device = -1
        print('[Message] use CPU')
    else:
        device = 0
        print('[Message] use GPU0')

    annotated = get_dataset(opt)
    unlabeled = get_unlabel_dataset(opt)
    print('[Message] loaded options')

    train_iter = SerialIterator(annotated, opt.batch_size, shuffle=True)
    print('[Message] converted to iterator (train)')
    semi_iter = SerialIterator(unlabeled, opt.batch_size, shuffle=True)
    print('[Message] converted to iterator (semi)')

    gen = ResNetDeepLab(opt)
    #gen = DilatedFCN(opt)
    #gen = UNet(opt)

    if device != -1:
        gen.to_gpu(device)  #use GPU
    g_optim = Adam(alpha=opt.g_lr, beta1=opt.g_beta1, beta2=opt.g_beta2)
    g_optim.setup(gen)
    if opt.g_weight_decay > 0:
        g_optim.add_hook(WeightDecay(opt.g_weight_decay))
    print('[Message] setuped Generator')

    dis = FCN(opt)
    if device != -1:
        dis.to_gpu(device)  #use GPU
    d_optim = Adam(alpha=opt.d_lr, beta1=opt.d_beta1, beta2=opt.d_beta2)
    d_optim.setup(dis)
    print('[Message] setuped Discriminator')

    updater = AdvSemiSeg_Updater(opt,
                                 iterator={
                                     'main': train_iter,
                                     'semi': semi_iter
                                 },
                                 optimizer={
                                     'gen': g_optim,
                                     'dis': d_optim
                                 },
                                 device=device)
    print('[Message] initialized Updater')

    trainer = Trainer(updater, (opt.max_epoch, 'epoch'), out=opt.out_dir)
    print('[Message] initialized Trainer')

    #chainer training extensions
    trainer.extend(ex.LogReport(log_name=None, trigger=(1, 'iteration')))
    trainer.extend(ex.ProgressBar((opt.max_epoch, 'epoch'), update_interval=1))

    trainer.extend(
        ex.PlotReport(['gen/adv_loss', 'dis/adv_loss', 'gen/semi_adv_loss'],
                      x_key='iteration',
                      file_name='adversarial_loss.png',
                      trigger=(100, 'iteration')))

    #test
    trainer.extend(
        ex.PlotReport(['gen/adv_loss'],
                      x_key='iteration',
                      file_name='adv_gen_loss.png',
                      trigger=(100, 'iteration')))

    trainer.extend(
        ex.PlotReport(['gen/ce_loss'],
                      x_key='iteration',
                      file_name='cross_entropy_loss.png',
                      trigger=(100, 'iteration')))

    trainer.extend(
        ex.PlotReport(['gen/semi_st_loss'],
                      x_key='iteration',
                      file_name='self_teach_loss.png',
                      trigger=(100, 'iteration')))

    trainer.extend(
        ex.PlotReport(['gen/loss', 'dis/loss', 'gen/semi_loss'],
                      x_key='iteration',
                      file_name='loss.png',
                      trigger=(100, 'iteration')))

    trainer.extend(
        ex.PlotReport(['gen/loss', 'dis/loss', 'gen/semi_loss'],
                      x_key='epoch',
                      file_name='loss_details.png',
                      trigger=(5, 'epoch')))

    trainer.extend(
        ex.PlotReport(['gen/semi_loss'],
                      x_key='epoch',
                      file_name='semi_loss.png',
                      trigger=(1, 'epoch')))

    #snap
    trainer.extend(ex.snapshot_object(
        gen, 'gen_snapshot_epoch-{.updater.epoch}.npz'),
                   trigger=(opt.snap_interval_epoch, 'epoch'))
    trainer.extend(ex.snapshot_object(
        dis, 'dis_snapshot_epoch-{.updater.epoch}.npz'),
                   trigger=(opt.snap_interval_epoch, 'epoch'))

    trainer.extend(lambda *args: updater.save_img(),
                   trigger=(opt.img_interval_iteration, 'iteration'),
                   priority=PRIORITY_READER)

    trainer.extend(lambda *args: updater.ignition_semi_learning(),
                   trigger=(opt.semi_ignit_iteration, 'iteration'),
                   priority=PRIORITY_READER)

    trainer.extend(lambda *args: adam_lr_poly(opt, trainer),
                   trigger=(100, 'iteration'))
    print('[Message] initialized extension')

    print('[Message] start training ...')
    trainer.run()  #start learning
Esempio n. 18
0
def main():
    args = get_args()
 
    generator = Generator(hidden=args.hidden)
    discriminator = Discriminator()
    chainer.cuda.get_device_from_id(args.gpu).use()
    generator.to_gpu() 
    discriminator.to_gpu()
 
    gen_optimizer = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5)
    gen_optimizer.setup(generator)
    gen_optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec')
    dis_optimizer = chainer.optimizers.Adam(alpha=0.0002, beta1=0.5)
    dis_optimizer.setup(discriminator)
    dis_optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001), 'hook_dec')
 
    train, _ = chainer.datasets.get_mnist(withlabel=False, ndim=3, scale=255.)
    train_iter = chainer.iterators.SerialIterator(train, args.batch_size)
    updater = Updater(models=(generator, discriminator), iterator=train_iter, optimizer={'generator':gen_optimizer, 'discriminator':dis_optimizer}, device=args.gpu)
    trainer = Trainer(updater, (args.epoch, 'epoch'), out="result")

    trainer.extend(extensions.LogReport(trigger=(1, 'epoch')))
    trainer.extend(extensions.PrintReport(
        ['epoch', 'generator/loss', 'discriminator/loss',]), trigger=(1, 'epoch'))
    trainer.extend(extensions.ProgressBar(update_interval=(1, 'epoch')))
    trainer.extend(extensions.snapshot(
        filename='snapshot_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch'))
    trainer.extend(extensions.snapshot_object(
        generator, 'gen_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch'))
    trainer.extend(extensions.snapshot_object(
        discriminator, 'dis_epoch_{.updater.epoch}.npz'), trigger=(1, 'epoch'))

    trainer.run()
Esempio n. 19
0
            cur_y += 20
        cur_x += 20

iterator = SerialIterator(image_pairs, batch, shuffle=True)

model = RgbNet()
model = model.to_gpu()

optimizer = optimizers.Adam().setup(model)

updater = CustomUpdater(iterator, optimizer, device)

trainer = Trainer(updater, (epoch, "epoch"), out="result")
# 学習の進展を表示するようにする
trainer.extend(extensions.ProgressBar())
trainer.extend(extensions.LogReport())

# 中間結果を保存する
n_save = 0


@chainer.training.make_extension(trigger=(10, 'epoch'))
def save_model(trainer):
    # NNのデータを保存
    global n_save
    n_save = n_save + 1
    chainer.serializers.save_hdf5('a-' + str(n_save) + '.hdf5', model)


trainer.extend(save_model)
Esempio n. 20
0
def main():
    comm = mn.create_communicator("pure_nccl")
    device = comm.intra_rank

    config = get_config()

    print("pid {}: mask loading...".format(comm.rank))
    load_mask_module = import_module(
        config["additional information"]["mask"]["loader"]["module"],
        config["additional information"]["mask"]["loader"]["package"])
    load_mask = getattr(
        load_mask_module,
        config["additional information"]["mask"]["loader"]["function"])
    mask = load_mask(
        **config["additional information"]["mask"]["loader"]["params"])
    print("pid {}: done.".format(comm.rank))
    if comm.rank == 0:
        print("mask.shape: {}".format(mask.shape))

    model_module = import_module(config["model"]["module"],
                                 config["model"]["package"])
    Model = getattr(model_module, config["model"]["class"])
    model = Model(comm=comm, mask=mask, **config["model"]["params"])

    optimizer_module = import_module(config["optimizer"]["module"],
                                     config["optimizer"]["package"])
    Optimizer = getattr(optimizer_module, config["optimizer"]["class"])
    optimizer = mn.create_multi_node_optimizer(
        Optimizer(**config["optimizer"]["params"]), comm)
    optimizer.setup(model)

    if device >= 0:
        chainer.backends.cuda.get_device_from_id(device).use()
        model.to_gpu()
        print("pid {}: GPU {} enabled".format(comm.rank, device))

    if comm.rank == 0:
        dataset_module = import_module(config["dataset"]["module"],
                                       config["dataset"]["package"])
        Dataset = getattr(dataset_module, config["dataset"]["class"])
        train_dataset = Dataset(**config["dataset"]["train"]["params"])
        valid_dataset = Dataset(**config["dataset"]["valid"]["params"])
    else:
        train_dataset = None
        valid_dataset = None

    train_dataset = mn.datasets.scatter_dataset(train_dataset,
                                                comm,
                                                shuffle=True)
    valid_dataset = mn.datasets.scatter_dataset(valid_dataset,
                                                comm,
                                                shuffle=True)

    train_iterator = Iterator(train_dataset, config["batch"]["train"])
    valid_iterator = Iterator(valid_dataset, config["batch"]["valid"], False,
                              False)

    updater = Updater(train_iterator, optimizer, device=device)
    trainer = Trainer(updater, **config["trainer"]["params"])

    checkpointer = mn.create_multi_node_checkpointer(config["general"]["name"],
                                                     comm)
    checkpointer.maybe_load(trainer, optimizer)
    trainer.extend(checkpointer,
                   trigger=tuple(config["trainer"]["snapshot_interval"]))

    evaluator = Evaluator(valid_iterator, model, device=device)
    evaluator = mn.create_multi_node_evaluator(evaluator, comm)
    trainer.extend(evaluator)

    trainer.extend(observe_lr(), trigger=config["trainer"]["log_interval"])
    if comm.rank == 0:
        trainer.extend(LogReport(trigger=config["trainer"]["log_interval"]))
        trainer.extend(PrintReport(
            ["epoch", "iteration", "main/loss", "validation/main/loss"]),
                       trigger=config["trainer"]["log_interval"])
        trainer.extend(ProgressBar(update_interval=1))

    trainer.run()
Esempio n. 21
0
        path = filepath(directory, f"dopt_{stage}_{trainer.updater.iteration}",
                        "hdf5")
        path = path if force else altfilepath(path)
        serializers.save_hdf5(path, discriminator_optimizer)

    return func


# Prepare trainer
logpath = filepath(args.result, "report", "log")
logname = basename(logpath if args.force else altfilepath(logpath))
plotpath = filepath(args.result, "plot", "png")
plotname = basename(plotpath if args.force else altfilepath(plotpath))
trainer = Trainer(updater, (args.epoch, "epoch"), out=args.result)
if args.print[0] > 0:
    trainer.extend(extensions.ProgressBar(update_interval=args.print[0]))
if args.print[1] > 0:
    trainer.extend(
        extensions.PrintReport([
            "epoch", "iteration", "alpha", "loss (gen)", "loss (dis)",
            "loss (grad)"
        ],
                               extensions.LogReport(trigger=(args.print[1],
                                                             "iteration"),
                                                    log_name=None)))
if args.write[0] > 0:
    trainer.extend(save_middle_images(averaged_generator, args.stage,
                                      args.result, args.number, args.batch,
                                      args.mix, args.force),
                   trigger=(args.write[0], "iteration"))
if args.write[1] > 0:
Esempio n. 22
0
def main():

    # Parse arguments.
    with open('params.yml') as stream:
        args = yaml.load(stream)

    # Prepare training data.
    train, val = chainer.datasets.get_mnist(ndim=3)
    if args['memory'] == 'gpu' and 0 <= args['gpu']:
        train = [(cp.array(x), cp.array(y)) for x, y in train]
        val = [(cp.array(x), cp.array(y)) for x, y in val]

    # Prepare model.
    class Classifier(chainer.Chain):
        def __init__(self, predictor):
            super(Classifier, self).__init__()

            with self.init_scope():
                self.predictor = predictor

        def forward(self, batch, labels):
            embeddings = self.predictor(batch)
            loss = functions.batch_all_triplet_loss(
                embeddings,
                labels,
                margin=args['margin'],
                dist_type=args['dist_type'])
            chainer.reporter.report(
                {
                    'loss':
                    loss,
                    'VAL':
                    functions.validation_rate(embeddings,
                                              labels,
                                              threshold=args['threshold'],
                                              dist_type=args['dist_type']),
                    'FAR':
                    functions.false_accept_rate(embeddings,
                                                labels,
                                                threshold=args['threshold'],
                                                dist_type=args['dist_type'])
                }, self)
            return loss

    predictor = FaceNet()
    model = Classifier(predictor)
    if 0 <= args['gpu']:
        chainer.backends.cuda.get_device_from_id(args['gpu']).use()
        model.to_gpu()

    # Prepare optimizer.
    optimizer = chainer.optimizers.AdaDelta()
    optimizer.setup(model)

    # Make output directory.
    timestamp = f'{datetime.datetime.now():%Y%m%d%H%M%S}'
    directory = f'./temp/{timestamp}/'
    os.makedirs(directory, exist_ok=True)
    shutil.copy('params.yml', f'{directory}params.yml')

    # Prepare extensions.
    if args['memory'] == 'cpu' and 0 <= args['gpu']:

        def converter(batch, device=None, padding=None):
            return concat_examples([(cp.array(x), cp.array(y))
                                    for x, y in batch],
                                   device=device,
                                   padding=padding)
    else:
        converter = concat_examples

    class DumpEmbeddings(chainer.training.extension.Extension):
        def __init__(self, iterator, model, converter, filename):
            self.iterator = iterator
            self.model = model
            self.converter = converter
            self.filename = filename
            self.xp = cp if 0 <= args['gpu'] else np

        def __call__(self, trainer):
            if hasattr(self.iterator, 'reset'):
                self.iterator.reset()
                it = self.iterator
            else:
                it = copy.copy(self.iterator)

            def forward(batch):
                x, _ = self.converter(batch)
                y = self.model.predictor(x)
                embeddings = y.data
                if 0 <= args['gpu']:
                    embeddings = chainer.backends.cuda.to_cpu(embeddings)
                return embeddings

            embeddings = np.vstack([forward(batch) for batch in it])
            np.save(os.path.join(trainer.out, self.filename.format(trainer)),
                    embeddings)

    train_iter = SerialIterator(train, args['batch_size'])
    test_iter = SerialIterator(val,
                               args['batch_size'],
                               repeat=False,
                               shuffle=False)
    updater = StandardUpdater(train_iter, optimizer, converter=converter)
    trainer = Trainer(updater,
                      stop_trigger=(args['epochs'], 'epoch'),
                      out=directory)
    trainer.extend(dump_graph('main/loss', out_name='model.dot'))
    trainer.extend(Evaluator(test_iter, model, converter=converter))
    trainer.extend(snapshot_object(target=model,
                                   filename='model-{.updater.epoch:04d}.npz'),
                   trigger=(args['checkpoint_interval'], 'epoch'))
    trainer.extend(LogReport(log_name='log'))
    trainer.extend(
        PlotReport(y_keys=['main/loss', 'validation/main/loss'],
                   x_key='epoch',
                   file_name='loss.png'))
    trainer.extend(
        PlotReport(y_keys=['main/VAL', 'validation/main/VAL'],
                   x_key='epoch',
                   file_name='VAL.png'))
    trainer.extend(
        PlotReport(y_keys=['main/FAR', 'validation/main/FAR'],
                   x_key='epoch',
                   file_name='FAR.png'))
    trainer.extend(
        PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/VAL',
            'validation/main/VAL', 'main/FAR', 'validation/main/FAR',
            'elapsed_time'
        ]))
    trainer.extend(DumpEmbeddings(test_iter,
                                  model,
                                  converter=converter,
                                  filename='embeddings-{.updater.epoch}.npy'),
                   trigger=(args['checkpoint_interval'], 'epoch'))
    trainer.extend(ProgressBar(update_interval=1))

    # Execute training.
    trainer.run()
Esempio n. 23
0
def main(arguments, neg_labels, pos_labels):
    output_dir_path = Path(arguments.output_dir)
    if not output_dir_path.exists():
        output_dir_path.mkdir()

    # settings
    adam_setting = {
        "alpha": arguments.adam_alpha,
        "beta1": arguments.adam_beta1,
        "beta2": arguments.adam_beta2
    }

    updater_setting = {
        "n_dis": arguments.n_dis,
        "l2_lam": arguments.l2_lam,
        "noise_std": arguments.noise_std
    }
    chainer.config.user_gpu_mode = (arguments.gpu_id >= 0)
    if chainer.config.user_gpu_mode:
        chainer.backends.cuda.get_device_from_id(arguments.gpu_id).use()

    # 訓練用正常データ
    mnist_neg = get_mnist_num(neg_labels)

    # iteratorを作成
    iterator_setting = {
        "batch_size": arguments.batch_size,
        "shuffle": True,
        "repeat": True
    }
    neg_iter = iterators.SerialIterator(mnist_neg, **iterator_setting)

    generator = Generator()
    discriminator = Discriminator()
    if chainer.config.user_gpu_mode:
        generator.to_gpu()
        discriminator.to_gpu()

    opt_g = optimizers.Adam(**adam_setting)
    opt_g.setup(generator)
    opt_d = optimizers.Adam(**adam_setting)
    opt_d.setup(discriminator)
    if arguments.weight_decay > 0.0:
        opt_g.add_hook(chainer.optimizer.WeightDecay(arguments.weight_decay))
        opt_d.add_hook(chainer.optimizer.WeightDecay(arguments.weight_decay))

    updater = GANUpdater(neg_iter, opt_g, opt_d, **updater_setting)
    trainer = Trainer(updater, (arguments.iteration, "iteration"),
                      out=str(output_dir_path))

    # テストデータを取得
    test_neg = get_mnist_num(neg_labels, train=False)
    test_pos = get_mnist_num(pos_labels, train=False)
    # 正常にラベル0,異常にラベル1を付与
    test_neg = chainer.datasets.TupleDataset(
        test_neg, np.zeros(len(test_neg), dtype=np.int32))
    test_pos = chainer.datasets.TupleDataset(
        test_pos, np.ones(len(test_pos), dtype=np.int32))
    test_ds = chainer.datasets.ConcatenatedDataset(test_neg, test_pos)
    test_iter = iterators.SerialIterator(test_ds,
                                         repeat=False,
                                         shuffle=True,
                                         batch_size=500)

    ev_target = EvalModel(generator, discriminator, arguments.noise_std)
    ev_target = ExtendedClassifier(ev_target)
    if chainer.config.user_gpu_mode:
        ev_target.to_gpu()
    evaluator = extensions.Evaluator(
        test_iter,
        ev_target,
        device=arguments.gpu_id if chainer.config.user_gpu_mode else None)
    trainer.extend(evaluator)

    # 訓練経過の表示などの設定
    trigger = (5000, "iteration")
    trainer.extend(extensions.LogReport(trigger=trigger))
    trainer.extend(extensions.PrintReport(
        ["iteration", "generator/loss", "generator/l2", "discriminator/loss"]),
                   trigger=trigger)
    trainer.extend(extensions.ProgressBar())
    trainer.extend(
        extensions.PlotReport(("generator/loss", "discriminator/loss"),
                              "iteration",
                              file_name="loss_plot.eps",
                              trigger=trigger))
    trainer.extend(
        extensions.PlotReport(["generator/l2"],
                              "iteration",
                              file_name="gen_l2_plot.eps",
                              trigger=trigger))
    trainer.extend(
        extensions.PlotReport(
            ("validation/main/F", "validation/main/accuracy"),
            "iteration",
            file_name="acc_plot.eps",
            trigger=trigger))
    trainer.extend(ext_save_img(generator, test_pos, test_neg,
                                output_dir_path / "out_images",
                                arguments.noise_std),
                   trigger=trigger)
    trainer.extend(extensions.snapshot_object(
        generator, "gen_iter_{.updater.iteration:06d}.model"),
                   trigger=trigger)
    trainer.extend(extensions.snapshot_object(
        discriminator, "dis_iter_{.updater.iteration:06d}.model"),
                   trigger=trigger)

    # 訓練開始
    trainer.run()
Esempio n. 24
0
def main():
    # chainer.config.autotune = True
    # chainer.config.cudnn_fast_batch_normalization = True

    print("dataset", CONFIG.dataset)
    print("output_dir:", output_dir)

    if CONFIG.dataset == "tennis_serve":
        dataset = load_penn_action(dataset_dir=CONFIG.dataset_path,
                                   stride=CONFIG.penn_action.stride,
                                   dict_ok=False)
        dataset_train = dataset[:115]
        dataset_test = dataset[115:]
    elif CONFIG.dataset == "pouring":
        dataset_train, dataset_test = load_pouring(
            dataset_dir=CONFIG.dataset_path,
            stride=CONFIG.pouring.stride,
            dict_ok=False)
    elif CONFIG.dataset == "multiview_pouring":
        dataset_train, dataset_test = load_multiview_pouring(
            dataset_dir=CONFIG.dataset_path,
            stride=CONFIG.multiview_pouring.stride,
            dict_ok=False)
    else:
        print("dataset error.")
        exit()

    dataset_train = load_dataset(dataset_train,
                                 augment=None,
                                 img_size=CONFIG.img_size,
                                 k=CONFIG.k)
    dataset_test = load_dataset(dataset_test,
                                augment=None,
                                img_size=CONFIG.img_size,
                                k=CONFIG.k)
    train_iter = MultiprocessIterator(dataset_train,
                                      batch_size=CONFIG.batchsize,
                                      n_processes=6)
    test_iter = MultiprocessIterator(dataset_test,
                                     batch_size=1,
                                     n_processes=6,
                                     repeat=False,
                                     shuffle=None)

    model = tcc(use_bn=True, k=CONFIG.k)
    device = chainer.get_device(OPTION.device)
    device.use()
    model.to_device(device)

    optimizer = make_optimizer(model)

    if CONFIG.weight_decay_rate != 0:
        for param in model.params():
            param.update_rule.add_hook(WeightDecay(CONFIG.weight_decay_rate))

    updater = tcc_updater({"main": train_iter}, optimizer, device)

    trainer = Trainer(updater, (CONFIG.iteration, 'iteration'), out=output_dir)

    display_interval = (100, 'iteration')
    plot_interval = (100, 'iteration')
    trainer.extend(extensions.ProgressBar(update_interval=5))
    trainer.extend(
        extensions.LogReport(trigger=display_interval, filename='log.txt'))
    trainer.extend(extensions.PrintReport(
        ["iteration", "main/loss", "test/loss", "test/tau", "elapsed_time"]),
                   trigger=display_interval)

    trainer.extend(extensions.PlotReport(["main/loss", "test/loss"],
                                         "iteration",
                                         file_name="loss.png"),
                   trigger=plot_interval)

    trainer.extend(evaluator(test_iter,
                             model,
                             device,
                             epoch=plot_interval[0],
                             out=output_dir),
                   trigger=plot_interval)
    trainer.extend(extensions.PlotReport(["test/tau"],
                                         "iteration",
                                         file_name="tau.png"),
                   trigger=plot_interval)

    trainer.extend(extensions.snapshot_object(model,
                                              "{.updater.iteration}" + ".npz"),
                   trigger=plot_interval)

    trainer.run()
Esempio n. 25
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpus', type=str, default="-1")
    parser.add_argument('--batchsize', type=int, default=2)
    parser.add_argument('--epoch', type=int, default=10)
    parser.add_argument('--mini', action="store_true")
    args = parser.parse_args()

    gpus = list(filter(lambda x: x >= 0, map(int, args.gpus.split(","))))

    num_class = len(voc_bbox_label_names)

    data_augmentation_transform = DataAugmentationTransform(512)
    center_detection_transform = CenterDetectionTransform(512, num_class, 4)

    train = TransformDataset(
        ConcatenatedDataset(
            VOCBboxDataset(year='2007', split='trainval'),
            VOCBboxDataset(year='2012', split='trainval')
        ),
        data_augmentation_transform
    )
    train = TransformDataset(train, center_detection_transform)
    if args.mini:
        train = datasets.SubDataset(train, 0, 100)
    train_iter = chainer.iterators.MultiprocessIterator(train, args.batchsize)

    test = VOCBboxDataset(
        year='2007', split='test',
        use_difficult=True, return_difficult=True)
    if args.mini:
        test = datasets.SubDataset(test, 0, 20)
    test_iter = chainer.iterators.SerialIterator(
        test, args.batchsize // len(gpus), repeat=False, shuffle=False)

    detector = CenterDetector(HourglassNet, 512, num_class)
    train_chain = CenterDetectorTrain(detector, 1, 0.1, 1)

    gpus.sort()
    first_gpu = gpus[0]
    remain_gpu = gpus[1:]
    train_chain.to_gpu(first_gpu)

    optimizer = Adam(amsgrad=True)
    optimizer.setup(train_chain)

    devices = {
        "main": first_gpu
    }

    for i, gpu in enumerate(remain_gpu):
        devices[f"{i + 2}"] = gpu

    updater = training.updaters.ParallelUpdater(
        train_iter,
        optimizer,
        devices=devices,
    )

    log_interval = 1, 'epoch'
    trainer = Trainer(updater, (args.epoch, 'epoch'))
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.observe_lr(), trigger=log_interval)
    trainer.extend(extensions.PrintReport(
        [
            'epoch', 'iteration', 'lr',
            'main/loss', 'main/hm_loss', 'main/wh_loss', 'main/offset_loss',
            'validation/main/map',
        ]),
        trigger=log_interval)
    trainer.extend(extensions.ProgressBar(update_interval=10))
    trainer.extend(
        DetectionVOCEvaluator(
            test_iter, detector, use_07_metric=True,
            label_names=voc_bbox_label_names),
        trigger=(1, 'epoch'))
    trainer.extend(
        extensions.snapshot_object(detector, 'detector{.updater.epoch:03}.npz'),
        trigger=(1, 'epoch')
    )

    trainer.run()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--batchsize', type=int, default=2)
    parser.add_argument('--epoch', type=int, default=10)
    parser.add_argument('--mini', action="store_true")
    parser.add_argument('--input_size', type=int, default=512)
    args = parser.parse_args()

    dtype = np.float32

    num_class = len(voc_bbox_label_names)

    data_augmentation_transform = DataAugmentationTransform(args.input_size)
    center_detection_transform = CenterDetectionTransform(args.input_size,
                                                          num_class,
                                                          4,
                                                          dtype=dtype)

    train = TransformDataset(
        ConcatenatedDataset(VOCBboxDataset(year='2007', split='trainval'),
                            VOCBboxDataset(year='2012', split='trainval')),
        data_augmentation_transform)
    train = TransformDataset(train, center_detection_transform)
    if args.mini:
        train = datasets.SubDataset(train, 0, 100)
    train_iter = chainer.iterators.MultiprocessIterator(train, args.batchsize)

    test = VOCBboxDataset(year='2007',
                          split='test',
                          use_difficult=True,
                          return_difficult=True)
    if args.mini:
        test = datasets.SubDataset(test, 0, 20)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 args.batchsize,
                                                 repeat=False,
                                                 shuffle=False)

    detector = CenterDetector(HourglassNet,
                              args.input_size,
                              num_class,
                              dtype=dtype)
    #detector = CenterDetector(SimpleCNN, args.input_size, num_class)
    train_chain = CenterDetectorTrain(detector, 1, 0.1, 1)
    #train_chain = CenterDetectorTrain(detector, 1, 0, 0)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        train_chain.to_gpu(args.gpu)

    optimizer = Adam(alpha=1.25e-4)
    #optimizer = SGD()
    optimizer.setup(train_chain)

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

    log_interval = 1, 'epoch'
    log_interval_mini = 500, 'iteration'
    trainer = Trainer(updater, (args.epoch, 'epoch'), out=f"result{args.gpu}")
    trainer.extend(extensions.LogReport(trigger=log_interval_mini))
    trainer.extend(extensions.observe_lr(), trigger=log_interval)
    trainer.extend(extensions.PrintReport([
        'epoch',
        'iteration',
        'lr',
        'main/loss',
        'main/hm_loss',
        'main/wh_loss',
        'main/offset_loss',
        'main/hm_mae',
        'main/hm_pos_loss',
        'main/hm_neg_loss',
        'validation/main/map',
    ]),
                   trigger=log_interval_mini)
    trainer.extend(extensions.ProgressBar(update_interval=10))
    trainer.extend(DetectionVOCEvaluator(test_iter,
                                         detector,
                                         use_07_metric=True,
                                         label_names=voc_bbox_label_names),
                   trigger=(1, 'epoch'))
    trainer.extend(extensions.snapshot_object(
        detector, 'detector{.updater.epoch:03}.npz'),
                   trigger=(1, 'epoch'))

    trainer.run()
Esempio n. 27
0
def main():
    args = create_args('train')
    result_dir = create_result_dir(args.model_name)

    # Prepare devices
    devices = get_gpu_dict(args.gpus)

    # Instantiate a model
    model = RegNet(epsilon=args.epsilon)

    # Instantiate a optimizer
    optimizer = get_optimizer(model, **vars(args))

    # Setting up datasets
    prep = TransformDataset(KITTI(args.kitti_path, 'train'),
                            CalibPrepare(args.init_pose))
    train, valid = split_dataset(
        prep, round(len(prep) * (1 - args.valid_proportion)))
    print("========== Model Parameters ==========")
    print("location loss weight (epsilon):", args.epsilon)
    print('train samples: {}, valid samples: {}'.format(
        len(train), len(valid)))

    # Iterator
    if DEBUG: Iterator = SerialIterator
    else: Iterator = MultiprocessIterator
    train_iter = Iterator(train, args.batchsize)
    valid_iter = Iterator(valid,
                          args.valid_batchsize,
                          repeat=False,
                          shuffle=False)

    # Updater
    if DEBUG:
        Updater = StandardUpdater(train_iter,
                                  optimizer,
                                  device=devices['main'])
    else:
        Updater = ParallelUpdater(train_iter, optimizer, devices=devices)
    trainer = Trainer(Updater, (args.epoch, 'epoch'), out=result_dir)

    # Extentions
    trainer.extend(extensions.Evaluator(valid_iter,
                                        model,
                                        device=devices['main']),
                   trigger=(args.valid_freq, 'epoch'))
    trainer.extend(extensions.snapshot(),
                   trigger=(args.snapshot_iter, 'iteration'))
    trainer.extend(extensions.LogReport(),
                   trigger=(args.show_log_iter, 'iteration'))
    trainer.extend(extensions.ProgressBar(update_interval=20))
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'iteration', 'main/loss', 'validation/main/loss',
            'elapsed_time'
        ]))

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

    # Train and save
    print("========== Training ==========")
    hook = CupyMemoryProfileHook()
    with hook:
        trainer.run()

    print("========== Saving ==========")
    chainer.serializers.save_hdf5(create_result_file(args.model_name), model)
    print("Done.")
    print("========== Memory Profiling ==========")
    hook.print_report()
Esempio n. 28
0
def main(args):
    s_train, s_test = dataset.load_svhn()
    t_train, t_test = dataset.load_mnist()

    s_train_iter = SerialIterator(
        s_train, args.batchsize, shuffle=True, repeat=True)
    t_train_iter = SerialIterator(
        t_test, args.batchsize, shuffle=True, repeat=True)
    s_test_iter = SerialIterator(
        s_test, args.batchsize, shuffle=False, repeat=False)
    t_test_iter = SerialIterator(
        t_test, args.batchsize, shuffle=False, repeat=False)

    model = drcn.DRCN()
    target_model = LossAndAccuracy(model)
    loss_list = ['loss_cla_s', 'loss_cla_t', 'loss_rec']
    optimizer = chainer.optimizers.RMSprop(args.lr)
    optimizer.setup(model)
    optimizers = {
        'model': optimizer
    }

    updater = Updater(s_train_iter, t_train_iter, optimizers, args)
    out_dir = utils.prepare_dir(args)
    trainer = Trainer(updater, (args.max_iter, 'iteration'), out=out_dir)
    trainer.extend(extensions.LogReport(trigger=(args.interval, args.unit)))
    trainer.extend(
        extensions.snapshot_object(model, filename='model'),
        trigger=MaxValueTrigger('acc_t', (args.interval, args.unit)))
    trainer.extend(extensions.Evaluator(t_test_iter, target_model,
                                        device=args.device), trigger=(args.interval, args.unit))
    trainer.extend(extensions.PrintReport([args.unit, *loss_list, 'acc_s', 'acc_t', 'elapsed_time']))
    trainer.extend(extensions.PlotReport([*loss_list], x_key=args.unit, file_name='loss.png', trigger=(args.interval, args.unit)))
    trainer.extend(extensions.PlotReport(['acc_s', 'acc_t'], x_key=args.unit, file_name='accuracy.png', trigger=(args.interval, args.unit)))
    trainer.extend(extensions.ProgressBar(update_interval=1))
    trainer.run()
Esempio n. 29
0
def main():
    parser = argparse.ArgumentParser(description='training mnist')
    parser.add_argument('--gpu', '-g', default=-1, type=int,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--epoch', '-e', type=int, default=100,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--batchsize', '-b', type=int, default=8,
                        help='Number of images in each mini-batch')
    parser.add_argument('--seed', '-s', type=int, default=0,
                        help='Random seed')
    parser.add_argument('--report_trigger', '-rt', type=str, default='1e',
                        help='Interval for reporting(Ex.100i, default:1e)')
    parser.add_argument('--save_trigger', '-st', type=str, default='1e',
                        help='Interval for saving the model(Ex.100i, default:1e)')
    parser.add_argument('--load_model', '-lm', type=str, default=None,
                        help='Path of the model object to load')
    parser.add_argument('--load_optimizer', '-lo', type=str, default=None,
                        help='Path of the optimizer object to load')
    args = parser.parse_args()

    start_time = datetime.now()
    save_dir = Path('output/{}'.format(start_time.strftime('%Y%m%d_%H%M')))

    random.seed(args.seed)
    np.random.seed(args.seed)
    cupy.random.seed(args.seed)

    backbone = 'mobilenet'
    model = ModifiedClassifier(DeepLab(n_class=13, task='semantic', backbone=backbone), lossfun=F.softmax_cross_entropy)
    if args.load_model is not None:
        serializers.load_npz(args.load_model, model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    optimizer = optimizers.Adam(alpha=1e-3)
    optimizer.setup(model)
    if args.load_optimizer is not None:
        serializers.load_npz(args.load_optimizer, optimizer)

    dir_path = './dataset/2D-3D-S/'
    augmentations = {'mirror': 0.5, 'flip': 0.5}
    train_data = Stanford2D3DS(dir_path, 'semantic', area='1 2 3 4', train=True)
    train_data.set_augmentations(crop=513, augmentations=augmentations)
    valid_data = Stanford2D3DS(dir_path, 'semantic', area='6', train=False, n_data=100)
    valid_data.set_augmentations(crop=513)

    train_iter = iterators.MultiprocessIterator(train_data, args.batchsize, n_processes=1)
    valid_iter = iterators.MultiprocessIterator(valid_data, args.batchsize, repeat=False, shuffle=False, n_processes=1)

    updater = StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = Trainer(updater, (args.epoch, 'epoch'), out=save_dir)

    label_list = list(valid_data.label_dict.keys())[1:]
    report_trigger = (int(args.report_trigger[:-1]), 'iteration' if args.report_trigger[-1] == 'i' else 'epoch')
    trainer.extend(extensions.LogReport(trigger=report_trigger))
    trainer.extend(ModifiedEvaluator(valid_iter, model, label_names=label_list,
                                     device=args.gpu), name='val', trigger=report_trigger)

    trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'main/loss', 'main/acc', 'val/main/loss',
                                           'val/main/acc', 'val/main/mean_class_acc', 'val/main/miou',
                                           'elapsed_time']), trigger=report_trigger)

    trainer.extend(extensions.PlotReport(['main/loss', 'val/main/loss'], x_key=report_trigger[1],
                                         marker='.', file_name='loss.png', trigger=report_trigger))
    trainer.extend(extensions.PlotReport(['main/acc', 'val/main/acc'], x_key=report_trigger[1],
                                         marker='.', file_name='accuracy.png', trigger=report_trigger))
    class_accuracy_report = ['val/main/mean_class_acc']
    class_accuracy_report.extend(['val/main/class_acc/{}'.format(label) for label in label_list])
    class_iou_report = ['val/main/miou']
    class_iou_report.extend(['val/main/iou/{}'.format(label) for label in label_list])
    trainer.extend(extensions.PlotReport(class_accuracy_report, x_key=report_trigger[1],
                                         marker='.', file_name='class_accuracy.png', trigger=report_trigger))
    trainer.extend(extensions.PlotReport(class_iou_report, x_key=report_trigger[1],
                                         marker='.', file_name='class_iou.png', trigger=report_trigger))

    save_trigger = (int(args.save_trigger[:-1]), 'iteration' if args.save_trigger[-1] == 'i' else 'epoch')
    trainer.extend(extensions.snapshot_object(model, filename='model_{0}-{{.updater.{0}}}.npz'
                                              .format(save_trigger[1])), trigger=save_trigger)
    trainer.extend(extensions.snapshot_object(optimizer, filename='optimizer_{0}-{{.updater.{0}}}.npz'
                                              .format(save_trigger[1])), trigger=save_trigger)

    if save_dir.exists():
        shutil.rmtree(save_dir)
    save_dir.mkdir()
    (save_dir / 'training_details').mkdir()

    # Write parameters text
    with open(save_dir / 'training_details/train_params.txt', 'w') as f:
        f.write('model: {}(backbone: {})\n'.format(model.predictor.__class__.__name__, backbone))
        f.write('n_epoch: {}\n'.format(args.epoch))
        f.write('batch_size: {}\n'.format(args.batchsize))
        f.write('n_data_train: {}\n'.format(len(train_data)))
        f.write('n_data_val: {}\n'.format(len(valid_data)))
        f.write('seed: {}\n'.format(args.seed))
        if len(augmentations) > 0:
            f.write('[augmentation]\n')
            for process in augmentations:
                f.write('  {}: {}\n'.format(process, augmentations[process]))

    trainer.run()
Esempio n. 30
0
def train(opt):
    if opt.use_cpu:
        device = -1
        print('[Msg] use CPU')
    else:
        device = 0
        print('[Msg] use GPU0')

    train, valid = get_dataset(opt.dataset_dir, augment=True)
    print('[Msg] loaded normal dataset')
    if opt.use_rough:
        rough_train, _ = get_dataset(opt.rough_dataset_dir, augment=False, is_valid=False)
        print('[Msg] loaded rough dataset')

    train_iter = SerialIterator(train, opt.batch_size, shuffle=True, repeat=True)
    valid_iter = SerialIterator(valid, opt.batch_size, shuffle=True, repeat=True)

    if opt.use_rough:
        rough_iter = SerialIterator(rough_train, opt.batch_size, shuffle=True, repeat=True)
    else:
        rough_iter = train_iter
    print('[Msg] convert dataset to iterator')

    gen = SPADEGenerator(opt)
    if device != -1:
        gen.to_gpu(device) #use GPU
    if opt.dis_snapshot:
        load_npz(opt.gen_snapshot, gen, strict=False)
        print('[Msg] loaded gen npz (' + opt.gen_snapshot + ')')
    g_optim = Adam(alpha=opt.g_lr, beta1=opt.g_beta1, beta2=opt.g_beta2)
    g_optim.setup(gen)
    print('[Msg] completed generator setup')

    dis = PatchDiscriminator(opt)
    if device != -1:
        dis.to_gpu(device) #use GPU
    if opt.dis_snapshot:
        load_npz(opt.dis_snapshot, dis, strict=False)
        print('[Msg] loaded dis npz (' + opt.dis_snapshot + ')')
    d_optim = Adam(alpha=opt.d_lr, beta1=opt.d_beta1, beta2=opt.d_beta2)
    d_optim.setup(dis)
    print('[Msg] completed discriminator setup')

    updater = pix2pix_Updater(opt,
        iterator={'main': train_iter, 'valid': valid_iter, 'rough': rough_iter},
        optimizer={'gen': g_optim, 'dis': d_optim},
        device=device)

    trainer = Trainer(updater, (opt.max_iteration, 'iteration'), out=opt.out_dir)
    print('[Msg] created updater and trainer')

    #chainer training extensions
    trainer.extend(ex.LogReport(log_name=None, trigger=(1, 'iteration')))
    trainer.extend(ex.ProgressBar((opt.max_iteration, 'iteration'), update_interval=1))

    #plot
    #adv loss
    trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss'],
        x_key='iteration', filename='adv-loss.png', trigger=(25, 'iteration')))

    trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss'],
        x_key='epoch', filename='adv-loss(details).png', trigger=(2, 'epoch')))

    #adv loss with gp
    trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp'],
        x_key='iteration', filename='adv-loss-with-gp.png', trigger=(25, 'iteration')))

    trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp'],
        x_key='epoch', filename='adv-loss-with-gp(details).png', trigger=(2, 'epoch')))

    trainer.extend(ex.PlotReport(['gen/adv_loss', 'dis/adv_loss_with_gp', 'gen/valid_adv_loss', 'dis/valid_adv_loss_with_gp'],
        x_key='epoch', filename='adv-loss-with-gp-and-valid.png', trigger=(2, 'epoch')))

    #all loss
    trainer.extend(ex.PlotReport(['gen/loss', 'dis/loss'],
        x_key='iteration', filename='loss.png', trigger=(25, 'iteration')))

    trainer.extend(ex.PlotReport(['gen/loss', 'dis/loss', 'gen/valid_loss', 'dis/valid_loss'],
        x_key='epoch', filename='loss-with-valid.png', trigger=(2, 'epoch')))

    #other
    trainer.extend(ex.PlotReport(['dis/gp'],
        x_key='iteration', filename='gp.png', trigger=(25, 'iteration')))
    trainer.extend(ex.PlotReport(['dis/gp'],
        x_key='epoch', filename='gp(details).png', trigger=(2, 'epoch')))

    trainer.extend(ex.PlotReport(['gen/perceptual_loss'],
        x_key='iteration', filename='perceptual_loss.png', trigger=(25, 'iteration')))
    trainer.extend(ex.PlotReport(['gen/perceptual_loss'],
        x_key='epoch', filename='perceptual_loss(details).png', trigger=(2, 'epoch')))

    trainer.extend(ex.PlotReport(['gen/fm_loss'],
        x_key='iteration', filename='fm_loss.png', trigger=(25, 'iteration')))
    trainer.extend(ex.PlotReport(['gen/fm_loss'],
        x_key='epoch', filename='fm_loss(details).png', trigger=(2, 'epoch')))

    #snap
    trainer.extend(ex.snapshot_object(gen, 'gen_snapshot_epoch-{.updater.epoch}.npz'),
        trigger=(opt.snap_interval_epoch, 'epoch'))
    trainer.extend(ex.snapshot_object(dis, 'dis_snapshot_epoch-{.updater.epoch}.npz'),
        trigger=(opt.snap_interval_epoch, 'epoch'))

    trainer.extend(lambda *args: updater.save_img(),
        trigger=(opt.img_interval_iteration, 'iteration'), priority=PRIORITY_READER)

    trainer.extend(lambda *args: adam_lr_poly(opt, trainer), trigger=(100, 'iteration'))

    print('[Msg] applied extention')

    print('[Msg] start training...')
    trainer.run() #start learning