vgg_input_tensor_name = 'image_input:0'
    vgg_keep_prob_tensor_name = 'keep_prob:0'
    vgg_layer3_out_tensor_name = 'layer3_out:0'
    vgg_layer4_out_tensor_name = 'layer4_out:0'
    vgg_layer7_out_tensor_name = 'layer7_out:0'

    tf.saved_model.loader.load(sess, [vgg_tag], vgg_path)
    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name(vgg_input_tensor_name)
    keep = graph.get_tensor_by_name(vgg_keep_prob_tensor_name)
    layer3_out = graph.get_tensor_by_name(vgg_layer3_out_tensor_name)
    layer4_out = graph.get_tensor_by_name(vgg_layer4_out_tensor_name)
    layer7_out = graph.get_tensor_by_name(vgg_layer7_out_tensor_name)
        
    return w1, keep, layer3_out, layer4_out, layer7_out
tests.test_load_vgg(load_vgg, tf)


def layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes):
    """
    Create the layers for a fully convolutional network.  Build skip-layers using the vgg layers.
    :param vgg_layer3_out: TF Tensor for VGG Layer 3 output
    :param vgg_layer4_out: TF Tensor for VGG Layer 4 output
    :param vgg_layer7_out: TF Tensor for VGG Layer 7 output
    :param num_classes: Number of classes to classify
    :return: The Tensor for the last layer of output
    """
    # upsampling on layer7 by 2
    input = tf.layers.conv2d(vgg_layer7_out, num_classes, 1, strides=(1,1), padding='same', 
                            kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
                            kernel_regularizer=tf.contrib.layers.l2_regularizer(1e-3))
Example #2
0
    vgg_layer3_out_tensor_name = 'layer3_out:0'
    vgg_layer4_out_tensor_name = 'layer4_out:0'
    vgg_layer7_out_tensor_name = 'layer7_out:0'

    tf.saved_model.loader.load(sess, [vgg_tag], vgg_path)
    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name(vgg_input_tensor_name)
    keep = graph.get_tensor_by_name(vgg_keep_prob_tensor_name)
    w3 = graph.get_tensor_by_name(vgg_layer3_out_tensor_name)
    w4 = graph.get_tensor_by_name(vgg_layer4_out_tensor_name)
    w7 = graph.get_tensor_by_name(vgg_layer7_out_tensor_name)

    return w1, keep, w3, w4, w7


tests.test_load_vgg(load_vgg, tf)


def layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes):
    """
    Create the layers for a fully convolutional network.  Build skip-layers using the vgg layers.
    :param vgg_layer3_out: TF Tensor for VGG Layer 3 output
    :param vgg_layer4_out: TF Tensor for VGG Layer 4 output
    :param vgg_layer7_out: TF Tensor for VGG Layer 7 output
    :param num_classes: Number of classes to classify
    :return: The Tensor for the last layer of output
    """
    # TODO: Implement function

    x = tf.layers.conv2d(
        vgg_layer7_out,
Example #3
0
def perform_tests():
    tests.test_for_kitti_dataset(data_dir)
    tests.test_load_vgg(load_vgg, tf)
    tests.test_layers(layers)
    tests.test_optimize(optimize)
    tests.test_train_nn(train_nn)
Example #4
0
def testRun():
    tests.test_load_vgg(load_vgg, tf)
    tests.test_layers(layers)
    tests.test_optimize(optimize)
    tests.test_train_nn(train_nn)
Example #5
0
 def run_tests(self):
     tests.test_load_vgg(self.load_vgg, tf)
     tests.test_layers(self.layers)
     tests.test_optimize(self.optimize_cross_entropy)
     tests.test_train_nn(self.train_nn)
Example #6
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'

    # Hyperparameters
    learning_rate = 0.0001
    lr = tf.constant(learning_rate)
    epochs = 100
    batch_size = 10

    # Check TensorFlow Version
    assert LooseVersion(tf.__version__) >= LooseVersion(
        '1.0'
    ), 'Please use TensorFlow version 1.0 or newer.  You are using {}'.format(
        tf.__version__)
    print('TensorFlow Version: {}'.format(tf.__version__))

    # Check for a GPU
    if not tf.test.gpu_device_name():
        warnings.warn(
            'No GPU found. Please use a GPU to train your neural network.')
    else:
        print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))

    # Perform unit tests
    tests.test_for_kitti_dataset(data_dir)
    tests.test_load_vgg(load_vgg, tf)
    tests.test_layers(layers)
    tests.test_optimize(optimize)
    tests.test_train_nn(train_nn)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    # OPTIONAL: Train and Inference on the cityscapes dataset instead of the Kitti dataset.
    # You'll need a GPU with at least 10 teraFLOPS to train on.
    #  https://www.cityscapes-dataset.com/

    with tf.Session() as sess:
        # Path to vgg model
        vgg_path = os.path.join(data_dir, 'vgg')
        # Create function to get batches
        get_batches_fn = helper.gen_batch_function(
            os.path.join(data_dir, 'data_road/training'), image_shape)

        # OPTIONAL: Augment Images for better results
        #  https://datascience.stackexchange.com/questions/5224/how-to-prepare-augment-images-for-neural-network

        # TODO: Build NN using load_vgg, layers, and optimize function
        correct_label = tf.placeholder(tf.float32,
                                       [None, None, None, num_classes])

        image_input, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        output_layer = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(
            output_layer, correct_label, lr, num_classes)

        # TODO: Train NN using the train_nn function
        sess.run(tf.global_variables_initializer())
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, image_input, correct_label, keep_prob, lr)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, image_input)
Example #7
0
def perform_tests():
    tests.test_for_kitti_dataset(data_dir)
    tests.test_load_vgg(load_vgg, tf)
    tests.test_layers(layers)
    tests.test_optimize(optimize)
    tests.test_train_nn(train_nn)
Example #8
0
def train(epochs: int = None, save_model_freq: int = None, batch_size: int = None, learning_rate: float = None,
          keep_prob: float = None, dataset: str = None):
    """
    Performs the FCN training from begining to end, that is, downloads required datasets and pretrained models,
    constructs the FNC architecture, trains it, and saves the trained model.
    :param epochs: number of epochs for training
    :param save_model_freq: save model each save_model_freq epoch
    :param batch_size: batch size for training
    :param learning_rate: learning rate for training
    :param keep_prob: keep probability for dropout layers for training
    :param dataset: dataset name
    """
    if None in [epochs, save_model_freq, batch_size, learning_rate, keep_prob, dataset]:
        raise ValueError('some parameters were not specified for function "%s"' % train.__name__)

    dataset = DATASETS[dataset]

    if not os.path.exists(dataset.data_root_dir):
        os.makedirs(dataset.data_root_dir)

    # Download Kitti Road dataset
    helper.maybe_download_dataset_from_yandex_disk(dataset)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg_from_yandex_disk(dataset.data_root_dir)

    # Run tests to check that environment is ready to execute the semantic segmentation pipeline
    if dataset.name == 'kitti_road':
        tests.test_for_kitti_dataset(dataset.data_root_dir)
    tests.test_load_vgg(load_vgg, tf)
    tests.test_layers(layers)
    tests.test_optimize(optimize)
    tests.test_train_nn(train_nn, dataset)

    # TODO: Train and Inference on the cityscapes dataset instead of the Kitti dataset.
    #  https://www.cityscapes-dataset.com/

    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
        # Path to vgg model
        vgg_path = os.path.join(dataset.data_root_dir, 'vgg')
        # Create function to get batches
        get_batches_fn = helper.gen_batch_function(dataset.data_training_dir, dataset.image_shape)

        # TODO: Augment Images for better results
        #  https://datascience.stackexchange.com/questions/5224/how-to-prepare-augment-images-for-neural-network

        image_input_tensor, keep_prob_tensor, layer3_out_tensor, layer4_out_tensor, layer7_out_tensor = \
            load_vgg(sess, vgg_path)
        output_layer_tensor = layers(layer3_out_tensor, layer4_out_tensor, layer7_out_tensor, dataset.num_classes)
        correct_label_tensor = tf.placeholder(tf.float32, (None, None, None, dataset.num_classes))
        learning_rate_tensor = tf.placeholder(tf.float32)
        logits_tensor, train_op_tensor, cross_entropy_loss_tensor, softmax_tensor = \
            optimize(output_layer_tensor, correct_label_tensor, learning_rate_tensor, dataset.num_classes)

        iou_tensor, iou_op_tensor = mean_iou(softmax_tensor, correct_label_tensor, dataset.num_classes)

        train_nn(sess, dataset, epochs, save_model_freq, batch_size, learning_rate, keep_prob,
                 get_batches_fn, train_op_tensor, cross_entropy_loss_tensor, image_input_tensor, correct_label_tensor,
                 keep_prob_tensor, learning_rate_tensor, iou_tensor, iou_op_tensor)

        save_model(sess, 'fcn8-final', dataset,
                   epochs=epochs, batch_size=batch_size, learning_rate=learning_rate, keep_prob=learning_rate)