Exemplo n.º 1
0
def train(network, training_function, test_function, output_file):
    X_train, y_train, X_val, y_val, X_test, y_test = load_mnist_data()
    report('Data OK.', output_file)

    # Start training
    epochs = 500
    batch_size = 500
    TL, VL = [], []
    report('Training over {} Epochs...'.format(epochs), output_file)

    header = ['Epoch', 'TL', 'VL', 'Time']
    report('{:<10}{:<20}{:<20}{:<20}'.format(*header), output_file)

    for e in xrange(epochs):
        start_time = time.time()
        tl, vl = 0., 0.
        t_batches, v_batches = 1, 1

        # Training round
        for batch in generate_batches(X_train, X_train, batch_size):
            data, targets = batch
            l = training_function(data, targets)
            tl += l; t_batches += 1
        tl /= t_batches
        TL.append(tl)

        # Validation round
        for batch in generate_batches(X_val, X_val, batch_size):
            data, targets = batch
            l = test_function(data, targets)[0]
            vl += l; v_batches += 1

        vl /= v_batches
        VL.append(vl)
        row = [e + 1, tl, vl, time.time() - start_time]
        report('{:<10}{:<20}{:<20}{:<20}'.format(*row),output_file)

    report('Finished training.', output_file)

    # Model's loss on test data (MSE)
    mse = test_function(X_test, X_test)[0]
    report('MSE on test data: {}.'.format(mse), output_file)

    return TL, VL
Exemplo n.º 2
0
    print("Model answer = {}.".format(model_answer))
    print("Correct answer = {}.".format(target[idx]))

    height = [sample.count(i) / float(T) for i in range(10)]
    left = [i for i in range(10)]
    tick_label = [str(i) for i in range(10)]

    plt.switch_backend('Agg')
    plt.subplot(1, 2, 1)
    plt.bar(left=left, height=height, align='center', tick_label=tick_label)
    plt.tick_params(axis='y', which='major', labelsize=15)
    plt.tick_params(axis='x', which='major', labelsize=20)
    plt.subplot(1, 2, 2)
    plt.imshow(data[idx].reshape(28, 28))
    plt.title('{}\n'.format(y_test[idx]), size=22)
    plt.axis('off')
    plt.savefig('./output/index_{}.eps'.format(idx), format='eps')
    plt.savefig('./output/index_{}.jpg'.format(idx), format='jpg')
    plt.close()


if __name__ == '__main__':
    X_train, y_train, X_val, y_val, X_test, y_test = load_mnist_data()
    net = neural_network()
    weights = pickle.load(
        gzip.open('./output/mnist_mc_dropout_weights.pkl.gz', 'rb'))
    nn.layers.set_all_param_values(net, weights)
    training_function, validation_function, test_function = functions(net)

    for idx in range(100):
        main(idx, net, X_test, y_test, test_function)
Exemplo n.º 3
0
from mpl_toolkits.mplot3d import Axes3D

from helpers import load_mnist_data
from auto_encoder import auto_encoder
from auto_encoder import functions


plt.switch_backend('Agg')

# Get 'well-distributed' list of colours for visualization
cm = plt.get_cmap(name="nipy_spectral")
C = [cm(1. - 1. * (i/10.)) for i in range(10)]

# Load data; show test input
X_train, y_train, X_val, y_val, X_test, y_test = load_mnist_data()
plt.figure(figsize=(12,3))

for i in range(10):
    plt.subplot(1, 10, i+1)
    plt.imshow(X_test[i].reshape(28,28))
    plt.axis('off')

plt.tight_layout()
plt.savefig('./output/3D_in.eps', format='eps')
plt.savefig('./output/3D_in.jpg', format='jpg')
plt.close()

# Retrieve model configuration
encoder, network = auto_encoder(d=3, encode_image=False)
F = functions(encoder, network)
Exemplo n.º 4
0
def main(argv):
    img_rows, img_cols = 28, 28
    channel = 1
    num_classes = 10
    dataset = 'mnist'
    batch_size = 1028
    nb_epoch = 10
    train_num = 10000
    test_num = 3000
    pop_size = 15
    generations = 4

    try:
        opts, args = getopt.getopt(argv, "hi:v:", ["dataset", "color"])
    except getopt.GetoptError:
        print('USAGE: main.py -d <dataset> -c <color>')
    for opt, args in opts:
        print("opt:%s args:%s" % (opt, args))
        if opts == '-h':
            print(
                'USAGE: main.py -d <dataset [mnist,cifar10,cifar100]> -c <color [rgb,grey]>'
            )
        elif opt in ("-d", "--dataset"):
            print('got d')
            if opt is "mnist":
                dataset = 'mnist'
                print('using mnist')
            elif opt is "cifar10":
                print('using cifar10')
                dataset = 'cifar10'
                img_rows, img_cols = 32, 32
                num_classes = 10
            elif opt is 'cifar100':
                print('using cifar100')
                dataset = 'cifar100'
                img_rows, img_cols = 32, 32
                num_classes = 100
        elif opt in ("-c", "--color"):
            if opt is "rgb":
                print('using RGB')
                channel = 3
            elif opt is "grey" or opt is "gray":
                print('using GRAYSCALE')
                channel = 1

    if dataset == 'mnist':
        x_train, y_train, x_test, y_test = load_mnist_data(
            img_rows,
            img_cols,
            nb_train_samples=train_num,
            nb_test_samples=test_num)
    elif dataset == 'cifar10':
        x_train, y_train, x_test, y_test = load_cifar10_data(
            img_rows,
            img_cols,
            nb_train_samples=train_num,
            nb_test_samples=test_num)
    elif dataset == 'cifar100':
        x_train, y_train, x_test, y_test = load_cifar100_data(
            img_rows,
            img_cols,
            nb_train_samples=train_num,
            nb_test_samples=test_num)

    x_test, x_valid, y_test, y_valid = train_test_split(x_test,
                                                        y_test,
                                                        test_size=0.2)

    # lenet model for testing
    conv1 = {
        'name': 'conv1',
        'type': 'Convolution2D',
        'border_mode': 'same',
        'nb_filter': 6,
        'nb_row': 3,
        'nb_col': 3,
        'activation': 'relu',
        'input_shape': (img_rows, img_cols, channel)
    }
    max1 = {
        'name': 'max1',
        'type': 'MaxPooling2D',
        'pool_size': (2, 2),
        'strides': (2, 2)
    }
    conv2 = {
        'name': 'conv2',
        'type': 'Convolution2D',
        'border_mode': 'same',
        'nb_filter': 16,
        'nb_row': 3,
        'nb_col': 3,
        'activation': 'relu'
    }
    max2 = {
        'name': 'max2',
        'type': 'MaxPooling2D',
        'pool_size': (2, 2),
        'strides': (2, 2)
    }
    flatten1 = {'name': 'flatten1', 'type': 'Flatten'}
    dense1 = {
        'name': 'dense2',
        'type': 'Dense',
        'output_dim': 120,
        'activation': 'relu'
    }
    dense2 = {
        'name': 'dense2',
        'type': 'Dense',
        'output_dim': 84,
        'activation': 'relu'
    }
    dense3 = {
        'name': 'dense2',
        'type': 'Dense',
        'output_dim': num_classes,
        'activation': 'softmax'
    }

    p = [conv1, max1, conv2, max2, flatten1, dense1, dense2, dense3]

    pop = Population(p, size=pop_size, k_best=3)

    for i in range(0, generations):
        if i > 0:
            pop.evolve()
        big_poppa = pop.train_evaluate_population(x_train, y_train, x_valid,
                                                  y_valid, batch_size,
                                                  nb_epoch, x_test, y_test)

    # plot all generations
    # help from
    # https://stackoverflow.com/questions/14777066/matplotlib-discrete-colorbar
    # https://stackoverflow.com/questions/11244514/modify-tick-label-text

    # concatenate separate histories
    lines = []
    name_list = []
    z = [0] * (generations * pop_size)
    for gen in range(0, generations):
        histories = pop.generation_histories[gen]
        i = 0
        for name, history in histories:
            if name not in name_list:
                lines.append(history.history['val_acc'])
                z[i + gen * pop_size] = gen
                i = i + 1
                name_list.append(name)

    new_z = [0, 1, 2, 3, 4]
    # fill in values for early stopping!
    for i in range(0, len(lines)):
        while len(lines[i]) < nb_epoch:
            lines[i].append(lines[i][-1])

    fig, ax = plt.subplots(1, 1, figsize=(20, 10), dpi=120)

    # define colormap, extract colors from map
    cmap = plt.cm.BuPu
    cmaplist = [cmap(i) for i in range(cmap.N)]
    cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
        'Custom cmap', cmaplist, cmap.N)

    # define bins and normalize
    bounds = np.linspace(0, generations, generations + 1)
    norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)

    # create plot
    x = np.arange(1, nb_epoch + 1)
    coord = [np.column_stack((x, y)) for y in lines]
    line_c = LineCollection(coord, linestyle='solid', cmap=cmap, norm=norm)
    line_c.set_array(np.array(x))
    lineplot = ax.add_collection(line_c)

    # do colorbar stuff
    cb = fig.colorbar(line_c,
                      cmap=cmap,
                      norm=norm,
                      ticks=bounds,
                      boundaries=bounds,
                      format='%1i')
    cb.set_label("Generation Number")
    ax.set_title("Evolution Summary")
    ax.set_xlabel("Epochs")
    ax.xaxis.set_ticks(np.arange(1, nb_epoch + 1))
    ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%1i'))
    ax.set_ylabel("Accuracy")
    ax.set_yscale('logit')

    plt.show()

    print("\n\n======== FINAL WINNER : " + big_poppa.name + " ========")

    # CONVERT AND SAVE TO TFLITE FILE
    print("\nConverting to TfLite File")
    keras_file = str(big_poppa.name) + ".h5"
    converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file(
        "models/" + keras_file)
    tflite_model = converter.convert()
    open("models/winners/" + str(big_poppa.name) + ".tflite",
         "wb").write(tflite_model)

    # ALSO CONVERT TO PROTOBUF
    K.set_learning_phase(0)
    # load model
    model = load_model("models/" + keras_file)
    builder = saved_model_builder.SavedModelBuilder("models/winners/")
    signature = predict_signature_def(inputs={"images": model.input},
                                      outputs={"scores": model.output})

    with K.get_session() as sess:
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tag_constants.SERVING],
            signature_def_map={"predict": signature})

    builder.save()