Exemple #1
0
def main(model='lenet', num_epochs=500, min_epochs=100, improve_epochs=50,
         subset_sizes=None, validation_intervals=1,
         batchsize=500,
         sample_chooser=None, refine=False, out_path=None,
         csv_path=None, indices_out_path=None):
    if subset_sizes is None:
        subset_sizes = [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000]

    subset_sizes = cmdline_helpers.coerce_num_list_parameter(subset_sizes,
                                                             num_type=int, name='subset_sizes')
    num_epochs = cmdline_helpers.coerce_num_list_parameter(num_epochs, N=len(subset_sizes),
                                                                  num_type=int, name='num_epochs')
    min_epochs = cmdline_helpers.coerce_num_list_parameter(min_epochs, N=len(subset_sizes),
                                                                  num_type=int, name='min_epochs')
    improve_epochs = cmdline_helpers.coerce_num_list_parameter(improve_epochs, N=len(subset_sizes),
                                                                  num_type=int, name='improve_epochs')
    validation_intervals = cmdline_helpers.coerce_num_list_parameter(validation_intervals, N=len(subset_sizes),
                                                                  num_type=int, name='validation_intervals')

    N_train, N_val, N_test = mnist_dataset.train_val_test_size()

    builder = mnist_architecture.network_builder(model)

    mnist = mnist_dataset.MNISTTrainValTest()

    trainer, indices_labelled_history, validation_error_history, test_error_history = \
        active_learning.active_learning_image_classifier(sample_chooser=sample_chooser, model_builder=builder, N_train=N_train,
                                                         batchsize=batchsize,
                                                         refine=refine, datasets_fn=mnist.datasets, subset_sizes=subset_sizes,
                                                         num_epochs=num_epochs,
                                                         min_epochs=min_epochs, improve_epochs=improve_epochs,
                                                         validation_intervals=validation_intervals,
                                                         batch_xform_fn=mnist_dataset.xform_mnist_batch,
                                                         n_train_repetitions_in_case_of_failure=3)

    print('Results:')
    print('N-train\t\tErr')
    for labelled_indices, err in zip(indices_labelled_history, test_error_history):
        print('{0}\t\t{1:.2f}%'.format(labelled_indices.shape[0], err * 100.0))

    if csv_path is not None:
        writer = csv.writer(open(csv_path, 'wb'))
        writer.writerow(['# samples', 'Error %'])
        for labelled_indices, err in zip(indices_labelled_history, test_error_history):
            writer.writerow([labelled_indices.shape[0], err * 100.0])

    if out_path is not None:
        print('Saving model to {0} ...'.format(out_path))
        utils.save_model(out_path, trainer.network)

    if indices_out_path is not None:
        np.save(indices_out_path, indices_labelled_history[-1])
def main(model='lenet',
         num_epochs=500,
         min_epochs=200,
         improve_epochs=250,
         batchsize=500,
         training_subset=None,
         aug_factor=1,
         out_path=None):
    # Load the dataset
    print("Loading data...")
    mnist = mnist_dataset.MNISTTrainValTest()

    # Generate the indices of the subset of the training set
    training_subset_indices = None
    if training_subset is not None:
        training_subset_indices = mnist.balanced_train_subset_indices(
            training_subset)

    # Get the train, validation and test sets
    train_ds, val_ds, test_ds = mnist.datasets(training_subset_indices)

    # Get network builder function for named model
    builder = mnist_architecture.network_builder(model)

    # Build the image classifier for the given model builder
    clf = image_classifier.ImageClassifier.for_model(builder)

    # Set verbosity
    clf.trainer.report(verbosity=trainer.VERBOSITY_EPOCH)

    # Set data transformation function
    clf.trainer.data_xform_fn(batch_xform_fn=mnist_dataset.xform_mnist_batch)

    # Set training length
    clf.trainer.train_for(num_epochs=num_epochs,
                          min_epochs=min_epochs,
                          val_improve_num_epochs=improve_epochs,
                          val_improve_epochs_factor=0)

    # Train
    clf.trainer.train(train_ds, val_ds, test_ds, batchsize=batchsize)

    if out_path is not None:
        print('Saving model to {0} ...'.format(out_path))
        utils.save_model(out_path, clf.network)
def main(model='lenet', num_epochs=500, min_epochs=200, improve_epochs=250, batchsize=500,
         training_subset=None, aug_factor=1, out_path=None):
    # Load the dataset
    print("Loading data...")
    mnist = mnist_dataset.MNISTTrainValTest()

    # Generate the indices of the subset of the training set
    training_subset_indices = None
    if training_subset is not None:
        training_subset_indices = mnist.balanced_train_subset_indices(training_subset)

    # Get the train, validation and test sets
    train_ds, val_ds, test_ds = mnist.datasets(training_subset_indices)

    # Get network builder function for named model
    builder = mnist_architecture.network_builder(model)

    # Build the image classifier for the given model builder
    clf = image_classifier.ImageClassifier.for_model(builder)

    # Set verbosity
    clf.trainer.report(verbosity=trainer.VERBOSITY_EPOCH)

    # Set data transformation function
    clf.trainer.data_xform_fn(batch_xform_fn=mnist_dataset.xform_mnist_batch)

    # Set training length
    clf.trainer.train_for(num_epochs=num_epochs, min_epochs=min_epochs, val_improve_num_epochs=improve_epochs,
                          val_improve_epochs_factor=0)

    # Train
    clf.trainer.train(train_ds, val_ds, test_ds, batchsize=batchsize)

    if out_path is not None:
        print('Saving model to {0} ...'.format(out_path))
        utils.save_model(out_path, clf.network)