示例#1
0
def run():

    num_classes = 2

    image_h, image_w = (160, 576)

    with tf.Session() as sess:

        # Path to vgg model
        vgg_path = join(data_dir, 'vgg')

        # Create function to get batches
        batch_generator = gen_batch_function(join(data_dir, 'data_road/training'), (image_h, image_w))

        # Load VGG pretrained
        image_input, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(sess, vgg_path)

        # Add skip connections
        output = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes)

        # Define placeholders
        labels = tf.placeholder(tf.float32, shape=[None, image_h, image_w, num_classes])
        learning_rate = tf.placeholder(tf.float32, shape=[])

        logits, train_op, cross_entropy_loss = optimize(output, labels, learning_rate, num_classes)

        # Training parameters
        train_nn(sess, args.training_epochs, args.batch_size, batch_generator, train_op, cross_entropy_loss,
                 image_input, labels, keep_prob, learning_rate)

        save_inference_samples(runs_dir, data_dir, sess, (image_h, image_w), logits, keep_prob, image_input)
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    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)

        epochs = 50
        batch_size = 5

        # TF placeholders
        correct_label = tf.placeholder(tf.int32, [None, None, None, num_classes], name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        input_image, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(sess, vgg_path)

        nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes)

        logits, train_op, cross_entropy_loss = optimize(nn_last_layer, correct_label, learning_rate, num_classes)

        train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image,
             correct_label, keep_prob, learning_rate)

        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
示例#3
0
def run():
    print("Run")
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'

    tests.test_for_kitti_dataset(data_dir)

    # 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/
    # Note: Not done
    
    kBatchSize = 5
    kEpochs = 10

    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
        # Note: Not implemented.

        correct_label = tf.placeholder(tf.int32, [None, None, None, num_classes], name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        # Build NN using load_vgg, layers, and optimize function
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(layer_output, correct_label, learning_rate, num_classes)
        
        # Train NN using the train_nn function
        train_nn(sess, kEpochs, kBatchSize, get_batches_fn, train_op, cross_entropy_loss, input_image, correct_label, keep_prob, learning_rate)

        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        
        # Save the variables to disk.
        print("Saving model...")
        saver = tf.train.Saver()
        save_path = saver.save(sess, "./model/semantic_segmentation_model.ckpt")
        print("Model saved in path: %s" % save_path)
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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

        # Build NN using load_vgg, layers, and optimize function
        epochs        = 50
        batch_size    = 8

        correct_label = tf.placeholder(tf.int32, (None, None, None, num_classes))
        learning_rate = tf.placeholder(tf.float32)

        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(layer_output, correct_label, learning_rate, num_classes)

        # Train NN using the train_nn function
        train_nn(sess,
                 epochs,
                 batch_size,
                 get_batches_fn,
                 train_op,
                 cross_entropy_loss,
                 input_image,
                 correct_label,
                 keep_prob,
                 learning_rate)

        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    epochs = 200
    batch_size = 16

    #do_inference_only = True

    # CAREFUL: this removes ALL models and log results of the last run
    if tf.gfile.Exists(LOGDIR):
        tf.gfile.DeleteRecursively(LOGDIR)
    tf.gfile.MakeDirs(LOGDIR)

    # hyperparameter search
    for l2_const in [0.002, 0.005]:
        for lrate in [0.0001, 0.00005]:
            for kprob in [0.8, 0.9]:

                hparam = make_hparam_string(kprob=kprob,
                                            lrate=lrate,
                                            l2_const=l2_const)
                print('Starting run for %s' % hparam)
                model_path = LOGDIR + hparam + "/model"

                # 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/

                tf.reset_default_graph()
                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
                    input_image, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
                        sess, vgg_path)
                    nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out,
                                           vgg_layer7_out, num_classes)

                    learning_rate = tf.placeholder(dtype=tf.float32)
                    correct_label = tf.placeholder(dtype=tf.float32,
                                                   shape=(None, None, None,
                                                          num_classes))

                    logits, train_op, cross_entropy_loss = optimize(
                        nn_last_layer, correct_label, learning_rate,
                        num_classes, l2_const)

                    # 'Saver' op to save and restore all the variables
                    saver = tf.train.Saver()

                    # 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, input_image,
                             correct_label, keep_prob, learning_rate, kprob,
                             lrate, hparam)

                    save_path = saver.save(sess, model_path)
                    print("Model saved in file: %s" % save_path)

                    # TODO: Save inference data using helper.save_inference_samples
                    helper.save_inference_samples(runs_dir, data_dir, sess,
                                                  image_shape, logits,
                                                  keep_prob, input_image)
示例#6
0
def run():
    parser = argparse.ArgumentParser(description="Train and Infer FCN")
    parser.add_argument('--epochs',
                        default=1,
                        type=int,
                        help='number of epochs')
    parser.add_argument('--batch_size', default=4, type=int, help='batch size')
    parser.add_argument(
        '--num-batches-train',
        default=None,
        type=int,
        help='number of train batches, only adjusted for testing')
    parser.add_argument(
        '--num-batches-dev',
        default=None,
        type=int,
        help='number of dev batches, only adjusted for testing')

    parser.add_argument('--fast',
                        action='store_true',
                        help='runs for 1 batch with 1 epoch')
    parser.add_argument('--data-source',
                        default='cityscapes',
                        help='kitti or cityscapes')
    parser.add_argument(
        '--use-classes',
        default=False,
        help='If true, predict cityscape classes instead of categories')
    parser.add_argument('--scale-factor',
                        default=4,
                        type=int,
                        help="Scales image down on each dimension")
    parser.add_argument('--save-train',
                        action='store_true',
                        help="If ON, saves output train images with labels")
    parser.add_argument('--save-test',
                        action='store_true',
                        help="If ON, saves output test images with labels")
    parser.add_argument('--save-val',
                        action='store_true',
                        help="If ON, saves output val images with labels")
    parser.add_argument(
        '--save-all-images',
        action='store_true',
        help="If ON, saves all train test val images with labels")
    parser.add_argument('--quiet',
                        '-q',
                        action='store_true',
                        help='If ON, does not print batch updates')
    parser.add_argument('--no-early-stop',
                        action='store_true',
                        help='If ON, will not early stop')
    parser.add_argument('--should-crop', action='store_true')
    parser.add_argument('--print-confusion', action='store_true')
    parser.add_argument(
        '--use-extra',
        action='store_true',
        help=
        'If ON, will use extras provided there is data inside /data/leftImg8bits/train_extra'
    )

    args = parser.parse_args()

    print("Running with arguments:")
    print(args)

    image_shape = (1024 // args.scale_factor, 2048 // args.scale_factor)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    epochs = args.epochs
    batch_size = args.batch_size
    fast_run = args.fast
    verbose = not args.quiet
    data_set = args.data_source
    num_batches_train = args.num_batches_train
    num_batches_dev = args.num_batches_dev
    use_classes = args.use_classes
    should_crop = args.should_crop
    print_confusion = args.print_confusion

    num_classes = 0
    class_to_ignore = 0
    early_stop = not args.no_early_stop
    use_extra = args.use_extra

    if data_set == "cityscapes":
        if use_classes:
            num_classes = NUM_CLASSES_CITYSCAPES
            class_to_ignore = 19
        else:
            num_classes = NUM_CATEGORIES_CITYSCAPES
    elif data_set == "kitti":
        class_to_ignore = -1
        num_classes = NUM_CLASSES_KITTI

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)
    label_util.init(use_classes)

    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,
            should_crop)

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

        # Build NN using load_vgg, layers, and optimize function
        image_input, _, _, _, _ = load_vgg(sess, vgg_path)
        # Fully Convolutional Network
        last_layer = layers(image_input, num_classes)
        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=(None, None, None))
        learning_rate = tf.placeholder(dtype=tf.float32)

        logits, train_op, cross_entropy_loss = optimize(
            last_layer, correct_label, learning_rate, num_classes)

        # Train NN using the train_nn function
        sess.run(tf.global_variables_initializer())
        train_nn(sess, epochs, batch_size, use_extra, get_batches_fn, logits,
                 train_op, cross_entropy_loss, image_input, correct_label, 1.,
                 learning_rate, num_classes, num_batches_train,
                 num_batches_dev, early_stop, class_to_ignore, print_confusion,
                 verbose)

        if args.save_test or args.save_all_images:
            helper.save_inference_samples(runs_dir, data_dir, sess,
                                          image_shape, logits, 1., image_input,
                                          "test")
        if args.save_train or args.save_all_images:
            helper.save_inference_samples(runs_dir, data_dir, sess,
                                          image_shape, logits, 1., image_input,
                                          "train")
        if args.save_val or args.save_all_images:
            helper.save_inference_samples(runs_dir, data_dir, sess,
                                          image_shape, logits, 1., image_input,
                                          "val")
示例#7
0
文件: main.py 项目: mholzel/semantic
def run(keep_prob_val, batch_size, num_classes):
    learning_rate_val = 1e-3
    num_validation_images = 2
    epochs = 10
    image_shape = (160, 576)
    image_dtype = np.uint8
    data_dir = '../../../Users/matth/Desktop/semantic/'
    if not os.path.exists(data_dir):
        data_dir = '../../../Desktop/semantic'
    runs_dir = './runs'
    if testing:
        tests.test_for_kitti_dataset(data_dir)

    # 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 training and validation batches
        get_batches_fn = helper.gen_batch_function(
            os.path.join(data_dir, 'data_road/training'), image_shape,
            generateColors(num_classes), image_dtype)
        valid_batches = helper.gen_batch_function(
            os.path.join(data_dir, 'data_road/testing'), image_shape,
            generateColors(num_classes), image_dtype)
        validation_images, validation_labels = next(
            valid_batches(num_validation_images))

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

        # Load VGG
        input_image, keep_prob, *vgg_layers = load_vgg(sess, vgg_path)

        # Now create an FCN on top of VGG
        output = layers(*vgg_layers, num_classes)

        # Training functions
        learning_rate = tf.placeholder(tf.float64,
                                       shape=(),
                                       name="learning_rate")
        correct_label = tf.placeholder(image_dtype,
                                       shape=(None, *image_shape, num_classes),
                                       name="correct_label")
        logits, train_op, cross_entropy_loss = optimize(
            output, correct_label, learning_rate, num_classes)

        # Prediction function
        predictions = tf.argmax(output, axis=3)

        # Finally, initial the
        sess.run(tf.global_variables_initializer())

        # Create a saver for training
        # saver = tf.train.Saver(max_to_keep=1)
        saver = None

        # TRAIN!!!
        post_fix = str(batch_size) + str(keep_prob_val)[1:4] + "_" + str(
            epochs) + "_" + str(num_classes)
        model_name = "models/model" + post_fix + "/model.ckpt"
        frame_generator = train_nn(sess,
                                   epochs,
                                   batch_size,
                                   get_batches_fn,
                                   predictions,
                                   train_op,
                                   cross_entropy_loss,
                                   input_image,
                                   model_name=model_name,
                                   correct_label=correct_label,
                                   keep_prob=keep_prob,
                                   learning_rate=learning_rate,
                                   keep_prob_val=keep_prob_val,
                                   learning_rate_val=learning_rate_val,
                                   validation_images=validation_images,
                                   saver=saver)
        moviename = "videos/movie" + post_fix + ".mp4"
        movify.process(frame_generator, output_path=moviename, fps=20)

        # Process all of the test images
        output_dir = 'images/' + post_fix
        try:
            os.makedirs(output_dir)
        except Exception as e:
            print(e)

        image_dir = os.path.join(data_dir, 'data_road/testing/image_2')
        overlay_prediction(sess, input_image, keep_prob, predictions,
                           image_dir, image_shape, output_dir)

        # Now save those images into a movie
        def frame_generator():
            for file in os.listdir(output_dir):
                yield scipy.misc.imresize(
                    scipy.misc.imread(os.path.join(output_dir, file)),
                    image_shape)

        movify.process(frame_generator(),
                       output_path=output_dir + ".mp4",
                       fps=5)
def run():
    runs_dir = './runs'

    print("\n\nTesting for datatset presence......")
    train_dat_count = tests.test_looking_for_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(vgg_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:
        # Create function to get batches
        get_batches_fn = helper.gen_batch_function(
            glob_trainig_images_path, glob_labels_trainig_image_path,
            image_shape)

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

        # Build NN using load_vgg, layers, and optimize function KK-DONE

        # TF placeholders
        correct_label = tf.placeholder(tf.int32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_dir)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        # tfImShape = tf.get_variable("image_shape")
        # tfLogits = tf.get_variable("logits")
        # tfKeepProb = tf.get_variable("keep_prob") TEM NO TF

        print(100 * '*')
        print(image_shape)
        #(160, 576)
        print(100 * '*')
        print(logits)
        #Tensor("Reshape:0", shape=(?, 2), dtype=float32)
        print(100 * '*')
        print(keep_prob)
        #Tensor("keep_prob:0", dtype=float32)
        print(100 * '*')
        print(input_image)
        #Tensor("image_input:0", shape=(?, ?, ?, 3), dtype=float32)
        print(100 * '*')

        init_op = tf.global_variables_initializer()

        sess.run(init_op)
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate, train_dat_count)

        folderToSaveModel = "model"

        # Add ops to save and restore all the variables.
        saver = tf.train.Saver()

        for i, var in enumerate(saver._var_list):
            print('Var {}: {}'.format(i, var))

        if not os.path.exists(folderToSaveModel):
            os.makedirs(folderToSaveModel)

        pathSaveModel = os.path.join(folderToSaveModel, "model.ckpt")
        pathSaveModel = saver.save(sess, pathSaveModel)
        print(colored("Model saved in path: {}".format(pathSaveModel),
                      'green'))
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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
        print('Loading VGG...')
        layer_input_image, layer_keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)

        print('Building FCN graph...')
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)

        batch_size = 16
        epochs = 25
        channels = 3
        learning_rate = tf.placeholder(tf.float32)
        #keep_prob = tf.placeholder(tf.float32)

        correct_label = tf.Variable(
            tf.zeros(shape=(batch_size, image_shape[0], image_shape[1],
                            num_classes)))
        input_image = tf.Variable(tf.zeros(shape=(batch_size, image_shape[0],
                                                  image_shape[1], channels)),
                                  dtype=tf.float32)

        print('Building optimize operation...')
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)
        sess.run(tf.global_variables_initializer())

        # TODO: Train NN using the train_nn function
        print('Train...')
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, layer_input_image, correct_label,
                 layer_keep_prob, learning_rate)

        #saver = tf.train.Saver()
        #saver.save(sess, 'trained_model', global_step=epochs)

        # TODO: Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        print('Saving inference samples...')
        helper.save_inference_samples(os.path.join(data_dir, 'test_results'),
                                      data_dir, sess, image_shape, logits,
                                      layer_keep_prob, layer_input_image)
示例#10
0
if __name__=='__main__':
    images_folder = './Train/CameraRGB/'
    labels_folder = './Train/CameraSeg/'
    image_shape = (352, 800)
    num_classes = 3 # Vehicle, road, other
    epochs = 50
    batch_size = 8
    train_model = 'vgg16'


    #with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.Session() as sess:

        nr_train_images = len(glob(os.path.join(images_folder, '*.png')))

        get_batches_fn = gen_batch_function(images_folder, labels_folder, image_shape)

        correct_label = tf.placeholder(tf.int32, shape=(None, image_shape[0], image_shape[1], num_classes))
        
        learning_rate = tf.placeholder(tf.float32)
        
        if train_model == 'vgg16':
            keep_prob = tf.placeholder(tf.float32, name="keep_prob")
            nn_input, nn_output = vgg16(keep_prob, num_classes, image_shape)
        
        elif train_model == 'vgg16_pretrained':
            vgg_path = './vgg'
            nn_input, keep_prob, l3, l4, l7 = load_vgg(sess, vgg_path)
            nn_output = layers(l3, l4, l7, num_classes)
        
        elif train_model == 'resnet50':
示例#11
0
def run():
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
    # TODO: tests.test_for_kitti_dataset(data_dir) chyba niepotrzebne??
    # TODO: test bach function - chyba działa
    # get_batches_fn = helper.gen_batch_function('../data', image_shape)
    # for X_batch, gt_batch in get_batches_fn(BATCH_SIZE):
    #     print(gt_batch[0].shape)
    #     plt.figure()
    #     plt.subplot(121)
    #     plt.imshow(X_batch[0])
    #     plt.subplot(122)
    #     plt.imshow(X_batch[0])
    #     plt.show()
    #    input('Pres enter to continue')

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)
    print('Pretrained model downloaded')
    # 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('../data', image_shape)
        print('Prepared function to get batches')
        # OPTIONAL: Augment Images for better results
        #  https://datascience.stackexchange.com/questions/5224/how-to-prepare-augment-images-for-neural-network

        with tf.Session() as session:
            # Returns the three layers, keep probability and input layer from the vgg architecture
            image_input, keep_prob, layer3, layer4, layer7 = load_vgg(session, vgg_path)
            print('Get layers form vgg - Done')
            # The resulting network architecture from adding a decoder on top of the given vgg model
            model_output = layers(layer3, layer4, layer7, num_classes)
            print('Added Decoder - Full network architecture On-Line')
            # Returns the output logits, training operation and cost operation to be used
            # - logits: each row represents a pixel, each column a class
            # - train_op: function used to get the right parameters to the model to correctly label the pixels
            # - cross_entropy_loss: function outputting the cost which we are minimizing,
            # lower cost should yield higher accuracy
            logits, train_op, cross_entropy_loss = optimize(model_output, correct_label, learning_rate, num_classes)
            print('Model_optimised')
            print('Initialize all variables - this might slow your computer, just do not worry and wait about 10 min')
            print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
            # # Initialize all variables
            session.run(tf.global_variables_initializer())
            session.run(tf.local_variables_initializer())
            print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
            print("Model build successful, starting training")

            # Train the neural network
            train_nn(session, EPOCHS, BATCH_SIZE, get_batches_fn,
                     train_op, cross_entropy_loss, image_input,
                     correct_label, keep_prob, learning_rate)
            print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
            print('Training Done! :)')
            # Run the model with the test images and save each painted output image (roads painted green)
            helper.save_inference_samples(runs_dir, test_data_dir, session, image_shape, logits, keep_prob, image_input)
            print('Saving model')
            saver = tf.train.Saver()
            output_dir = '../Trained_Model/' + str(time.time())
            if os.path.exists(output_dir):
                shutil.rmtree(output_dir)
            os.makedirs(output_dir)
            saver.save(session, output_dir + '/model.ckpt')
            print("All done!")
示例#12
0
def run():
    tests.test_for_kitti_dataset(DATA_DIR)

    # 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:
        vgg_tensors = [
            vgg_input_tensor, vgg_keep_prob_tensor, vgg_layer3_out_tensor,
            vgg_layer4_out_tensor, vgg_layer7_out_tensor
        ] = load_vgg(sess, VGG_PATH)

        output = layers(vgg_layer3_out_tensor,
                        vgg_layer4_out_tensor,
                        vgg_layer7_out_tensor,
                        num_classes=NUM_CLASSES)

        #########################################################################################################
        # THIS SECTION IS FOR DEBUGGING ONLY.
        #
        # # Print the shapes of the tensors (need to evaluate them, since not all dimensions are known statically).
        # print('Tensor shapes:')
        # sess.run(tf.local_variables_initializer())
        # sess.run(tf.global_variables_initializer())
        # tensors_to_print = [vgg_input_tensor,
        #                     vgg_layer3_out_tensor,
        #                     vgg_layer4_out_tensor,
        #                     vgg_layer7_out_tensor,
        #                     output]
        # shapes = sess.run([tf.shape(t) for t in tensors_to_print],
        #                   # Feed dummy data.
        #                   {vgg_input_tensor: np.zeros([1, IMAGE_SHAPE[0], IMAGE_SHAPE[1], 3]),
        #                    vgg_keep_prob_tensor: 1.0})
        # name_to_shape = {t.name : shape for t, shape in zip(tensors_to_print, shapes)}
        # print(name_to_shape)
        # # Make sure the shape of the output matches the one of the input.
        # in_shape = name_to_shape[vgg_input_tensor.name]
        # out_shape = name_to_shape[output.name]
        # np.testing.assert_array_equal(np.array(in_shape[:3]), np.array(out_shape[:3]))
        #########################################################################################################

        correct_label = tf.placeholder(
            dtype=tf.int32,
            shape=[None, IMAGE_SHAPE[0], IMAGE_SHAPE[1], NUM_CLASSES])
        learning_rate = tf.placeholder(dtype=tf.float32, shape=[])

        logits, train_op, loss = optimize(output, correct_label, learning_rate,
                                          NUM_CLASSES)
        get_batches_fn = helper.gen_batch_function(
            os.path.join(DATA_DIR, 'data_road/training'), IMAGE_SHAPE)

        train_nn(sess, NUM_EPOCHS, BATCH_SIZE, get_batches_fn, train_op, loss,
                 vgg_input_tensor, correct_label, vgg_keep_prob_tensor,
                 learning_rate)

        helper.save_inference_samples(RUNS_DIR, DATA_DIR, sess, IMAGE_SHAPE,
                                      logits, vgg_keep_prob_tensor,
                                      vgg_input_tensor)
示例#13
0
def run():

    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'

    correct_label = tf.placeholder(tf.float32, [None, None, None, NUM_CLASSES],
                                   name='correct_label')
    #input_image = tf.placeholder(tf.float32, name='input_image')
    #keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    learning_rate = tf.placeholder(tf.float32, name='learning_rate')

    tests.test_for_kitti_dataset(data_dir)

    # 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
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        # final layer of the graph
        layer_output = layers(layer3_out, layer4_out, layer7_out, NUM_CLASSES)
        # optimizer
        logits, train_op, cross_entropy_loss, iou_obj = optimize(
            layer_output,
            correct_label,
            learning_rate,
            NUM_CLASSES,
            with_accuracy=True)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        saver = tf.train.Saver(max_to_keep=5)
        START_TIME = time.time()
        # TODO: Train NN using the train_nn function
        train_nn(sess, EPOCHS, BATCH_SIZE, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate, iou_obj, saver)

        EPOCH_TIME = time.time()

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        END_TIME = time.time()
        print("save time :", END_TIME - EPOCH_TIME, " TOTAL TIME:",
              END_TIME - START_TIME)
示例#14
0
def run():
    print('start run()')
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    ##tests.test_for_kitti_dataset(data_dir)
    epochs = 7 # 10
    batch_size = 7
    # 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

		## build inference graph
        input_image, keep_prob, layer3, layer4, layer7 = load_vgg(sess, vgg_path)
		##debug
        '''
        print('start debug 1 section')
        for batch_image, batch_label in get_batches_fn(batch_size):
            #print(type(layer7))
            print(batch_image.shape)
            temp = sess.run(input_image, feed_dict={input_image: batch_image})
            print(temp.shape)
            print(type(layer3))
            temp = sess.run(layer7, feed_dict={input_image: batch_image, keep_prob: 0.5})
            print(temp.shape)
            break
        print('debug 1 section over')
        '''
        ##print('type of keep_prob: ' )
        ##print(type(keep_prob))
        nn_last_layer = layers(layer3, layer4, layer7, num_classes)
		## build training graph
        correct_label = tf.placeholder(tf.float32, (None, None, None, num_classes))
        learning_rate = tf.placeholder(tf.float32)
        logits, train_op, cross_entropy_loss = optimize(nn_last_layer, correct_label, learning_rate, num_classes)

        ## run this initializer after your graph has been defined!!!
        sess.run(tf.global_variables_initializer())


        # TODO: Train NN using the train_nn function
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op, cross_entropy_loss, input_image, correct_label, keep_prob,
                                                                     learning_rate)

        writer = tf.summary.FileWriter("./tmp/log1", sess.graph)
        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
def run():
    num_classes = 3
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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
        print("Loading data...")
        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)
        print("Data loaded...")

        # 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
        "Building network..."
        input_image, keep_prob, vgg_l3, vgg_l4, vgg_l7 = load_vgg(
            sess, vgg_path)
        output_layer = layers(vgg_l3, vgg_l4, vgg_l7, num_classes)

        correct_label = tf.placeholder(tf.float32,
                                       (None, None, None, num_classes),
                                       name="correct_label")
        learning_rate = tf.Variable(0.001,
                                    name="learning_rate",
                                    dtype=tf.float32)
        logits, train_op, loss = optimize(output_layer, correct_label,
                                          learning_rate, num_classes)
        "Network built..."

        # TODO:? Train NN using the train_nn function
        print("Initializing network...")
        epochs = 12
        batch_size = 8

        init_op = tf.global_variables_initializer()
        saver = tf.train.Saver()
        sess.run(init_op)
        print("Network initialized...")

        print("Training network...")
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op, loss,
                 input_image, correct_label, keep_prob, learning_rate)
        print("Network trained...")

        ## Save the network here.
        save_path = saver.save(sess, "data/models/final_model")

        # TODO:? Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)
        print("Saved")
示例#16
0
def run():
    num_classes = 2
    image_shape = (160, 576)  # KITTI dataset uses 160x576 images
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    #PROB = 0.5
    #RATE = 0.01
    # 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/

    learning_rate = tf.placeholder(dtype=tf.float32, name='learning_rate')
    correct_label = tf.placeholder(dtype=tf.float32,
                                   shape=[None, None, None, num_classes],
                                   name='correct_label')
    epochs = 20
    #batch_size = 256
    #batch_size = 128
    #batch_size = 64
    batch_size = 32

    #config = tf.ConfigProto()
    #config.gpu_options.allow_growth = True
    #with tf.Session(config=config) as sess:
    with tf.Session() as sess:

        # Need to define learning rate, # epochs, templates for learning rate and correct lables
        # learning rate is just a float val, correct labels is a 4D value
        # batch, height, width, # classes (tf placeholder)

        # 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
        input_image, keep_probability, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        # Final layer of DNN (result)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        # gives logits, cross entropy
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, 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, input_image, correct_label,
                 keep_probability, learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_probability, input_image)
示例#17
0
def run():
    num_classes = 2
    epochs = 30
    batch_size = 5
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    action = 'eval'
    #action='eval'
    #action='train_evail'
    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

        try:
            #try to load graph from previously saved model
            #print("***************************************1***************************************")
            imported_meta = tf.train.import_meta_graph("fcn.meta")
            imported_meta.restore(sess, tf.train.latest_checkpoint('./'))
            #print("***************************************2***************************************")
            graph = tf.get_default_graph()
            image_input = graph.get_tensor_by_name("image_input:0")
            keep_prob = graph.get_tensor_by_name("keep_prob:0")
            correct_label = graph.get_tensor_by_name("correct_label:0")
            learning_rate = graph.get_tensor_by_name("learning_rate:0")
            layer_output = graph.get_tensor_by_name("fcn_final:0")
            print("loaded model form file")
        except:
            #print("***************************************3***************************************")
            print("create model from scratch")

            #Build NN using load_vgg, layers, and optimize function
            image_input, keep_prob, layer3, layer4, layer7 = load_vgg(
                sess, vgg_path)
            layer_output = layers(layer3, layer4, layer7, num_classes)

            correct_label = tf.placeholder(tf.int32,
                                           [None, None, None, num_classes],
                                           name='correct_label')
            learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        logits, train_op, loss_op = optimize(layer_output, correct_label,
                                             learning_rate, num_classes)

        #Train NN using the train_nn function
        if action == 'train' or action == 'train_eval':
            train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                     loss_op, image_input, correct_label, keep_prob,
                     learning_rate)

        if action == 'train' or action == 'train_eval':
            saver = tf.train.Saver()
            saver.save(sess, './fcn')

        #Save inference data using helper.save_inference_samples
        if action == 'eval' or action == 'train_eval':
            helper.save_inference_samples(runs_dir, data_dir, sess,
                                          image_shape, logits, keep_prob,
                                          image_input)
batch_size = 1
save_model_path = '3_datasets_batchsize1_lr00001_epoch' + str(epochs)

export_dir = './pb_dir_' + save_model_path + '_' + str(int(time.time()))
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

with tf.Session() as sess:
    correct_label = tf.placeholder(tf.int32, [None, None, None, num_classes],
                                   name='correct_label')
    learning_rate = tf.placeholder(tf.float32, name='learning_rate')

    # 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, ''),
                                               image_shape)

    # Build NN using load_vgg, layers, and optimize function
    input_image, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
        sess, vgg_path)
    nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out,
                           num_classes)
    logits, train_op, cross_entropy_loss = optimize(nn_last_layer,
                                                    correct_label,
                                                    learning_rate, num_classes)

    sess.run(tf.global_variables_initializer())

    # Train NN using the train_nn function
    train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
             cross_entropy_loss, input_image, correct_label, keep_prob,
def run():
    num_classes = 2
    epochs = 45
    batch_size = 4
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    log_dir = runs_dir + '/tf_logs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    tf.reset_default_graph()

    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

        # Augmentations implemented in gen_batch_function()

        # TODO: Build NN using load_vgg, layers, and optimize function
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)

        correct_label = tf.placeholder(tf.int32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        saver = tf.train.Saver()
        tf_file_writer = tf.summary.FileWriter(log_dir, tf.get_default_graph())

        # TODO: Train NN using the train_nn function
        sess.run(tf.global_variables_initializer())

        train_nn(sess, tf_file_writer, epochs, batch_size, get_batches_fn,
                 train_op, cross_entropy_loss, input_image, correct_label,
                 keep_prob, learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        # save trained model
        saver.save(sess, './runs/semseg_model.ckpt')

        # close tensorboard writer
        tf_file_writer.close()
示例#20
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    # 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)

    epochs = 32
    batch_size = 1

    # with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.Session() as sess:

        #with tf.device("/gpu:0"):

        # get layers from stored VGG
        image_input, keep_prob, layer3, layer4, layer7 = load_vgg(
            sess, vgg_path)

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

        # create graph with skip connections, return last layer, inference part
        final_layer = layers(layer3, layer4, layer7, num_classes)

        correct_label = tf.placeholder(
            tf.float32, [None, image_shape[0], image_shape[1], num_classes])
        learning_rate = tf.constant(0.0001)

        # continue with graph, add loss and training part
        logits, train_op, cross_entropy_loss = optimize(
            final_layer, correct_label, learning_rate, num_classes)

        sess.run(tf.global_variables_initializer())

        print('start training')
        time_start = time.time()
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, image_input, correct_label, keep_prob,
                 learning_rate)
        time_training = time.time() - time_start
        print('training time: ', datetime.timedelta(seconds=time_training))

        print('saving inference data')
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, image_input)

        # OPTIONAL: Apply the trained model to a video
        print('... finished')
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    epochs = 20
    batch_size = 8

    # Allocate fixed memory
    #https://stackoverflow.com/questions/34199233/
    #how-to-prevent-tensorflow-from-allocating-the-totality-of-a-gpu-memory
    config = tf.ConfigProto()
    # only allocate 90% of the total memory of each GPU
    config.gpu_options.allocator_type = 'BFC'
    config.gpu_options.per_process_gpu_memory_fraction = 0.90

    print("\nIn run...")
    with tf.Session(config=config) 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
        augment_image = helper.image_augmentation()

        # TODO: Build NN using load_vgg, layers, and optimize function
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)

        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)

        correct_label = tf.placeholder(tf.float32,
                                       shape=[None, None, None, num_classes])

        learning_rate = tf.placeholder(tf.float32)

        # TODO: Train NN using the train_nn function
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        sess.run(tf.global_variables_initializer())

        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate, augment_image)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)
示例#22
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # Set parameters
    epochs = 20
    batch_size = 4
    # TODO: set learning rate through params here

    # 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
        image_input, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
            sess, vgg_path)

        # Build Fully Convolutional Network
        last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out,
                            num_classes)

        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=(None, None, None, num_classes))
        learning_rate = tf.placeholder(dtype=tf.float32)

        # TODO: where does logits get used?`
        logits, train_op, cross_entropy_loss = optimize(
            last_layer, correct_label, learning_rate, 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,
                 learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        # code snippet from ncondo
        saver = tf.train.Saver()
        saver.save(sess, 'checkpoints/model1.ckpt')
        saver.export_meta_graph('checkpoints/model1.meta')
        tf.train.write_graph(sess.graph_def, './checkpoints/', 'model1.pb',
                             False)

        # Save image outputs
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, image_input)
示例#23
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = '/data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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
        # input_image and keep_prob are placeholders
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)

        # Placeholder for variable resolution images
        # (the placeholders are filled with values during the training and loading of the images)
        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=(None, None, None, num_classes))
        learning_rate = tf.placeholder(dtype=tf.float32)

        logits, training_operation, cross_entropy_loss = optimize(
            nn_last_layer=layer_output,
            correct_label=correct_label,
            learning_rate=learning_rate,
            num_classes=num_classes)

        # TODO: Train NN using the train_nn function
        sess.run(tf.global_variables_initializer())
        train_nn(sess,
                 epochs=EPOCHS,
                 batch_size=BATCH_SIZE,
                 get_batches_fn=get_batches_fn,
                 train_op=training_operation,
                 cross_entropy_loss=cross_entropy_loss,
                 input_image=input_image,
                 correct_label=correct_label,
                 keep_prob=keep_prob,
                 learning_rate=learning_rate)

        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        # OPTIONAL: Apply the trained model to a video
        print('All finished.')
def run():
    num_classes = 2
    #image_shape = (260, 576)
    image_shape = (256, 512)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.5

    correct_label = tf.placeholder(tf.float32,
                                   shape=(None, image_shape[0], image_shape[1],
                                          2))
    learning_rate = tf.placeholder(tf.float32)

    with tf.Session(config=config) 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
        image_input, keep_prob, layer3, layer4, layer7 = load_vgg(
            sess, vgg_path)
        #debug shapes onebyone, trans1, trans2, nn_last_out = layers(layer3, layer4, layer7, num_classes)
        nn_last_out = layers(layer3, layer4, layer7, num_classes)

        ### TODO
        logits, train_op, cross_entropy_loss = \
            optimize(nn_last_out, correct_label, learning_rate, num_classes)

        # TODO: Train NN using the train_nn function
        #batch_size = 1;
        batch_size = 20
        epochs = 1000
        init = tf.global_variables_initializer()
        sess.run(init)
        """ ### debug the dynamic shapes
        sess_out = sess.run([tf.shape(layer3),
                         tf.shape(layer4),
                         tf.shape(layer7),
                         tf.shape(onebyone),
                         tf.shape(trans1),
                         tf.shape(trans2),
                         tf.shape(nn_last_out),
                         tf.shape(logits),
                         ], feed_dict={image_input: np.zeros(
                                          (1, image_shape[0], image_shape[1], 3)),
                                       'keep_prob:0': 0.5})
        print ("*** dyn shapes ")
        names = ["layer3", "layer4", "layer7", "onebyone",
                 "trans1", "trans2", "nn_last_out", "logtis" ]
        for n, s in zip(names, sess_out):
            print(n, s)
        """

        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, image_input, correct_label, keep_prob,
                 learning_rate)

        # 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)
示例#25
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    epochs = 40  # tested on 10, 30, 40, 60 epochs
    # 40 epochs: loss ~0.025
    # slows down significantly after 40 epochs, starts to overfit
    # after 30 epochs: loss ~0.05, with artifacts on cars, not ideal lighting

    batch_size = 20
    # large batch sizes lead to out of memory error

    #keep_prob = 0.5
    #learning_rate = 0.001

    # 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
        vgg_input_tensor, vgg_keep_prob_tensor, vgg_layer3_out_tensor, vgg_layer4_out_tensor, vgg_layer7_out_tensor = load_vgg(
            sess, vgg_path)
        deconv3 = layers(vgg_layer3_out_tensor, vgg_layer4_out_tensor,
                         vgg_layer7_out_tensor, num_classes)

        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=(None, None, None, num_classes))

        learning_rate = tf.placeholder(
            dtype=tf.float32)  # can not convert float to tensor error

        logits, train_op, cross_entropy_loss = optimize(
            deconv3, correct_label, learning_rate, 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, vgg_input_tensor, correct_label,
                 vgg_keep_prob_tensor, learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, vgg_keep_prob_tensor,
                                      vgg_input_tensor)
示例#26
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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/

    epochs = 50
    batch_size = 5
    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

        # TF Placeholders
        correct_label = tf.placeholder(dtype=tf.float32,
                                       shape=(None, None, None, num_classes),
                                       name='correct_label')
        learning_rate = tf.placeholder(dtype=tf.float32, name='learning_rate')

        vgg_input, vgg_keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
            sess, vgg_path)

        nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out,
                               num_classes)

        logits, train_op, cross_entropy_loss = optimize(
            nn_last_layer, correct_label, learning_rate, 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, vgg_input, correct_label, vgg_keep_prob,
                 learning_rate)

        # 'Saver' op to save and restore all the variables
        saver = tf.train.Saver()
        save_path = saver.save(sess, 'checkpoints/model1.ckpt')
        saver.export_meta_graph('checkpoints/model1.meta')
        tf.train.write_graph(sess.graph_def, './checkpoints/', 'model1.pb',
                             False)
        print("Model saved in file: %s" % save_path)

        # TODO: Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, vgg_keep_prob, vgg_input)
示例#27
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    # data_dir = './data'
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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

        correct_label = tf.placeholder(tf.int32, [None, None, None, num_classes], name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(layer_output,
                                                        correct_label, learning_rate, num_classes)

        # set up a saver object
        saver = tf.train.Saver()

        ckpt = tf.train.get_checkpoint_state('./model_ckpt/')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)


        # Train NN using the train_nn function
        train_nn(sess, 50, 10, get_batches_fn, train_op, cross_entropy_loss, input_image,
                 correct_label, keep_prob, learning_rate, saver)

        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)

        # OPTIONAL: Apply the trained model to a video
        # Import everything needed to edit/save/watch video clips

        imp=helper.ImageProcess(sess,keep_prob,input_image,logits,image_shape)

        videogen = skvideo.io.vreader('./driving.mp4')

        writer = skvideo.io.FFmpegWriter("./runs/results.mp4")
        for i,frame in enumerate(videogen):
            if i<5:
                print("frame:",i)
                new_frame=imp.pipeline(frame)
                writer.writeFrame(new_frame)

        writer.close()
示例#28
0
def run():
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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
        path = os.path.join(data_dir, 'data_road/training')
        print(path)
        get_batches_fn = helper.gen_batch_function(path, image_shape)

        #
        epochs = 50
        batch_size = 32

        correct_label = tf.placeholder(tf.int32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')
        #

        # 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
        input_image, keep_prob, layer_3, layer_4, layer_7 = load_vgg(
            sess, vgg_path)
        layer_output = layers(layer_3, layer_4, layer_7, num_classes)

        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        # TODO: Train NN using the train_nn function

        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter('/tmp/log/train', sess.graph)
        tf.global_variables_initializer().run()

        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        # OPTIONAL: Apply the trained model to a video

        writer.close()
示例#29
0
def run():
    num_classes = NUM_CLASSES
    image_shape = IMAGE_SHAPE
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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, 'vgg16/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

        # TODO: Train NN using the train_nn function

        # TODO: Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)

        # OPTIONAL: Apply the trained model to a video

        epochs = EPOCHS
        batch_size = BATCH_SIZE

        # TF placeholders
        correct_label = tf.placeholder(tf.int32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        input_image, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
            sess, vgg_path)
        #sess.run(tf.Print(vgg_layer7_out, [tf.shape(vgg_layer7_out)]))

        #nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out, num_classes)
        nn_last_layer = layers(vgg_layer3_out,
                               vgg_layer4_out,
                               vgg_layer7_out,
                               num_classes,
                               sess=sess,
                               vgg_input=input_image,
                               keep_prob=keep_prob)

        logits, train_op, cross_entropy_loss = optimize(
            nn_last_layer, correct_label, learning_rate, num_classes)

        # TODO: Train NN using the train_nn function

        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)
        print("running on images - done")
示例#30
0
def run():
    num_classes = CLASSES
    image_shape = IMAGE_SHAPE
    data_dir = DATA_DIR
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # 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

        # Build NN using load_vgg, layers, and optimize function
        correct_label = tf.placeholder(tf.int32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        input_image, keep_prob, vgg_layer3_out, vgg_layer4_out, vgg_layer7_out = load_vgg(
            sess, vgg_path)

        nn_last_layer = layers(vgg_layer3_out, vgg_layer4_out, vgg_layer7_out,
                               num_classes)

        logits, train_op, cross_entropy_loss, decaying_learning_rate = optimize(
            nn_last_layer, correct_label, learning_rate, num_classes)

        start_time = time.time()

        epoch_loss = train_nn(sess, EPOCHS, BATCH_SIZE, get_batches_fn,
                              train_op, cross_entropy_loss, input_image,
                              correct_label, keep_prob, learning_rate,
                              decaying_learning_rate)

        end_time = time.time()
        elapsed = end_time - start_time
        hours = elapsed // 3600
        minutes = (elapsed % 3600) // 60
        seconds = (elapsed % 3600) % 60
        print(
            "Training time: {:.0f} hours {:.0f} minutes {:.0f} seconds".format(
                hours, minutes, seconds))

        log_file_path = './' + str(EPOCHS) + '_log.txt'
        log_file = open(log_file_path, 'w')
        log_file.write('Epoch,Loss\n')
        for key in epoch_loss.keys():
            log_file.write('{},{}\n'.format(key, epoch_loss[key]))
        log_file.close()

        start_time = time.time()
        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        end_time = time.time()
        elapsed = end_time - start_time
        hours = elapsed // 3600
        minutes = (elapsed % 3600) // 60
        seconds = (elapsed % 3600) % 60
        print("Inference time: {:.0f} hours {:.0f} minutes {:.0f} seconds".
              format(hours, minutes, seconds))

        # Save model for later use
        #saver = tf.train.Saver()
        #saver.save(sess, MODEL_DIR + '/model_meta')
        #print("Model saved to {} directory".format(MODEL_DIR))

        # OPTIONAL: Apply the trained model to a video
        start_time = time.time()
        processed_frames = []
        # Load video
        #video_clip = VideoFileClip(VIDEO_DIR + '/project_video.mp4')
        video_clip = VideoFileClip(VIDEO_DIR + '/solidWhiteRight.mp4')
        frame_counter = 1
        for frame in video_clip.iter_frames():
            processed_frame = helper.segment_single_image(
                sess, logits, keep_prob, input_image, frame, image_shape)
            # Collect processed frame
            processed_frames.append(processed_frame)
            print("Frame {} processed".format(frame_counter))
            frame_counter += 1
        # Stitcha all frames to get the video
        processed_video = ImageSequenceClip(processed_frames,
                                            fps=video_clip.fps)
        processed_video.write_videofile(VIDEO_DIR +
                                        '/solidWhiteRight_processed.mp4',
                                        audio=False)
        print("Processed video written to {} directory".format(VIDEO_DIR))
        end_time = time.time()
        elapsed = end_time - start_time
        hours = elapsed // 3600
        minutes = (elapsed % 3600) // 60
        seconds = (elapsed % 3600) % 60
        print(
            "Video processing time: {:.0f} hours {:.0f} minutes {:.0f} seconds"
            .format(hours, minutes, seconds))
def run():
    #tests.test_for_kitti_dataset(data_dir)

    # 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/

    # Memory parameters https://medium.com/@lisulimowicz/tensorflow-cpus-and-gpus-configuration-9c223436d4ef
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:

        tf.global_variables_initializer()
        tf.local_variables_initializer()

        # Placeholders
        labels = tf.placeholder(tf.float32,
                                shape=[None, None, None, num_classes])
        learning_rate = tf.placeholder(tf.float32)

        # 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

        # Build NN using load_vgg, layers, and optimize function
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)
        logits, train_op, cross_entropy_loss = optimize(
            layer_output, labels, learning_rate, num_classes)

        # Train NN using the train_nn function
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, labels, keep_prob,
                 learning_rate)

        # Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        # Apply the trained model to a video
        def complete_pipeline(img):
            """
            Sample code taken from gen_test_output helper method
            """
            image = scipy.misc.imresize(img, image_shape)
            im_softmax = sess.run([tf.nn.softmax(logits)], {
                keep_prob: 1.0,
                input_image: [image]
            })
            im_softmax = im_softmax[0][:, 1].reshape(image_shape[0],
                                                     image_shape[1])
            segmentation = (im_softmax > 0.5).reshape(image_shape[0],
                                                      image_shape[1], 1)
            mask = np.dot(segmentation, np.array([[0, 255, 0, 127]]))
            mask = scipy.misc.toimage(mask, mode="RGBA")
            street_im = scipy.misc.toimage(image)
            street_im.paste(mask, box=None, mask=mask)
            return np.array(street_im)

        def video_pipeline(current_image):
            ''' Complete video pipeline '''
            return complete_pipeline(current_image)

        def generate_video(output, process_image):
            ''' Generate a video '''
            print('Generating video to: {}'.format(output))
            clip1 = VideoFileClip(
                input_file)  #.subclip(1,10) # 0,5  TODO remove this
            video_clip = clip1.fl_image(process_image)
            video_clip.write_videofile(output, audio=False)
            clip1.reader.close()
            return output

        video_output = generate_video(output_file, video_pipeline)
示例#32
0
def run():
    #global variables
    global tensorboard, trainning
    #Arguments
    parser = argparse.ArgumentParser(
        description='Semantic Segmentation Trainning')
    parser.add_argument('-tb',
                        '--tensorboard',
                        action='store_true',
                        help='Save data for TensorBoard')
    parser.add_argument('-to',
                        '--trainning_off',
                        action='store_false',
                        help='Set Trainning OFF for the network')
    parser.add_argument('-v',
                        '--video',
                        default=None,
                        type=str,
                        help='Apply Segmentation FCN network over the video')
    args = parser.parse_args()
    tensorboard = args.tensorboard
    trainning = args.trainning_off
    video = args.video

    #Tranning values
    epochs = 8
    learning_rate = tf.placeholder(tf.float32, name='learning_rate')
    batch_size = 4
    num_classes = 2
    image_shape = (160, 576)
    data_dir = './data'
    runs_dir = './runs'
    tests.test_for_kitti_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(data_dir)

    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)

        # Build NN using load_vgg, layers, and optimize function
        # Load VGG
        input_tensor, keep_prob_tensor, layer3_tensor, layer4_tensor, layer7_tensor = load_vgg(
            sess, vgg_path)
        # Transfor into a FCN
        output = layers(layer3_tensor, layer4_tensor, layer7_tensor,
                        num_classes)
        # Load Optmizer
        correct_label = tf.placeholder(tf.float32,
                                       [None, None, None, num_classes],
                                       name='correct_label')
        logits, train_op, cross_entropy_loss = optimize(
            output, correct_label, learning_rate, num_classes)

        # Train NN using the train_nn function

        if trainning:
            print("Trainning Model")
            train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                     cross_entropy_loss, input_tensor, correct_label,
                     keep_prob_tensor, learning_rate)

            # Save inference data using helper.save_inference_samples
            helper.save_inference_samples(runs_dir, data_dir, sess,
                                          image_shape, logits,
                                          keep_prob_tensor, input_tensor)
        else:
            print("Loading pre-trained model")
            #load model
            saver = tf.train.Saver()
            saver.restore(sess, "./data/model/model.ckpt")

        #Apply the trained model to a video
        if video is not None:
            print("Loading video " + video)
            print("Creating Video, saved at result.mp4")
            #clip = VideoFileClip('driving_10.mp4')
            clip = VideoFileClip(video)
            create_video(clip, sess, logits, keep_prob_tensor, input_tensor,
                         image_shape)
示例#33
0
def run():
    num_classes = 4  #  CW: red, yellow, green, unknown
    proportion_train = 0.75  # rest validation. Don't have big enough set for separate test set really!

    # CW: both real Carla images and simulator exports are 800x600.
    # We might find shrinking them helps with performance in terms of
    # speed or memory, though classification quality will suffer if
    # we go too far. Semantic segregation project chose a size with
    # reasonably high power-of-two factors to allow for the repeated halving
    # of resolution going up the CNN funnel (160x576, or 2^5*5 x 2^6*9)
    # without any awkward padding issues. 800 already divides nicely,
    # but 600 is 2^3*3*5^2 so it can only be halved cleanly 3 times.
    # But there is not too much happening at the bottom of any of our
    # images, so clipping a little to 800x576 should be quite nice,
    # maybe with a 1/2 or 1/4 shrink to speed things up.
    # TODO clipping logic -- for now just shrinking to avoid code changes
    image_shape = (
        288, 384
    )  # Initial experiment size (heightxwidth) -- out of GPU memory trying 576*800. Multiples of 32.

    data_dir = './data'
    runs_dir = './runs'

    # Walkthrough: maybe ~6 epochs to start with. Batches not too big because large amount of information.
    epochs = 20  # To get started
    batch_size = 1  # Already getting memory warnings!
    # Other hyperparameters in train_nn(); would have put them here but went with template calling structure

    # 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')

        # Split images into training and validation sets
        training_image_paths, validation_image_paths =  \
                    helper.get_split_image_paths(proportion_train, '../data/training_images')

        # Create function to get batches
        get_batches_fn = helper.gen_batch_function(training_image_paths,
                                                   image_shape, num_classes)

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

        # Walkthrough: correct labels will be 4D (batch, height, width, num classes)
        # CW: see my comments in get_batches_fn() to remind self of why... final (num classes) axis is one-hot
        #     with [0]=1 for background and [1]=1 for (any) road

        # DONE: Build NN using load_vgg, layers, and optimize function

        # CW: load VGG16 (actually already modified version for FCN) and pick out tensors corresponding
        #     to layers we want to attach to
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, vgg_path)

        # CW: add our own layers to do transpose convolution skip connections from encoder
        layer_output = layers(layer3_out, layer4_out, layer7_out,
                              num_classes)  # get final layer out

        # CW: for debug, want to visualise model structure in Tensorboard; initially did this
        # before adding my layers to understand how to connect to unmodified VGG layers. Now
        # doing afterwards to include picture in write-up that includes my layers.
        if True:  # Turned off for most runs when not debugging
            print(tf.trainable_variables()
                  )  # also trying to understand what we've got
            log_path = os.path.join(vgg_path, 'logs')
            writer = tf.summary.FileWriter(log_path, graph=sess.graph)
            # Then visualise as follows:
            # >tensorboard --logdir=C:\Users\UK000044\git\CarND-Semantic-Segmentation\data\vgg\logs --host localhost
            # Open http://localhost:6006 in browser (if don't specify --host, in Windows 10 uses PC name, and
            #                         localhost or 127.0.0.1 find no server, whereas http://pc_name:6006 does work)

        # CW: add operations to classify each pixel by class and assess performance
        # Input label size dynamic because have odd number of images as last batch; can get away without specifying
        # shape in complete detail up front but specifying those we know to hopefully make bugs more apparent
        correct_label = tf.placeholder(tf.float32,
                                       shape=[None, num_classes],
                                       name='correct_label')

        # Reshape labels as one-hot matrix spanning all of the pixels from all of the images concatenated together
        flattened_label = tf.reshape(correct_label, (-1, num_classes),
                                     name='flattened_label')

        learning_rate = tf.placeholder(tf.float32,
                                       shape=(),
                                       name='learning_rate')

        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        # CW: have to initialise variables at some point
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        # DONE: Train NN using the train_nn function
        train_nn(sess, epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, flattened_label, keep_prob,
                 learning_rate)

        # DONE: Save inference data using helper.save_inference_samples
        helper.save_inference_samples(runs_dir, validation_image_paths, sess,
                                      image_shape, logits, keep_prob,
                                      input_image)
示例#34
0
def run(data_arg, vgg_arg, output_arg, run_arg):
    num_classes = 2
    image_shape = (
        256, 256
    )  # KITTI dataset uses 160x576 images, puzzle dataset uses 256x256
    data_dir = data_arg
    vgg_path = vgg_arg
    runs_dir = run_arg
    # tests.test_for_kitti_dataset(data_dir)

    # Download pretrained vgg model
    helper.maybe_download_pretrained_vgg(vgg_path)

    # 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/

    # own parameters
    n_epochs = 1
    batch_size = 4

    print("==========\nSession start.\n==========")
    with tf.Session() as sess:
        # Path to vgg model
        # Create function to get batches
        get_batches_fn = helper.gen_batch_function(os.path.join(
            data_dir, 'training'),
                                                   image_shape,
                                                   puzzle=True)

        # 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
        print("Building NN...")
        input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg(
            sess, os.path.join(vgg_path, "vgg"))
        layer_output = layers(layer3_out, layer4_out, layer7_out, num_classes)

        # create placeholders
        correct_label = tf.placeholder(tf.int32, (None, None, None, None),
                                       name="correct_label")
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')

        logits, train_op, cross_entropy_loss = optimize(
            layer_output, correct_label, learning_rate, num_classes)

        # TODO: Train NN using the train_nn function
        print("Training NN...")
        train_nn(sess, n_epochs, batch_size, get_batches_fn, train_op,
                 cross_entropy_loss, input_image, correct_label, keep_prob,
                 learning_rate)

        # TODO: Save inference data using helper.save_inference_samples
        #  helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image)
        print("Saving examples")
        helper.save_inference_samples(runs_dir, data_dir, sess, image_shape,
                                      logits, keep_prob, input_image)

        print("saving model...")

        # Save the variables to disk.
        saver = tf.train.Saver()
        save_path = saver.save(sess, output_arg)
        print("Model saved in path: %s" % save_path)

        # OPTIONAL: Apply the trained model to a video
        print("==========\nSession end.\n==========")