def train(self, inputs, targets, lr=1, batch_size=30, epochs=100, plot=False, kernel='linear'):

        self.batch_size = batch_size
        # init the kernel
        self.set_kernel(kernel)

        # set optimization method (Gradient Descent)
        self.optimization = tf.train.GradientDescentOptimizer(lr)
        self.training_step = self.optimization.minimize(self.loss)
        self.init = tf.global_variables_initializer()
        self.session.run(self.init)

        # set training data
        train_inputs, train_target = inputs, targets

        # performance tracking
        train_loss_result, train_accuracy_result = [], []

        # for each epoch
        for i in range(epochs):

            # generate random indexes for each batch
            batch_index = np.random.choice(len(train_inputs), size=batch_size)
            self.session.run(self.training_step, feed_dict={self.inputs: train_inputs[batch_index],
                                                            self.target: train_target[:, batch_index]})
            # if plotting, record every epoch
            if plot:
                # record accuracy
                train_accuracy, train_loss = self.generate_step_tracking_data(
                    train_inputs[batch_index], train_target[:, batch_index])
                train_accuracy_result.append(train_accuracy)
                train_loss_result.append(train_loss)

            if (i+1) % (epochs / 5) == 0:
                # if not plotting, get intermittent accuracy and loss
                if not plot:
                    # record accuracy
                    train_accuracy, train_loss = self.generate_step_tracking_data(
                        train_inputs[batch_index], train_target[:, batch_index])
                utl.print_progress(i, epochs, train_loss, train_accuracy)

        # plot results
        if plot:
            if not self.features == 2:
                print('Plotting only supported for 2 feature data sets... skipping output')
            else:
                utl.plot_loss(train_loss_result)
                utl.plot_accuracy(train_accuracy_result)
                grid = utl.generate_grid(train_inputs)
                grid_predictions = self.session.run(self.prediction, feed_dict={self.inputs: train_inputs[batch_index],
                                                                                self.target: train_target[:, batch_index],
                                                                                self.grid: grid})
                # plot the result grid
                utl.plot_result(grid_predictions, inputs, targets)

        # commit data points for the last support vectors used
        self.support_vector_data = [train_inputs[batch_index], train_target[:, batch_index]]
    def train_KT_AT(self):
        # summary for current training loop and a running average object for loss
        # Use tqdm for progress bar
        accuracy_dict = {}

        for epoch in tqdm(range(self.num_epochs), total=self.num_epochs):
            self.student_model.train()
            self.train()

            if epoch % self.log_num == 0:
                acc = self.test()
                accuracy_dict[epoch] = acc
                utils.log_accuracy("KD_AT.csv", accuracy_dict)
                print(f"\nAccuracy: {acc:05.3f}")
                self.save_model()

            self.scheduler.step()

        utils.plot_accuracy("KD_AT.csv")
Example #3
0
def main():
    mnist = mn.input_data.read_data_sets("./data/", one_hot=True, reshape=True)
    X_train, y_train = mnist.train.images, mnist.train.labels
    X_valid, y_valid = mnist.validation.images, mnist.validation.labels
    X_test, y_test = mnist.test.images, mnist.test.labels

    classify_time = time.time()

    # Shuffle the training data.
    X_train, y_train = shuffle(X_train, y_train)

    exist_or_create_folder(weights_path)

    accuracy_valid = []
    with tf.Session() as sess:
        # build LeNet.
        net = build_network(sess)
        saver = tf.train.Saver()
        x, y = net[0]
        out_softmax, loss, train_op, accuracy_op = net[1]

        num_examples = len(X_train)
        print('Training...')
        for i in range(EPOCHS):
            X_train, y_train = shuffle(X_train, y_train)
            # Batch gradient descent.
            n_batches = num_examples // BATCH_SIZE
            for ind in range(0, n_batches):
                start = ind * BATCH_SIZE
                end = (ind + 1) * BATCH_SIZE
                batch_x, batch_y = X_train[start:end], y_train[start:end]
                sess.run(train_op, feed_dict={x: batch_x, y: batch_y})

            # evaluate accuracy on the validation set.
            num_examples = len(X_valid)
            total_accuracy = 0
            sess = tf.get_default_session()
            for offset in range(0, num_examples, BATCH_SIZE):
                batch_x, batch_y = X_valid[offset:offset +
                                           BATCH_SIZE], y_valid[offset:offset +
                                                                BATCH_SIZE]
                accuracy = sess.run(accuracy_op,
                                    feed_dict={
                                        x: batch_x,
                                        y: batch_y
                                    })
                total_accuracy += (accuracy * len(batch_x))
            validation_accuracy = total_accuracy / num_examples

            accuracy_valid.append(validation_accuracy)

            print('EPOCH {0} | Validation Accuracy = {1:.3f}'.format(
                i + 1, validation_accuracy))

        saver.save(sess, weights_path)

    np.savetxt('{0}accuracy.out'.format(log_path), np.asarray(accuracy_valid),
               '%f')
    plot_accuracy(accuracy_valid, log_path, 'feed forward NN')

    with tf.Session() as sess:
        saver.restore(sess, weights_path)
        # evaluate accuracy on the test set.
        num_examples = len(X_test)
        total_accuracy = 0
        sess = tf.get_default_session()
        for offset in range(0, num_examples, BATCH_SIZE):
            batch_x, batch_y = X_test[offset:offset +
                                      BATCH_SIZE], y_test[offset:offset +
                                                          BATCH_SIZE]
            accuracy = sess.run(accuracy_op,
                                feed_dict={
                                    x: batch_x,
                                    y: batch_y
                                })
            total_accuracy += (accuracy * len(batch_x))
        test_accuracy = total_accuracy / num_examples
        print("Test Accuracy = {:.3f}".format(test_accuracy))

    classified_time = time.time() - classify_time
    print('Classified time: {0:.4f} seconds.'.format(classified_time))
    write_to_file('{0}time.txt'.format(log_path), classified_time, True)
def main(args):

    logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
    device = torch.device(
        'cuda' if args.use_cuda and torch.cuda.is_available() else 'cpu')

    # Create output folder
    if (args.output_folder is not None):
        if not os.path.exists(args.output_folder):
            os.makedirs(args.output_folder)
            logging.debug('Creating folder `{0}`'.format(args.output_folder))

        output_folder = os.path.join(args.output_folder,
                                     time.strftime('%Y-%m-%d_%H%M%S'))
        os.makedirs(output_folder)
        logging.debug('Creating folder `{0}`'.format(output_folder))

        args.datafolder = os.path.abspath(args.datafolder)
        args.model_path = os.path.abspath(
            os.path.join(output_folder, 'model.th'))

        # Save the configuration in a config.json file
        with open(os.path.join(output_folder, 'config.json'), 'w') as f:
            json.dump(vars(args), f, indent=2)
        logging.info('Saving configuration file in `{0}`'.format(
            os.path.abspath(os.path.join(output_folder, 'config.json'))))

    # Get datasets and load into meta learning format
    meta_train_dataset, meta_val_dataset, _ = get_datasets(
        args.dataset,
        args.datafolder,
        args.num_ways,
        args.num_shots,
        args.num_shots_test,
        augment=augment,
        fold=args.fold,
        download=download_data)

    meta_train_dataloader = BatchMetaDataLoader(meta_train_dataset,
                                                batch_size=args.batch_size,
                                                shuffle=True,
                                                num_workers=args.num_workers,
                                                pin_memory=True)

    meta_val_dataloader = BatchMetaDataLoader(meta_val_dataset,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              num_workers=args.num_workers,
                                              pin_memory=True)

    # Define model
    model = Unet(device=device, feature_scale=args.feature_scale)
    model = model.to(device)
    print(f'Using device: {device}')

    # Define optimizer
    meta_optimizer = torch.optim.Adam(model.parameters(),
                                      lr=args.meta_lr)  #, weight_decay=1e-5)
    #meta_optimizer = torch.optim.RMSprop(model.parameters(), lr=learning_rate, momentum = 0.99)

    # Define meta learner
    metalearner = ModelAgnosticMetaLearning(
        model,
        meta_optimizer,
        first_order=args.first_order,
        num_adaptation_steps=args.num_adaption_steps,
        step_size=args.step_size,
        learn_step_size=False,
        loss_function=loss_function,
        device=device)

    best_value = None

    # Training loop
    epoch_desc = 'Epoch {{0: <{0}d}}'.format(1 +
                                             int(math.log10(args.num_epochs)))
    train_losses = []
    val_losses = []
    train_ious = []
    train_accuracies = []
    val_accuracies = []
    val_ious = []

    start_time = time.time()

    for epoch in range(args.num_epochs):
        print('start epoch ', epoch + 1)
        print('start train---------------------------------------------------')
        train_loss, train_accuracy, train_iou = metalearner.train(
            meta_train_dataloader,
            max_batches=args.num_batches,
            verbose=args.verbose,
            desc='Training',
            leave=False)
        print(f'\n train accuracy: {train_accuracy}, train loss: {train_loss}')
        print('end train---------------------------------------------------')
        train_losses.append(train_loss)
        train_accuracies.append(train_accuracy)
        train_ious.append(train_iou)

        # Evaluate in given intervals
        if epoch % args.val_step_size == 0:
            print(
                'start evaluate-------------------------------------------------'
            )
            results = metalearner.evaluate(meta_val_dataloader,
                                           max_batches=args.num_batches,
                                           verbose=args.verbose,
                                           desc=epoch_desc.format(epoch + 1),
                                           is_test=False)
            val_acc = results['accuracy']
            val_loss = results['mean_outer_loss']
            val_losses.append(val_loss)
            val_accuracies.append(val_acc)
            val_ious.append(results['iou'])
            print(
                f'\n validation accuracy: {val_acc}, validation loss: {val_loss}'
            )
            print(
                'end evaluate-------------------------------------------------'
            )

            # Save best model
            if 'accuracies_after' in results:
                if (best_value is None) or (best_value <
                                            results['accuracies_after']):
                    best_value = results['accuracies_after']
                    save_model = True
            elif (best_value is None) or (best_value >
                                          results['mean_outer_loss']):
                best_value = results['mean_outer_loss']
                save_model = True
            else:
                save_model = False

            if save_model and (args.output_folder is not None):
                with open(args.model_path, 'wb') as f:
                    torch.save(model.state_dict(), f)

        print('end epoch ', epoch + 1)

    elapsed_time = time.time() - start_time
    print('Finished after ',
          time.strftime('%H:%M:%S', time.gmtime(elapsed_time)))

    r = {}
    r['train_losses'] = train_losses
    r['train_accuracies'] = train_accuracies
    r['train_ious'] = train_ious
    r['val_losses'] = val_losses
    r['val_accuracies'] = val_accuracies
    r['val_ious'] = val_ious
    r['time'] = time.strftime('%H:%M:%S', time.gmtime(elapsed_time))
    with open(os.path.join(output_folder, 'train_results.json'), 'w') as g:
        json.dump(r, g)
        logging.info('Saving results dict in `{0}`'.format(
            os.path.abspath(os.path.join(output_folder,
                                         'train_results.json'))))

    # Plot results
    plot_errors(args.num_epochs,
                train_losses,
                val_losses,
                val_step_size=args.val_step_size,
                output_folder=output_folder,
                save=True,
                bce_dice_focal=bce_dice_focal)
    plot_accuracy(args.num_epochs,
                  train_accuracies,
                  val_accuracies,
                  val_step_size=args.val_step_size,
                  output_folder=output_folder,
                  save=True)
    plot_iou(args.num_epochs,
             train_ious,
             val_ious,
             val_step_size=args.val_step_size,
             output_folder=output_folder,
             save=True)

    if hasattr(meta_train_dataset, 'close'):
        meta_train_dataset.close()
        meta_val_dataset.close()
Example #5
0
"""Testing A Simple Prediction"""
#print("Feature vector: %s" % X_test[:1])
print("Label: %s" % str(y_test[0]))
print("Predicted: %s" % str(net0.predict(X_test[:1])))


"""Metrics"""

# layer_info = PrintLayerInfo()
# net0.verbose = 3
# net0.initialize()
#print layer_info(net0)

print "[Classification Report]: "
print classification_report(y_test, predicted)
print "[Train dataset] Score: ", net0.score(X_train, y_train)
print "[Test dataset] Score: ", net0.score(X_test, y_test)
plot_matrix(net0, X_test, y_test, filename)

valid_accuracies = np.array([i["valid_accuracy"] for i in net0.train_history_])
plot_accuracy(valid_accuracies, filename)

train_loss = [row['train_loss'] for row in net0.train_history_]
valid_loss = [row['valid_loss'] for row in net0.train_history_]
plot_loss(valid_loss, train_loss, filename)

y_score = net0.predict_proba(X_test) #[:, 1]
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename=filename)
Example #6
0
def main():
    mnist = mn.input_data.read_data_sets("./data/",
                                         one_hot=False,
                                         reshape=False)
    X_train, y_train = mnist.train.images, mnist.train.labels
    X_valid, y_valid = mnist.validation.images, mnist.validation.labels
    X_test, y_test = mnist.test.images, mnist.test.labels

    classify_time = time.time()

    # Pad images with 0s
    X_train = np.pad(X_train, ((0, 0), (2, 2), (2, 2), (0, 0)),
                     'constant',
                     constant_values=(0, 0))
    X_valid = np.pad(X_valid, ((0, 0), (2, 2), (2, 2), (0, 0)),
                     'constant',
                     constant_values=(0, 0))
    X_test = np.pad(X_test, ((0, 0), (2, 2), (2, 2), (0, 0)),
                    'constant',
                    constant_values=(0, 0))

    # # Show a training sample.
    # index = random.randint(0, len(X_train))
    # image = X_train[index].squeeze()
    # plt.figure(figsize=(1, 1))
    # plt.imshow(image, cmap="gray")
    # plt.show()
    # print(y_train[index])

    # Shuffle the training data.
    X_train, y_train = shuffle(X_train, y_train)

    exist_or_create_folder(weights_path)

    accuracy_valid = []
    with tf.Session() as sess:
        # build LeNet.
        net = build_network(sess)
        saver = tf.train.Saver()
        x, y, one_hot_y = net[0]
        logits, loss, train_op, correct_prediction, accuracy_op = net[1]

        num_examples = len(X_train)
        print('Training...')
        for i in range(EPOCHS):
            X_train, y_train = shuffle(X_train, y_train)
            # Batch gradient descent.
            n_batches = num_examples // BATCH_SIZE
            for ind in range(0, n_batches):
                start = ind * BATCH_SIZE
                end = (ind + 1) * BATCH_SIZE
                batch_x, batch_y = X_train[start:end], y_train[start:end]
                sess.run(train_op, feed_dict={x: batch_x, y: batch_y})

            # evaluate accuracy on the validation set.
            num_examples = len(X_valid)
            total_accuracy = 0
            sess = tf.get_default_session()
            for offset in range(0, num_examples, BATCH_SIZE):
                batch_x, batch_y = X_valid[offset:offset +
                                           BATCH_SIZE], y_valid[offset:offset +
                                                                BATCH_SIZE]
                accuracy = sess.run(accuracy_op,
                                    feed_dict={
                                        x: batch_x,
                                        y: batch_y
                                    })
                total_accuracy += (accuracy * len(batch_x))
            validation_accuracy = total_accuracy / num_examples

            accuracy_valid.append(validation_accuracy)

            print('EPOCH {0} | Validation Accuracy = {1:.3f}'.format(
                i + 1, validation_accuracy))

        saver.save(sess, weights_path)

    np.savetxt('{0}accuracy.out'.format(log_path), np.asarray(accuracy_valid),
               '%f')
    plot_accuracy(accuracy_valid, log_path, 'Convolutional NN')

    with tf.Session() as sess:
        saver.restore(sess, weights_path)

        # evaluate accuracy on the test set.
        num_examples = len(X_test)
        total_accuracy = 0
        sess = tf.get_default_session()
        for offset in range(0, num_examples, BATCH_SIZE):
            batch_x, batch_y = X_test[offset:offset +
                                      BATCH_SIZE], y_test[offset:offset +
                                                          BATCH_SIZE]
            accuracy = sess.run(accuracy_op,
                                feed_dict={
                                    x: batch_x,
                                    y: batch_y
                                })
            total_accuracy += (accuracy * len(batch_x))
        test_accuracy = total_accuracy / num_examples
        print("Test Accuracy = {:.3f}".format(test_accuracy))

        # # Visualize the deep features.
        index = random.randint(0, len(X_train))
        image = X_train[index].squeeze()
        plt.figure(figsize=(1, 1))
        plt.imshow(image, cmap="gray")
        plt.savefig('./logs/cnn/image.png')
        print(y_train[index])

        conv1, pool_1, conv2, pool_2, fc1, fc2, logits = net[2]
        v_conv1, v_pool_1, v_conv2, v_pool_2, v_fc1, v_fc2, v_logits = sess.run(
            [conv1, pool_1, conv2, pool_2, fc1, fc2, logits],
            feed_dict={
                x: X_train[index][np.newaxis, :],
                y: np.array([y_train[index]])
            })
        image_conv1 = v_conv1.squeeze()
        image_pool_1 = v_pool_1.squeeze()
        image_conv2 = v_conv2.squeeze()
        image_pool_2 = v_pool_2.squeeze()
        image_fc1 = v_fc1
        image_fc2 = v_fc2
        image_logits = v_logits.squeeze()
        fig = plt.figure(figsize=(8, 12))
        fig.suptitle('Layer 1: convolutional (28*28*6)')
        for i in range(0, 6):
            fig.add_subplot(2, 3, i + 1)
            plt.imshow(image_conv1[:, :, i], cmap="gray")
        plt.savefig('./logs/cnn/ly1_conv.png')
        fig = plt.figure(figsize=(8, 12))
        fig.suptitle('layer 2: pooling (14*14*6)')
        for i in range(0, 6):
            fig.add_subplot(2, 3, i + 1)
            plt.imshow(image_pool_1[:, :, i], cmap="gray")
        plt.savefig('./logs/cnn/ly2_pool.png')
        fig = plt.figure(figsize=(10, 10))
        fig.suptitle('layer 3: convolutional (10*10*16)')
        for i in range(0, 16):
            fig.add_subplot(4, 4, i + 1)
            plt.imshow(image_conv2[:, :, i], cmap="gray")
        plt.savefig('./logs/cnn/ly3_conv.png')
        fig = plt.figure(figsize=(10, 10))
        fig.suptitle('layer 4: pooling (5*5*16)')
        for i in range(0, 16):
            fig.add_subplot(4, 4, i + 1)
            plt.imshow(image_pool_2[:, :, i], cmap="gray")
        plt.savefig('./logs/cnn/ly4_pool.png')
        fig = plt.figure(figsize=(15, 2))
        fig.suptitle('layer 5: fully-connected (1*120)')
        plt.imshow(image_fc1, cmap="gray")
        plt.savefig('./logs/cnn/ly5_fc.png')
        fig = plt.figure(figsize=(15, 2))
        fig.suptitle('layer 6: fully-connected (1*84)')
        plt.imshow(image_fc2, cmap="gray")
        plt.savefig('./logs/cnn/ly6_fc.png')
        fig = plt.figure(figsize=(15, 2))
        fig.suptitle('layer 7: fully-connected (1*10)')
        plt.imshow(image_logits[np.newaxis, :], cmap="gray")
        plt.savefig('./logs/cnn/ly7_output.png')

    classified_time = time.time() - classify_time
    print('Classified time: {0:.4f} seconds.'.format(classified_time))
    write_to_file('{0}time.txt'.format(log_path), classified_time, True)
Example #7
0
              input_length=MAX_SEQUENCE_LENGTH,
              trainable=False))
model.add(Conv1D(32, 5, activation='relu'))
model.add(MaxPooling1D(5))
model.add(Conv1D(32, 5, activation='relu'))
model.add(MaxPooling1D(5))
model.add(Conv1D(32, 5, activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))

tensorBoardCallback = TensorBoard(log_dir='./logs', write_graph=True)

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['acc', fmeasure, precision, recall])
plot_model(model, to_file='model.png', show_shapes=True)

print("Train...")
history = model.fit(x_train,
                    y_train,
                    batch_size=64,
                    epochs=50,
                    validation_data=(x_val, y_val),
                    callbacks=[tensorBoardCallback])

if args.plot:
    plot_accuracy(history.history)
    plot_loss(history.history)
lr_scheduler = LearningRateScheduler(schedule_fn)
tb = TensorBoard(log_dir=tb_logs, batch_size=batch_size)
model_chp = ModelCheckpoint(os.path.join(results_path, 'best_weights'),
                            monitor='val_loss',
                            save_best_only=True,
                            save_weights_only=True)
try:
    os.makedirs(os.path.join(os.getcwd(), 'logs'))
except:
    pass

sgd = SGD(lr=lr, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(x=x_train,
                    y=y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    callbacks=[lr_scheduler, tb, model_chp],
                    validation_data=(x_test, y_test))

# Save and plot history
with open(os.path.join(results_path, 'cifar10_history'), 'wb') as f:
    pickle.dump(history.history, f)

utils.plot_accuracy(history)
utils.plot_loss(history)
Example #9
0
net0 = NeuralNetConstructor(93)
net0.fit(X_train, y_train)
predicted = net0.predict(X_test)
"""Testing A Simple Prediction"""
#print("Feature vector: %s" % X_test[:1])
print("Label: %s" % str(y_test[0]))
print("Predicted: %s" % str(net0.predict(X_test[:1])))
"""Metrics"""

# layer_info = PrintLayerInfo()
# net0.verbose = 3
# net0.initialize()
#print layer_info(net0)

print "[Classification Report]: "
print classification_report(y_test, predicted)
print "[Train dataset] Score: ", net0.score(X_train, y_train)
print "[Test dataset] Score: ", net0.score(X_test, y_test)
plot_matrix(net0, X_test, y_test, filename)

valid_accuracies = np.array([i["valid_accuracy"] for i in net0.train_history_])
plot_accuracy(valid_accuracies, filename)

train_loss = [row['train_loss'] for row in net0.train_history_]
valid_loss = [row['valid_loss'] for row in net0.train_history_]
plot_loss(valid_loss, train_loss, filename)

y_score = net0.predict_proba(X_test)  #[:, 1]
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename=filename)
Example #10
0
def train_model(ques_train_map, ans_train_map, img_train_map, ques_train_ids,
                ques_to_img_train, ques_val_map, ans_val_map, img_val_map,
                ques_val_ids, ques_to_img_val, id_to_ans, train_dim, val_dim,
                ans_types, params):

    # training parameters
    num_epochs = params['num_epochs']
    batch_size = params['batch_size']
    num_batches_train = train_dim // batch_size
    num_batches_val = val_dim // batch_size
    eval_every = 5

    train_loss, train_acc = [], []
    val_loss, val_acc = [], []
    eval_acc = []

    print "Loading model"
    model = get_model(dropout_rate=float(params['dropout_rate']),
                      regularization_rate=float(params['regularization_rate']),
                      embedding_size=int(params['embedding_size']),
                      num_classes=int(params['num_answers']),
                      model_name=params['model'])

    if not ans_types:
        savedir = "models/%s_%s" % (params['model'], str(
            params['num_answers']))
    else:
        savedir = "models/%s_%s_%s" % (params['model'],
                                       ans_types.replace("/", ""),
                                       str(params['num_answers']))

    if not os.path.exists(savedir):
        os.mkdir(savedir)

    for k in range(num_epochs):
        loss, acc = train_epoch(k + 1, model, num_batches_train, batch_size,
                                ques_train_map, ans_train_map, img_train_map,
                                ques_train_ids, ques_to_img_train)
        train_loss.append(loss)
        train_acc.append(acc)
        loss, acc = val_epoch(k + 1, model, num_batches_val, batch_size,
                              ques_val_map, ans_val_map, img_val_map,
                              ques_val_ids, ques_to_img_val)
        val_loss.append(loss)
        val_acc.append(acc)
        if (k + 1) % eval_every == 0:
            model.save_weights("%s/%s_epoch_%d_weights.h5" %
                               (savedir, params['model'], (k + 1)),
                               overwrite=True)
            eval_accuracy = evaluate(model, vqa_val, batch_size, ques_val_map,
                                     img_val_map, id_to_ans,
                                     params['ans_types'])
            print("Eval accuracy: %.2f" % eval_accuracy)
            eval_acc.append(eval_accuracy)

    plot_loss(train_loss, val_loss, savedir)
    plot_accuracy(train_acc, val_acc, savedir)
    plot_eval_accuracy(eval_acc, savedir)

    best_epoch = (1 + np.argmax(np.array(eval_acc))) * eval_every
    print "Best accuracy %.02f on epoch %d" % (max(eval_acc), best_epoch)

    model.load_weights("%s/%s_epoch_%d_weights.h5" %
                       (savedir, params['model'], best_epoch))
    evaluate(model,
             vqa_val,
             batch_size,
             ques_val_map,
             img_val_map,
             id_to_ans,
             params['ans_types'],
             verbose=True)
# Initiate the train and test generators with data augumentation
train_generator, valid_generator, test_generator = load_data('./data/')

# Save the model according to certain conditions
# checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_acc',
                      min_delta=0,
                      patience=10,
                      verbose=1,
                      mode='auto')

# Train the model
history = model_final.fit_generator(
    train_generator,
    steps_per_epoch=train_generator.n // batch_size,
    epochs=epochs,
    validation_data=valid_generator,
    validation_steps=valid_generator.n // batch_size,
    callbacks=[early])  # ,checkpoint])

STEP_SIZE_TEST = test_generator.n // test_generator.batch_size
score = model_final.evaluate_generator(generator=test_generator,
                                       steps=STEP_SIZE_TEST)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

plot_accuracy(history, path.join('./result/', out_file_name))
plot_loss(history, path.join('./result/', out_file_name))
save_model(model_final, path.join('./out/', out_file_name))
save_history(history, path.join('./out/', out_file_name))
Example #12
0
    print("Training Completed using L1 Regularization")
    print("Plot Accuracy")
    model_l1.plot_accuracy("L1 Regularization")
    print("Plot Error")
    model_l1.plot_error("L1 Regularization")
    #Read test csv file
    data.read(config.TEST_PATH)
    print("Test Data Read Successfully")
    model_l1.test(data)
    print("Predicted test values using L1 Regularization!!!!")

    #"""
    #L2 Regularization
    data.read(config.TRAIN_PATH)
    print("train data read successfully")
    model_l2 = model_L2.Model(data.size[1])
    acc_list_L2, error_list_L2 = model_l2.train(data)
    print("Training Completed using L2 Regularization")
    print("Plot Accuracy")
    model_l2.plot_accuracy("L2 Regularization")
    print("Plot Error")
    model_l2.plot_error("L2 Regularization")
    #Read test csv file
    data.read(config.TEST_PATH)
    print("Test Data Read Successfully")
    model_l2.test(data)
    print("Predicted test values using L2 Regularization!!!!")
    #"""
    utils.plot_accuracy(acc_list, acc_list_L1, acc_list_L2)
    utils.plot_error(error_list, error_list_L1, error_list_L2)