def main():
    parser = argparse.ArgumentParser(description='estimation from formant to vocal tube model parameter')
    parser.add_argument('--batchsize', '-b', type=int, default=100, help='Number of images in each mini-batch')
    parser.add_argument('--epoch', '-e', type=int, default=650, help='Number of sweeps over the dataset to train')
    parser.add_argument('--frequency', '-f', type=int, default=(- 1), help='Frequency of taking a snapshot')
    parser.add_argument('--gpu', '-g', type=int, default=(- 1), help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out', '-o', default='result', help='Prefix Directory Name to output the result')
    parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot')
    parser.add_argument('--unit', '-u', type=int, default=100, help='Number of units')
    parser.add_argument('--noplot', dest='plot', action='store_false', help='Disable PlotReport extension')
    parser.add_argument('--delta', '-d', type=float, default=0.5, help='delta for length and area: for train')
    parser.add_argument('--delta_for_test', '-t', type=float, default=1.5, help='delta for length and area: for test')
    parser.add_argument('--model_type', '-m', default='MLP', help='choice MLP or CNN')
    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))
    if (args.model_type == 'CNN'):
        CNN_flag = True
        print('# CNN')
    else:
        CNN_flag = False
        print('# MLP')
    train = TM_DatsSet(args.delta, args.delta, CNN_flag)
    test = TM_DatsSet(args.delta_for_test, args.delta_for_test, CNN_flag)
    n_out = train.n_out
    out_dir = (args.out + train.suffix_list)
    print('# result directory: ', out_dir)
    if CNN_flag:
        model = L.Classifier(CNN(args.unit, n_out), lossfun=mean_absolute_error)
    else:
        model = L.Classifier(MLP(args.unit, n_out), lossfun=mean_absolute_error)
    model.compute_accuracy = False
    if (args.gpu >= 0):
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()
    optimizer = chainer.optimizers.Adam(alpha=0.001)
    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=out_dir)
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))
    trainer.extend(extensions.dump_graph('main/loss'))
    frequency = (args.epoch if (args.frequency == (- 1)) else max(1, args.frequency))
    trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch'))
    trainer.extend(extensions.LogReport())
    if (args.plot and extensions.PlotReport.available()):
        trainer.extend(PlotReport2(['main/loss', 'validation/main/loss'], 'epoch', file_name='loss.png', LogScale=True))
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']))
    trainer.extend(extensions.ProgressBar())
    if args.resume:
        chainer.serializers.load_npz(args.resume, trainer)
    trainer.run()
    print('checking accuracy rate... ')
    c0 = 0
    for loop in range(train.__len__()):
        x1 = train.get_example(loop)
        if (args.gpu >= 0):
            x_batch = cuda.cupy.asarray([x1[0]])
            y_gpu = model.predictor(x_batch)
            y = cuda.to_cpu(y_gpu.data)
        else:
            x_batch = np.asarray([x1[0]])
            y_cpu = model.predictor(x_batch)
            y = y_cpu.data[0]
        if (np.round(y) == np.round(x1[1])):
            c0 += 1
    print('Accuracy rate (index is equal, ratio[%]) ', ((c0 * 100.0) / train.__len__()))
def main():
    parser = argparse.ArgumentParser(description='estimation from formant to vocal tube model parameter')
    parser.add_argument('--batchsize', '-b', type=int, default=100, help='Number of images in each mini-batch')
    parser.add_argument('--epoch', '-e', type=int, default=650, help='Number of sweeps over the dataset to train')
    parser.add_argument('--frequency', '-f', type=int, default=(- 1), help='Frequency of taking a snapshot')
    parser.add_argument('--gpu', '-g', type=int, default=(- 1), help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out', '-o', default='result', help='Prefix Directory Name to output the result')
    parser.add_argument('--resume', '-r', default=, help='Resume the training from snapshot')
    parser.add_argument('--unit', '-u', type=int, default=100, help='Number of units')
    parser.add_argument('--noplot', dest='plot', action='store_false', help='Disable PlotReport extension')
    parser.add_argument('--delta', '-d', type=float, default=0.5, help='delta for length and area: for train')
    parser.add_argument('--delta_for_test', '-t', type=float, default=1.5, help='delta for length and area: for test')
    parser.add_argument('--model_type', '-m', default='MLP', help='choice MLP or CNN')
    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))
    if (args.model_type == 'CNN'):
        CNN_flag = True
        print('# CNN')
    else:
        CNN_flag = False
        print('# MLP')
    train = TM_DatsSet(args.delta, args.delta, CNN_flag)
    test = TM_DatsSet(args.delta_for_test, args.delta_for_test, CNN_flag)
    n_out = train.n_out
    out_dir = (args.out + train.suffix_list)
    print('# result directory: ', out_dir)
    if CNN_flag:
        model = L.Classifier(CNN(args.unit, n_out), lossfun=mean_absolute_error)
    else:
        model = L.Classifier(MLP(args.unit, n_out), lossfun=mean_absolute_error)
    model.compute_accuracy = False
    if (args.gpu >= 0):
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()
    optimizer = chainer.optimizers.Adam(alpha=0.001)
    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=out_dir)
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))
    trainer.extend(extensions.dump_graph('main/loss'))
    frequency = (args.epoch if (args.frequency == (- 1)) else max(1, args.frequency))
    trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch'))
    trainer.extend(extensions.LogReport())
    if (args.plot and extensions.PlotReport.available()):
        trainer.extend(PlotReport2(['main/loss', 'validation/main/loss'], 'epoch', file_name='loss.png', LogScale=True))
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']))
    trainer.extend(extensions.ProgressBar())
    if args.resume:
        chainer.serializers.load_npz(args.resume, trainer)
    trainer.run()
    print('checking accuracy rate... ')
    c0 = 0
    for loop in range(train.__len__()):
        x1 = train.get_example(loop)
        if (args.gpu >= 0):
            x_batch = cuda.cupy.asarray([x1[0]])
            y_gpu = model.predictor(x_batch)
            y = cuda.to_cpu(y_gpu.data)
        else:
            x_batch = np.asarray([x1[0]])
            y_cpu = model.predictor(x_batch)
            y = y_cpu.data[0]
        if (np.round(y) == np.round(x1[1])):
            c0 += 1
    print('Accuracy rate (index is equal, ratio[%]) ', ((c0 * 100.0) / train.__len__()))