コード例 #1
0
ファイル: cnn_train.py プロジェクト: dsuarezv/nuts-ml
def train():
    from keras.metrics import categorical_accuracy

    transform = (TransformImage(0).by('rerange', 0, 255, 0, 1, 'float32'))
    build_batch = (BuildBatch(BATCH_SIZE).by(0, 'image', 'float32').by(
        1, 'one_hot', 'uint8', NUM_CLASSES))
    plot = PlotLines((0, 1), layout=(2, 1), every_sec=1)

    print('loading data...')
    train_samples, test_samples = load_samples()

    print('creating network ...')
    network = create_network()

    print('training...', NUM_EPOCHS)
    for epoch in range(NUM_EPOCHS):
        print('EPOCH:', epoch)

        t_loss, t_acc = (train_samples >> PrintProgress(train_samples) >>
                         Pick(PICK) >> transform >> Shuffle(100) >> build_batch
                         >> network.train() >> plot >> Unzip())
        print('train loss : {:.6f}'.format(t_loss >> Mean()))
        print('train acc  : {:.1f}'.format(100 * (t_acc >> Mean())))

        e_acc = (test_samples >> transform >> build_batch >> network.evaluate(
            [categorical_accuracy]))
        print('test acc   : {:.1f}'.format(100 * e_acc))

        network.save_best(e_acc, isloss=False)
コード例 #2
0
ファイル: train.py プロジェクト: maet3608/example-gcn-project
def train(cfg: CFG, net: Network, traindata: Samples, valdata: Samples):
    plotlines = PlotLines((0, 1, 2),
                          layout=(3, 1),
                          figsize=(8, 12),
                          titles=('loss', 'train-acc', 'val-acc'))
    train_cache = Cache(cfg.cachedir + '/train', cfg.cacheclear)
    val_cache = Cache(cfg.cachedir + '/val', cfg.cacheclear)
    optimizer = to.optim.Adam(net.parameters(), lr=cfg.lr)

    max_val = 0
    for epoch in range(cfg.n_epochs):
        start = time.time()
        net.train()
        losses, accs = (
            traindata >> LoadGraph() >> train_cache >> Shuffle(100) >>
            MakeBatch(cfg.batchsize) >> TrainBatch(net, optimizer) >> Unzip())
        loss, acc = np.mean(losses), np.mean(accs)

        if cfg.verbose:
            msg = "{:d}..{:d}  {:s} : {:.4f}  ({:.1f}% {:.1f}%)"
            elapsed = time.strftime("%M:%S", time.gmtime(time.time() - start))
            print(msg.format(epoch, cfg.n_epochs, elapsed, loss, acc, max_val))

        val_acc = validate(cfg, net, valdata, val_cache)
        if val_acc >= max_val:
            max_val = val_acc
            save_wgts(net)

        if cfg.verbose > 1:
            plotlines((loss, acc, max_val))
コード例 #3
0
def train():
    from keras.metrics import categorical_accuracy

    rerange = TransformImage(0).by('rerange', 0, 255, 0, 1, 'float32')
    build_batch = (BuildBatch(BATCH_SIZE).by(0, 'image', 'float32').by(
        1, 'one_hot', 'uint8', NUM_CLASSES))
    p = 0.1
    augment = (AugmentImage(0).by('identical', 1.0).by(
        'elastic', p, [5, 5], [100, 100],
        [0, 100]).by('brightness', p,
                     [0.7, 1.3]).by('color', p, [0.7, 1.3]).by(
                         'shear', p,
                         [0, 0.1]).by('fliplr', p).by('rotate', p, [-10, 10]))
    plot_eval = PlotLines((0, 1), layout=(2, 1))

    print('creating network...')
    network = create_network()

    print('loading data...')
    train_samples, test_samples = load_samples()
    train_samples, val_samples = train_samples >> SplitRandom(0.8)

    print('training...', len(train_samples), len(val_samples))
    for epoch in range(NUM_EPOCHS):
        print('EPOCH:', epoch)

        t_loss, t_acc = (train_samples >> PrintProgress(train_samples) >>
                         Pick(PICK) >> augment >> rerange >> Shuffle(100) >>
                         build_batch >> network.train() >> Unzip())
        t_loss, t_acc = t_loss >> Mean(), t_acc >> Mean()
        print("train loss : {:.6f}".format(t_loss))
        print("train acc  : {:.1f}".format(100 * t_acc))

        v_loss, v_acc = (val_samples >> rerange >> build_batch >>
                         network.validate() >> Unzip())
        v_loss, v_acc = v_acc >> Mean(), v_acc >> Mean()
        print('val loss   : {:.6f}'.format(v_loss))
        print('val acc    : {:.1f}'.format(100 * v_acc))

        network.save_best(v_acc, isloss=False)
        plot_eval((t_acc, v_acc))

    print('testing...', len(test_samples))
    e_acc = (test_samples >> rerange >> build_batch >> network.evaluate(
        [categorical_accuracy]))
    print('test acc   : {:.1f}'.format(100 * e_acc))