Example #1
0
def main(_):
    '''
    Main body of code for loading a pre-trained model and accessing
    various functionalities of the system.
    '''
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=False,
                  shuffle_per_epoch=True)
    img_size = mnist.original_image_shape
    num_classes = mnist.return_num_classes()
    # Load model
    model = tf.keras.models.load_model('best_model.h5')
    logits = model.predict(x=mnist.test_x)
    predictions = np.argmax(logits, axis=1)

    # Plot the confusion matrix
    print_confusion_matrix(labels=mnist.test_y_cls,
                           predictions=predictions,
                           num_classes=num_classes)
    # Plot images that are classified incorrectly
    wrong_indeces, wrong_images, wrong_labels, correct_labels = find_wrong_predictions(
        labels=mnist.test_y_cls, predictions=predictions, images=mnist.test_x)
    incorrect_logits = model.predict(wrong_images)
    # Need to find a way to get the output on the different layers of the
    # model. Here, the pre-activation dense layer values are necessary for
    # plotting.
    plot_images(images=wrong_images[:5],
                y_pred=incorrect_logits[:5],
                logits=incorrect_logits[:5],
                cls_true=correct_labels[:5],
                cls_pred=wrong_labels[:5],
                img_shape=img_size)
    # Get model summary
    model.summary()
    model.evaluate(x=mnist.test_x, y=mnist.test_y)
    # Get the input layer
    input_layer = model.layers[0]
    # Retrieve convolutional layers and weights
    conv1_layer = model.layers[2]
    conv1_weights = conv1_layer.get_weights()[0]
    conv2_layer = model.layers[5]
    conv2_weights = conv2_layer.get_weights()[0]
    # Plot convolutional weights
    plot_conv_weights(conv1_weights)
    plot_conv_weights(conv2_weights)
    # Output of convolutions
    conv1_output = tf.keras.backend.function(inputs=[input_layer.input],
                                             outputs=[conv1_layer.output])
    conv2_output = tf.keras.backend.function(inputs=[input_layer.input],
                                             outputs=[conv2_layer.output])
    # Get an example image to plot the convolutions
    image_instance = np.array([mnist.test_x[0]])
    conv_1_ex_output = conv1_output(image_instance)[0]
    conv_2_ex_output = conv2_output(image_instance)[0]
    plot_conv_layer(conv_1_ex_output)
    plot_conv_layer(conv_2_ex_output)
Example #2
0
def main(_):
    '''
    Main body of code for loading the pre-trained models and accessing
    various functionalities of the system.
    '''
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=False,
                  shuffle_per_epoch=True)
    img_size = mnist.original_image_shape
    num_classes = mnist.return_num_classes()

    # Find number of models in folder
    model_files = os.listdir(FLAGS.ensemble_networks_path)
    num_models = len(model_files)
    # Lists to append the models and the logits
    ensemble_models = []
    ensemble_logits = []

    for i in range(1, num_models + 1):
        print("Loading model {}/{}".format(i, num_models))
        # Load model and get the performance
        model = tf.keras.models.load_model(
            os.path.join(FLAGS.ensemble_networks_path,
                         'ensemble_network_{}.h5'.format(i)))
        model.evaluate(x=mnist.test_x,
                       y=mnist.test_y,
                       batch_size=FLAGS.batch_size)
        logits = model.predict(x=mnist.test_x)
        ensemble_logits.append(logits)
        predictions = np.argmax(logits, axis=1)

    # Get the mean of the logits from the models and
    # their predictions
    ensemble_logits = np.array(ensemble_logits)
    mean_logits = np.mean(ensemble_logits, axis=0)
    predictions = np.argmax(mean_logits, axis=1)

    # Plot the confusion matrix
    print_confusion_matrix(labels=mnist.test_y_cls,
                           predictions=predictions,
                           num_classes=num_classes)
    # Get images that are classified incorrectly
    wrong_indeces, wrong_images, wrong_labels, correct_labels = find_wrong_predictions(
        labels=mnist.test_y_cls, predictions=predictions, images=mnist.test_x)
    incorrect_logits = mean_logits[wrong_indeces]
    plot_images(images=wrong_images[:5],
                y_pred=incorrect_logits[:5],
                logits=incorrect_logits[:5],
                cls_true=correct_labels[:5],
                cls_pred=wrong_labels[:5],
                img_shape=img_size)
    # Get the accuracy of the ensemble, to compare to the accuracy
    # of the individual networks.
    print("Accuracy of ensemble network: {}".format(1 -
                                                    (wrong_images.shape[0] /
                                                     mnist.test_x.shape[0])))
Example #3
0
def main(_):
    '''
    Main body of code for creating a two-layer convolutional neural
    network, with max pooling, flatten dense layer and softmax output.
    '''
    check_and_create_folder(FLAGS.model_filepath)
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size, normalize_data=True,
                  one_hot_encoding=True, flatten_images=False,
                  shuffle_per_epoch=True)

    for i in range(1, FLAGS.ensemble_size+1):
        # Path to save the model
        model_filepath = os.path.join(FLAGS.model_filepath, 'ensemble_network_{}.h5'.format(i))
        # Partition the train set randomly and train the network
        partition = (mnist.train_y.shape[0]/FLAGS.ensemble_size)/mnist.train_y.shape[0]
        train_x, train_y = random_partition_test_set(mnist.train_x, mnist.train_y,
                                                     partition=partition)
        # Data information
        img_size = mnist.original_image_shape
        num_classes = mnist.return_num_classes()
        num_channels = 1 # Because this is greyscale (need to introduce in the MNIST class)

        # Construct the model
        inputs = tf.keras.layers.Input(shape=(img_size[0], img_size[1]))
        net = inputs
        net = tf.keras.layers.Reshape((img_size[0], img_size[1], num_channels))(net)
        net = tf.keras.layers.Conv2D(kernel_size=FLAGS.filter_size_1, strides=1,
                                     filters=FLAGS.num_filters_1, padding='same',
                                     activation='relu', name='conv_layer1')(net)
        net = tf.keras.layers.MaxPooling2D(pool_size=2, strides=2)(net)
        net = tf.keras.layers.Dropout(FLAGS.dropout_rate)(net)
        net = tf.keras.layers.Conv2D(kernel_size=FLAGS.filter_size_2, strides=1,
                                     filters=FLAGS.num_filters_2, padding='same',
                                     activation='relu', name='conv_layer2')(net)
        net = tf.keras.layers.MaxPooling2D(pool_size=2, strides=2)(net)
        net = tf.keras.layers.Dropout(FLAGS.dropout_rate)(net)
        net = tf.keras.layers.Flatten()(net)
        net = tf.keras.layers.Dense(FLAGS.fc_size, activation='relu')(net)
        net = tf.keras.layers.Dropout(FLAGS.dropout_rate)(net)
        net = tf.keras.layers.Dense(num_classes, activation='softmax')(net)

        outputs = net
        # Construct keras model by passing inputs and outputs to the graph
        model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
        # Optimization and cost
        model.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
        # Callbacks for saving and early stopping
        callbacks = [tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5),
                     tf.keras.callbacks.ModelCheckpoint(filepath=model_filepath,
                                                        monitor='val_loss',
                                                        save_best_only=True)]
        print("Training model {}/{}".format(i, FLAGS.ensemble_size))
        model.fit(x=train_x, y=train_y,
                  epochs=FLAGS.epochs, batch_size=FLAGS.batch_size,
                  callbacks=callbacks,
                  validation_data=(mnist.test_x, mnist.test_y))
Example #4
0
def main(_):
    '''
    Main body of code for constructing a denoising autoencoder
    model for reconstructing the MNIST dataset
    '''
    # Load MNIST dataset
    mnist = MNIST(batch_size=FLAGS.batch_size, normalize_data=True,
                  one_hot_encoding=True, flatten_images=True,
                  shuffle_per_epoch=True)

    # Data information
    img_size = mnist.return_input_shape()

    # Input placeholder and corruption mask
    x_input = tf.placeholder(tf.float32, [None, img_size], name='input')

    # Build the graph
    autoencoder = AutoencoderLayer(input_data=x_input,
                                   num_inputs=img_size,
                                   num_outputs=FLAGS.autoencoder_size)

    # Cost and optimization
    cost = tf.reduce_sum(tf.pow(x_input-autoencoder.decoded_input, 2))
    train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(cost)
    predict_op = autoencoder.decoded_input

    # Initialize tensorflow session
    session = tf.Session()
    session.run(tf.global_variables_initializer())

    loss = 0

    with tqdm(total=FLAGS.epochs, postfix='Loss = {:.3f}'.format(loss)) as epoch_progress:
        # Iterate through epochs
        for epoch in range(FLAGS.epochs):
            with tqdm(total=len(mnist), postfix='Loss: {:.3f}'.format(loss), mininterval=1e-4,
                      leave=True) as batch_progress:
                # Train model per batch
                for batch, (batch_x, _) in enumerate(mnist):
                    batch_noise = corrupt_data_with_noise(batch_x.shape, FLAGS.corruption_level)
                    feed_dict_train = {x_input:batch_x, autoencoder.mask:batch_noise}
                    _, loss = session.run([train_op, cost], feed_dict=feed_dict_train)
                    # Update bar
                    batch_progress.set_postfix(Loss=loss)
                    batch_progress.update()

            test_set_noise = corrupt_data_with_noise(mnist.test_x.shape, FLAGS.corruption_level)
            feed_dict_test = {x_input: mnist.test_x, autoencoder.mask: test_set_noise}
            test_loss = session.run(cost, feed_dict=feed_dict_test)
            # Update bar
            epoch_progress.set_postfix(Loss=test_loss)
            epoch_progress.update()

    weights = transform_tf_variable(session, autoencoder.weights)
    plot_autoencoder_weights(weights)
    subset = mnist.test_x[:100]
    save_images(subset, x_input, autoencoder.mask, predict_op, session)
Example #5
0
def main(_):
    '''
    Main body of code for creating a two-layer convolutional neural
    network, with max pooling, flatten dense layer and softmax output.
    '''
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=False,
                  shuffle_per_epoch=True)

    # Data information
    img_size = mnist.original_image_shape
    num_classes = mnist.return_num_classes()
    num_channels = 1  # Because this is greyscale (need to introduce in the MNIST class)

    # Construct the model
    model = tf.keras.models.Sequential()
    model.add(
        tf.keras.layers.InputLayer(input_shape=(img_size[0], img_size[1])))
    model.add(tf.keras.layers.Reshape(
        (img_size[0], img_size[1], num_channels)))
    model.add(
        tf.keras.layers.Conv2D(kernel_size=FLAGS.filter_size_1,
                               strides=1,
                               filters=FLAGS.num_filters_1,
                               padding='same',
                               activation='relu',
                               name='conv_layer1'))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=2, strides=2))
    model.add(tf.keras.layers.Dropout(FLAGS.dropout_rate))
    model.add(
        tf.keras.layers.Conv2D(kernel_size=FLAGS.filter_size_1,
                               strides=1,
                               filters=FLAGS.num_filters_1,
                               padding='same',
                               activation='relu',
                               name='conv_layer2'))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=2, strides=2))
    model.add(tf.keras.layers.Dropout(FLAGS.dropout_rate))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(FLAGS.fc_size, activation='relu'))
    model.add(tf.keras.layers.Dropout(FLAGS.dropout_rate))
    model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

    # Optimization and cost for compiling the model
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    # Training
    model.fit(x=mnist.train_x,
              y=mnist.train_y,
              epochs=FLAGS.epochs,
              batch_size=FLAGS.batch_size)
    # Evaluation
    model.evaluate(x=mnist.test_x, y=mnist.test_y)
    logits = model.predict(x=mnist.test_x)
    predictions = np.argmax(logits, axis=1)

    # Plot the confusion matrix
    print_confusion_matrix(labels=mnist.test_y_cls,
                           predictions=predictions,
                           num_classes=num_classes)
    # Plot images that are classified incorrectly
    wrong_indeces, wrong_images, wrong_labels, correct_labels = find_wrong_predictions(
        labels=mnist.test_y_cls, predictions=predictions, images=mnist.test_x)
    incorrect_logits = model.predict(wrong_images)
    # Need to find a way to get the output on the different layers of the
    # model. Here, the pre-activation dense layer values are necessary for
    # plotting.
    plot_images(images=wrong_images[:5],
                y_pred=incorrect_logits[:5],
                logits=incorrect_logits[:5],
                cls_true=correct_labels[:5],
                cls_pred=wrong_labels[:5],
                img_shape=img_size)
Example #6
0
def main(_):
    '''
    Main body of code for creating a two-layer convolutional neural
    network, with max pooling, flatten dense layer and softmax output.
    '''
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=False,
                  shuffle_per_epoch=True)

    # Data information
    img_size = mnist.original_image_shape
    num_classes = mnist.return_num_classes()
    num_channels = 1  # Because this is greyscale (need to introduce in the MNIST class)

    # Placeholders
    x_input = tf.placeholder(tf.float32,
                             shape=[None, img_size[0], img_size[1]],
                             name='x_input')
    x_image = tf.reshape(x_input,
                         shape=[-1, img_size[0], img_size[1], num_channels])

    y_true = tf.placeholder(tf.float32,
                            shape=[None, num_classes],
                            name='y_true')
    y_true_cls = tf.argmax(y_true, axis=1)

    dropout_rate = tf.placeholder_with_default(0.0,
                                               shape=(),
                                               name='dropout_rate')

    # First convolutional layer
    conv_1 = Conv2DLayer(input_data=x_image,
                         num_input_channels=num_channels,
                         filter_size=FLAGS.filter_size_1,
                         num_filters=FLAGS.num_filters_1)
    max_pool1 = MaxPoolLayer(input_layer=conv_1.output_layer)
    # Second convolutional layer
    conv_2 = Conv2DLayer(input_data=max_pool1.output_layer,
                         num_input_channels=FLAGS.num_filters_1,
                         filter_size=FLAGS.filter_size_2,
                         num_filters=FLAGS.num_filters_2)
    max_pool2 = MaxPoolLayer(input_layer=conv_2.output_layer)
    # Flatten layer and pass to dense layer
    layer_flat = FlattenLayer(input_layer=max_pool2.output_layer)
    dense_1 = DenseLayer(input_data=layer_flat.output_layer,
                         num_inputs=layer_flat.num_features,
                         num_outputs=FLAGS.fc_size)
    dropout_layer = tf.nn.dropout(x=dense_1.output_layer, rate=dropout_rate)
    # Softmax layer
    softmax_layer = DenseLayer(input_data=dropout_layer,
                               num_inputs=FLAGS.fc_size,
                               num_outputs=num_classes,
                               activation=tf.nn.softmax)

    # Predictions
    y_pred = softmax_layer.output_layer
    y_pred_cls = tf.argmax(y_pred, axis=1)

    # Loss
    cross_entropy = tf.losses.softmax_cross_entropy(
        onehot_labels=y_true, logits=softmax_layer.pre_activation_layer)
    cost = tf.reduce_mean(cross_entropy)

    # Optimizer
    optimizer = tf.train.AdamOptimizer(
        learning_rate=FLAGS.learning_rate).minimize(cost)

    # Performance measure
    correct_prediction = tf.equal(y_pred_cls, y_true_cls)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # Run session
    session = tf.Session()
    session.run(tf.global_variables_initializer())

    loss = 0
    acc = 0

    with tqdm(total=FLAGS.epochs,
              postfix='Accuracy = {:.3f}'.format(acc)) as epoch_progress:
        for epoch in range(FLAGS.epochs):
            with tqdm(total=len(mnist),
                      postfix='Loss: {:.3f}'.format(loss),
                      mininterval=1e-4,
                      leave=True) as batch_progress:
                # Train model per batch
                for batch_x, batch_y in mnist:
                    feed_dict_train = {
                        x_input: batch_x,
                        y_true: batch_y,
                        dropout_rate: FLAGS.dropout_rate
                    }
                    _, loss = session.run([optimizer, cost],
                                          feed_dict=feed_dict_train)
                    batch_progress.set_postfix(Loss=loss)
                    batch_progress.update()

                # Test model for entire test set
                feed_dict_test = {
                    x_input: mnist.test_x,
                    y_true: mnist.test_y,
                    y_true_cls: mnist.test_y_cls
                }

                acc, predictions = session.run([accuracy, y_pred_cls],
                                               feed_dict=feed_dict_test)
                epoch_progress.set_postfix(Accuracy=acc)
                epoch_progress.update()

    # Plot the filter weights for the convolutional layers
    conv_1_weights = transform_tf_variable(session, conv_1.weights)
    conv_2_weights = transform_tf_variable(session, conv_2.weights)
    plot_conv_weights(conv_1_weights)
    plot_conv_weights(conv_2_weights)
    # Plot the outputs of the convolutional layers.
    conv_1_layer_output = get_layer_output(session, x_input, [mnist.test_x[0]],
                                           conv_1.output_layer)
    max_pool_1_layer_output = get_layer_output(session, x_input,
                                               [mnist.test_x[0]],
                                               max_pool1.output_layer)
    conv_2_layer_output = get_layer_output(session, x_input, [mnist.test_x[0]],
                                           conv_2.output_layer)
    max_pool_2_layer_output = get_layer_output(session, x_input,
                                               [mnist.test_x[0]],
                                               max_pool2.output_layer)
    plot_conv_layer(conv_1_layer_output)
    plot_conv_layer(max_pool_1_layer_output)
    plot_conv_layer(conv_2_layer_output)
    plot_conv_layer(max_pool_2_layer_output)
    # Plot the confusion matrix
    print_confusion_matrix(labels=mnist.test_y_cls,
                           predictions=predictions,
                           num_classes=num_classes)
    # Plot images that are classified incorrectly
    wrong_indeces, wrong_images, wrong_labels, correct_labels = find_wrong_predictions(
        labels=mnist.test_y_cls, predictions=predictions, images=mnist.test_x)
    incorrect_logits = get_layer_output(session, x_input, wrong_images,
                                        softmax_layer.pre_activation_layer)
    incorrect_predictions = get_layer_output(session, x_input, wrong_images,
                                             softmax_layer.output_layer)
    plot_images(images=wrong_images[:5],
                y_pred=incorrect_predictions[:5],
                logits=incorrect_logits[:5],
                cls_true=correct_labels[:5],
                cls_pred=wrong_labels[:5],
                img_shape=img_size)
Example #7
0
def main(_):
    '''
    Main body of code for constructing a stacked denoising autoencoder
    model for reconstructing the MNIST dataset.
    '''
    # Load MNIST dataset
    mnist = MNIST(batch_size=FLAGS.batch_size, normalize_data=True,
                  one_hot_encoding=True, flatten_images=True,
                  shuffle_per_epoch=True)

    # Data information
    img_size = mnist.return_input_shape()

    # Input placeholder and corruption mask
    x_input = tf.placeholder(tf.float32, [None, img_size], name='input')

    # Build the graph
    # Encoder part
    autoencoder_1 = AutoencoderLayer(input_data=x_input,
                                     num_inputs=img_size,
                                     num_outputs=FLAGS.autoencoder_size,
                                     encoder_activation=tf.nn.relu,
                                     decoder_activation=tf.nn.sigmoid)
    autoencoder_2 = AutoencoderLayer(input_data=autoencoder_1.encoded_input,
                                     num_inputs=FLAGS.autoencoder_size,
                                     num_outputs=512,
                                     encoder_activation=tf.nn.relu,
                                     decoder_activation=tf.nn.softplus)
    autoencoder_3 = AutoencoderLayer(input_data=autoencoder_2.encoded_input,
                                     num_inputs=512,
                                     num_outputs=256,
                                     encoder_activation=tf.nn.relu,
                                     decoder_activation=tf.nn.softplus)
    # Latent space
    latent_space = autoencoder_3.encoded_input
    # Reconstructed input
    layer_3_reconstruction = autoencoder_3.decoder(latent_space)
    layer_2_reconstruction = autoencoder_2.decoder(layer_3_reconstruction)
    layer_1_reconstruction = autoencoder_1.decoder(layer_2_reconstruction)
    input_reconstruction = layer_1_reconstruction
    # Cost and optimization
    cost = tf.reduce_sum(tf.abs(input_reconstruction - x_input))
    train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(cost)
    predict_op = autoencoder_1.decoded_input

    # Initialize tensorflow session
    session = tf.Session()
    session.run(tf.global_variables_initializer())

    loss = 0

    with tqdm(total=FLAGS.epochs, postfix='Loss = {:.3f}'.format(loss)) as epoch_progress:
        # Iterate through epochs
        for epoch in range(FLAGS.epochs):
            with tqdm(total=len(mnist), postfix='Loss: {:.3f}'.format(loss), mininterval=1e-4,
                      leave=True) as batch_progress:
                # Train model per batch
                for batch, (batch_x, _) in enumerate(mnist):
                    batch_noise_1 = corrupt_data_with_noise(batch_x.shape, FLAGS.corruption_level)
                    batch_noise_2 = corrupt_data_with_noise([batch_x.shape[0],
                                                             autoencoder_1.num_outputs],
                                                            FLAGS.corruption_level)
                    batch_noise_3 = corrupt_data_with_noise([batch_x.shape[0],
                                                             autoencoder_2.num_outputs],
                                                            FLAGS.corruption_level)
                    feed_dict_train = {x_input:batch_x,
                                       autoencoder_1.mask:batch_noise_1,
                                       autoencoder_2.mask:batch_noise_2,
                                       autoencoder_3.mask:batch_noise_3}
                    _, loss = session.run([train_op, cost], feed_dict=feed_dict_train)
                    # Update bar
                    batch_progress.set_postfix(Loss=loss)
                    batch_progress.update()

            # Noise for the different layers
            test_set_noise_layer_1 = corrupt_data_with_noise(mnist.test_x.shape,
                                                             FLAGS.corruption_level)
            test_set_noise_layer_2 = corrupt_data_with_noise([mnist.test_x.shape[0],
                                                              autoencoder_1.num_outputs],
                                                             FLAGS.corruption_level)
            test_set_noise_layer_3 = corrupt_data_with_noise([mnist.test_x.shape[0],
                                                              autoencoder_2.num_outputs],
                                                             FLAGS.corruption_level)
            feed_dict_test = {x_input: mnist.test_x,
                              autoencoder_1.mask: test_set_noise_layer_1,
                              autoencoder_2.mask: test_set_noise_layer_2,
                              autoencoder_3.mask: test_set_noise_layer_3}
            test_loss = session.run(cost, feed_dict=feed_dict_test)
            # Update bar
            epoch_progress.set_postfix(Loss=test_loss)
            epoch_progress.update()

    weights = transform_tf_variable(session, autoencoder_1.weights)
    plot_autoencoder_weights(weights)
    # Define a subset of the test set for reconstruction visualization.
    subset = mnist.test_x[:100]
    # Noise for the different layers
    subset_layer_1 = corrupt_data_with_noise(subset.shape,
                                             FLAGS.corruption_level)
    subset_layer_2 = corrupt_data_with_noise([subset.shape[0], autoencoder_1.num_outputs],
                                             FLAGS.corruption_level)
    subset_layer_3 = corrupt_data_with_noise([subset.shape[0], autoencoder_2.num_outputs],
                                             FLAGS.corruption_level)
    feed_dict_vis = {x_input: subset,
                     autoencoder_1.mask: subset_layer_1,
                     autoencoder_2.mask: subset_layer_2,
                     autoencoder_3.mask: subset_layer_3}
    save_images(subset, feed_dict_vis, predict_op, session)
def main(_):
    '''
    Main body of code for creating a deep denoising autoencoder
    network, for performing deep reconstruction of a noisy dataset.
    '''
    # Import data
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=True,
                  shuffle_per_epoch=True)

    # Data information
    img_size = mnist.return_input_shape()

    # First layer
    layer1_input, layer1_encoder, layer1_decoder, \
    layer1_encoded_data = autoencoder_layer(
        input_shape=img_size,
        layer_size=FLAGS.autoencoder1_size,
        target_data=mnist.train_x,
        noise_rate=FLAGS.corruption_level_1)

    # Second layer
    layer2_input, layer2_encoder, layer2_decoder, \
    layer2_encoded_data = autoencoder_layer(
        input_shape=FLAGS.autoencoder1_size,
        layer_size=FLAGS.autoencoder2_size,
        target_data=layer1_encoded_data,
        noise_rate=FLAGS.corruption_level_2)

    # Third layer
    layer3_input, layer3_encoder, layer3_decoder, \
    layer3_encoded_data = autoencoder_layer(
        input_shape=FLAGS.autoencoder2_size,
        layer_size=FLAGS.autoencoder3_size,
        target_data=layer2_encoded_data,
        noise_rate=FLAGS.corruption_level_3)

    # Deep reconstruction
    # Connect the various layers together
    reconstruction_input = layer1_input
    first_layer_encoder = layer1_encoder(reconstruction_input)
    second_layer_encoder = layer2_encoder(first_layer_encoder)
    third_layer_encoder = layer3_encoder(second_layer_encoder)
    third_layer_decoder = layer3_decoder(third_layer_encoder)
    second_layer_decoder = layer2_decoder(third_layer_decoder)
    first_layer_decoder = layer1_decoder(second_layer_decoder)

    # Define the mode with inputs and outputs
    reconstruction_model = tf.keras.models.Model(inputs=reconstruction_input,
                                                 outputs=first_layer_decoder)

    # Corrupt the input and output data
    corrupted_train_data = add_noise(mnist.train_x, FLAGS.corruption_level_1)
    corrupted_test_data = add_noise(mnist.test_x, FLAGS.corruption_level_1)

    # Optimize and fit the model
    reconstruction_model.compile(optimizer='adam', loss='binary_crossentropy')
    reconstruction_model.fit(x=corrupted_train_data,
                             y=mnist.train_x,
                             epochs=FLAGS.finetuning_epochs,
                             batch_size=FLAGS.batch_size,
                             shuffle=True,
                             validation_data=(corrupted_test_data,
                                              mnist.test_x))
    # Retrieve the reconstructions and plot
    reconstructions = reconstruction_model.predict(corrupted_test_data,
                                                   batch_size=FLAGS.batch_size)
    plotting_function(corrupted_test_data, reconstructions)
Example #9
0
def main(_):
    '''
    Main function to execute. Loading MNIST and training model.
    '''

    # Load MNIST dataset
    mnist = MNIST(batch_size=FLAGS.batch_size,
                  normalize_data=True,
                  one_hot_encoding=True,
                  flatten_images=True,
                  shuffle_per_epoch=True)

    epochs = 1
    original_image_shape = mnist.original_image_shape
    img_size_flat = mnist.return_input_shape()
    num_classes = mnist.return_num_classes()

    # Call model
    model = LogisticRegression(batch_size=FLAGS.batch_size,
                               num_iterations=FLAGS.epochs,
                               input_size=img_size_flat,
                               output_layer_size=num_classes)

    # Main train loop to iterate over epochs and then
    # over batches. Once per epoch we validate the system's
    # accuracy on the test set.
    # Predefine loss and acc for printing
    loss = 0
    acc = 0
    with tqdm(total=epochs,
              postfix='Accuracy = {:.3f}'.format(acc)) as epoch_progress:
        for epoch in range(epochs):
            with tqdm(total=len(mnist),
                      postfix='Loss: {:.3f}'.format(loss),
                      mininterval=1e-4,
                      leave=True) as batch_progress:
                for batch, (batch_x, batch_y) in enumerate(mnist):
                    loss = model.train_step(batch_x, batch_y)
                    batch_progress.set_postfix(Loss=loss)
                    batch_progress.update()
                acc = model.validation_cycle(mnist.test_x, mnist.test_y,
                                             mnist.test_y_cls)
                epoch_progress.set_postfix(Accuracy=acc)
                epoch_progress.update()

    # Plot weights
    weights = model.return_weights()
    plot_weights(weights, original_image_shape)
    # Plot confusion matrix
    predictions = model.return_predictions(test_x=mnist.test_x,
                                           test_y=mnist.test_y,
                                           test_y_cls=mnist.test_y_cls)
    print_confusion_matrix(labels=mnist.test_y_cls,
                           predictions=predictions,
                           num_classes=num_classes)
    # Plot images that are classified incorrectly
    wrong_indeces, wrong_images, wrong_labels, correct_labels = find_wrong_predictions(
        labels=mnist.test_y_cls, predictions=predictions, images=mnist.test_x)
    logits, y_pred = model.return_logits(data=wrong_images)
    plot_images(images=wrong_images[:5],
                y_pred=y_pred[:5],
                logits=logits[:5],
                cls_true=correct_labels[:5],
                cls_pred=wrong_labels[:5],
                img_shape=original_image_shape)