def main(_):
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.INFO,
                        stream=sys.stdout)
    img1 = scp.misc.imread("/Users/limeng/Downloads/kitti/data_road/training/image_2/uu_000000.png")
    with tf.Session() as sess:
        images = tf.placeholder("float")
        feed_dict = {images: img1}
        batch_images = tf.expand_dims(images, 0)

        vgg_fcn = fcn8_vgg.FCN8VGG(vgg16_npy_path="/Users/limeng/Downloads/vgg16.npy")
        with tf.name_scope("content_vgg"):
            vgg_fcn.build(batch_images, debug=True, num_classes=2)

        print('Finished building Network.')

        logging.warning("Score weights are initialized random.")
        logging.warning("Do not expect meaningful results.")

        logging.info("Start Initializing Variabels.")

        init = tf.global_variables_initializer()
        sess.run(init)

        print('Running the Network')
        tensors = [vgg_fcn.pred, vgg_fcn.pred_up]
        down, up = sess.run(tensors, feed_dict=feed_dict)

        down_color = color_image(down[0], 2)
        up_color = color_image(up[0], 2)

        scp.misc.imsave('fcn8_downsampled.png', down_color)
        scp.misc.imsave('fcn8_upsampled.png', up_color)
Beispiel #2
0
    def _BuildGraph(self):
        """Builds computational graph using pretrained VGG16"""

        putils.Log_and_print("Building computational graph ...")

        # restrict GPU usage
        putils.AllocateGPU(self.N_GPUs)

        tf.reset_default_graph()

        # Load and apply pretrained network
        vgg = fcn8_vgg.FCN8VGG()

        # Placeholders and feed data
        vgg.images = tf.placeholder("float")
        vgg.labels = tf.placeholder("float")
        vgg.cumloss = tf.placeholder(
            "float")  # cumulative loss from previous sub-batches

        # AUGMENTATION
        if (not self.IS_TESTING) and self.AUGMENT:
            # Random brightness and contrast adjustment
            vgg.images = tf.image.random_brightness(vgg.images, max_delta=63)
            vgg.images = tf.image.random_contrast(vgg.images,
                                                  lower=0.2,
                                                  upper=1.8)

        with tf.name_scope("content_vgg"):
            vgg.build(vgg.images, train = (not self.IS_TESTING), \
                      num_classes = self.Model.NUM_CLASSES, \
                      random_init_fc8 = (not self.IS_TESTING), debug = False)

        # define loss and optimizer
        vgg.cost = loss.loss(vgg.upscore32, vgg.labels, \
                             num_classes = self.Model.NUM_CLASSES, \
                             head = self.Model.CLASSWEIGHTS)

        vgg.cumLoss = vgg.cost + vgg.cumloss
        vgg.optimizer = tf.train.AdamOptimizer(self.LEARN_RATE).minimize(
            vgg.cumLoss)

        putils.Log_and_print('Finished building Network.')

        # check trinable variables
        # tf.trainable_variables()

        # Assign graph as a class attribute
        self.vgg = vgg
Beispiel #3
0
def inference(hypes, images, vgg16_npy_path, train=True): 
    """Build the MNIST model up to where it may be used for inference.

    Args:
      images: Images placeholder, from inputs().
      train: whether the network is used for train of inference

    Returns:
      softmax_linear: Output tensor with the computed logits.
    """
    vgg_fcn = fcn8_vgg.FCN8VGG(vgg16_npy_path=vgg16_npy_path)

    vgg_fcn.wd = hypes['wd']

    vgg_fcn.build(images, train=train, num_classes=hypes['arch']['num_classes'], random_init_fc8=True)

    logits = {}

    logits['images'] = images

    if hypes['arch']['fcn_in'] == 'pool5':
        logits['fcn_in'] = vgg_fcn.pool5
    elif hypes['arch']['fcn_in'] == 'fc7':
        logits['fcn_in'] = vgg_fcn.fc7
    else:
        raise NotImplementedError

    logits['feed2'] = vgg_fcn.pool4
    logits['feed4'] = vgg_fcn.pool3

    logits['feed4'] = vgg_fcn.pool3

    logits['fcn_logits'] = vgg_fcn.upscore32

    logits['deep_feat'] = vgg_fcn.pool5
    logits['early_feat'] = vgg_fcn.conv4_3

    return logits
Beispiel #4
0

logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                    level=logging.INFO,
                    stream=sys.stdout)

with tf.Session() as sess:

    # build fcn net
    images = tf.placeholder(tf.float32, [None, 400, 400, 3])
    labels = tf.placeholder(tf.int8, [None, 400, 400, 2])
    learning_rate = 1e-5
    batch_size = 1
    epoch_size = 7000

    vgg_fcn = fcn8_vgg.FCN8VGG()

    with tf.name_scope("content_vgg"):
        vgg_fcn.build(images,
                      debug=True,
                      train=True,
                      num_classes=2,
                      random_init_fc8=True)

    logging.warning("Score weights are initialized random.")
    logging.info("Start training.")

    # compute loss
    logits = vgg_fcn.pred_up
    softmax_loss, false_p = softmaxoutput_loss(logits, labels, 2)
    correct_pred = tf.equal(tf.argmax(logits, 3), tf.argmax(labels, 3))
Beispiel #5
0
def train():
	'''Do training
	'''
	# Create queue coordinator.
	coord = tf.train.Coordinator()
	h, w = INPUT_SIZE
	sess = tf.Session()
	
	# Load reader.
	# print ('Load reader.......................................')
	with tf.name_scope("create_inputs"):
		reader = LIP_reader.ImageReader(TRAIN_IMAGE_PATH, TRAIN_LABEL_PATH, TRAIN_ID_LIST, 
										INPUT_SIZE, N_CLASSES, RANDOM_SCALE, RANDOM_MIRROR, SHUFFLE, coord)
		image_batch, label_batch = reader.dequeue(BATCH_SIZE)

	# # Set image_batch and label_batch as placeholder
	# # logits = tf.placeholder(tf.float32, shape = [BATCH_SIZE, w, h, N_CLASSES])
	# image_batch = tf.placeholder(tf.float32, shape = [BATCH_SIZE, w, h, 3])
	# label_batch = tf.placeholder(tf.float32, shape = [BATCH_SIZE, w, h, N_CLASSES])
	
	# Build FCN network
	fcn = fcn8_vgg.FCN8VGG()
	# fcn.build(image_batch, train=True, num_classes=N_CLASSES, random_init_fc8=True, debug=True)

	# fcn.build(image_batch, train=True, num_classes=N_CLASSES, random_init_fc8=False, debug=True)
	with tf.name_scope("content_vgg"):
		fcn.build(image_batch, num_classes=N_CLASSES, random_init_fc8=False, debug=True)
	
	print('Finished building Network.')

	# Define loss and optimisation parameters.
	with tf.name_scope('loss'):
		logits = fcn.upscore32
		labels = label_batch
		# print (logits)
		# print (labels)
		loss_ = loss.loss(logits, labels, num_classes=N_CLASSES)
		# total_loss = tf.get_collection('losses')	
		loss_summary = tf.summary.scalar("loss", loss_)

	# Summary
	merged = tf.summary.merge_all()
	summary_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)

	train_op = tf.train.AdamOptimizer(ADAM_OPT_RATE).minimize(loss_)

	# Saver for storing checkpoints of the model.
	saver = tf.train.Saver(max_to_keep=5)

	# Start queue threads.
	threads = tf.train.start_queue_runners(coord=coord, sess=sess)

	# Initialize
	logging.info("Start Initializing Variables.")
	init = tf.global_variables_initializer()
	sess.run(init)
	
	# print ('Getting para.................')
	# tvars = tf.trainable_variables()
	# root_dir = '/versa/alexissanchez/LIP-FCN-hair-mask/layer_para/'
	# print (tvars)
	# for i in range(1,6):
	# 	for j in range(1,3):
	# 		conv_name = 'conv{:d}_{:d}'.format(i,j)
			
	# 		if not os.path.exists(root_dir+conv_name):
	# 			os.makedirs(root_dir+conv_name)

	# 		filter_name = conv_name + '/filter:0'
	# 		filter_var = sess.run(filter_name)
	# 		print (filter_name)
	# 		print (filter_var.shape)
	# 		for x in range(3):
	# 			for y in range(3):
	# 				np.savetxt('./layer_para/' + filter_name + '_{:d}_{:d}.txt'.format(x,y), filter_var[x,y,:,:] , fmt = '%.3f')

	# 		bias_name = conv_name + '/biases:0'
	# 		bias_var = sess.run(bias_name)
	# 		print (bias_name)
	# 		print (bias_var.shape)
	# 		np.savetxt('./layer_para/' + bias_name + '.txt', bias_var , fmt = '%.3f')

	# var_name = 'score_fr/biases:0'
	# var = sess.run(var_name)
	# print (var_name + '.................')
	# print (var.shape)
	# np.savetxt('./layer_para/'+var_name+'.txt', var , fmt = '%.3f')

	# Checking demo save path 
	demo_dir = os.getcwd() + '/train_demo/train_2/'
	shutil.rmtree(demo_dir)
	subdir_list = ['image','label','predict']
	for subdir in subdir_list:
		if not os.path.exists(demo_dir+subdir):
			os.makedirs(demo_dir+subdir)

	# Iterate over training steps.
	print ('Start training......')	
	for step in range(NUM_STEPS):
		start_time = time.time()
		loss_value = 0

		# Do training process
		tensors = [merged, loss_, train_op, image_batch, label_batch, fcn.pred_up]
		merged_summary, loss_value, _, origin_image, origin_label, pred_up= sess.run(tensors)
		# merged_summary, loss_value, _, pred_up, bgr = sess.run([merged, loss_, train_op, fcn.pred_up, fcn.bgr])
		summary_writer.add_summary(merged_summary, step)

		if step % PRINT_PRED_EVERY == 0:
			duration = time.time() - start_time
			print('step {:d} \t loss = {:.3f}, ({:.3f} sec/step)'.format(step, loss_value, duration))

		if step % PREDICT_EVERY == 0:
			print ('	Doing Demo......')

			origin_image = np.array(origin_image, np.int32)
			origin_label = np.array(origin_label, np.int32)
			# print (origin_image.shape)
			# print (origin_label.shape)

			for im in range(BATCH_SIZE):
				# print (origin_image[im].shape)
				image_color = convert_RGB_TO_BGR(origin_image[im])
				label_color = color_label(origin_label[im])
				pred_result = color_image(pred_up[im])
				cv2.imwrite('{:s}/image/image_{:d}_{:d}.png'.format(demo_dir, step,im), image_color)
				cv2.imwrite('{:s}/label/label_{:d}_{:d}.png'.format(demo_dir, step,im), label_color)
				cv2.imwrite('{:s}/predict/predict_{:d}_{:d}.png'.format(demo_dir, step,im), pred_result)

			duration = time.time() - start_time
			print ('	Done. {:.3f} sec'.format(duration))
		

		if step % SAVE_PRED_EVERY == 0:
			print ('	Saving Model......')

			save_path = SNAPSHOT_DIR + '/model.ckpt'
			saver.save(sess,save_path, global_step = step)

			duration = time.time() - start_time
			print ('	Done. {:.3f} sec'.format(duration))

		
		
	coord.request_stop()
	coord.join(threads)
Beispiel #6
0
def test():
	print ('Test...............................')
	# Create queue coordinator.
	coord = tf.train.Coordinator()
	h, w = INPUT_SIZE
	sess = tf.Session()

	

	# Load reader
	with tf.name_scope("create_inputs"):
		reader = LIP_reader.ImageReader(TRAIN_IMAGE_PATH, TRAIN_LABEL_PATH, TRAIN_ID_LIST, 
										INPUT_SIZE, N_CLASSES, RANDOM_SCALE, RANDOM_MIRROR, SHUFFLE, coord)
		image_batch, label_batch = reader.dequeue(BATCH_SIZE)

	# Build FCN network
	# images = tf.placeholder("float")
	# image_batch = tf.expand_dims(images, 0)

	fcn = fcn8_vgg.FCN8VGG()

	with tf.name_scope("content_vgg"):
		fcn.build(image_batch, num_classes=N_CLASSES, random_init_fc8=False, debug=True)
	
	print('Finished building Network.')
	
	# Summary
	merged = tf.summary.merge_all()
	summary_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)

	# Generate test input
	# img1 = cv2.imread('./test_data/timg.jpg')
	# img1 = cv2.resize(img1, (h,w))
	
	# print ('image shape')
	# print (img1.shape)
	
	
	# Start queue threads.
	threads = tf.train.start_queue_runners(coord=coord, sess=sess)

	# Init
	init = tf.global_variables_initializer()
	sess.run(init)

	print('Running the Network')
	
	# Do test process	
	# feed_dict = {images: img1}
	#merged_summary, pred_up, pool5 = sess.run([merged, fcn.pred_up, fcn.pool5], feed_dict = feed_dict)
	merged_summary, pred_up, pool5, bgr = sess.run([merged, fcn.pred_up, fcn.pool5, fcn.bgr])
	summary_writer.add_summary(merged_summary, 0)

	# Write results 
	print ('Write results.................')
	print ('Input bgr')
	print (bgr.shape)


	print ('pred_up')
	print (pred_up.shape)
	pred_result = color_image(pred_up[0])
	cv2.imwrite('./test_demo/pred_result.png', pred_result)

	print ('pool5')
	print (pool5.shape)
	np.savetxt('./test_demo/pool5.txt', pool5[0,0,:,:] , fmt = '%.3f') 

	coord.request_stop()
	coord.join(threads)
Beispiel #7
0
def testModel(args, inputLoader):
    print("Testing saved model")

    os.system("rm -rf " + args.imagesOutDir)
    os.system("mkdir " + args.imagesOutDir)

    args.imageHeight = 500
    args.imageWidth = 500

    # Now we make sure the variable is now a constant, and that the graph still produces the expected result.
    with tf.Session() as session:
        with tf.variable_scope('FCN8_VGG'):
            batchInputImages = tf.placeholder(dtype=tf.float32,
                                              shape=[
                                                  None, args.imageHeight,
                                                  args.imageWidth,
                                                  args.imageChannels
                                              ],
                                              name="batchInputImages")
            batchInputLabels = tf.placeholder(
                dtype=tf.float32,
                shape=[None, args.imageHeight, args.imageWidth, 1],
                name="batchInputLabels")
            keepProb = tf.placeholder(dtype=tf.float32, name="keepProb")

        vgg_fcn = fcn8.FCN8VGG(batchSize=args.batchSize,
                               enableTensorboard=args.tensorboard,
                               vgg16_npy_path=args.pretrained)

        with tf.name_scope('Model'):
            vgg_fcn.build(rgb=batchInputImages,
                          keepProb=keepProb,
                          num_classes=args.numClasses,
                          random_init_fc8=True,
                          debug=(args.verbose > 0))

        with tf.name_scope('Loss'):
            # weights = tf.cast(batchInputLabels != args.ignoreLabel, dtype=tf.float32)
            loss = cost.loss(vgg_fcn.upscore32_pred, batchInputLabels,
                             inputLoader.getAnnotationClasses())

        with tf.name_scope('Optimizer'):
            optimizer = tf.train.AdamOptimizer(learning_rate=args.learningRate)

            gradients = tf.gradients(loss, tf.trainable_variables())
            gradients = list(zip(gradients, tf.trainable_variables()))
            applyGradients = optimizer.apply_gradients(
                grads_and_vars=gradients)

        # saver = tf.train.import_meta_graph(args.modelDir + args.modelName + ".meta")
        saver = tf.train.Saver()
        saver.restore(session, args.modelDir + args.modelName)

        # Get reference to placeholders
        # outputNode = session.graph.get_tensor_by_name("Model/probabilities:0")
        # inputBatchImages = session.graph.get_tensor_by_name("FCN8_VGG/batchInputImages:0")
        # inputKeepProbability = session.graph.get_tensor_by_name("FCN8_VGG/keepProb:0")

        # Sample 50 test batches
        args.batchSize = 1  # 50
        numBatch = 8
        for i in tqdm(range(1, numBatch), desc='Testing'):
            # print("Processing batch # %d" % i)
            batchImagesTest, _ = inputLoader.getTestBatch(
                readMask=False)  # For testing without GT mask
            imagesProbabilityMap = session.run(vgg_fcn.probabilities,
                                               feed_dict={
                                                   batchInputImages:
                                                   batchImagesTest,
                                                   keepProb: 1.0
                                               })
            # Save image results
            print("Saving images...")
            inputLoader.saveLastBatchResults(imagesProbabilityMap,
                                             isTrain=False)

    print("Model tested!")
    return
Beispiel #8
0
def trainModel(args):
    step = 1
    print('Train mode')
    with tf.variable_scope('FCN8_VGG'):

        trainDataGen = ImageDataGenerator(args,
                                          args.imagesInDir + 'train.txt',
                                          args.numClasses,
                                          'training',
                                          args.batchSize,
                                          num_preprocess_threads=8,
                                          shuffle=True,
                                          min_queue_examples=1000)

        images = trainDataGen.img_batch
        labels = trainDataGen.label_batch

        NUM_GPU = 1

        batch_queue = tf.contrib.slim.prefetch_queue.prefetch_queue(
            [images, labels], capacity=2 * NUM_GPU)

        image_batch, label_batch = batch_queue.dequeue()

    # tf.summary.image('Image', image_batch, max_outputs=10)
    tf.summary.image('label', label_batch, max_outputs=10)

    vgg_fcn = fcn8.FCN8VGG(enableTensorboard=args.tensorboard,
                           vgg16_npy_path=args.pretrained)

    with tf.name_scope('Model'):
        vgg_fcn.build(rgb=image_batch,
                      keepProb=args.keepProb,
                      num_classes=args.numClasses,
                      random_init_fc8=True,
                      debug=(args.verbose > 0))

    with tf.name_scope('Loss'):
        # weights = tf.cast(batchInputLabels != args.ignoreLabel, dtype=tf.float32)
        loss = cost.loss(vgg_fcn.upscore32_pred, label_batch,
                         trainDataGen.getAnnotationClasses())

    with tf.name_scope('Optimizer'):
        optimizer = tf.train.AdamOptimizer(learning_rate=args.learningRate)

        gradients = tf.gradients(loss, tf.trainable_variables())
        gradients = list(zip(gradients, tf.trainable_variables()))
        applyGradients = optimizer.apply_gradients(grads_and_vars=gradients)

    init = tf.global_variables_initializer()

    if args.tensorboard:
        tf.summary.scalar("loss", loss)
        if False:
            for var in tf.trainable_variables():
                tf.summary.histogram(var.name, var)
            for grad, var in gradients:
                tf.summary.histogram(var.name + '/gradient', grad)
        mergedSummaryOp = tf.summary.merge_all()

    saver = tf.train.Saver()

    ###
    config = tf.ConfigProto(device_count={'GPU': 1})

    with tf.Session() as sess:

        sess.run(init)

        tf.train.start_queue_runners(sess=sess)

        if args.clean:
            print("Removing previous checkpoints and logs")
            os.system("rm -rf " + args.logsDir)
            os.system("rm -rf " + args.imagesOutDir)
            os.system("rm -rf " + args.modelDir)
            os.system("mkdir " + args.imagesOutDir)
            os.system("mkdir " + args.modelDir)
        else:
            # Restore checkpoint
            print("Restoring from checkpoint")
            # saver = tf.train.import_meta_graph(args.modelDir + args.modelName + ".meta")
            saver.restore(sess, args.modelDir + args.modelName)

        img, img_l = sess.run([image_batch, label_batch])
        np.save('batch', [img])
        np.save('batch_l', [img_l])

        if args.tensorboard:
            # Op for writing logs to Tensorboard
            summaryWriter = tf.summary.FileWriter(args.logsDir,
                                                  graph=tf.get_default_graph())

        print("Starting network training")

        # Keep training until reach max iterations
        for step in range(1, EPOCHS):
            print('Steps: ' + str(step))

            # Run optimization op (backprop)
            start_time = time.time()
            if args.tensorboard:
                _, summary = sess.run([applyGradients, mergedSummaryOp])
                summaryWriter.add_summary(summary, step)
            else:
                [trainLoss, _] = sess.run([loss, applyGradients])
                print("Iteration: %d, Minibatch Loss: %f" % (step, trainLoss))

                if (np.isnan(trainLoss)):
                    print("Nan reached. Terminating training.")
                    break
            duration = time.time() - start_time

            if step % 10 == 0:
                num_examples_per_step = args.batchSize
                examples_per_sec = num_examples_per_step / duration

                format_str = ('%s: step %d, (%.1f examples/sec')
                print(format_str % (datetime.now(), step, examples_per_sec))

            if step % args.saveStep == 0:
                # Save model weights to disk
                saver.save(sess, args.modelDir + args.modelName + str(step))
                print("######## Intermediate Model saved: %s" %
                      (args.modelDir + args.modelName))

        # Save final model weights to disk
        saver.save(sess, args.modelDir + args.modelName)
        print("Final Model saved: %s" % (args.modelDir + args.modelName))

        print("Optimization Finished!")

    return
def trainModel(args, inputLoader):
    step = 1
    print('Train mode')
    with tf.variable_scope('FCN8_VGG'):
        batchInputImages = tf.placeholder(dtype=tf.float32,
                                          shape=[
                                              None, args.imageHeight,
                                              args.imageWidth,
                                              args.imageChannels
                                          ],
                                          name="batchInputImages")
        batchInputLabels = tf.placeholder(
            dtype=tf.float32,
            shape=[None, args.imageHeight, args.imageWidth, 1],
            name="batchInputLabels")
        keepProb = tf.placeholder(dtype=tf.float32, name="keepProb")

    vgg_fcn = fcn8.FCN8VGG(enableTensorboard=args.tensorboard,
                           vgg16_npy_path=args.pretrained)

    with tf.name_scope('Model'):
        vgg_fcn.build(rgb=batchInputImages,
                      keepProb=keepProb,
                      num_classes=args.numClasses,
                      random_init_fc8=True,
                      debug=(args.verbose > 0))

    with tf.name_scope('Loss'):
        # weights = tf.cast(batchInputLabels != args.ignoreLabel, dtype=tf.float32)
        loss = cost.loss(vgg_fcn.upscore32_pred, batchInputLabels,
                         inputLoader.getAnnotationClasses())

    with tf.name_scope('Optimizer'):
        optimizer = tf.train.AdamOptimizer(learning_rate=args.learningRate)

        gradients = tf.gradients(loss, tf.trainable_variables())
        gradients = list(zip(gradients, tf.trainable_variables()))
        applyGradients = optimizer.apply_gradients(grads_and_vars=gradients)

    init = tf.global_variables_initializer()

    if args.tensorboard:
        tf.summary.scalar("loss", loss)
        for var in tf.trainable_variables():
            tf.summary.histogram(var.name, var)
        for grad, var in gradients:
            tf.summary.histogram(var.name + '/gradient', grad)
        mergedSummaryOp = tf.summary.merge_all()

    saver = tf.train.Saver()

    ###

    with tf.Session() as sess:

        sess.run(init)

        if args.clean:
            print("Removing previous checkpoints and logs")
            os.system("rm -rf " + args.logsDir)
            os.system("rm -rf " + args.imagesOutDir)
            os.system("rm -rf " + args.modelDir)
            os.system("mkdir " + args.imagesOutDir)
            os.system("mkdir " + args.modelDir)
        else:
            # Restore checkpoint
            print("Restoring from checkpoint")
            # saver = tf.train.import_meta_graph(args.modelDir + args.modelName + ".meta")
            saver.restore(sess, args.modelDir + args.modelName)

        if args.tensorboard:
            # Op for writing logs to Tensorboard
            summaryWriter = tf.summary.FileWriter(args.logsDir,
                                                  graph=tf.get_default_graph())

        print("Starting network training")

        # Keep training until reach max iterations
        while True:
            batchImagesTrain, batchLabelsTrain = inputLoader.getTrainBatch()

            if batchImagesTrain is None:
                print("Training completed!")
                break

            # Run optimization op (backprop)
            if args.tensorboard:
                _, summary = sess.run(
                    [applyGradients, mergedSummaryOp],
                    feed_dict={
                        batchInputImages: batchImagesTrain,
                        batchInputLabels: batchLabelsTrain,
                        keepProb: args.keepProb
                    })
                summaryWriter.add_summary(summary, step)
            else:
                [trainLoss, _] = sess.run(
                    [loss, applyGradients],
                    feed_dict={
                        batchInputImages: batchImagesTrain,
                        batchInputLabels: batchLabelsTrain,
                        keepProb: args.keepProb
                    })
                print("Iteration: %d, Minibatch Loss: %f" % (step, trainLoss))

                if (np.isnan(trainLoss)):
                    print("Nan reached. Terminating training.")
                    break

            if step % args.displayStep == 0:
                [trainLoss, trainImagesProbabilityMap] = sess.run(
                    [loss, vgg_fcn.probabilities],
                    feed_dict={
                        batchInputImages: batchImagesTrain,
                        batchInputLabels: batchLabelsTrain,
                        keepProb: 1.0
                    })

                # Save image results
                print("Saving images...")
                inputLoader.saveLastBatchResults(trainImagesProbabilityMap,
                                                 isTrain=True)
                print("Saved images.")
            print('step: ' + str(step))
            step += 1

            if step % args.saveStep == 0:
                # Save model weights to disk
                saver.save(sess, args.modelDir + args.modelName + str(step))
                print("Model saved: %s" % (args.modelDir + args.modelName))

            # Check the accuracy on test data
            if step % args.evaluateStep == 0:
                batchImagesTest, batchLabelsTest = inputLoader.getTestBatch()

                if args.evaluateStepDontSaveImages:
                    [testLoss] = sess.run(
                        [loss],
                        feed_dict={
                            batchInputImages: batchImagesTest,
                            batchInputLabels: batchLabelsTest,
                            keepProb: 1.0
                        })
                    print("Test loss: %f" % testLoss)

                else:
                    [testLoss, testImagesProbabilityMap] = sess.run(
                        [loss, vgg_fcn.probabilities],
                        feed_dict={
                            batchInputImages: batchImagesTest,
                            batchInputLabels: batchLabelsTest,
                            keepProb: 1.0
                        })
                    print("Test loss: %f" % testLoss)

                    # Save image results
                    print("Saving images")
                    inputLoader.saveLastBatchResults(testImagesProbabilityMap,
                                                     isTrain=False)

        # Save final model weights to disk
        saver.save(sess, args.modelDir + args.modelName)
        print("Model saved: %s" % (args.modelDir + args.modelName))

        # Report loss on test data
        batchImagesTest, batchLabelsTest = inputLoader.getTestBatch()
        testLoss = sess.run(loss,
                            feed_dict={
                                batchInputImages: batchImagesTest,
                                batchInputLabels: batchLabelsTest,
                                keepProb: 1.0
                            })
        print("Test loss (current): %f" % testLoss)

        print("Optimization Finished!")

    return
Beispiel #10
0
        dataset_split="validation")
    train_images = batchTrain['image']
    train_annotations = batchTrain['label']  #[N,H,W,1]
    valid_images = batchTest['image']
    valid_annotations = batchTest['label']
    valid_names = batchTest['image_name']
    valid_height = batchTest['height']
    valid_width = batchTest['width']

is_training = tf.Variable(initial_value=args.is_training,
                          trainable=False,
                          name='train_stat',
                          dtype=tf.bool)

#Setting up network
vgg_fcn = fcn8_vgg.FCN8VGG(args.vgg16_path)
logits = vgg_fcn.build(train_images,
                       num_classes=dataset.classes,
                       train=is_training,
                       debug=False)  #upscore32 = [N,H,W,151]

loss_func = loss(logits,
                 train_annotations,
                 dataset.classes,
                 ignore_label=dataset.ignore_label)
tf.summary.scalar("loss", loss_func)

lr = tf.Variable(initial_value=0.,
                 trainable=False,
                 name='lr',
                 dtype=tf.float32)
    # Preprocess images
    image = tf.placeholder(tf.float32)
    label = tf.placeholder(tf.int32)
    img_lab = tf.concat([image, tf.cast(label, tf.float32)], axis=2)
    img_lab = tf.random_crop(img_lab, size_random_crop)
    img_lab = tf.image.random_flip_left_right(img_lab)
    img_lab = tf.image.random_flip_up_down(img_lab)
    img_lab = tf.cond(
        tf.reshape(tf.random_uniform([1]), []) > 0.5, lambda: img_lab,
        lambda: tf.image.transpose_image(img_lab))
    img_proc, lab_proc = tf.split(tf.expand_dims(img_lab, 0), [3, 1], 3)
    lab_proc = tf.cast(tf.reshape(lab_proc,
                                  tf.shape(lab_proc)[0:-1]), tf.int32)

    # Build network
    vgg_fcn = fcn8_vgg.FCN8VGG(vgg16_npy_path='vgg16.npy',
                               load_semantic_net=False)
    with tf.name_scope("content_vgg"):
        vgg_fcn.build(img_proc, debug=True, num_classes=3, train=True)

    # Determine loss
    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=lab_proc, logits=vgg_fcn.upscore32))

    # Declare optimizer
    global_step = tf.Variable(0, trainable=False)
    learning_rate = tf.train.exponential_decay(starter_learning_rate,
                                               global_step,
                                               decay_every,
                                               learning_decay_rate,
                                               staircase=False)