Beispiel #1
0
def main(args):
    batch_size = 128
    ds_train, ds_test, info = load_data(batch_size)

    image_shape = info.features["image"].shape
    image_size = tf.reduce_prod(image_shape)
    num_train_samples = info.splits["train"].num_examples
    num_test_samples = info.splits["test"].num_examples

    cnn = Cnn()
    if "init_model" in args.cnn_mode:
        cnn.create_model(batch_size=batch_size,
                         image_shape=image_shape,
                         feature_outputs=int(100))
    if "train" in args.cnn_mode:
        cnn.train(ds_train,
                  ds_test,
                  epochs=10,
                  steps_per_epoch=int(num_train_samples / batch_size))
    if "save" in args.cnn_mode:
        cnn.save()
    if "load" in args.cnn_mode:
        cnn.load_combined_model()
    if "test" in args.cnn_mode:
        cnn.test(ds_test)

    gp = DeepKernelGP(ds_train,
                      num_train_samples,
                      image_size,
                      10,
                      cnn.feature_extractor,
                      num_inducing_points=100)
    if "train" in args.gp_mode:
        gp.train(10000)
    if "test" in args.gp_mode:
        gp.test(ds_test, image_size, batch_size, num_test_samples)
Beispiel #2
0
def main():
    epochs = 100
    #x, y = load_data(DATA_PATH, verbose=False, num_samples=5)
    x = np.load('/global/scratch/alex_vlissidis/X.npz')['x']
    y = np.load('/global/scratch/alex_vlissidis/y.npz')['y']

    print("training data shapes:", x.shape, y.shape)

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

    print("building model...")
    print(x.shape)
    print(y.shape[1])
    model = Cnn(x.shape[1:], y.shape[1])
    model.build()

    history = model.train(x_train, y_train, epochs=epochs)

    print("Saving model...")
    model.model.save('models/model.h5')

    print("Plotting...")
    f, (ax1, ax2) = plt.subplots(2, 1)
    ax1.plot(range(1, epochs + 1),
             history.history['val_acc'],
             'tab:blue',
             label="validation accuracy")
    ax1.plot(range(1, epochs + 1),
             history.history['acc'],
             'tab:red',
             label="training accuracy")

    ax2.plot(range(1, epochs + 1),
             history.history['loss'],
             'tab:orange',
             label="loss")
    ax2.plot(range(1, epochs + 1),
             history.history['val_loss'],
             'tab:green',
             label="validation loss")

    ax1.legend()
    ax2.legend()

    f.savefig('figures/training.png', dpi=300)
    print("Done.")
Beispiel #3
0
    testingAccuracy = []
    testingLoss = []

    epochs = list(range(0, cfg.epochs))
    #training
    for epoch in range(0, cfg.epochs):

        #records weights to see if they change after training
        weights1 = []
        for param in cnn.parameters():
            weights1.append(param.clone())

        #training
        print("Training Epoch", str(epoch + 1), "of", str(cfg.epochs) + ":")
        trainAcc, trainLoss = cnn.train(trainImgs, trainOneHotVecs,
                                        cfg.trainBatchSize)
        trainingAccuracy.append(trainAcc)
        trainingLoss.append(trainLoss)

        #prints if weights are unchanged
        weights2 = []
        unchangedWeights = 0
        for param in cnn.parameters():
            weights2.append(param.clone())
        for i in zip(weights1, weights2):
            if torch.equal(i[0], i[1]):
                unchangedWeights += 1

        if unchangedWeights > 0:
            print(unchangedWeights, "of", len(weights1), "Weights Unchanged")