Esempio n. 1
0
    def test_split_dataset_random(self):
        original = [1, 2, 3, 4, 5]
        subset1, subset2 = datasets.split_dataset_random(original, 2)
        reconst = sorted(set(subset1).union(subset2))
        self.assertEqual(reconst, original)

        subset1a, subset2a = datasets.split_dataset_random(original, 2, seed=3)
        reconst = sorted(set(subset1a).union(subset2a))
        self.assertEqual(reconst, original)
        subset1b, subset2b = datasets.split_dataset_random(original, 2, seed=3)
        self.assertEqual(set(subset1a), set(subset1b))
        self.assertEqual(set(subset2a), set(subset2b))
        reconst = sorted(set(subset1a).union(subset2a))
        self.assertEqual(reconst, original)
Esempio n. 2
0
# xに設計変数を格納
x = input_data.loc[:, ["Column_num","Color_white","Color_black", "Icon_A", "Icon_B", "Icon_C", "Layout_thin", "Layout_thick", "Layout_ratio", "Menu_rectangle", "Menu_float", "Menu_convex", "Shape_corner", "Shape_round", "Shape_chamfer", "Header_none", "Header_half", "Header_full", "Char_none", "Char_half", "Char_full"]] #df.loc[:, ['col_1','col_2']]で烈ラベル指定
# tに15点満点のアンケート結果(Total)を格納
t = input_data.loc[:,["Total"]]

#Chainerがデフォルトで用いるfloat32型に変換
x = np.array(x,np.float32)
t = np.array(t,np.float32) # 分類型のときはint, 回帰型のときはfloat

# TupleDatasetを使ってxとtをセットにする
from chainer.datasets import TupleDataset
dataset = TupleDataset(x,t)

# datasetを任意の割合でtrain(学習用データ)とvalid(検証用データ)に分割する
from chainer.datasets import split_dataset_random
train, valid = split_dataset_random(dataset, int(len(dataset) * 0.8), seed=0) # 抽出するデータは固定

# 用意したデータをどう抽出し,どうグループ(バッチ)わけするか設定する
from chainer import iterators
batchsize = 128
train_iter = iterators.SerialIterator(train, batchsize, shuffle=True, repeat=True)
valid_iter = iterators.SerialIterator(valid, batchsize, shuffle=True, repeat=True)
# 何周も何周も全データを繰り返し読み出す必要がある場合はrepeat引数をTrue
# 1周が終わったらそれ以上データを取り出したくない場合はこれをFalse
# デフォルトではTrueなので本当は書かなくてもいい

# ------------------------------------------------------------------------------
# ネットワークの定義と最適化手法の選択
import chainer.links as L
import chainer.functions as F
# ネットワークの定義
Esempio n. 3
0
 def test_split_dataset_random(self):
     original = [1, 2, 3, 4, 5]
     subset1, subset2 = datasets.split_dataset_random(original, 2)
     reconst = sorted(set(subset1).union(subset2))
     self.assertEqual(reconst, original)
Esempio n. 4
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label != 'all':
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = None
        cache_dir = os.path.join('input', '{}_all'.format(method))
        class_num = len(D.get_qm9_label_names())

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached dataset from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()

        if num_data >= 0:
            # Select the first `num_data` samples from the dataset.
            target_index = numpy.arange(num_data)
            dataset = D.get_qm9(preprocessor,
                                labels=labels,
                                target_index=target_index)
        else:
            # Load the entire dataset.
            dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the laded dataset.
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)
        if isinstance(dataset, NumpyTupleDataset):
            NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Scale the label values, if necessary.
    if args.scale == 'standardize':
        print('Fit StandardScaler to the labels.')
        scaler = StandardScaler()
        if isinstance(dataset, NumpyTupleDataset):
            scaler.fit(dataset.get_datasets()[-1])
        else:
            y = numpy.array([data.y for data in dataset])
            scaler.fit(y)
    else:
        print('No standard scaling was selected.')
        scaler = None

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, valid = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(method, args.unit_num, args.conv_layers,
                                 class_num, scaler)

    # Set up the regressor.
    device = chainer.get_device(args.device)
    metrics_fun = {'mae': F.mean_absolute_error, 'rmse': rmse}
    regressor = Regressor(predictor,
                          lossfun=F.mean_squared_error,
                          metrics_fun=metrics_fun,
                          device=device)

    print('Training...')
    run_train(regressor,
              train,
              valid=valid,
              batch_size=args.batchsize,
              epoch=args.epoch,
              out=args.out,
              extensions_list=None,
              device=device,
              converter=dataset.converter,
              resume_path=None)

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained model to {}...'.format(model_path))
    regressor.save_pickle(model_path, protocol=args.protocol)
Esempio n. 5
0
def main():
    # Supported preprocessing/network list
    method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn']
    label_names = [
        'A', 'B', 'C', 'mu', 'alpha', 'h**o', 'lumo', 'gap', 'r2', 'zpve',
        'U0', 'U', 'H', 'G', 'Cv'
    ]
    scale_list = ['standardize', 'none']

    parser = argparse.ArgumentParser(description='Regression with QM9.')
    parser.add_argument('--method',
                        '-m',
                        type=str,
                        choices=method_list,
                        default='nfp')
    parser.add_argument('--label',
                        '-l',
                        type=str,
                        choices=label_names,
                        default='',
                        help='target label for regression, '
                        'empty string means to predict all '
                        'property at once')
    parser.add_argument('--scale',
                        type=str,
                        choices=scale_list,
                        default='standardize',
                        help='Label scaling method')
    parser.add_argument('--conv-layers', '-c', type=int, default=4)
    parser.add_argument('--batchsize', '-b', type=int, default=32)
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--out', '-o', type=str, default='result')
    parser.add_argument('--epoch', '-e', type=int, default=20)
    parser.add_argument('--unit-num', '-u', type=int, default=16)
    parser.add_argument('--seed', '-s', type=int, default=777)
    parser.add_argument('--train-data-ratio', '-t', type=float, default=0.7)
    args = parser.parse_args()

    seed = args.seed
    train_data_ratio = args.train_data_ratio
    method = args.method
    if args.label:
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = None
        cache_dir = os.path.join('input', '{}_all'.format(method))
        class_num = len(D.get_qm9_label_names())

    # Dataset preparation
    dataset = None

    if os.path.exists(cache_dir):
        print('load from cache {}'.format(cache_dir))
        dataset = NumpyTupleDataset.load(os.path.join(cache_dir, 'data.npz'))
    if dataset is None:
        print('preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        dataset = D.get_qm9(preprocessor, labels=labels)
        os.makedirs(cache_dir)
        NumpyTupleDataset.save(os.path.join(cache_dir, 'data.npz'), dataset)

    if args.scale == 'standardize':
        # Standard Scaler for labels
        ss = StandardScaler()
        labels = ss.fit_transform(dataset.get_datasets()[-1])
        dataset = NumpyTupleDataset(*dataset.get_datasets()[:-1], labels)

    train_data_size = int(len(dataset) * train_data_ratio)
    train, val = split_dataset_random(dataset, train_data_size, seed)

    # Network
    n_unit = args.unit_num
    conv_layers = args.conv_layers
    if method == 'nfp':
        print('Train NFP model...')
        model = GraphConvPredictor(
            NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'ggnn':
        print('Train GGNN model...')
        model = GraphConvPredictor(
            GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'schnet':
        print('Train SchNet model...')
        model = GraphConvPredictor(
            SchNet(out_dim=class_num, hidden_dim=n_unit, n_layers=conv_layers),
            None)
    elif method == 'weavenet':
        print('Train WeaveNet model...')
        n_atom = 20
        n_sub_layer = 1
        weave_channels = [50] * conv_layers
        model = GraphConvPredictor(
            WeaveNet(weave_channels=weave_channels,
                     hidden_dim=n_unit,
                     n_sub_layer=n_sub_layer,
                     n_atom=n_atom), MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'rsgcn':
        print('Train RSGCN model...')
        model = GraphConvPredictor(
            RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    else:
        raise ValueError('[ERROR] Invalid method {}'.format(method))

    train_iter = I.SerialIterator(train, args.batchsize)
    val_iter = I.SerialIterator(val,
                                args.batchsize,
                                repeat=False,
                                shuffle=False)

    def scaled_abs_error(x0, x1):
        if isinstance(x0, Variable):
            x0 = cuda.to_cpu(x0.data)
        if isinstance(x1, Variable):
            x1 = cuda.to_cpu(x1.data)
        if args.scale == 'standardize':
            scaled_x0 = ss.inverse_transform(cuda.to_cpu(x0))
            scaled_x1 = ss.inverse_transform(cuda.to_cpu(x1))
            diff = scaled_x0 - scaled_x1
        elif args.scale == 'none':
            diff = cuda.to_cpu(x0) - cuda.to_cpu(x1)
        return numpy.mean(numpy.absolute(diff), axis=0)[0]

    classifier = L.Classifier(model,
                              lossfun=F.mean_squared_error,
                              accfun=scaled_abs_error)

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

    optimizer = O.Adam()
    optimizer.setup(classifier)

    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       device=args.gpu,
                                       converter=concat_mols)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(
        E.Evaluator(val_iter,
                    classifier,
                    device=args.gpu,
                    converter=concat_mols))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(
        E.PrintReport([
            'epoch', 'main/loss', 'main/accuracy', 'validation/main/loss',
            'validation/main/accuracy', 'elapsed_time'
        ]))
    trainer.extend(E.ProgressBar())
    trainer.run()
Esempio n. 6
0
def get_mpii_dataset(insize,
                     image_root,
                     annotations,
                     train_size=0.5,
                     min_num_keypoints=1,
                     use_cache=False,
                     seed=0):
    dataset_type = 'mpii'
    annotations = json.load(open(annotations, 'r'))

    # filename => keypoints, bbox, is_visible, is_labeled
    images = {}

    # DEBUGGING OVERFITTING
    # create a list containing the actual iamges in folder -> for debugging
    from os import listdir
    files = listdir(image_root)

    for filename in np.unique([anno['filename'] for anno in annotations]):
        if filename in files:
            images[filename] = [], [], [], [], [], [
            ]  # include scale and position

    for anno in annotations:
        if anno['filename'] in files:
            is_visible = [anno['is_visible'][k] for k in KEYPOINT_NAMES[1:]]
            if sum(is_visible) < min_num_keypoints:
                continue
            keypoints = [
                anno['joint_pos'][k][::-1] for k in KEYPOINT_NAMES[1:]
            ]

            x1, y1, x2, y2 = anno['head_rect']

            entry = images[anno['filename']]
            entry[0].append(np.array(keypoints))  # array of y,x
            entry[1].append(np.array([x1, y1, x2 - x1, y2 - y1]))  # x, y, w, h
            entry[2].append(np.array(is_visible, dtype=np.bool))
            is_labeled = np.ones(len(is_visible), dtype=np.bool)
            entry[3].append(is_labeled)
            entry[4].append(np.array(anno['scale']))
            entry[5].append(np.array(anno['position']))

    # THIS IS THE OFFICIAL CODE WHEN ALL IMAGES OR USED
    # for filename in np.unique([anno['filename'] for anno in annotations]):
    #     images[filename] = [], [], [], [], [], [] # include scale and position
    #
    # for anno in annotations:
    #     is_visible = [anno['is_visible'][k] for k in KEYPOINT_NAMES[1:]]
    #     if sum(is_visible) < min_num_keypoints:
    #         continue
    #     keypoints = [anno['joint_pos'][k][::-1] for k in KEYPOINT_NAMES[1:]]
    #
    #     x1, y1, x2, y2 = anno['head_rect']
    #
    #     entry = images[anno['filename']]
    #     entry[0].append(np.array(keypoints))  # array of y,x
    #     entry[1].append(np.array([x1, y1, x2 - x1, y2 - y1]))  # x, y, w, h
    #     entry[2].append(np.array(is_visible, dtype=np.bool))
    #     is_labeled = np.ones(len(is_visible), dtype=np.bool)
    #     entry[3].append(is_labeled)
    #     entry[4].append(np.array(anno['scale']))
    #     entry[5].append(np.array(anno['position']))

    # split dataset
    train_images, test_images = split_dataset_random(
        list(images.keys()), int(len(images) * train_size), seed=seed)

    train_set = KeypointDataset2D(
        dataset_type=dataset_type,
        insize=insize,
        keypoint_names=KEYPOINT_NAMES,
        edges=np.array(EDGES),
        flip_indices=FLIP_INDICES,
        keypoints=[images[i][0] for i in train_images],
        bbox=[images[i][1] for i in train_images],
        is_visible=[images[i][2] for i in train_images],
        is_labeled=[images[i][3] for i in train_images],
        scale=[images[i][4] for i in train_images],
        position=[images[i][5] for i in train_images],
        image_paths=train_images,
        image_root=image_root,
        use_cache=use_cache,
        do_augmentation=False  # TODO must be True
    )

    test_set = KeypointDataset2D(
        dataset_type=dataset_type,
        insize=insize,
        keypoint_names=KEYPOINT_NAMES,
        edges=np.array(EDGES),
        flip_indices=FLIP_INDICES,
        keypoints=[images[i][0] for i in test_images],
        bbox=[images[i][1] for i in test_images],
        is_visible=[images[i][2] for i in test_images],
        is_labeled=[images[i][3] for i in test_images],
        scale=[images[i][4] for i in test_images],
        position=[images[i][5] for i in test_images],
        image_paths=test_images,
        image_root=image_root,
        use_cache=use_cache,
        do_augmentation=False)
    return train_set, test_set
Esempio n. 7
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()
Esempio n. 8
0
def main():
    parser = argparse.ArgumentParser(
        description='Chainer example: POS-tagging')
    parser.add_argument('--batchsize', '-b', type=int, default=30,
                        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('--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()

    vocab = collections.defaultdict(lambda: len(vocab))
    pos_vocab = collections.defaultdict(lambda: len(pos_vocab))

    # Convert word sequences and pos sequences to integer sequences.
    nltk.download('brown')
    data = []
    for sentence in nltk.corpus.brown.tagged_sents():
        xs = numpy.array([vocab[lex] for lex, _ in sentence], 'i')
        ys = numpy.array([pos_vocab[pos] for _, pos in sentence], 'i')
        data.append((xs, ys))

    print('# of sentences: {}'.format(len(data)))
    print('# of words: {}'.format(len(vocab)))
    print('# of pos: {}'.format(len(pos_vocab)))

    model = CRF(len(vocab), len(pos_vocab))
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()
        model.to_gpu(args.gpu)
    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001))

    test_data, train_data = datasets.split_dataset_random(
        data, len(data) // 10, seed=0)

    train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test_data, args.batchsize,
                                                 repeat=False, shuffle=False)
    updater = training.StandardUpdater(
        train_iter, optimizer, converter=convert, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    evaluator = extensions.Evaluator(
        test_iter, model, device=args.gpu, converter=convert)
    # Only validate in each 1000 iteration
    trainer.extend(evaluator, trigger=(1000, 'iteration'))
    trainer.extend(extensions.LogReport(trigger=(100, 'iteration')),
                   trigger=(100, 'iteration'))

    trainer.extend(
        extensions.MicroAverage(
            'main/correct', 'main/total', 'main/accuracy'))
    trainer.extend(
        extensions.MicroAverage(
            'validation/main/correct', 'validation/main/total',
            'validation/main/accuracy'))

    trainer.extend(
        extensions.PrintReport(
            ['epoch', 'main/loss', 'validation/main/loss',
             'main/accuracy', 'validation/main/accuracy', 'elapsed_time']),
        trigger=(100, 'iteration'))

    trainer.extend(extensions.ProgressBar(update_interval=10))

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

    trainer.run()
Esempio n. 9
0
def main():
    parser = argparse.ArgumentParser(description="DCGAN")
    parser.add_argument("--batchsize", "-b", type=int, default=128)
    parser.add_argument("--epoch", "-e", type=int, default=100)
    parser.add_argument("--gpu", "-g", type=int, default=0)
    parser.add_argument("--snapshot_interval", "-s", type=int, default=10)
    parser.add_argument("--display_interval", "-d", type=int, default=1)
    parser.add_argument("--n_dimz", "-z", type=int, default=100)
    parser.add_argument("--dataset", "-ds", type=str, default="mnist")
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--out", "-o", type=str, default="result")
    parser.add_argument("--resume", '-r', default='')
    args = parser.parse_args()

    #import .py
    import Updater
    import Visualize
    import Network.mnist_net as Network
    #print settings
    print("GPU:{}".format(args.gpu))
    print("epoch:{}".format(args.epoch))
    print("Minibatch_size:{}".format(args.batchsize))
    print("Dataset:{}".format(args.dataset))
    print('')
    out = os.path.join(args.out, args.dataset)
    #Set up NN
    gen = Network.Generator(n_hidden=args.n_dimz)
    dis = Network.Discriminator()
    enc = Network.Encoder(n_hidden=args.n_dimz)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        gen.to_gpu()
        dis.to_gpu()
    #Make optimizer
    def make_optimizer(model, alpha=0.0002, beta1=0.5):
        optimizer = optimizers.Adam(alpha=alpha, beta1=beta1) #init_lr = alpha
        optimizer.setup(model)
        optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(0.0001), 'hook_dec')
        return optimizer
    opt_gen = make_optimizer(gen)
    opt_dis = make_optimizer(dis)
    opt_enc = make_optimizer(enc)

    #Get dataset
    train_valid, test = mnist.get_mnist(withlabel=True, ndim=3)
    train, valid = split_dataset_random(train_valid, 50000, seed=0)
    train = [i[0] for i in train  if(i[1]==1)] #ラベル1のみを選択
    #Setup iterator
    train_iter = iterators.SerialIterator(train, args.batchsize)
    #Setup updater
    updater = Updater.DCGANUpdater(
        models=(gen, dis, enc),
        iterator=train_iter,
        optimizer={'gen':opt_gen, 'dis':opt_dis, 'enc':opt_enc},
        device=args.gpu)

    #Setup trainer
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=out)
    snapshot_interval = (args.snapshot_interval, 'epoch')
    display_interval = (args.display_interval, 'epoch')

    trainer.extend(extensions.snapshot_object(
        gen, 'gen_epoch_{.updater.epoch}.npz'),
        trigger=snapshot_interval)
    trainer.extend(extensions.snapshot_object(
        dis, 'dis_epoch_{.updater.epoch}.npz'),
        trigger=snapshot_interval)
    trainer.extend(extensions.snapshot_object(
        enc, 'enc_epoch_{.updater.epoch}.npz'),
        trigger=snapshot_interval)
    trainer.extend(extensions.LogReport(
        trigger=display_interval))
    trainer.extend(extensions.PrintReport([
        'epoch', 'gen/loss', 'dis/loss', 'enc/loss', 'elapsed_time'
    ]), trigger=display_interval)
    trainer.extend(extensions.ProgressBar())
    trainer.extend(Visualize.out_generated_image(
        gen, dis, enc,
        10, 10, args.seed, args.out, args.dataset),
        trigger=snapshot_interval)

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

    trainer.run()
def main():
    # Parse the arguments.
    args = parse_arguments()

    if args.label:
        labels = args.label
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        raise ValueError('No target label was specified.')

    # Dataset preparation. Postprocessing is required for the regression task.
    def postprocess_label(label_list):
        return numpy.asarray(label_list, dtype=numpy.float32)

    # Apply a preprocessor to the dataset.
    print('Preprocessing dataset...')
    preprocessor = preprocess_method_dict[args.method]()
    parser = CSVFileParser(preprocessor,
                           postprocess_label=postprocess_label,
                           labels=labels,
                           smiles_col='SMILES')
    dataset = parser.parse(args.datafile)['dataset']

    # Scale the label values, if necessary.
    if args.scale == 'standardize':
        scaler = StandardScaler()
        scaler.fit(dataset.get_datasets()[-1])
    else:
        scaler = None

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, _ = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(args.method,
                                 args.unit_num,
                                 args.conv_layers,
                                 class_num,
                                 label_scaler=scaler)

    # Set up the regressor.
    device = chainer.get_device(args.device)
    metrics_fun = {'mae': F.mean_absolute_error, 'rmse': rmse}
    regressor = Regressor(predictor,
                          lossfun=F.mean_squared_error,
                          metrics_fun=metrics_fun,
                          device=device)

    print('Training...')
    converter = converter_method_dict[args.method]
    run_train(regressor,
              train,
              valid=None,
              batch_size=args.batchsize,
              epoch=args.epoch,
              out=args.out,
              extensions_list=None,
              device=device,
              converter=converter,
              resume_path=None)

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained model to {}...'.format(model_path))

    # TODO(nakago): ChainerX array cannot be sent to numpy array when internal
    # state has gradients.
    if hasattr(regressor.predictor.graph_conv, 'reset_state'):
        regressor.predictor.graph_conv.reset_state()

    regressor.save_pickle(model_path, protocol=args.protocol)
Esempio n. 11
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    if args.label:
        labels = args.label
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        raise ValueError('No target label was specified.')

    # Dataset preparation. Postprocessing is required for the regression task.
    def postprocess_label(label_list):
        label_arr = np.asarray(label_list, dtype=np.int32)
        return label_arr

    # Apply a preprocessor to the dataset.
    print('Preprocessing dataset...')
    preprocessor = preprocess_method_dict[args.method]()
    parser = CSVFileParserForPair(preprocessor,
                                  postprocess_label=postprocess_label,
                                  labels=labels,
                                  smiles_cols=['smiles_1', 'smiles_2'])
    dataset = parser.parse(args.datafile)['dataset']

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, val = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(args.method, args.unit_num, args.conv_layers,
                                 class_num)

    # Set up the iterator.
    train_iter = SerialIterator(train, args.batchsize)
    val_iter = SerialIterator(val, args.batchsize, repeat=False, shuffle=False)

    # Set up the regressor.
    metric_fun = {
        'accuracy': F.accuracy,
        # 'precision': F.precision,
        # 'recall': F.recall,
        # 'F1-score': F.f1_score,
    }
    classifier = Classifier(predictor,
                            lossfun=F.sigmoid_cross_entropy,
                            metrics_fun=F.accuracy,
                            device=args.gpu)

    # Set up the optimizer.
    optimizer = optimizers.Adam()
    optimizer.setup(classifier)

    # Set up the updater.
    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       device=args.gpu,
                                       converter=concat_mols)
    # updater = training.ParallelUpdater(train_iter, optimizer, devices={'main': 0, 'second': 1},
    #                                    converter=concat_mols)

    # Set up the trainer.
    print('Training...')
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    trainer.extend(
        E.Evaluator(val_iter,
                    classifier,
                    device=args.gpu,
                    converter=concat_mols))

    train_eval_iter = SerialIterator(train,
                                     args.batchsize,
                                     repeat=False,
                                     shuffle=False)
    trainer.extend(
        ROCAUCEvaluator(train_eval_iter,
                        classifier,
                        eval_func=predictor,
                        device=args.gpu,
                        converter=concat_mols,
                        name='train_roc',
                        pos_labels=1,
                        ignore_labels=-1,
                        raise_value_error=False))
    # extension name='validation' is already used by `Evaluator`,
    # instead extension name `val` is used.
    trainer.extend(
        ROCAUCEvaluator(val_iter,
                        classifier,
                        eval_func=predictor,
                        device=args.gpu,
                        converter=concat_mols,
                        name='val_roc',
                        pos_labels=1,
                        ignore_labels=-1))

    trainer.extend(
        PRCAUCEvaluator(train_eval_iter,
                        classifier,
                        eval_func=predictor,
                        device=args.gpu,
                        converter=concat_mols,
                        name='train_prc',
                        pos_labels=1,
                        ignore_labels=-1,
                        raise_value_error=False))
    # extension name='validation' is already used by `Evaluator`,
    # instead extension name `val` is used.
    trainer.extend(
        PRCAUCEvaluator(val_iter,
                        classifier,
                        eval_func=predictor,
                        device=args.gpu,
                        converter=concat_mols,
                        name='val_prc',
                        pos_labels=1,
                        ignore_labels=-1))

    trainer.extend(
        E.PrintReport([
            'epoch',
            'main/loss',
            'main/accuracy',
            # 'train_roc/main/roc_auc', 'train_prc/main/prc_auc',
            'validation/main/loss',
            'validation/main/accuracy',
            # 'val_roc/main/roc_auc', 'val_prc/main/prc_auc',
            'elapsed_time'
        ]))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(E.ProgressBar())
    trainer.run()

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained models to {}...'.format(model_path))
    classifier.save_pickle(model_path, protocol=args.protocol)
Esempio n. 12
0
    print(ans)
    imagedata = Image.open(imgname[i])
    imgarr = [np.asarray(imagedata)]
    arr.append(np.asarray(imgarr))

arr = np.asarray(arr)

pictureArray = arr.reshape(imglen, 1, 795, 492).astype(np.float32)

pictureArray /= pictureArray.max()

npAnserArray = np.asarray(anserArray)
npAnserArray = npAnserArray.reshape(imglen).astype(np.int32)

#%%
train, test = datasets.split_dataset_random(
    chainer.datasets.TupleDataset(pictureArray, npAnserArray), 700)
#%%
train_iter = iterators.SerialIterator(train, 50)
test_iter = iterators.SerialIterator(test, 1, repeat=False, shuffle=False)


#%%
class MLP(Chain):
    def __init__(self):
        super(MLP, self).__init__(
            conv1=F.Convolution2D(1, 30, 5,
                                  stride=1),  # 入力3 枚、出力30枚、フィルタサイズ5ピクセル
            conv2=F.Convolution2D(30, 30, 6,
                                  stride=1),  # 入力30>枚、出力30枚、フィルタサイズ5ピクセル
            conv3=F.Convolution2D(30, 30, 5,
                                  stride=1),  # 入力30>枚、出力30枚、フィルタサイズ6ピクセル
Esempio n. 13
0
from chainer.training import extensions

import numpy as np

import matplotlib
matplotlib.use('Agg')

mushroomsfile = 'mushrooms.csv'
data_array = np.genfromtxt(
    mushroomsfile, delimiter=',', dtype=str, skip_header=1)
for col in range(data_array.shape[1]):
    data_array[:, col] = np.unique(data_array[:, col], return_inverse=True)[1]

X = data_array[:, 1:].astype(np.float32)
Y = data_array[:, 0].astype(np.int32)[:, None]
train, test = datasets.split_dataset_random(
    datasets.TupleDataset(X, Y), int(data_array.shape[0] * .7))

train_iter = ch.iterators.SerialIterator(train, 100)
test_iter = ch.iterators.SerialIterator(
    test, 100, repeat=False, shuffle=False)


# Network definition
def MLP(n_units, n_out):
    layer = ch.Sequential(L.Linear(n_units), F.relu)
    model = layer.repeat(2)
    model.append(L.Linear(n_out))

    return model

Esempio n. 14
0
def split_train(train_val, num=50000, seed=0):
    train, valid = split_dataset_random(train_val, num, seed)

    return train, valid
Esempio n. 15
0
from chainer.datasets import mnist
from chainer.datasets import split_dataset_random
from network import network
from chainer import training
from chainer import optimizers
from chainer import iterators
from chainer.training import extensions
import chainer.links as L
import numpy as np

# setup mnist dataset(example)
train_val, test = mnist.get_mnist(withlabel=True, ndim=1)
train, valid = split_dataset_random(train_val, round(len(train_val) * 0.8))

# training params
batchsize = 64

# setup iterators
train_iter = iterators.SerialIterator(train, batchsize)
valid_iter = iterators.SerialIterator(valid, batchsize, False, False)
test_iter = iterators.SerialIterator(test, batchsize, False, False)

# gpu params
gpu_id = 0

# defining updater
net = L.Classifier(network())

# optimizer
optimizer = optimizers.SGD(lr=0.01).setup(net)
Esempio n. 16
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label:
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
    else:
        labels = D.get_qm9_label_names()
        cache_dir = os.path.join('input', '{}_all'.format(method))

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached data from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the newly preprocessed dataset.
        if not os.path.exists(cache_dir):
            os.mkdir(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Load the standard scaler parameters, if necessary.
    if args.scale == 'standardize':
        scaler_path = os.path.join(args.in_dir, 'scaler.pkl')
        print('Loading scaler parameters from {}.'.format(scaler_path))
        with open(scaler_path, mode='rb') as f:
            scaler = pickle.load(f)
    else:
        print('No standard scaling was selected.')
        scaler = None

    # Split the dataset into training and testing.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    _, test = split_dataset_random(dataset, train_data_size, args.seed)

    # Use a predictor with scaled output labels.
    model_path = os.path.join(args.in_dir, args.model_filename)
    regressor = Regressor.load_pickle(model_path, device=args.gpu)

    # Replace the default predictor with one that scales the output labels.
    scaled_predictor = ScaledGraphConvPredictor(regressor.predictor)
    scaled_predictor.scaler = scaler
    regressor.predictor = scaled_predictor

    # This callback function extracts only the inputs and discards the labels.
    def extract_inputs(batch, device=None):
        return concat_mols(batch, device=device)[:-1]

    # Predict the output labels.
    print('Predicting...')
    y_pred = regressor.predict(test, converter=extract_inputs)

    # Extract the ground-truth labels.
    t = concat_mols(test, device=-1)[-1]
    n_eval = 10

    # Construct dataframe.
    df_dict = {}
    for i, l in enumerate(labels):
        df_dict.update({
            'y_pred_{}'.format(l): y_pred[:, i],
            't_{}'.format(l): t[:, i],
        })
    df = pandas.DataFrame(df_dict)

    # Show a prediction/ground truth table with 5 random examples.
    print(df.sample(5))
    for target_label in range(y_pred.shape[1]):
        diff = y_pred[:n_eval, target_label] - t[:n_eval, target_label]
        print('target_label = {}, y_pred = {}, t = {}, diff = {}'.format(
            target_label, y_pred[:n_eval, target_label],
            t[:n_eval, target_label], diff))

    # Run an evaluator on the test dataset.
    print('Evaluating...')
    test_iterator = SerialIterator(test, 16, repeat=False, shuffle=False)
    eval_result = Evaluator(test_iterator,
                            regressor,
                            converter=concat_mols,
                            device=args.gpu)()
    print('Evaluation result: ', eval_result)

    # Save the evaluation results.
    with open(os.path.join(args.in_dir, 'eval_result.json'), 'w') as f:
        json.dump(eval_result, f)
Esempio n. 17
0
def main():
    parser = argparse.ArgumentParser(description='Chanier example: MNIST')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=32,
                        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('--epoch',
                        '-e',
                        type=int,
                        default=40,
                        help='Number of sweeps over the datasdt to train')
    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('--unit',
                        '-u',
                        type=int,
                        default=1000,
                        help='Number of units')
    args = parser.parse_args()
    print('GPU: {}'.format(args.gpu))
    print('# unit: {}'.format(args.unit))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('')

    dataset_gomi = []
    preprocessing(dataset_gomi, "sample_images_can1/pic", 10, 1)
    preprocessing(dataset_gomi, "sample_images_can2/pic", 10, 1)
    preprocessing(dataset_gomi, "sample_images_can3/pic", 20, 1)
    preprocessing(dataset_gomi, "sample_images_can4/pic", 10, 1)
    preprocessing(dataset_gomi, "sample_images_can5/pic", 10, 1)
    preprocessing(dataset_gomi, "sample_images_bin1/pic", 10, 2)
    preprocessing(dataset_gomi, "sample_images_bin2/pic", 10, 2)
    preprocessing(dataset_gomi, "sample_images_bin3/pic", 20, 2)
    preprocessing(dataset_gomi, "sample_images_pet1/pic", 10, 3)
    preprocessing(dataset_gomi, "sample_images_pet2/pic", 10, 3)
    preprocessing(dataset_gomi, "sample_images_pet3/pic", 10, 3)
    preprocessing(dataset_gomi, "sample_images_pet4/pic", 10, 3)

    train, test = split_dataset_random(dataset_gomi, 120, seed=0)

    model = L.Classifier(MLP(), lossfun=F.softmax_cross_entropy)
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).user()
        model.to_gpu()
    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)
    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 args.batchsize,
                                                 repeat=False,
                                                 shuffle=False)
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))
    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(extensions.snapshot(), trigger=(args.epoch, 'epoch'))
    #trainer.extend(extensions.PlotReport(['main/loss','validation/main/accuracy'],'epoch',file_name='loss.png'))
    trainer.extend(
        extensions.PlotReport(['main/accuracy', 'validation/main/accuracy'],
                              'epoch',
                              file_name='accuracy.png'))
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'elapsed_time'
        ]))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(extensions.LogReport())

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

    trainer.run()
    model.to_cpu()

    modelname = args.out + "/MLP.model"
    print('same the trained model: {}'.format(modelname))
    chainer.serializers.save_npz("model.npz", model)
Esempio n. 18
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    if args.label:
        labels = args.label
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        raise ValueError('No target label was specified.')

    # Dataset preparation. Postprocessing is required for the regression task.
    def postprocess_label(label_list):
        return numpy.asarray(label_list, dtype=numpy.float32)

    # Apply a preprocessor to the dataset.
    print('Preprocessing dataset...')
    preprocessor = preprocess_method_dict[args.method]()
    parser = CSVFileParser(preprocessor, postprocess_label=postprocess_label,
                           labels=labels, smiles_col='SMILES')
    dataset = parser.parse(args.datafile)['dataset']

    # Scale the label values, if necessary.
    if args.scale == 'standardize':
        scaler = StandardScaler()
        labels = scaler.fit_transform(dataset.get_datasets()[-1])
        dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1] + (labels,)))
    else:
        scaler = None

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, _ = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(args.method, args.unit_num,
                                 args.conv_layers, class_num)

    # Set up the iterator.
    train_iter = SerialIterator(train, args.batchsize)

    # Set up the regressor.
    metrics_fun = {'mean_abs_error': MeanAbsError(scaler=scaler),
                   'root_mean_sqr_error': RootMeanSqrError(scaler=scaler)}
    regressor = Regressor(predictor, lossfun=F.mean_squared_error,
                          metrics_fun=metrics_fun, device=args.gpu)

    # Set up the optimizer.
    optimizer = optimizers.Adam()
    optimizer.setup(regressor)

    # Set up the updater.
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu,
                                       converter=concat_mols)

    # Set up the trainer.
    print('Training...')
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(E.PrintReport(['epoch', 'main/loss', 'main/mean_abs_error',
                                  'main/root_mean_sqr_error', 'elapsed_time']))
    trainer.extend(E.ProgressBar())
    trainer.run()

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained model to {}...'.format(model_path))
    regressor.save_pickle(model_path, protocol=args.protocol)

    # Save the standard scaler's parameters.
    if scaler is not None:
        with open(os.path.join(args.out, 'scaler.pkl'), mode='wb') as f:
            pickle.dump(scaler, f, protocol=args.protocol)

# In[]
batchsize = 256
learnrate = 0.05
epoch = 300
gpu = 0
out = 'result'
resume = ''

# In[]
class_labels = 5
train_val, test = make_datasets()
# model = L.Classifier(VGG16Net.VGG16Net(class_labels))
train_size = int(len(train_val) * 0.9)
train, valid = split_dataset_random(train_val, train_size, seed=0)
model = L.Classifier(VGG_chainer.VGG(class_labels))
#GPUのセットアップ
if gpu >= 0:
    # Make a specified GPU current
    chainer.backends.cuda.get_device_from_id(gpu).use()
    model.to_gpu(gpu)  # Copy the model to the GPU
    xp = cuda.cupy

optimizer = chainer.optimizers.MomentumSGD(lr=learnrate).setup(model)
optimizer.add_hook(chainer.optimizer.WeightDecay(0.0005))

# In[]
train_iter = chainer.iterators.SerialIterator(train, batchsize)
valid_iter = chainer.iterators.SerialIterator(valid,
                                              batchsize,
Esempio n. 20
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=64)
    parser.add_argument("--dataset", "-d", type=str, default='mnist')

    args = parser.parse_args()
    #import program
    import Updater
    import Evaluator

    #print settings
    print("GPU:{}".format(args.gpu))
    print("epoch:{}".format(args.epoch))
    print("Minibatch_size:{}".format(args.batchsize))
    print('')

    batchsize = args.batchsize
    gpu_id = args.gpu
    max_epoch = args.epoch

    if args.dataset == "mnist":
        train_val, test = mnist.get_mnist(withlabel=False, ndim=3, scale=255.)
        import Network.mnist_net as Network
    else:
        train_val, test = chainer.datasets.get_cifar10(withlabel=False,
                                                       scale=255.)
        import Network.cifar10_net as Network
    train, valid = split_dataset_random(train_val, 50000, seed=0)

    model = Network.AE(n_dimz=args.n_dimz)

    #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 = make_optimizer(model)
    #trainer
    updater = Updater.AEUpdater(model=model,
                                iterator=train_iter,
                                optimizer=opt,
                                device=args.gpu)

    trainer = training.Trainer(updater, (max_epoch, 'epoch'), out='result')
    #trainer.extend(extensions.ExponentialShift('lr', 0.5),trigger=(30, 'epoch'))
    trainer.extend(extensions.LogReport(log_name='log'))
    trainer.extend(
        Evaluator.AEEvaluator(iterator=valid_iter,
                              target=model,
                              device=args.gpu))
    trainer.extend(extensions.snapshot_object(
        model, filename='model_snapshot_epoch_{.updater.epoch}.npz'),
                   trigger=(args.snapshot, 'epoch'))
    #trainer.extend(extensions.snapshot_object(optimizer, filename='optimizer_snapshot_epoch_{.updater.epoch}'), trigger=(args.snapshot, 'epoch'))
    trainer.extend(
        extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss']))
    trainer.extend(extensions.ProgressBar())
    trainer.run()
    del trainer
Esempio n. 21
0
def iris_nn():
    """Untitled1.ipynb

    Automatically generated by Colaboratory.

    Original file is located at
        https://colab.research.google.com/drive/1VP7qclm8kv1T7PDDy7l2cW1IjjnlUBCr
    """
    epochnum = 1000
    from sklearn.datasets import load_iris
    iris = load_iris()

    # print(iris)

    # print(len(iris.data))
    # print(len(iris.target))

    from chainer import functions as F
    from chainer import links as L
    import chainer

    from chainer.datasets import split_dataset_random

    class MyNN(chainer.Chain):
        def __init__(self, output_label):
            super(MyNN, self).__init__()
            weight_initializer = chainer.initializers.Normal(scale=0.08,
                                                             dtype=None)

            with self.init_scope():
                self.l1 = L.Linear(4, 128)
                self.l2 = L.Linear(None, 128)
                self.l3 = L.Linear(None, 128)
                self.l4 = L.Linear(None, 256)
                self.l5 = L.Linear(None, 512)
                self.fc1 = L.Linear(None, 256)
                self.fc2 = L.Linear(None, output_label)

        def __call__(self, x, t=None, train=True):
            h1 = F.relu(self.l1(x))

            h2 = F.relu(self.l2(h1))
            h3 = F.relu(h2)
            h4 = F.dropout(h3)
            h6 = self.fc1(h4)
            h7 = self.fc2(h6)

            return h7  # y if train else F.softmax(h5)

    model = MyNN(3)
    model = L.Classifier(model)
    optimizer = chainer.optimizers.SGD()
    optimizer.setup(model)

    iris["data"] = iris["data"].astype(np.float32)

    data_and_labels = list(zip(iris["data"], iris["target"]))

    np.random.shuffle(data_and_labels)

    threshold = 0.7
    l = len(data_and_labels)
    lth = int(l * threshold)
    print(lth)
    print("************")
    data = [d[0] for d in data_and_labels]
    labels = [d[1] for d in data_and_labels]
    """ 
    classnum = np.max(labels)
    tmplabels = np.zeros((len(labels), classnum + 1), dtype=np.int)
    
    for i, j in zip(tmplabels, labels):
        i[j] = 1
    print(tmplabels)
    
    labels = tmplabels
    """

    train = chainer.datasets.tuple_dataset.TupleDataset(
        data[:lth], labels[:lth])
    test = chainer.datasets.tuple_dataset.TupleDataset(data[lth:],
                                                       labels[lth:])

    split_at = int(len(data) * 0.7)
    train, test = split_dataset_random(
        chainer.datasets.tuple_dataset.TupleDataset(data, labels), split_at)

    batch_size = 16
    test_batch_size = 32

    train_iter = chainer.iterators.SerialIterator(train,
                                                  batch_size,
                                                  shuffle=True)
    test_iter = chainer.iterators.SerialIterator(test,
                                                 test_batch_size,
                                                 repeat=False,
                                                 shuffle=False)

    from chainer import training, datasets, iterators, optimizers
    from chainer.training import extensions
    from chainer.training.extensions import LogReport
    from chainer.backends import cuda

    updater = training.StandardUpdater(train_iter, optimizer)

    trainer = training.Trainer(updater, (epochnum, 'epoch'), out='result')
    trainer.extend(extensions.Evaluator(test_iter, model), name='validation')
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
            'validation/main/accuracy', 'elapsed_time'
        ]))

    # trainer.extend(extensions.PrintReport(['epoch', 'val/main/loss']))
    trainer.extend(extensions.ProgressBar())

    trainer.run()
Esempio n. 22
0
def main():
    # Supported preprocessing/network list
    method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn']
    label_names = [
        'A', 'B', 'C', 'mu', 'alpha', 'h**o', 'lumo', 'gap', 'r2', 'zpve',
        'U0', 'U', 'H', 'G', 'Cv'
    ]
    scale_list = ['standardize', 'none']

    parser = argparse.ArgumentParser(description='Regression with QM9.')
    parser.add_argument('--method',
                        '-m',
                        type=str,
                        choices=method_list,
                        default='nfp')
    parser.add_argument('--label',
                        '-l',
                        type=str,
                        choices=label_names,
                        default='',
                        help='target label for regression, '
                        'empty string means to predict all '
                        'property at once')
    parser.add_argument('--scale',
                        type=str,
                        choices=scale_list,
                        default='standardize',
                        help='Label scaling method')
    parser.add_argument('--conv-layers', '-c', type=int, default=4)
    parser.add_argument('--batchsize', '-b', type=int, default=32)
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--out', '-o', type=str, default='result')
    parser.add_argument('--epoch', '-e', type=int, default=20)
    parser.add_argument('--unit-num', '-u', type=int, default=16)
    parser.add_argument('--seed', '-s', type=int, default=777)
    parser.add_argument('--train-data-ratio', '-t', type=float, default=0.7)
    parser.add_argument('--protocol', type=int, default=2)
    parser.add_argument('--model-filename', type=str, default='regressor.pkl')
    parser.add_argument('--num-data',
                        type=int,
                        default=-1,
                        help='Number of data to be parsed from parser.'
                        '-1 indicates to parse all data.')
    args = parser.parse_args()

    seed = args.seed
    train_data_ratio = args.train_data_ratio
    method = args.method
    if args.label:
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = None
        cache_dir = os.path.join('input', '{}_all'.format(method))
        class_num = len(D.get_qm9_label_names())

    # Dataset preparation
    dataset = None

    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)
    if os.path.exists(dataset_cache_path):
        print('load from cache {}'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        if num_data >= 0:
            # only use first 100 for debug
            target_index = numpy.arange(num_data)
            dataset = D.get_qm9(preprocessor,
                                labels=labels,
                                target_index=target_index)
        else:
            dataset = D.get_qm9(preprocessor, labels=labels)
        os.makedirs(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    if args.scale == 'standardize':
        # Standard Scaler for labels
        ss = StandardScaler()
        labels = ss.fit_transform(dataset.get_datasets()[-1])
    else:
        ss = None
    dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1] + (labels, )))

    train_data_size = int(len(dataset) * train_data_ratio)
    train, val = split_dataset_random(dataset, train_data_size, seed)

    # Network
    n_unit = args.unit_num
    conv_layers = args.conv_layers
    if method == 'nfp':
        print('Train NFP model...')
        model = GraphConvPredictor(
            NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'ggnn':
        print('Train GGNN model...')
        model = GraphConvPredictor(
            GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'schnet':
        print('Train SchNet model...')
        model = GraphConvPredictor(
            SchNet(out_dim=class_num, hidden_dim=n_unit, n_layers=conv_layers),
            None)
    elif method == 'weavenet':
        print('Train WeaveNet model...')
        n_atom = 20
        n_sub_layer = 1
        weave_channels = [50] * conv_layers
        model = GraphConvPredictor(
            WeaveNet(weave_channels=weave_channels,
                     hidden_dim=n_unit,
                     n_sub_layer=n_sub_layer,
                     n_atom=n_atom), MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'rsgcn':
        print('Train RSGCN model...')
        model = GraphConvPredictor(
            RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    else:
        raise ValueError('[ERROR] Invalid method {}'.format(method))

    train_iter = I.SerialIterator(train, args.batchsize)
    val_iter = I.SerialIterator(val,
                                args.batchsize,
                                repeat=False,
                                shuffle=False)

    regressor = Regressor(
        model,
        lossfun=F.mean_squared_error,
        metrics_fun={'abs_error': ScaledAbsError(scale=args.scale, ss=ss)},
        device=args.gpu)

    optimizer = O.Adam()
    optimizer.setup(regressor)

    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       device=args.gpu,
                                       converter=concat_mols)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(
        E.Evaluator(val_iter,
                    regressor,
                    device=args.gpu,
                    converter=concat_mols))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(
        E.PrintReport([
            'epoch', 'main/loss', 'main/abs_error', 'validation/main/loss',
            'validation/main/abs_error', 'elapsed_time'
        ]))
    trainer.extend(E.ProgressBar())
    trainer.run()

    # --- save regressor & standardscaler ---
    protocol = args.protocol
    regressor.save_pickle(os.path.join(args.out, args.model_filename),
                          protocol=protocol)
    if args.scale == 'standardize':
        with open(os.path.join(args.out, 'ss.pkl'), mode='wb') as f:
            pickle.dump(ss, f, protocol=protocol)
Esempio n. 23
0
def main():
    set_random_seed(0)

    parser = argparse.ArgumentParser(
        description='Document Classification Example')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=64,
                        help='Number of documents in each mini-batch')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=30,
                        help='Number of training epochs')
    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('--unit',
                        '-u',
                        type=int,
                        default=200,
                        help='Number of units')
    parser.add_argument('--vocab',
                        '-v',
                        type=int,
                        default=50000,
                        help='Vocabulary size')
    parser.add_argument('--layer',
                        '-l',
                        type=int,
                        default=1,
                        help='Number of layers of LSMT')
    parser.add_argument('--dropout',
                        '-d',
                        type=float,
                        default=0.4,
                        help='Dropout rate')
    parser.add_argument('--gradclip',
                        type=float,
                        default=5,
                        help='Gradient clipping threshold')
    parser.add_argument('--train_file',
                        '-train',
                        default='data/train.seg.csv',
                        help='Trainig data file.')
    parser.add_argument('--test_file',
                        '-test',
                        default='data/test.seg.csv',
                        help='Test data file.')
    parser.add_argument('--model',
                        '-m',
                        help='read model parameters from npz file')
    parser.add_argument(
        '--vcb_file',
        '-vf',
        default=
        '/mnt/gold/users/s18153/prjPyCharm/prjNLP_GPU/data/vocab_train_w_NoReplace.vocab_file',
        help='Vocabulary data file.')
    parser.add_argument('--case',
                        '-c',
                        default='original',
                        help='Select NN Architecture.')
    parser.add_argument('--opt', default='sgd', help='Select Optimizer.')
    parser.add_argument('--dbg_on',
                        action='store_true',
                        help='No save, MiniTrain')
    args = parser.parse_args()
    print(args)
    # train_val = data.DocDataset(args.train_file, vocab_size=args.vocab)

    if os.path.exists(args.vcb_file):  # args.vocab_fileの存在確認(作成済みの場合ロード)
        with open(args.vcb_file, 'rb') as f_vocab_data:
            train_val = pickle.load(f_vocab_data)
            if len(train_val.get_vocab()) != args.vocab:
                warnings.warn('vocab size incorrect (not implemented...)')
    else:
        train_val = data.DocDataset(
            args.train_file,
            vocab_size=args.vocab)  # make vocab from training data
        with open(args.vcb_file, 'wb') as f_vocab_save:
            pickle.dump(train_val, f_vocab_save)

    if args.dbg_on:
        len_train_data = len(train_val)
        N = 1000
        print('N', N)
        rnd_ind = np.random.permutation(range(len_train_data))[:N]
        train_val = train_val[rnd_ind]
        (train, valid) = split_dataset_random(train_val, 800, seed=0)
    else:
        (train, valid) = split_dataset_random(train_val, 4000, seed=0)

    train_iter = iterators.SerialIterator(train, args.batchsize)
    valid_iter = iterators.SerialIterator(valid,
                                          args.batchsize,
                                          repeat=False,
                                          shuffle=False)

    # test = data.DocDataset(args.test_file, train_val.get_vocab())
    # test_iter = iterators.SerialIterator(test, args.batchsize, repeat=False, shuffle=False)

    print('case', args.case)
    if args.case == 'original':
        print('originalで実行されます')
        result_path = 'result/original'
        model = L.Classifier(
            nets_B.DocClassify(n_vocab=args.vocab + 1,
                               n_units=args.unit,
                               n_layers=args.layer,
                               n_out=4,
                               dropout=args.dropout))
    elif args.case == 'bi':
        print('biで実行されます')
        result_path = 'result/bi'
        model = L.Classifier(
            nets_B.DocClassifyBi(n_vocab=args.vocab + 1,
                                 n_units=args.unit,
                                 n_layers=args.layer,
                                 n_out=4,
                                 dropout=args.dropout))
    elif args.case == 'bi2' or args.case == 'bi_adam_2layer':
        print('bi改良版')
        result_path = 'result/bi2'
        model = L.Classifier(
            nets_B.DocClassifyBi2(n_vocab=args.vocab + 1,
                                  n_units=args.unit,
                                  n_layers=args.layer,
                                  n_out=4,
                                  dropout=args.dropout))
    else:
        warnings.warn('指定したケースは存在しません。デフォルトで実行します')
        result_path = 'result/sample_result'
        model = L.Classifier(
            nets_B.DocClassify(n_vocab=args.vocab + 1,
                               n_units=args.unit,
                               n_layers=args.layer,
                               n_out=4,
                               dropout=args.dropout))

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

    if args.opt == 'sgd':
        result_path += '_sgd'
        print('SGD')
        optimizer = optimizers.SGD(lr=0.01)
    elif args.opt == 'adam':
        result_path += '_adam'
        print('Adam')
        optimizer = optimizers.Adam()
    elif args.opt == 'bi_adam_2layer':
        result_path += '_adam_2layer'
        print('Adam')
        optimizer = optimizers.Adam()
    else:
        print('指定なしのためSGDで実行')
        optimizer = optimizers.SGD(lr=0.01)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.GradientClipping(args.gradclip))
    # optimizer.add_hook(chainer.optimizer.Lasso(0.01))

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

    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=result_path)
    trainer.extend(extensions.LogReport())
    if not args.dbg_on:
        trainer.extend(
            extensions.snapshot(filename='snapshot_epoch-{.updater.epoch}'))
    trainer.extend(extensions.Evaluator(valid_iter,
                                        model,
                                        converter=convert_seq,
                                        device=args.gpu),
                   name='val')
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'main/accuracy', 'val/main/loss',
            'val/main/accuracy', 'elapsed_time'
        ]))
    trainer.extend(
        extensions.ParameterStatistics(model.predictor.doc_enc,
                                       {'std': np.std}))
    trainer.extend(
        extensions.PlotReport(['main/loss', 'val/main/loss'],
                              x_key='epoch',
                              file_name='loss.png'))
    trainer.extend(
        extensions.PlotReport(['main/accuracy', 'val/main/accuracy'],
                              x_key='epoch',
                              file_name='accuracy.png'))
    trainer.extend(extensions.dump_graph('main/loss'))

    if args.model:
        serializers.load_npz(args.model, trainer)

    trainer.run()

    pass
def run_train(net, optimizer, dataset, save_dir, gpu_id):
    n_batch = 64
    n_epoch = 50

    SAVE_MODEL_PER_ITER = 1000

    # GPUに転送
    if gpu_id is not None:
        net.to_gpu(gpu_id)
    # log
    results_train, results_valid = {}, {}
    results_train['loss'], results_train['accuracy'] = [], []
    results_valid['loss'], results_valid['accuracy'] = [], []

    # 入力データを分割
    train_val, test_data = split_dataset_random(dataset,
                                                int(len(dataset) * 0.8),
                                                seed=0)
    train, valid = split_dataset_random(train_val,
                                        int(len(train_val) * 0.8),
                                        seed=0)

    # iteration数出力
    print('# of epoch:', n_epoch)
    print('# of batch:', n_batch)
    print('# of train data:', len(train))
    print('# of valid data:', len(valid))
    print('# of iteration:', int(max(n_epoch,
                                     n_epoch * len(train) / n_batch)), '\n')

    # ぷよぷよAIを参考にbatch_sizeは64
    train_iter = SerialIterator(train,
                                batch_size=n_batch,
                                repeat=True,
                                shuffle=True)

    count = 0
    for epoch in range(n_epoch):
        while True:
            # ミニバッチの取得
            train_batch = train_iter.next()

            # [(x1,t1), (x2, t2), ...]形式から,[[x1,x2,x3,...], [t1,t2,t3,...]]形式へ
            x0_train, x1_train, t_train = concat_samples(train_batch, gpu_id)

            # 予測値と目的関数の計算
            y_train = net(x0_train, x1_train)
            loss_train = F.softmax_cross_entropy(y_train, t_train)
            acc_train = F.accuracy(y_train, t_train)

            # 勾配の初期化と勾配の計算
            net.cleargrads()
            loss_train.backward()
            # パラメータの更新
            optimizer.update()

            # iteration カウントアップ
            count += 1

            # SAVE_MODEL_PER_ITER iterationごとにモデルを保存
            if count % SAVE_MODEL_PER_ITER == 0:
                # 各epochのモデルの保存
                save_filename = os.path.join(save_dir,
                                             'net_{:03d}.npz'.format(count))
                save_model(net, gpu_id, save_filename)
                print('save model (iteration {}) to {}\n'.format(
                    count, save_filename))

            # 1エポック終えたら、valid データで評価する
            if train_iter.is_new_epoch or count % SAVE_MODEL_PER_ITER == 0:
                # 検証用データに対する結果の確認
                with chainer.using_config('train',
                                          False), chainer.using_config(
                                              'enable_backprop', False):
                    # x_valid, t_valid = chainer.dataset.concat_examples(valid, gpu_id)
                    x0_valid, x1_valid, t_valid = concat_samples(valid, gpu_id)
                    y_valid = net(x0_valid, x1_valid)
                    loss_valid = F.softmax_cross_entropy(y_valid, t_valid)
                    acc_valid = F.accuracy(y_valid, t_valid)
                # 注意:GPU で計算した結果はGPU上に存在するため、CPU上に転送します
                if gpu_id is not None:
                    loss_train.to_cpu()
                    loss_valid.to_cpu()
                    acc_train.to_cpu()
                    acc_valid.to_cpu()
                # 結果の表示
                print(
                    'epoch: {}, iteration: {}, loss (train): {:.4f}, loss (valid): {:.4f}\n'
                    'acc (train): {:.4f}, acc (valid): {:.4f}\n'.format(
                        epoch, count, loss_train.array.mean(),
                        loss_valid.array.mean(), acc_train.array.mean(),
                        acc_valid.array.mean()))
                if train_iter.is_new_epoch:
                    # 可視化用に保存
                    results_train['loss'].append(loss_train.array)
                    results_train['accuracy'].append(acc_train.array)
                    results_valid['loss'].append(loss_valid.array)
                    results_valid['accuracy'].append(acc_valid.array)
                    break

    # モデルの保存
    save_filename = os.path.join(save_dir, 'net_final.npz')
    save_model(net, gpu_id, save_filename)
    print('save model to {} at {}\n'.format(count, save_filename))

    # 損失 (loss)
    plt.plot(results_train['loss'], label='train')  # label で凡例の設定
    plt.plot(results_valid['loss'], label='valid')  # label で凡例の設定
    plt.legend()  # 凡例の表示
    plt.savefig(os.path.join(save_dir, 'loss.png'))
    plt.figure()
    # 精度 (accuracy)
    plt.plot(results_train['accuracy'], label='train')  # label で凡例の設定
    plt.plot(results_valid['accuracy'], label='valid')  # label で凡例の設定
    plt.legend()  # 凡例の表示
    plt.savefig(os.path.join(save_dir, 'accuracy.png'))
Esempio n. 25
0
def get_datasets():
    labels = pd.read_csv(LabelsPath, index_col=0)
    # label: 1 -> 102
    ds = datasets.LabeledImageDataset(list(zip(labels["image"], labels["label"] - 1)), PreProcessedFlowerImagesDirectory)
    return datasets.split_dataset_random(ds, int(len(ds) * 0.8), seed=SplitDatasetSeed)
Esempio n. 26
0
from chainer import datasets
from chainer import training
from chainer.training import extensions

import numpy as np

mushroomsfile = 'mushrooms.csv'

data_array = np.genfromtxt(
    mushroomsfile, delimiter=',', dtype=str, skip_header=1)
for col in range(data_array.shape[1]):
    data_array[:, col] = np.unique(data_array[:, col], return_inverse=True)[1]

X = data_array[:, 1:].astype(np.float32)
Y = data_array[:, 0].astype(np.int32)[:, None]
train, test = datasets.split_dataset_random(
    datasets.TupleDataset(X, Y), int(data_array.shape[0] * .7))

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


# Network definition
class MLP(chainer.Chain):
    def __init__(self, n_units, n_out):
        super(MLP, self).__init__()
        with self.init_scope():
            # the input size to each layer inferred from the layer before
            self.l1 = L.Linear(n_units)  # n_in -> n_units
            self.l2 = L.Linear(n_units)  # n_units -> n_units
            self.l3 = L.Linear(n_out)  # n_units -> n_out
Esempio n. 27
0
def TrainUNet(X,
              Y,
              model_=None,
              optimizer_=None,
              epoch=40,
              alpha=0.001,
              gpu_id=0,
              loop=1,
              earlystop=True):
    assert (len(X) == len(Y))
    d_time = datetime.datetime.now().strftime("%m-%d-%H-%M-%S")

    # 1. Model load.

    # print(sum(p.data.size for p in model.unet.params()))
    if model_ is not None:
        model = Regressor(model_)
        print("## model loaded.")
    else:
        model = Regressor(UNet())

    model.compute_accuracy = False

    if gpu_id >= 0:
        model.to_gpu(gpu_id)

    # 2. optimizer load.

    if optimizer_ is not None:
        opt = optimizer_
        print("## optimizer loaded.")
    else:
        opt = optimizers.Adam(alpha=alpha)
        opt.setup(model)

    # 3. Data Split.
    dataset = Unet_DataSet(X, Y)
    print("# number of patterns", len(dataset))

    train, valid = \
        split_dataset_random(dataset, int(len(dataset) * 0.8), seed=0)

    # 4. Iterator
    train_iter = SerialIterator(train, batch_size=C.BATCH_SIZE)
    test_iter = SerialIterator(valid,
                               batch_size=C.BATCH_SIZE,
                               repeat=False,
                               shuffle=False)

    # 5. config train, enable backprop
    chainer.config.train = True
    chainer.config.enable_backprop = True

    # 6. UnetUpdater
    updater = UnetUpdater(train_iter, opt, model, device=gpu_id)

    # 7. EarlyStopping
    if earlystop:
        stop_trigger = triggers.EarlyStoppingTrigger(
            monitor='validation/main/loss',
            max_trigger=(epoch, 'epoch'),
            patients=5)
    else:
        stop_trigger = (epoch, 'epoch')

    # 8. Trainer
    trainer = training.Trainer(updater, stop_trigger, out=C.PATH_TRAINRESULT)

    # 8.1. UnetEvaluator
    trainer.extend(UnetEvaluator(test_iter, model, device=gpu_id))

    trainer.extend(SaveRestore(),
                   trigger=triggers.MinValueTrigger('validation/main/loss'))

    # 8.2. Extensions LogReport
    trainer.extend(extensions.LogReport())

    # 8.3. Extension Snapshot
    # trainer.extend(extensions.snapshot(filename='snapshot_epoch-{.updater.epoch}'))
    # trainer.extend(extensions.snapshot_object(model.unet, filename='loop' + str(loop) + '.model'))

    # 8.4. Print Report
    trainer.extend(extensions.observe_lr())
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss', 'elapsed_time', 'lr'
        ]))

    # 8.5. Extension Graph
    trainer.extend(
        extensions.PlotReport(['main/loss', 'validation/main/loss'],
                              x_key='epoch',
                              file_name='loop-' + str(loop) + '-loss' +
                              d_time + '.png'))
    # trainer.extend(extensions.dump_graph('main/loss'))

    # 8.6. Progree Bar
    trainer.extend(extensions.ProgressBar())

    # 9. Trainer run
    trainer.run()

    chainer.serializers.save_npz(C.PATH_TRAINRESULT / ('loop' + str(loop)),
                                 model.unet)
    return model.unet, opt
Esempio n. 28
0
def main():
    
    # オプション処理
    parser = argparse.ArgumentParser(description='話者認識モデルの学習')
    parser.add_argument('--batchsize', '-b', type=int, default=50,
                        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('--out', '-o', default='result',
                        help='Directory to output the result')
    parser.add_argument('--datadir', '-d', default='train',
                        help='学習データのディレクトリ')
    args = parser.parse_args()

    sys.stderr.write('GPU: {}\n'.format(args.gpu))
    sys.stderr.write('# minibatch-size: {}\n'.format(args.batchsize))
    sys.stderr.write('# epoch: {}\n'.format(args.epoch))

    trainf = []
    label = 0
    print('loading dataset')
    mfcc = os.listdir(args.datadir)
    for i in [f for f in mfcc if ('mfcc' in f)]:
        trainf.append([os.path.join(args.datadir, i), label])            
        label += 1
    #print('{}'.format(trainf))
    input = []
    target = []
    if not os.path.exists(args.out):
        os.mkdir(args.out)
    log = open(args.out+"/class_log",'w')
    for file in trainf:
        print('{}'.format(file))
        log.write('{},{}\n'.format(file[0].strip().split("/")[-1].split(".")[0],file[1]))
        with open(file[0], 'r') as f:
            lines = f.readlines()         
        for i, l in enumerate(lines):
            #print('{}'.format(len(lines)))
            tmp = []
            flag = False
            for j in range(3): # 3フレームで評価
                if i + 2 < len(lines):
                    frame = lines[i+j].strip().split(" ")
                    #print("i:{},file:{}".format(i,file))
                    np.array(frame,dtype=np.float32)
                    tmp.extend(frame)
                    flag = True
                    #print('{}'.format(tmp))
            if flag:
                input.append(tmp)
                target.append(file[1])
            #    print('{}'.format(input))
    log.close()
    #print('{}'.format(input))  
    #sys.exit()
    #print(np.array(input))
    input = np.array(input).astype(np.float32)
    target = np.array(target).astype(np.int32)
    #print('{},{}'.format(len(input), len(target)))
    train = D.TupleDataset(input, target)
    #sys.stderr.write(train)
    #print(len(input)*0.9)
    train, test = D.split_dataset_random(train, int(len(input)*0.9))
    print('{},{}'.format(len(train), len(test)))
    #sys.exit()
        
    model = L.Classifier(DNN(label)) # CNNにする
    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)

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                 repeat=False, shuffle=False)
    
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

    trainer.extend(extensions.dump_graph('main/loss'))
    trainer.extend(extensions.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(extensions.LogReport())
    # trainer.extend(
    #     extensions.PlotReport('main/loss', 'epoch', file_name='loss.png'))
    # trainer.extend(
    #     extensions.PlotReport('main/accuracy', 'epoch', file_name='accuracy.png'))
    trainer.extend(extensions.PrintReport(
        ['epoch', 'main/loss', 'validation/main/loss',
         'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
    #trainer.extend(extensions.PrintReport(
    #    ['epoch', 'main/loss', 'main/accuracy', 'elapsed_time']))
    trainer.extend(extensions.ProgressBar())

    print('training start!')
    trainer.run()
    # モデルをCPU対応へ
    model.to_cpu()
    # 保存
    modelname = args.out + "/speaker.model"
    print('save the trained model: {}'.format(modelname))
    chainer.serializers.save_npz(modelname, model)
Esempio n. 29
0
def main():
    # Parse the arguments.
    args = parse_arguments()
    device = args.gpu

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label != 'all':
        label = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, label))
        labels = [label]
    else:
        labels = D.get_qm9_label_names()
        cache_dir = os.path.join('input', '{}_all'.format(method))

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached data from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the newly preprocessed dataset.
        if not os.path.exists(cache_dir):
            os.mkdir(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Use a predictor with scaled output labels.
    model_path = os.path.join(args.in_dir, args.model_filename)
    regressor = Regressor.load_pickle(model_path, device=device)
    scaler = regressor.predictor.scaler

    if scaler is not None:
        original_t = dataset.get_datasets()[-1]
        if args.gpu >= 0:
            scaled_t = cuda.to_cpu(scaler.transform(
                cuda.to_gpu(original_t)))
        else:
            scaled_t = scaler.transform(original_t)

        dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1] +
                                      (scaled_t,)))

    # Split the dataset into training and testing.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    _, test = split_dataset_random(dataset, train_data_size, args.seed)

    # This callback function extracts only the inputs and discards the labels.
    def extract_inputs(batch, device=None):
        return concat_mols(batch, device=device)[:-1]

    def postprocess_fn(x):
        if scaler is not None:
            scaled_x = scaler.inverse_transform(x)
            return scaled_x
        else:
            return x

    # Predict the output labels.
    print('Predicting...')
    y_pred = regressor.predict(
        test, converter=extract_inputs,
        postprocess_fn=postprocess_fn)

    # Extract the ground-truth labels.
    t = concat_mols(test, device=device)[-1]
    original_t = cuda.to_cpu(scaler.inverse_transform(t))

    # Construct dataframe.
    df_dict = {}
    for i, l in enumerate(labels):
        df_dict.update({'y_pred_{}'.format(l): y_pred[:, i],
                        't_{}'.format(l): original_t[:, i], })
    df = pandas.DataFrame(df_dict)

    # Show a prediction/ground truth table with 5 random examples.
    print(df.sample(5))

    n_eval = 10
    for target_label in range(y_pred.shape[1]):
        label_name = labels[target_label]
        diff = y_pred[:n_eval, target_label] - original_t[:n_eval,
                                                          target_label]
        print('label_name = {}, y_pred = {}, t = {}, diff = {}'
              .format(label_name, y_pred[:n_eval, target_label],
                      original_t[:n_eval, target_label], diff))

    # Run an evaluator on the test dataset.
    print('Evaluating...')
    test_iterator = SerialIterator(test, 16, repeat=False, shuffle=False)
    eval_result = Evaluator(test_iterator, regressor, converter=concat_mols,
                            device=device)()
    print('Evaluation result: ', eval_result)
    # Save the evaluation results.
    save_json(os.path.join(args.in_dir, 'eval_result.json'), eval_result)

    # Calculate mean abs error for each label
    mae = numpy.mean(numpy.abs(y_pred - original_t), axis=0)
    eval_result = {}
    for i, l in enumerate(labels):
        eval_result.update({l: mae[i]})
    save_json(os.path.join(args.in_dir, 'eval_result_mae.json'), eval_result)
Esempio n. 30
0
def main():
    # Supported preprocessing/network list
    method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn']
    label_names = [
        'A', 'B', 'C', 'mu', 'alpha', 'h**o', 'lumo', 'gap', 'r2', 'zpve',
        'U0', 'U', 'H', 'G', 'Cv'
    ]
    scale_list = ['standardize', 'none']

    parser = argparse.ArgumentParser(description='Regression with QM9.')
    parser.add_argument('--method',
                        '-m',
                        type=str,
                        choices=method_list,
                        default='nfp')
    parser.add_argument('--label',
                        '-l',
                        type=str,
                        choices=label_names,
                        default='',
                        help='target label for regression, '
                        'empty string means to predict all '
                        'property at once')
    parser.add_argument('--scale',
                        type=str,
                        choices=scale_list,
                        default='standardize',
                        help='Label scaling method')
    parser.add_argument('--batchsize', '-b', type=int, default=32)
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--in-dir', '-i', type=str, default='result')
    parser.add_argument('--seed', '-s', type=int, default=777)
    parser.add_argument('--train-data-ratio', '-t', type=float, default=0.7)
    parser.add_argument('--model-filename', type=str, default='regressor.pkl')
    parser.add_argument('--num-data',
                        type=int,
                        default=-1,
                        help='Number of data to be parsed from parser.'
                        '-1 indicates to parse all data.')
    args = parser.parse_args()

    seed = args.seed
    train_data_ratio = args.train_data_ratio
    method = args.method
    if args.label:
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        # class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = D.get_qm9_label_names()
        cache_dir = os.path.join('input', '{}_all'.format(method))
        # class_num = len(labels)

    # Dataset preparation
    dataset = None

    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    dataset_cache_path = os.path.join(cache_dir, dataset_filename)
    if os.path.exists(dataset_cache_path):
        print('load from cache {}'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        dataset = D.get_qm9(preprocessor, labels=labels)
        if not os.path.exists(cache_dir):
            os.mkdir(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    if args.scale == 'standardize':
        # Standard Scaler for labels
        with open(os.path.join(args.in_dir, 'ss.pkl'), mode='rb') as f:
            ss = pickle.load(f)
    else:
        ss = None

    train_data_size = int(len(dataset) * train_data_ratio)
    train, val = split_dataset_random(dataset, train_data_size, seed)

    regressor = Regressor.load_pickle(os.path.join(args.in_dir,
                                                   args.model_filename),
                                      device=args.gpu)  # type: Regressor

    # We need to feed only input features `x` to `predict`/`predict_proba`.
    # This converter extracts only inputs (x1, x2, ...) from the features which
    # consist of input `x` and label `t` (x1, x2, ..., t).
    def extract_inputs(batch, device=None):
        return concat_mols(batch, device=device)[:-1]

    def postprocess_fn(x):
        if ss is not None:
            # Model's output is scaled by StandardScaler,
            # so we need to rescale back.
            if isinstance(x, Variable):
                x = x.data
                scaled_x = ss.inverse_transform(cuda.to_cpu(x))
                return scaled_x
        else:
            return x

    print('Predicting...')
    y_pred = regressor.predict(val,
                               converter=extract_inputs,
                               postprocess_fn=postprocess_fn)

    print('y_pred.shape = {}, y_pred[:5, 0] = {}'.format(
        y_pred.shape, y_pred[:5, 0]))

    t = concat_mols(val, device=-1)[-1]
    n_eval = 10

    # Construct dataframe
    df_dict = {}
    for i, l in enumerate(labels):
        df_dict.update({
            'y_pred_{}'.format(l): y_pred[:, i],
            't_{}'.format(l): t[:, i],
        })
    df = pandas.DataFrame(df_dict)

    # Show random 5 example's prediction/ground truth table
    print(df.sample(5))

    for target_label in range(y_pred.shape[1]):
        diff = y_pred[:n_eval, target_label] - t[:n_eval, target_label]
        print('target_label = {}, y_pred = {}, t = {}, diff = {}'.format(
            target_label, y_pred[:n_eval, target_label],
            t[:n_eval, target_label], diff))

    # --- evaluate ---
    # To calc loss/accuracy, we can use `Evaluator`, `ROCAUCEvaluator`
    print('Evaluating...')
    val_iterator = SerialIterator(val, 16, repeat=False, shuffle=False)
    eval_result = Evaluator(val_iterator,
                            regressor,
                            converter=concat_mols,
                            device=args.gpu)()
    print('Evaluation result: ', eval_result)
Esempio n. 31
0
def main():
    # Supported preprocessing/network list
    method_list = ['nfp', 'ggnn', 'schnet', 'weavenet', 'rsgcn']
    scale_list = ['standardize', 'none']

    parser = argparse.ArgumentParser(
        description='Regression with own dataset.')
    parser.add_argument('--datafile', type=str, default='dataset.csv')
    parser.add_argument('--method',
                        '-m',
                        type=str,
                        choices=method_list,
                        default='nfp')
    parser.add_argument('--label',
                        '-l',
                        nargs='+',
                        default=['value1', 'value2'],
                        help='target label for regression')
    parser.add_argument('--scale',
                        type=str,
                        choices=scale_list,
                        default='standardize',
                        help='Label scaling method')
    parser.add_argument('--conv-layers', '-c', type=int, default=4)
    parser.add_argument('--batchsize', '-b', type=int, default=32)
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--out', '-o', type=str, default='result')
    parser.add_argument('--epoch', '-e', type=int, default=20)
    parser.add_argument('--unit-num', '-u', type=int, default=16)
    parser.add_argument('--seed', '-s', type=int, default=777)
    parser.add_argument('--train-data-ratio', '-t', type=float, default=0.7)
    parser.add_argument('--protocol', type=int, default=2)
    args = parser.parse_args()

    seed = args.seed
    train_data_ratio = args.train_data_ratio
    method = args.method
    if args.label:
        labels = args.label
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        sys.exit("Error: No target label is specified.")

    # Dataset preparation
    # Postprocess is required for regression task
    def postprocess_label(label_list):
        return numpy.asarray(label_list, dtype=numpy.float32)

    print('Preprocessing dataset...')
    preprocessor = preprocess_method_dict[method]()
    parser = CSVFileParser(preprocessor,
                           postprocess_label=postprocess_label,
                           labels=labels,
                           smiles_col='SMILES')
    dataset = parser.parse(args.datafile)["dataset"]

    if args.scale == 'standardize':
        # Standard Scaler for labels
        scaler = StandardScaler()
        labels = scaler.fit_transform(dataset.get_datasets()[-1])
        dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1] +
                                      (labels, )))
    else:
        # Not use scaler
        scaler = None

    train_data_size = int(len(dataset) * train_data_ratio)
    train, val = split_dataset_random(dataset, train_data_size, seed)

    # Network
    n_unit = args.unit_num
    conv_layers = args.conv_layers
    if method == 'nfp':
        print('Train NFP model...')
        model = GraphConvPredictor(
            NFP(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'ggnn':
        print('Train GGNN model...')
        model = GraphConvPredictor(
            GGNN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'schnet':
        print('Train SchNet model...')
        model = GraphConvPredictor(
            SchNet(out_dim=class_num, hidden_dim=n_unit, n_layers=conv_layers),
            None)
    elif method == 'weavenet':
        print('Train WeaveNet model...')
        n_atom = 20
        n_sub_layer = 1
        weave_channels = [50] * conv_layers
        model = GraphConvPredictor(
            WeaveNet(weave_channels=weave_channels,
                     hidden_dim=n_unit,
                     n_sub_layer=n_sub_layer,
                     n_atom=n_atom), MLP(out_dim=class_num, hidden_dim=n_unit))
    elif method == 'rsgcn':
        print('Train RSGCN model...')
        model = GraphConvPredictor(
            RSGCN(out_dim=n_unit, hidden_dim=n_unit, n_layers=conv_layers),
            MLP(out_dim=class_num, hidden_dim=n_unit))
    else:
        raise ValueError('[ERROR] Invalid method {}'.format(method))

    train_iter = iterators.SerialIterator(train, args.batchsize)
    val_iter = iterators.SerialIterator(val,
                                        args.batchsize,
                                        repeat=False,
                                        shuffle=False)

    regressor = Regressor(
        model,
        lossfun=F.mean_squared_error,
        metrics_fun={'abs_error': ScaledAbsError(scaler=scaler)},
        device=args.gpu)

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

    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       device=args.gpu,
                                       converter=concat_mols)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(
        E.Evaluator(val_iter,
                    regressor,
                    device=args.gpu,
                    converter=concat_mols))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    # Note that original scale absolute errors are reported in
    # (validation/)main/abs_error
    trainer.extend(
        E.PrintReport([
            'epoch', 'main/loss', 'main/abs_error', 'validation/main/loss',
            'validation/main/abs_error', 'elapsed_time'
        ]))
    trainer.extend(E.ProgressBar())
    trainer.run()

    # --- save regressor's parameters ---
    protocol = args.protocol
    model_path = os.path.join(args.out, 'model.npz')
    print('saving trained model to {}'.format(model_path))
    serializers.save_npz(model_path, regressor)
    if scaler is not None:
        with open(os.path.join(args.out, 'scaler.pkl'), mode='wb') as f:
            pickle.dump(scaler, f, protocol=protocol)

    # Example of prediction using trained model
    smiles = 'c1ccccc1'
    mol = Chem.MolFromSmiles(smiles)
    preprocessor = preprocess_method_dict[method]()
    standardized_smiles, mol = preprocessor.prepare_smiles_and_mol(mol)
    input_features = preprocessor.get_input_features(mol)
    atoms, adjs = concat_mols([input_features], device=args.gpu)
    prediction = model(atoms, adjs).data[0]
    print('Prediction for {}:'.format(smiles))
    for i, label in enumerate(args.label):
        print('{}: {}'.format(label, prediction[i]))
Esempio n. 32
0
def main():
    # Parse the arguments.
    args = parse_arguments()
    device = chainer.get_device(args.device)

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label != 'all':
        label = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, label))
        labels = [label]
    else:
        labels = D.get_qm9_label_names()
        cache_dir = os.path.join('input', '{}_all'.format(method))

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached data from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()
        if num_data >= 0:
            # Select the first `num_data` samples from the dataset.
            target_index = numpy.arange(num_data)
            dataset = D.get_qm9(preprocessor,
                                labels=labels,
                                target_index=target_index)
        else:
            # Load the entire dataset.
            dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the newly preprocessed dataset.
        if not os.path.exists(cache_dir):
            os.mkdir(cache_dir)
        if isinstance(dataset, NumpyTupleDataset):
            NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Use a predictor with scaled output labels.
    model_path = os.path.join(args.in_dir, args.model_filename)
    regressor = Regressor.load_pickle(model_path, device=device)

    # Split the dataset into training and testing.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    _, test = split_dataset_random(dataset, train_data_size, args.seed)

    # This callback function extracts only the inputs and discards the labels.
    # TODO(nakago): consider how to switch which `converter` to use.
    if isinstance(dataset, NumpyTupleDataset):
        converter = converter_method_dict[method]

        @chainer.dataset.converter()
        def extract_inputs(batch, device=None):
            return converter(batch, device=device)[:-1]

        # Extract the ground-truth labels as numpy array.
        original_t = converter(test, device=-1)[-1]
    else:
        converter = dataset.converter
        extract_inputs = converter

        # Extract the ground-truth labels as numpy array.
        original_t = converter(test, device=-1).y

    # Predict the output labels.
    print('Predicting...')
    y_pred = regressor.predict(test, converter=extract_inputs)

    df_dict = {}
    for i, l in enumerate(labels):
        df_dict.update({
            'y_pred_{}'.format(l): y_pred[:, i],
            't_{}'.format(l): original_t[:, i],
        })
    df = pandas.DataFrame(df_dict)

    # Show a prediction/ground truth table with 5 random examples.
    print(df.sample(5))

    n_eval = 10
    for target_label in range(y_pred.shape[1]):
        label_name = labels[target_label]
        diff = y_pred[:n_eval, target_label] - original_t[:n_eval,
                                                          target_label]
        print('label_name = {}, y_pred = {}, t = {}, diff = {}'.format(
            label_name, y_pred[:n_eval, target_label],
            original_t[:n_eval, target_label], diff))

    # Run an evaluator on the test dataset.
    print('Evaluating...')
    test_iterator = SerialIterator(test, 16, repeat=False, shuffle=False)
    eval_result = Evaluator(test_iterator,
                            regressor,
                            converter=converter,
                            device=device)()
    print('Evaluation result: ', eval_result)
    # Save the evaluation results.
    save_json(os.path.join(args.in_dir, 'eval_result.json'), eval_result)

    # Calculate mean abs error for each label
    mae = numpy.mean(numpy.abs(y_pred - original_t), axis=0)
    eval_result = {}
    for i, l in enumerate(labels):
        eval_result.update({l: mae[i]})
    save_json(os.path.join(args.in_dir, 'eval_result_mae.json'), eval_result)
Esempio n. 33
0
    for j in range(len(dataset)):
        if dataset[j][1] == labelids[i]:
            for k in range(int(max(labeldist) / labeldist[i])):
                for l in range(extend):
                    datas.append(dataset[j][0])
                    labels.append(i)
conf = np.unique(labels, return_counts=True)
header = ['Label_No', 'ExtendedCount', 'Probability']
table = [conf[0], conf[1], ext]
result = tabulate(np.array(table).transpose(), header, tablefmt="grid")
log.write(result + "\n")
dataset = TupleDataset(datas, labels)
log.write("data extended\n")
#データをシャッフルする
from chainer.datasets import split_dataset_random
dataset, null = split_dataset_random(dataset, len(dataset), seed=0)
log.write("data randomized\n")
#データセット全体を 7 : 3 の比率でランダム分割し、学習用、検証用のデータセットとする
from chainer.datasets import split_dataset_random
n_train = int(len(dataset) * 0.7)
n_valid = int(len(dataset) * 0.3)
train, valid = split_dataset_random(dataset, n_train, seed=0)
train_len = len(train)
valid_len = len(valid)
log.write('Training dataset size:' + str(train_len) + "\n")
log.write('Validation dataset size:' + str(valid_len) + "\n")
train_datas = []
train_labels = []
valid_datas = []
valid_labels = []
def get_mpii_dataset(insize,
                     image_root,
                     annotations,
                     train_size=0.5,
                     min_num_keypoints=1,
                     use_cache=False,
                     seed=0):
    dataset_type = 'mpii'
    annotations = json.load(open(annotations, 'r'))

    # filename => keypoints, bbox, is_visible, is_labeled
    images = {}

    for filename in np.unique([anno['filename'] for anno in annotations]):
        images[filename] = [], [], [], []

    for anno in annotations:
        is_visible = [anno['is_visible'][k] for k in KEYPOINT_NAMES[1:]]
        if sum(is_visible) < min_num_keypoints:
            continue
        keypoints = [anno['joint_pos'][k][::-1] for k in KEYPOINT_NAMES[1:]]

        x1, y1, x2, y2 = anno['head_rect']

        entry = images[anno['filename']]
        entry[0].append(np.array(keypoints))  # array of y,x
        entry[1].append(np.array([x1, y1, x2 - x1, y2 - y1]))  # x, y, w, h
        entry[2].append(np.array(is_visible, dtype=np.bool))
        entry[3].append(np.ones(len(is_visible), dtype=np.bool))

    # split dataset
    train_images, test_images = split_dataset_random(
        list(images.keys()), int(len(images) * train_size), seed=seed)

    train_set = KeypointDataset2D(
        dataset_type=dataset_type,
        insize=insize,
        keypoint_names=KEYPOINT_NAMES,
        edges=np.array(EDGES),
        flip_indices=FLIP_INDICES,
        keypoints=[images[i][0] for i in train_images],
        bbox=[images[i][1] for i in train_images],
        is_visible=[images[i][2] for i in train_images],
        is_labeled=[images[i][3] for i in train_images],
        image_paths=train_images,
        image_root=image_root,
        use_cache=use_cache,
        do_augmentation=True)

    test_set = KeypointDataset2D(
        dataset_type=dataset_type,
        insize=insize,
        keypoint_names=KEYPOINT_NAMES,
        edges=np.array(EDGES),
        flip_indices=FLIP_INDICES,
        keypoints=[images[i][0] for i in test_images],
        bbox=[images[i][1] for i in test_images],
        is_visible=[images[i][2] for i in test_images],
        is_labeled=[images[i][3] for i in test_images],
        image_paths=test_images,
        image_root=image_root,
        use_cache=use_cache,
        do_augmentation=False)
    return train_set, test_set
Esempio n. 35
0
def main():
    parser = argparse.ArgumentParser(
        description='Chainer example: POS-tagging')
    parser.add_argument('--batchsize', '-b', type=int, default=30,
                        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('--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()

    vocab = collections.defaultdict(lambda: len(vocab))
    pos_vocab = collections.defaultdict(lambda: len(pos_vocab))

    # Convert word sequences and pos sequences to integer sequences.
    nltk.download('brown')
    data = []
    for sentence in nltk.corpus.brown.tagged_sents():
        xs = numpy.array([vocab[lex] for lex, _ in sentence], numpy.int32)
        ys = numpy.array([pos_vocab[pos] for _, pos in sentence], numpy.int32)
        data.append((xs, ys))

    print('# of sentences: {}'.format(len(data)))
    print('# of words: {}'.format(len(vocab)))
    print('# of pos: {}'.format(len(pos_vocab)))

    model = CRF(len(vocab), len(pos_vocab))
    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu(args.gpu)
    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001))

    test_data, train_data = datasets.split_dataset_random(
        data, len(data) // 10, seed=0)

    train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test_data, args.batchsize,
                                                 repeat=False, shuffle=False)
    updater = training.updaters.StandardUpdater(
        train_iter, optimizer, converter=convert, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    evaluator = extensions.Evaluator(
        test_iter, model, device=args.gpu, converter=convert)
    # Only validate in each 1000 iteration
    trainer.extend(evaluator, trigger=(1000, 'iteration'))
    trainer.extend(extensions.LogReport(trigger=(100, 'iteration')),
                   trigger=(100, 'iteration'))

    trainer.extend(
        extensions.MicroAverage(
            'main/correct', 'main/total', 'main/accuracy'))
    trainer.extend(
        extensions.MicroAverage(
            'validation/main/correct', 'validation/main/total',
            'validation/main/accuracy'))

    trainer.extend(
        extensions.PrintReport(
            ['epoch', 'main/loss', 'validation/main/loss',
             'main/accuracy', 'validation/main/accuracy', 'elapsed_time']),
        trigger=(100, 'iteration'))

    trainer.extend(extensions.ProgressBar(update_interval=10))

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

    trainer.run()
Esempio n. 36
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label != 'all':
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = None
        cache_dir = os.path.join('input', '{}_all'.format(method))
        class_num = len(D.get_qm9_label_names())

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached dataset from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()

        if num_data >= 0:
            # Select the first `num_data` samples from the dataset.
            target_index = numpy.arange(num_data)
            dataset = D.get_qm9(preprocessor,
                                labels=labels,
                                target_index=target_index)
        else:
            # Load the entire dataset.
            dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the laded dataset.
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Scale the label values, if necessary.
    if args.scale == 'standardize':
        print('Applying standard scaling to the labels.')
        scaler = StandardScaler()
        scaled_t = scaler.fit_transform(dataset.get_datasets()[-1])
        dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1] +
                                      (scaled_t, )))
    else:
        print('No standard scaling was selected.')
        scaler = None

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, valid = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(method, args.unit_num, args.conv_layers,
                                 class_num, scaler)

    # Set up the iterators.
    train_iter = iterators.SerialIterator(train, args.batchsize)
    valid_iter = iterators.SerialIterator(valid,
                                          args.batchsize,
                                          repeat=False,
                                          shuffle=False)

    # Set up the regressor.
    device = args.gpu
    metrics_fun = {
        'mae': MeanAbsError(scaler=scaler),
        'rmse': RootMeanSqrError(scaler=scaler)
    }
    regressor = Regressor(predictor,
                          lossfun=F.mean_squared_error,
                          metrics_fun=metrics_fun,
                          device=device)

    # Set up the optimizer.
    optimizer = optimizers.Adam()
    optimizer.setup(regressor)

    # Set up the updater.
    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       device=device,
                                       converter=concat_mols)

    # Set up the trainer.
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(
        E.Evaluator(valid_iter,
                    regressor,
                    device=device,
                    converter=concat_mols))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(
        E.PrintReport([
            'epoch', 'main/loss', 'main/mae', 'main/rmse',
            'validation/main/loss', 'validation/main/mae',
            'validation/main/rmse', 'elapsed_time'
        ]))
    trainer.extend(E.ProgressBar())
    trainer.run()

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained model to {}...'.format(model_path))
    regressor.save_pickle(model_path, protocol=args.protocol)
Esempio n. 37
0
def main():
    # Parse the arguments.
    args = parse_arguments()

    # Set up some useful variables that will be used later on.
    method = args.method
    if args.label != 'all':
        labels = args.label
        cache_dir = os.path.join('input', '{}_{}'.format(method, labels))
        class_num = len(labels) if isinstance(labels, list) else 1
    else:
        labels = None
        cache_dir = os.path.join('input', '{}_all'.format(method))
        class_num = len(D.get_qm9_label_names())

    # Get the filename corresponding to the cached dataset, based on the amount
    # of data samples that need to be parsed from the original dataset.
    num_data = args.num_data
    if num_data >= 0:
        dataset_filename = 'data_{}.npz'.format(num_data)
    else:
        dataset_filename = 'data.npz'

    # Load the cached dataset.
    dataset_cache_path = os.path.join(cache_dir, dataset_filename)

    dataset = None
    if os.path.exists(dataset_cache_path):
        print('Loading cached dataset from {}.'.format(dataset_cache_path))
        dataset = NumpyTupleDataset.load(dataset_cache_path)
    if dataset is None:
        print('Preprocessing dataset...')
        preprocessor = preprocess_method_dict[method]()

        if num_data >= 0:
            # Select the first `num_data` samples from the dataset.
            target_index = numpy.arange(num_data)
            dataset = D.get_qm9(preprocessor, labels=labels,
                                target_index=target_index)
        else:
            # Load the entire dataset.
            dataset = D.get_qm9(preprocessor, labels=labels)

        # Cache the laded dataset.
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)
        NumpyTupleDataset.save(dataset_cache_path, dataset)

    # Scale the label values, if necessary.
    if args.scale == 'standardize':
        print('Applying standard scaling to the labels.')
        scaler = StandardScaler()
        scaled_t = scaler.fit_transform(dataset.get_datasets()[-1])
        dataset = NumpyTupleDataset(*(dataset.get_datasets()[:-1]
                                      + (scaled_t,)))
    else:
        print('No standard scaling was selected.')
        scaler = None

    # Split the dataset into training and validation.
    train_data_size = int(len(dataset) * args.train_data_ratio)
    train, valid = split_dataset_random(dataset, train_data_size, args.seed)

    # Set up the predictor.
    predictor = set_up_predictor(method, args.unit_num, args.conv_layers,
                                 class_num, scaler)

    # Set up the iterators.
    train_iter = iterators.SerialIterator(train, args.batchsize)
    valid_iter = iterators.SerialIterator(valid, args.batchsize, repeat=False,
                                          shuffle=False)

    # Set up the regressor.
    device = args.gpu
    metrics_fun = {'mae': MeanAbsError(scaler=scaler),
                   'rmse': RootMeanSqrError(scaler=scaler)}
    regressor = Regressor(predictor, lossfun=F.mean_squared_error,
                          metrics_fun=metrics_fun, device=device)

    # Set up the optimizer.
    optimizer = optimizers.Adam()
    optimizer.setup(regressor)

    # Set up the updater.
    updater = training.StandardUpdater(train_iter, optimizer, device=device,
                                       converter=concat_mols)

    # Set up the trainer.
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
    trainer.extend(E.Evaluator(valid_iter, regressor, device=device,
                               converter=concat_mols))
    trainer.extend(E.snapshot(), trigger=(args.epoch, 'epoch'))
    trainer.extend(E.LogReport())
    trainer.extend(E.PrintReport([
        'epoch', 'main/loss', 'main/mae', 'main/rmse', 'validation/main/loss',
        'validation/main/mae', 'validation/main/rmse', 'elapsed_time']))
    trainer.extend(E.ProgressBar())
    trainer.run()

    # Save the regressor's parameters.
    model_path = os.path.join(args.out, args.model_filename)
    print('Saving the trained model to {}...'.format(model_path))
    regressor.save_pickle(model_path, protocol=args.protocol)