source = load_data_source(args.data_source)
source.load_data(args.data_dir, 0.1)
print('[i] # training samples:   ', source.num_training)
print('[i] # validation samples: ', source.num_validation)
print('[i] # classes:            ', source.num_classes)
print('[i] Image size:           ', source.image_size)
train_generator = source.train_generator
valid_generator = source.valid_generator
label_colors = source.label_colors

#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    net = FCNVGG(sess)
    net.build_from_vgg(args.vgg_dir, source.num_classes, progress_hook='tqdm')

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

    optimizer, loss = net.get_optimizer(labels)

    summary_writer = tf.summary.FileWriter(args.tensorboard_dir, sess.graph)
    saver = tf.train.Saver(max_to_keep=10)

    label_mapper = tf.argmax(labels, axis=3)
    n_train_batches = int(math.ceil(source.num_training / args.batch_size))

    initialize_uninitialized_variables(sess)
    print('[i] Training...')
#-------------------------------------------------------------------------------
print('[i] Project name:      ', args.name)
print('[i] Network checkpoint:', checkpoint_file)
print('[i] Metagraph file:    ', metagraph_file)
print('[i] Number of samples: ', len(samples))
print('[i] Output directory:  ', args.output_dir)
print('[i] Image size:        ', source.image_size)
print('[i] # classes:         ', source.num_classes)
print('[i] Batch size:        ', args.batch_size)

#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    net = FCNVGG(sess, source.num_classes)
    net.build_from_metagraph(metagraph_file, checkpoint_file)

    #---------------------------------------------------------------------------
    # Process the images
    #---------------------------------------------------------------------------
    generator = sample_generator(samples, source.image_size, args.batch_size)
    n_sample_batches = int(math.ceil(len(samples) / args.batch_size))
    description = '[i] Processing samples'

    for x, names in tqdm(generator,
                         total=n_sample_batches,
                         desc=description,
                         unit='batches'):
        feed = {net.image_input: x, net.keep_prob: 1, net.is_training: False}
        img_labels = sess.run(net.classes, feed_dict=feed)
Beispiel #3
0
except (ImportError, AttributeError, RuntimeError) as e:
    print('[!] Unable to load data source:', str(e))
    sys.exit(1)


loss_list = []
val_loss_list = []
psnr_list = []
#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    rd_feature = tf.placeholder(
        tf.float32, shape=[None, None, None, 3], name='rd_feature_train')
    net = FCNVGG(sess, rd_feature)
    net.build_from_vgg(args.vgg_dir, source.num_classes, progress_hook='tqdm')

    labels = tf.placeholder(tf.int32,
                            shape=[None, None, None, source.num_classes], name='labels_train')

    raw_image = tf.placeholder(
        tf.float32, shape=[None, None, None, 3], name='raw_img_train')

    optimizer, loss_mse, loss_ce, psnr, fm = net.get_optimizer(labels)

    summary_writer = tf.summary.FileWriter(args.tensorboard_dir, sess.graph)
    saver = tf.train.Saver(max_to_keep=10)

    label_mapper = tf.argmax(labels, axis=3)
    n_train_batches = int(math.ceil(source.num_training/args.batch_size))
#-------------------------------------------------------------------------------
print('[i] Project name:      ', args.name)
print('[i] Network checkpoint:', checkpoint_file)
print('[i] Metagraph file:    ', metagraph_file)
print('[i] Number of samples: ', len(samples))
print('[i] Output directory:  ', args.output_dir)
print('[i] Image size:        ', source.image_size)
print('[i] # classes:         ', source.num_classes)
print('[i] Batch size:        ', args.batch_size)

#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    net = FCNVGG(sess)
    net.build_from_metagraph(metagraph_file, checkpoint_file)

    #---------------------------------------------------------------------------
    # Process the images
    #---------------------------------------------------------------------------
    generator = sample_generator(samples, source.image_size, args.batch_size)
    n_sample_batches = int(math.ceil(len(samples)/args.batch_size))
    description = '[i] Processing samples'

    for x, names in tqdm(generator, total=n_sample_batches,
                        desc=description, unit='batches'):
        feed = {net.image_input:  x,
                net.keep_prob:    1}
        img_labels = sess.run(net.classes, feed_dict=feed)
        imgs = draw_labels_batch(x, img_labels, label_colors, False)
Beispiel #5
0
#-------------------------------------------------------------------------------
print('[i] Project name:      ', args.name)
print('[i] Network checkpoint:', checkpoint_file)
print('[i] Metagraph file:    ', metagraph_file)
print('[i] Number of samples: ', len(samples))
print('[i] Output directory:  ', args.output_dir)
print('[i] Image size:        ', source.image_size)
print('[i] # classes:         ', source.num_classes)
print('[i] Batch size:        ', args.batch_size)

#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    net = FCNVGG(sess)
    net.build_from_metagraph(metagraph_file, checkpoint_file)

    #---------------------------------------------------------------------------
    # Process the images
    #---------------------------------------------------------------------------
    generator = sample_generator(samples, source.image_size, args.batch_size)
    n_sample_batches = int(math.ceil(len(samples) / args.batch_size))
    description = '[i] Processing samples'

    for x, names in tqdm(generator,
                         total=n_sample_batches,
                         desc=description,
                         unit='batches'):
        feed = {net.image_input: x, net.keep_prob: 1}
        img_labels = sess.run(net.classes, feed_dict=feed)
Beispiel #6
0
    def run(self):
        self.__disp_config()
        source = self.__load_data_source()
        #-------------------------------------------------------------------------------
        # Create the network
        #-------------------------------------------------------------------------------
        with tf.Session() as sess:
            print('[i] Creating the model...')
            net = FCNVGG(sess, source.num_classes)
            net.build_from_vgg(args.vgg_dir,
                               source.num_classes,
                               progress_hook='tqdm')

            net.get_optimizer(net.labels)
            net.add_summary_nodes(args.tensorboard_dir)

            validation_imgs = tf.placeholder(tf.float32,
                                             shape=[None, None, None, 3])
            validation_img_summary_op = tf.summary.image(
                'validation_img', validation_imgs)
            train_img_summary_op = tf.summary.image('train_img',
                                                    validation_imgs)
            saver = tf.train.Saver(max_to_keep=100)

            print('[i] Training...')

            utils.initialize_uninitialized_variables(sess)
            n_train_batches = int(
                math.ceil(source.num_training / args.batch_size))

            cur_step = 0
            step_num = n_train_batches * args.epochs

            for e in range(args.epochs):

                train_generator = source.train_generator(args.batch_size)

                for x, y in train_generator:
                    cur_step += 1
                    feed = {
                        net.image_input: x,
                        net.labels: y,
                        net.keep_prob: 0.5,
                        net.is_training: True
                    }

                    sess.run(net.reset_iou_op)
                    summary, _, loss_batch, _, label_mapper, img_classes, f1, accuracy = sess.run(
                        [
                            net.merged, net.update_iou_op, net.loss,
                            net.optimizer, net.label_mapper, net.classes,
                            net.f1, net.accuracy
                        ],
                        feed_dict=feed)
                    net.train_writer.add_summary(summary, cur_step)
                    iou, summary = sess.run(
                        [net.metric_iou__op, net.merged_update])
                    net.train_writer.add_summary(summary, cur_step)
                    print(
                        "step {}:{}/{}: loss={}, iou={}, f1={}, acc={}".format(
                            e + 1, cur_step, step_num, loss_batch, iou, f1,
                            accuracy))
                    #output trainig input image
                    if (cur_step) % 10 == 0:
                        val_imgs = x[:1, :, :, :]
                        val_img_labels = img_classes[:1, :, :]
                        val_img_labels_gt = label_mapper[:1, :, :]
                        imgs_inferred = utils.draw_labels_batch(
                            val_imgs, val_img_labels, source.label_colors)
                        imgs_gt = utils.draw_labels_batch(
                            val_imgs, val_img_labels_gt, source.label_colors)
                        val_imgs = utils.convert_rgb_batch(val_imgs)
                        all_imgs = np.concatenate(
                            [val_imgs, imgs_gt, imgs_inferred], axis=0)

                        summary = sess.run(
                            train_img_summary_op,
                            feed_dict={validation_imgs: all_imgs})
                        net.train_writer.add_summary(summary, cur_step)
                    #monitor inference on valiaton data
                    if (cur_step) % 10 == 0:
                        val_generator = source.valid_generator(args.batch_size)
                        #jut try out one batch
                        x, y = next(val_generator)
                        feed = {
                            net.image_input: x,
                            net.labels: y,
                            net.keep_prob: 1.0,
                            net.is_training: False
                        }

                        sess.run(net.reset_iou_op)
                        summary, _, loss_batch, label_mapper, img_classes, f1, accuracy = sess.run(
                            [
                                net.merged, net.update_iou_op, net.loss,
                                net.label_mapper, net.classes, net.f1,
                                net.accuracy
                            ],
                            feed_dict=feed)
                        net.val_writer.add_summary(summary, cur_step)
                        iou, summary = sess.run(
                            [net.metric_iou__op, net.merged_update])
                        net.val_writer.add_summary(summary, cur_step)
                        print(
                            "#####validation: step {}/{}: loss={}, iou={}, f1={}, acc={}#####"
                            .format(cur_step, step_num, loss_batch, iou, f1,
                                    accuracy))

                        val_imgs = x[:1, :, :, :]
                        val_img_labels = img_classes[:1, :, :]
                        val_img_labels_gt = label_mapper[:1, :, :]
                        imgs_inferred = utils.draw_labels_batch(
                            val_imgs, val_img_labels, source.label_colors)
                        imgs_gt = utils.draw_labels_batch(
                            val_imgs, val_img_labels_gt, source.label_colors)
                        val_imgs = utils.convert_rgb_batch(val_imgs)
                        all_imgs = np.concatenate(
                            [val_imgs, imgs_gt, imgs_inferred], axis=0)

                        summary = sess.run(
                            validation_img_summary_op,
                            feed_dict={validation_imgs: all_imgs})
                        net.val_writer.add_summary(summary, cur_step)

                if (e + 1) % args.checkpoint_interval == 0:
                    checkpoint = '{}/e{}.ckpt'.format(args.name, e + 1)
                    saver.save(sess, checkpoint)
                    print('Checkpoint saved:', checkpoint)

        return
def load_fcnvgg(session, num_classes):
    return FCNVGG(session, num_classes)
    print('[i] # validation samples: ', source.num_validation)
    print('[i] # classes:            ', source.num_classes)
    print('[i] Image size:           ', source.image_size)
    train_generator = source.train_generator
    valid_generator = source.valid_generator
    label_colors    = source.label_colors
except (ImportError, AttributeError, RuntimeError) as e:
    print('[!] Unable to load data source:', str(e))
    sys.exit(1)

#-------------------------------------------------------------------------------
# Create the network
#-------------------------------------------------------------------------------
with tf.Session() as sess:
    print('[i] Creating the model...')
    net = FCNVGG(sess)
    net.build_from_vgg(args.vgg_dir, source.num_classes, progress_hook='tqdm')

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

    optimizer, loss = net.get_optimizer(labels)

    summary_writer  = tf.summary.FileWriter(args.tensorboard_dir, sess.graph)
    saver           = tf.train.Saver(max_to_keep=10)

    label_mapper    = tf.argmax(labels, axis=3)
    n_train_batches = int(math.ceil(source.num_training/args.batch_size))

    initialize_uninitialized_variables(sess)
    print('[i] Training...')