Пример #1
0
def main(argv=None):
    #Create placeholders:keep_probability, image, annotation
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

    #Prediction
    pred_annotation, logits = inference(image, keep_probability)

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    
    print(len(train_records))
    print(len(valid_records))


    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    
    #read dataset of validation    
    validation_dataset_reader = dataset.BatchDatset(valid_records, image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    
    #load model
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
        

    if FLAGS.mode == "visualize":
    
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
        
        #Accuracy on validation 
        valid_acc = tf.reduce_mean(tf.cast(tf.equal(pred_annotation, valid_annotations), tf.float32))
         
        pred, valid_acc = sess.run([pred_annotation, valid_acc], feed_dict={image: valid_images, annotation: valid_annotations,
                                                    keep_probability: 1.0})
        
        print('Accuracy on valication: ' + str(valid_acc))   

        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)
        
        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(1+itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(1+itr))
            utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(1+itr))
            
            print("Saved image: %d" % itr)
Пример #2
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))

    print("Setting up image reader...")
    _, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(valid_records))

    print("Setting up dataset reader...")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
    else:
        sys.exit("No checkpoint file is found.")

    print("Export the Model...")
    graph_def = sess.graph.as_graph_def()
    freeze_graph_def = graph_util.convert_variables_to_constants(
        sess, graph_def, ["inference/conv_t3"])
    with open(FLAGS.output_file, 'wb') as f:
        f.write(freeze_graph_def.SerializeToString())

    total_loss = 0
    for itr in xrange(FLAGS.num_batches):
        valid_images, valid_annotations = validation_dataset_reader.next_batch(
            FLAGS.batch_size)
        valid_loss = sess.run(loss,
                              feed_dict={
                                  image: valid_images,
                                  annotation: valid_annotations,
                                  keep_probability: 1.0
                              })
        total_loss += valid_loss
        print('%d iteration: validation loss = %g' % (itr + 1, valid_loss))

    sess.close()
Пример #3
0
def main(argv=None):
    if not FLAGS.tensorflow_dir:
        raise ValueError(
            'You must supply the tensorflow directory with --tensorflow_dir')
    if not FLAGS.tflite_model:
        raise ValueError('You must supply the frozen pb with --tflite_model')

    logits = tf.placeholder(
        tf.float32,
        shape=[None, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CLASSES],
        name="logits")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))

    print("Preparing run_tflite command...")
    eval_dir = os.path.dirname(FLAGS.tflite_model)
    cmds = prepare_run_tflite_commands(eval_dir)

    print("Setting up image reader...")
    _, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(valid_records))

    print("Setting up dataset reader...")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    total_loss = 0
    for itr in xrange(FLAGS.num_batches):
        valid_images, valid_annotations = validation_dataset_reader.next_batch(
            FLAGS.batch_size)
        np.save(os.path.join(eval_dir, 'origin.npy'), valid_images)
        valid_images = valid_images - np.array(VGG_MEAN)
        valid_images = valid_images.astype(np.float32)
        np.save(os.path.join(eval_dir, 'batch_xs.npy'), valid_images)
        subprocess.check_output(cmds)
        ys = np.load(os.path.join(eval_dir, 'output_ys.npy'))

        with tf.Session() as sess:
            valid_loss = sess.run(loss,
                                  feed_dict={
                                      logits: ys,
                                      annotation: valid_annotations
                                  })
            total_loss += valid_loss
            print('%d iteration: validation loss = %g' % (itr + 1, valid_loss))

    sess.close()
Пример #4
0
def main(_):
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    train_dataset_reader = dataset.BatchDatset(train_records, image_options)
    eval_dataset_reader = dataset.BatchDatset(valid_records, image_options)

    model = Model()
    if FLAGS.mode == 'train':
        model.train(train_dataset_reader, eval_dataset_reader)
    elif FLAGS.mode == 'validation':
        model.eval(train_dataset_reader, eval_dataset_reader)
Пример #5
0
def main(argv=None):
    with tf.device('/device:GPU:0'):
    	keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    	image = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], name="input_image")
    	annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 1], name="annotation")
    	targets = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 2], name="targets")
	pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2)
    tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2)

    with tf.device('/device:GPU:0'):
    	#class_weight = tf.constant([0.03068287037037037, 0.9693171296296297])
    	#scaled_logits = tf.multiply(logits, class_weight)
    	#loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=scaled_logits,labels=tf.squeeze(annotation, squeeze_dims=[3]), name="entropy"))
	loss = tf.reduce_mean((tf.nn.weighted_cross_entropy_with_logits(targets=targets,
                                                                          logits=logits,
                                                                          pos_weight=30.,
                                                                          name="entropy")))
    tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)

    with tf.device('/device:GPU:0'):
    	train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records, image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records, image_options)

    config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config= config)

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir, '1515042674')
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
	    train_targets = np.concatenate((train_annotations, 1-train_annotations), axis=3).astype(np.float32)
            feed_dict = {image: train_images, annotation: train_annotations, targets: train_targets, keep_probability: 0.85}

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op], feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            # if itr % 500 == 0:
            #     valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
            #     valid_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations,
            #                                            keep_probability: 1.0})
            #     print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))
            #     saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

            if itr % 5000 == 0:
                print("Saving checkpoint")
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        for itr in range(validation_dataset_reader.get_dataset_size()):
            valid_images, valid_annotations = validation_dataset_reader.get_data_at_idx(itr)
            valid_targets = np.concatenate((valid_annotations, 1-valid_annotations), axis=3).astype(np.float32)
            pred, test_loss, new_loss = sess.run([pred_annotation, loss, weighted_loss], feed_dict={image: valid_images, annotation: valid_annotations, targets: valid_targets, keep_probability: 1.0})
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            utils.save_image(valid_images[0].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(itr))
            utils.save_image(valid_annotations[0].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(itr))
            utils.save_image(pred[0].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(itr))
            print("Test loss: %.3f" % test_loss)
            print("Weighted loss: %.3f" % new_loss)
            print("Saved image: %d" % itr)
Пример #6
0
def main(argv=None):
    '''
    #定义一下regularization:
    regularization = tf.Variable(0, dtype=tf.float32)
    '''
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")   #dropout
    image = tf.placeholder(tf.float32, shape=[None, None, None, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, None, None, 1], name="annotation")
    mean = tf.placeholder(tf.float32, name='mean')     #给mean一个占位符.
    pred_annotation, logits = inference(image, keep_probability, mean)

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                       labels=tf.squeeze(annotation, squeeze_dims=[3]),
                                                       name="entropy"))
    tf.add_to_collection('losses', loss)
    loss = tf.add_n(tf.get_collection('losses'))    #loss为总的正则化+weights之和.

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)  # 数据的转换输入.
    print(len(train_records))
    print(len(valid_records))

    trainable_var = tf.trainable_variables()  # Variable被收集在名为tf.GraphKeys.VARIABLES的colletion中

    train_op = train(loss, trainable_var)

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_RESIZE}  # 将IMAGE_SIZE大小作为一个batch
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records, image_options)  # 这两个是对象定义

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()


    # 这里的初始化方式可能可以修改以改善结果.
    sess.run(tf.global_variables_initializer())

    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)  # 生成check_point,下次可以从check_point继续训练
    if ckpt and ckpt.model_checkpoint_path:  # 这两行的作用是:tf.train.get_checkpoint_state()函数通过checkpoint文件(它存储所有模型的名字)找到目录下最新的模型.
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        #mymean = train_dataset_reader._read_images()   #强行运行这个函数,把mean传过来.
        #train_dataset_reader 调用._read_images()  需要在里面修改mean是否运算和retrun.
        #生成本次数据集的mean值.
        #print("mean:")
        #print(mymean)
        mymean = [73.8613, 73.8613, 73.8613]     #这里要根据数据集更新.

        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
            feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85, mean: mymean}
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss = sess.run(loss, feed_dict=feed_dict)
                # print()
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                # summary_writer.add_summary(summary_str, itr)
            if itr % 500 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        im_2017_list = []
        global im_2017
        for i in range(30):
            b = im_2017[0:5106, i * 500:i * 500 + 500, 3]
            b = np.array([np.array([b for i in range(3)])])
            b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
        im_2017_list.append(
            np.array([np.array([im_2017[0:5106, 15000:15106, 3] for i in range(3)])]).transpose(0, 2, 3, 1))

        allImg = []
        mymean = [73.8613, 73.8613, 73.8613]   #这里要根据数据集更新.

        for n, im_2017_part in enumerate(im_2017_list):
            feed_dict_valid = {image: im_2017_part, keep_probability: 1.0, mean:mymean}
            a = sess.run(pred_annotation, feed_dict=feed_dict_valid)
            a = np.mean(a, axis=(0, 3))
            allImg.append(a)

        result = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
        tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/deep_1015_1.tif', result)
Пример #7
0
def main(argv=None):
    # 1. input placeholders
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=(None, IMAGE_SIZE, IMAGE_SIZE, 3),
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=(None, IMAGE_SIZE, IMAGE_SIZE, 1),
                                name="annotation")
    # global_step = tf.Variable(0, trainable=False, name='global_step')

    # 2. construct inference network
    pred_annotation, logits, net = unetinference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)

    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # 3. loss measure
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    # 4. optimizing
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var, net['global_step'])

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader from ", FLAGS.data_dir, "...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print("data dir:", FLAGS.data_dir)
    print("train_records length :", len(train_records))
    print("valid_records length :", len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    # 5. paramter setup
    # 5.1 init params
    sess.run(tf.global_variables_initializer())
    # 5.2 restore params if possible
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    # 6. train-mode
    if FLAGS.mode == "train":
        valid = list()
        step = list()
        lo = list()

        global_step = sess.run(net['global_step'])
        global_step = 0
        for itr in xrange(global_step, MAX_ITERATION):
            # 6.1 load train and GT images
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            # print("train_image:", train_images.shape)
            # print("annotation :", train_annotations.shape)

            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }
            # 6.2 traininging

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)
                if itr % 500 == 0:
                    lo.append(train_loss)
            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))

                global_step = sess.run(net['global_step'])
                saver.save(sess,
                           FLAGS.logs_dir + "model.ckpt",
                           global_step=global_step)

                #Save the accuracy to the figure
                #print("valid", valid, "step", step)

                valid.append(valid_loss)
                step.append(itr)
                #print("valid", valid, "step", step)

                plt.plot(step, valid)
                plt.ylabel("Accuracy")
                plt.xlabel("Step")
                plt.title('Training Loss')
                plt.savefig(FLAGS.logs_dir + "Faccuracy.jpg")

                plt.clf()
                plt.plot(step, lo)
                plt.title('Loss')
                plt.ylabel("Loss")
                plt.xlabel("Step")
                plt.savefig(FLAGS.logs_dir + "Floss.jpg")

                plt.clf()

                plt.plot(step, lo)
                plt.plot(step, valid)
                plt.ylabel("Loss")
                plt.xlabel("Step")
                plt.title('Result')
                plt.legend(['Training Loss', 'Accuracy'], loc='upper right')
                plt.savefig(FLAGS.logs_dir + "Fmerge.jpg")

    # test-mode
    elif FLAGS.mode == "visualize":

        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir + "Image/",
                             name="inp_" + str(5 + itr))
            utils.save_image(
                (valid_annotations[itr].astype(np.uint8)) * 255 / 18,
                FLAGS.logs_dir + "Image/",
                name="gt_" + str(5 + itr))
            utils.save_image((pred[itr].astype(np.uint8)) * 255 / 18,
                             FLAGS.logs_dir + "Image/",
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":  # heejune added
        print(">>>>>>>>>>>>>>>>Test mode")
        crossMats = list()
        mIOU_all = list()
        validation_dataset_reader.reset_batch_offset(0)
        pred_e = list()
        # print(">>>>>>>>>>>>>>>>Test mode")
        for itr1 in range(validation_dataset_reader.get_num_of_records() // 2):
            # print(">>>>>>>>>>>>>>>>Test mode")
            valid_images, valid_annotations = validation_dataset_reader.next_batch(
                FLAGS.batch_size)
            pred, logits1 = sess.run(
                [pred_annotation, logits],
                feed_dict={
                    image: valid_images,
                    annotation: valid_annotations,
                    keep_probability: 1.0
                })
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            #logits1 = np.squeeze(logits1)
            pred = np.squeeze(pred)
            print("logits shape:", logits1.shape)
            np.set_printoptions(threshold=np.inf)
            # print("logits:", logits)
            for itr2 in range(FLAGS.batch_size):

                print("Output file: ",
                      FLAGS.logs_dir + "crf_" + str(itr1 * 2 + itr2))
                crfoutput = crf(valid_images[itr2].astype(np.uint8),
                                logits1[itr2])

                fig = plt.figure()
                pos = 240 + 1
                plt.subplot(pos)
                plt.imshow(valid_images[itr2].astype(np.uint8))
                plt.axis('off')
                plt.title('Original')

                pos = 240 + 2
                plt.subplot(pos)
                plt.imshow(valid_annotations[itr2].astype(np.uint8),
                           cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('GT')

                pos = 240 + 3
                plt.subplot(pos)
                plt.imshow(pred[itr2].astype(np.uint8),
                           cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('Prediction')

                pos = 240 + 4
                plt.subplot(pos)
                plt.imshow(crfoutput, cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('CRFPostProcessing')

                pos = 240 + 6
                plt.subplot(pos)
                ret, errorImage = cv2.threshold(
                    cv2.absdiff(pred[itr2].astype(np.uint8),
                                valid_annotations[itr2].astype(np.uint8)), 0.5,
                    255, cv2.THRESH_BINARY)
                plt.imshow(errorImage, cmap=plt.get_cmap('gray'))
                plt.axis('off')
                plt.title('Pred Error:' + str(np.count_nonzero(errorImage)))
                pred_e.append(np.count_nonzero(errorImage))

                crossMat = _calcCrossMat(
                    valid_annotations[itr2].astype(np.uint8),
                    pred[itr2].astype(np.uint8), NUM_OF_CLASSESS)
                crossMats.append(crossMat)
                #print(crossMat)
                IoUs = _calcIOU(valid_annotations[itr2].astype(np.uint8),
                                pred[itr2].astype(np.uint8), NUM_OF_CLASSESS)
                mIOU_all.append(IoUs)

                crfoutput = cv2.normalize(crfoutput, None, 0, 255,
                                          cv2.NORM_MINMAX)
                valid_annotations[itr2] = cv2.normalize(
                    valid_annotations[itr2], None, 0, 255, cv2.NORM_MINMAX)

                pos = 240 + 8
                plt.subplot(pos)
                ret, errorImage = cv2.threshold(
                    cv2.absdiff(crfoutput.astype(np.uint8),
                                valid_annotations[itr2].astype(np.uint8)), 10,
                    255, cv2.THRESH_BINARY)
                plt.imshow(errorImage, cmap=plt.get_cmap('gray'))
                plt.axis('off')
                plt.title('CRF Error:' + str(np.count_nonzero(errorImage)))

                #np.set_printoptions(threshold=np.inf)

                #plt.show()

                np.savetxt(FLAGS.logs_dir + "Image/Crossmatrix" +
                           str(itr1 * 2 + itr2) + ".csv",
                           crossMat,
                           fmt='%4i',
                           delimiter=',')
                np.savetxt(FLAGS.logs_dir + "Image/IoUs" +
                           str(itr1 * 2 + itr2) + ".csv",
                           IoUs,
                           fmt='%4f',
                           delimiter=',')
                plt.savefig(FLAGS.logs_dir + "Image/resultSum_" +
                            str(itr1 * 2 + itr2))
                # ---------------------------------------------
                utils.save_image(valid_images[itr2].astype(np.uint8),
                                 FLAGS.logs_dir + "Image/",
                                 name="inp_" + str(itr1 * 2 + itr2))
                utils.save_image(valid_annotations[itr2].astype(np.uint8),
                                 FLAGS.logs_dir + "Image/",
                                 name="gt_" + str(itr1 * 2 + itr2))
                utils.save_image(pred[itr2].astype(np.uint8),
                                 FLAGS.logs_dir + "Image/",
                                 name="pred_" + str(itr1 * 2 + itr2))
                utils.save_image(crfoutput,
                                 FLAGS.logs_dir + "Image/",
                                 name="crf_" + str(itr1 * 2 + itr2))

                plt.close('all')
                print("Saved image: %d" % (itr1 * 2 + itr2))
                # save list of error to file
        with open(FLAGS.logs_dir + 'pred_e.txt', 'w') as file:
            for error in pred_e:
                file.write("%i\n" % error)
        np.savetxt(FLAGS.logs_dir + "Crossmatrix.csv",
                   np.sum(crossMats, axis=0),
                   fmt='%4i',
                   delimiter=',')
        np.savetxt(FLAGS.logs_dir + "mIoUs" + ".csv",
                   np.mean(mIOU_all, axis=0),
                   fmt='%4f',
                   delimiter=',')
Пример #8
0
def main(argv=None):
    # 인풋 이미지와 타겟 이미지, 드롭아웃 확률을 받을 플레이스홀더를 정의합니다.
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    # FCN 그래프를 선언하고 TensorBoard를 위한 summary들을 지정합니다.
    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # 손실함수를 선언하고 손실함수에 대한 summary를 지정합니다.
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    # 옵티마이저를 선언하고 파라미터를 한스텝 업데이트하는 train_step 연산을 정의합니다.
    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
    train_step = optimizer.minimize(loss)

    # TensorBoard를 위한 summary들을 하나로 merge합니다.
    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    # training 데이터와 validation 데이터의 개수를 불러옵니다.
    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    # training 데이터와 validation 데이터를 불러옵니다.
    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    # 세션을 엽니다.
    sess = tf.Session()

    # 학습된 파라미터를 저장하기 위한 tf.train.Saver()와
    # tensorboard summary들을 저장하기 위한 tf.summary.FileWriter를 선언합니다.
    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    # 변수들을 초기화하고 저장된 ckpt 파일이 있으면 저장된 파라미터를 불러옵니다.
    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in range(MAX_ITERATION):
            # 학습 데이터를 불러오고 feed_dict에 데이터를 지정합니다
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            # train_step을 실행해서 파라미터를 한 스텝 업데이트합니다.
            sess.run(train_step, feed_dict=feed_dict)

            # 10회 반복마다 training 데이터 손실 함수를 출력합니다.
            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("반복(Step): %d, Training 손실함수(Train_loss):%g" %
                      (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            # 500회 반복마다 validation 데이터 손실 함수를 출력하고 학습된 모델의 파라미터를 model.ckpt 파일로 저장합니다.
            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation 손실함수(Validation_loss): %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        # validation data로 prediction을 진행합니다.
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        # Input Data, Ground Truth, Prediction Result를 저장합니다.
        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    # 세션을 닫습니다.
    sess.close()
Пример #9
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2)
    tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                                          labels=tf.squeeze(annotation, squeeze_dims=[3]),
                                                                          name="entropy")))
    loss_summary = tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records, image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records, image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    # create two summary writers to show training loss and validation loss in the same graph
    # need to create two folders 'train' and 'validation' inside FLAGS.logs_dir
    train_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train', sess.graph)
    validation_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/validation')

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
            feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85}

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, loss_summary], feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                train_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
                valid_loss, summary_sva = sess.run([loss, loss_summary], feed_dict={image: valid_images, annotation: valid_annotations,
                                                       keep_probability: 1.0})
                print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))

                # add validation loss to TensorBoard
                validation_writer.add_summary(summary_sva, itr)
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
        pred = sess.run(pred_annotation, feed_dict={image: valid_images, annotation: valid_annotations,
                                                    keep_probability: 1.0})
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr))
            utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5+itr))
            print("Saved image: %d" % itr)
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.float32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                                name="annotation")
    z = tf.placeholder(tf.float32, shape=[None, 7, 7, 3], name="z")

    pred_annotation, logits = inference(image, keep_probability, z)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    #    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
    #                                                                         labels=tf.squeeze(annotation, squeeze_dims=[3]),
    #                                                                    name="entropy")))

    loss = tf.reduce_mean(tf.squared_difference(logits, annotation))
    loss_summary = tf.summary.scalar("entropy", loss)

    grads = train_z(loss, z)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    # create two summary writers to show training loss and validation loss in the same graph
    # need to create two folders 'train' and 'validation' inside FLAGS.logs_dir
    train_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train', sess.graph)
    validation_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/validation')

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state('/scratch1/ram095/nips20/logs')
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):

            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            xx = 50
            xy = 50
            h = 50
            w = 50
            train_images[:, xx:xx + h, xy:xy + w, :] = 0

            z_ = np.random.uniform(low=-1.0,
                                   high=1.0,
                                   size=(FLAGS.batch_size, 7, 7, 3))

            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85,
                z: z_
            }
            #train_images[:,50:100,50:100,:] =0
            v = 0

            for p in range(10):
                z_ol = np.copy(z_)
                # print("666666666666666666666666666666666666666")
                z_loss, summ = sess.run([loss, loss_summary],
                                        feed_dict=feed_dict)
                print("Step: %d, z_step: %d, Train_loss:%g" % (itr, p, z_loss))
                #                print(z_)
                g = sess.run([grads], feed_dict=feed_dict)
                v_prev = np.copy(v)
                # print(g[0][0].shape)
                v = 0.001 * v - 0.1 * g[0][0]
                z_ += 0.001 * v_prev + (1 + 0.001) * v
            # z_ = np.clip(z_, -1.0, 1.0)
            # print(v.shape)
            # print(z_.shape)
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, loss_summary],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))

                train_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                xx = 50
                xy = 50
                h = 50
                w = 50
                valid_images[:, xx:xx + h, xy:xy + w, :] = 0
                valid_loss, summary_sva = sess.run(
                    [loss, loss_summary],
                    feed_dict={
                        image: valid_images,
                        annotation: valid_annotations,
                        keep_probability: 1.0,
                        z: z_
                    })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))

                # add validation loss to TensorBoard
                validation_writer.add_summary(summary_sva, itr)
                saver.save(
                    sess,
                    '/scratch1/ram095/nips20/logs/' + "model_z_center.ckpt",
                    500)

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            2)
        xx = 50
        xy = 50
        h = 50
        w = 50
        valid_images[:, xx:xx + h, xy:xy + w, :] = 0
        z_ = np.random.uniform(low=-1.0,
                               high=1.0,
                               size=(FLAGS.batch_size, 7, 7, 3))
        feed_dict = {
            image: valid_images,
            annotation: valid_annotations,
            keep_probability: 0.85,
            z: z_
        }
        v = 0
        for p in range(10):
            z_ol = np.copy(z_)
            # print("666666666666666666666666666666666666666")
            z_loss, summ = sess.run([loss, loss_summary], feed_dict=feed_dict)
            print("z_step: %d, Train_loss:%g" % (p, z_loss))
            #                print(z_)
            g = sess.run([grads], feed_dict=feed_dict)
            v_prev = np.copy(v)
            # print(g[0][0].shape)
            v = 0.001 * v - 0.1 * g[0][0]
            z_ += 0.001 * v_prev + (1 + 0.001) * v
        # z_ = np.clip(z_, -1.0, 1.0)

        pred = sess.run(logits,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            z: z_,
                            keep_probability: 1.0
                        })

        # valid_annotations = np.squeeze(valid_annotations, axis=3)
        # pred = np.squeeze(pred, axis=3)
        print(valid_images.shape)
        print(valid_annotations.shape)
        print(pred.shape)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
Пример #11
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    loss_summary = tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    # create two summary writers to show training loss and validation loss in the same graph
    # need to create two folders 'train' and 'validation' inside FLAGS.logs_dir
    train_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train', sess.graph)
    validation_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/validation')

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, loss_summary],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                train_writer.add_summary(summary_str, itr)

            if itr % 70 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss, summary_sva = sess.run(
                    [loss, loss_summary],
                    feed_dict={
                        image: valid_images,
                        annotation: valid_annotations,
                        keep_probability: 1.0
                    })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))

                # add validation loss to TensorBoard
                validation_writer.add_summary(summary_sva, itr)
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "predict":
        #keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
        #image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")

        seg_dress_file = FLAGS.input
        seg_dress_path = seg_dress_file.rsplit(".", 1)[0]

        segmented_dir = seg_dress_path + "_analyzed/"
        if os.path.exists(segmented_dir):
            shutil.rmtree(segmented_dir)
        os.makedirs(segmented_dir)
        img = cv2.imread(FLAGS.input)
        tempImg = img.copy()
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        valid_images = cv2.resize(img, (224, 224))
        valid_images = np.expand_dims(valid_images, axis=0)

        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            keep_probability: 1.0
                        })

        #tf.reset_default_graph()
        pred = np.squeeze(pred, axis=3)
        annot = pred[0].astype(np.uint8)
        annot[annot == 1] = 100
        utils.save_image(annot.astype(np.uint8),
                         FLAGS.logs_dir,
                         name="test_" + str(5))

        utils.save_image(pred[0].astype(np.uint8),
                         FLAGS.logs_dir,
                         name="pred_" + str(5))
        img = cv2.imread(
            os.path.join(FLAGS.logs_dir, "test_" + str(5) + ".png"), 0)
        arr = np.array([])
        scaledImage = cv2.normalize(img,
                                    arr,
                                    alpha=0,
                                    beta=255,
                                    norm_type=cv2.NORM_MINMAX)
        backtorgb = cv2.applyColorMap(scaledImage, cv2.COLORMAP_HSV)
        scaledImage2 = cv2.resize(scaledImage,
                                  (tempImg.shape[1], tempImg.shape[0]))
        edges = cv2.Canny(scaledImage2, 100, 100)
        cv2.imwrite(segmented_dir + 'color.png', scaledImage2)
        cv2.imwrite(segmented_dir + 'edges.png', edges)
        file = open(segmented_dir + 'Data.txt', 'w')
        file.write('Oil area ratio: ' +
                   str(np.count_nonzero(annot) / (224.0 * 224.0)))
        file.close()

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            20)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(20):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            annot = valid_annotations[itr].astype(np.uint8)
            annot[annot == 1] = 100
            prediction = pred[itr].astype(np.uint8)
            prediction[prediction == 1] = 100
            print(prediction)
            utils.save_image(prediction,
                             FLAGS.logs_dir,
                             name="prede_" + str(5 + itr))
            utils.save_image(annot,
                             FLAGS.logs_dir,
                             name="annot_" + str(5 + itr))
            print("Saved image: %d" % itr)
Пример #12
0
def main(argv=None):

    # 1. input placeholders
    keep_probability = tf.placeholder(tf.float32, name="keep_probability")
    image = tf.placeholder(tf.float32,
                           shape=(None, IMAGE_SIZE, IMAGE_SIZE, 3),
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=(None, IMAGE_SIZE, IMAGE_SIZE, 1),
                                name="annotation")
    #global_step = tf.Variable(0, trainable=False, name='global_step')

    # 2. construct inference network
    pred_annotation, logits, net = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=3)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=3)

    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=3)

    # 3. loss measure
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    # 4. optimizing
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var, net['global_step'])

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader from ", FLAGS.data_dir, "...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print("data dir:", FLAGS.data_dir)
    print("train_records length :", len(train_records))
    print("valid_records length :", len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    # 5. paramter setup
    # 5.1 init params
    sess.run(tf.global_variables_initializer())
    # 5.2 restore params if possible
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    # 6. train-mode
    if FLAGS.mode == "train":

        fd.mode_train(sess, FLAGS, net, train_dataset_reader,
                      validation_dataset_reader, train_records,
                      pred_annotation, image, annotation, keep_probability,
                      logits, train_op, loss, summary_op, summary_writer,
                      saver, DISPLAY_STEP)

    # test-random-validation-data mode
    elif FLAGS.mode == "visualize":

        fd.mode_visualize(sess, FLAGS, TEST_DIR, validation_dataset_reader,
                          pred_annotation, image, annotation, keep_probability,
                          NUM_OF_CLASSESS)

    # test-full-validation-dataset mode
    elif FLAGS.mode == "test":  # heejune added

        fd.mode_test(sess, FLAGS, TEST_DIR, validation_dataset_reader,
                     valid_records, pred_annotation, image, annotation,
                     keep_probability, logits, NUM_OF_CLASSESS)

    sess.close()
Пример #13
0
def main(argv=None):
    # dropout保留率
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    # 图像占坑
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    # 标签占坑
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    # 预测一个batch图像  获得预测图[b,h,w,c=1]  结果特征图[b,h,w,c=151]
    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    # 空间交叉熵损失函数[b,h,w,c=151]  和labels[b,h,w]    每一张图分别对比
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    # 返回需要训练的变量列表
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)

    # 传入损失函数和需要训练的变量列表
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    # 生成绘图数据
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    # data_dir = Data_zoo/MIT_SceneParsing/
    # training: [{image: 图片全路径, annotation:标签全路径, filename:图片名字}] [{}][{}]
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))  # 长度
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        # 读取图片 产生类对象 其中包含所有图片信息
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    # logs/
    if fine_tuning:
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)  # 训练断点回复
        if ckpt and ckpt.model_checkpoint_path:  # 如果存在checkpoint文件 则恢复sess
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("Model restored...")

    if FLAGS.mode == "train":
        for itr in range(MAX_ITERATION):
            # 读取下一batch
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            # 迭代优化需要训练的变量
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                # 迭代10次打印显示
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                # 迭代500 次验证
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                # 保存模型
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        # 可视化
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        # pred_annotation预测结果图
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
Пример #14
0
def main(argv=None):

    # 1. input placeholders
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=(None, IMAGE_SIZE, IMAGE_SIZE, 3),
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=(None, IMAGE_SIZE, IMAGE_SIZE, 1),
                                name="annotation")

    # 2. construct inference network
    pred_annotation, logits, net = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # 3. loss measure
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))

    # for CRF
    if FLAGS.mode == 'crftest' or FLAGS.mode == 'predonly':
        probability = tf.nn.softmax(logits=logits, axis=3)  # the axis!

    tf.summary.scalar("entropy", loss)

    # 4. optimizing
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var, net['global_step'])

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader from ", FLAGS.data_dir, "...")
    train_records, valid_records = scene_parsing.read_dataset(
        FLAGS.data_dir)  # now it is ignored and hard coded to 10kdataset
    print("data dir:", FLAGS.data_dir)
    print("***** mode:", FLAGS.mode)
    print("train_records length :", len(train_records))
    print("valid_records length :", len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        print("Loading Training Images.....")
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    print("Loading Validation Images.....")
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    # for simply check the memory size
    #x = input()
    # exit()

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    # 5. paramter setup
    # 5.1 init params
    sess.run(tf.global_variables_initializer())
    # 5.2 restore params if possible
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    # 6. train-mode
    if FLAGS.mode == "train":

        global_step = sess.run(net['global_step'])

        for itr in xrange(global_step, MAX_ITERATION):
            # 6.1 load train and GT images
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            #print("train_image:", train_images.shape)
            #print("annotation :", train_annotations.shape)

            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }
            # 6.2 traininging
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                global_step = sess.run(net['global_step'])
                saver.save(sess,
                           FLAGS.logs_dir + "model.ckpt",
                           global_step=global_step)

    # test-mode
    elif FLAGS.mode == "visualize":

        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":  # heejune added

        validation_dataset_reader.reset_batch_offset(0)
        for itr1 in range(validation_dataset_reader.get_num_of_records() // 2):
            valid_images, valid_annotations = validation_dataset_reader.next_batch(
                FLAGS.batch_size)
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: valid_images,
                                annotation: valid_annotations,
                                keep_probability: 1.0
                            })
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)

            for itr2 in range(FLAGS.batch_size):
                utils.save_image(valid_images[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="inp_" + str(itr1 * 2 + itr2))
                utils.save_image(valid_annotations[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="gt_" + str(itr1 * 2 + itr2))
                utils.save_image(pred[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="pred_" + str(itr1 * 2 + itr2))
                print("Saved image: %d" % (itr1 * 2 + itr2))

    elif FLAGS.mode == "crftest":  # Tuan added for CRF postprocessing

        accuracies = np.zeros(
            (validation_dataset_reader.get_num_of_records(), 3, 2))
        nFailed = 0
        validation_dataset_reader.reset_batch_offset(0)
        for itr1 in range(validation_dataset_reader.get_num_of_records() // 2):
            valid_images, valid_annotations = validation_dataset_reader.next_batch(
                FLAGS.batch_size)

            # pred = sess.run(pred_annotation, feed_dict={image: valid_images, annotation: valid_annotations,
            #                                        keep_probability: 1.0})

            predprob, pred = sess.run(
                [probability, pred_annotation],
                feed_dict={
                    image: valid_images,
                    annotation: valid_annotations,
                    keep_probability: 1.0
                })
            np.set_printoptions(threshold=10)
            # print("Prediction Probability:", predprob)
            # print("Preprob shape:", predprob.shape)
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred)
            predprob = np.squeeze(predprob)

            # @TODO: convert np once not repeatedly
            for itr2 in range(FLAGS.batch_size):
                # if(itr1 * 2 + itr2 <= 831):
                #    continue

                # 1. run CRF
                #print("Output file: ", FLAGS.logs_dir + "crf_" + str(itr1*2+itr2))
                #crfimage, crfoutput = crf(valid_images[itr2].astype(np.uint8),pred[itr2].astype(np.uint8))
                crfwithlabeloutput = mycrf.crf_with_labels(
                    valid_images[itr2].astype(np.uint8),
                    pred[itr2].astype(np.uint8), NUM_OF_CLASSESS)
                crfwithprobsoutput = mycrf.crf_with_probs(
                    valid_images[itr2].astype(np.uint8), predprob[itr2],
                    NUM_OF_CLASSESS)
                # 2. show result display
                if False:
                    print("orig:", valid_images[itr2].dtype)
                    print("annot:", valid_annotations[itr2].dtype, " shape:",
                          valid_annotations[itr2].shape)
                    print("pred:", pred[itr2].dtype, " shape:",
                          pred[itr2].shape)
                    print("crfwithlabel:", crfwithlabeloutput.dtype)
                    print("crfwithprobs:", crfwithprobsoutput.dtype)

                orignal = valid_images[itr2].astype(np.uint8)
                groundtruth = valid_annotations[itr2].astype(np.uint8)
                fcnpred = pred[itr2].astype(np.uint8)
                crfwithlabelpred = crfwithlabeloutput.astype(np.uint8)
                crfwithprobspred = crfwithprobsoutput.astype(np.uint8)

                if False:
                    savefile = FLAGS.logs_dir + "result/error_result_" + \
                        str(itr1 * 2 + itr2) + ".png"
                    myplot.showResultImages(orignal, groundtruth, fcnpred,
                                            crfwithlabelpred,
                                            crfwithprobspred)  # , savefile)

                # 3. Calculate confusion matrix between gtimage and prediction image and store to file
                pred_confusion_matrix = mycrf.calcConfussionMat(
                    groundtruth, fcnpred, NUM_OF_CLASSESS)
                crfwithlabelpred_confusion_matrix = mycrf.calcConfussionMat(
                    groundtruth, crfwithlabelpred, NUM_OF_CLASSESS)
                crfwithprobspred_confusion_matrix = mycrf.calcConfussionMat(
                    groundtruth, crfwithprobspred, NUM_OF_CLASSESS)
                accuracies[itr1 * 2 + itr2][0] = mycrf.calcuateAccuracy(
                    pred_confusion_matrix, False)
                accuracies[itr1 * 2 + itr2][1] = mycrf.calcuateAccuracy(
                    crfwithlabelpred_confusion_matrix, False)
                accuracies[itr1 * 2 + itr2][2] = mycrf.calcuateAccuracy(
                    crfwithprobspred_confusion_matrix, True)
                T_full = 0.9
                T_fgnd = 0.85
                if (accuracies[itr1 * 2 + itr2][2][1] < T_full
                        or accuracies[itr1 * 2 + itr2][2][0] < T_fgnd):
                    nFailed += 1
                    print("Failed Image (%d-th): %d" %
                          (nFailed, itr1 * 2 + itr2))

                if False:
                    np.save(
                        FLAGS.logs_dir + "pred_cross_matrix_" +
                        str(itr1 * 2 + itr2), pred_confusion_matrix)
                    np.save(
                        FLAGS.logs_dir + "crfwithlabelpred_cross_matrix_" +
                        str(itr1 * 2 + itr2),
                        crfwithlabelpred_confusion_matrix)
                    np.save(
                        FLAGS.logs_dir + "crfwithprobspred_cross_matrix_" +
                        str(itr1 * 2 + itr2),
                        crfwithprobspred_confusion_matrix)

                # 4. saving result
                if True:
                    # filenum = str(itr1 * 2 + itr2 +1) # to number the same as input
                    filenum = str(itr1 * 2 + itr2)  # now we have 0-index image

                    #print(FLAGS.logs_dir + "resultSum_" + filenum)
                    #plt.savefig(FLAGS.logs_dir + "resultSum_" + filenum)
                    utils.save_image(orignal,
                                     FLAGS.logs_dir,
                                     name="in_" + filenum)
                    utils.save_image(groundtruth,
                                     FLAGS.logs_dir,
                                     name="gt_" + filenum)
                    #utils.save_image(fcnpred, FLAGS.logs_dir, name="pred_" + filenum)
                    #utils.save_image(crfwithlabelpred, FLAGS.logs_dir, name="crfwithlabel_" + filenum)
                    utils.save_image(crfwithprobspred,
                                     FLAGS.logs_dir,
                                     name="crf_" + filenum)

                    # ---End calculate cross matrix
                    print("Saved image: %s" % filenum)

        np.save(FLAGS.logs_dir + "accuracy", accuracies)

    elif FLAGS.mode == "predonly":  # only predict, no accuracy calcuation when no groundtruth

        nFailed = 0
        validation_dataset_reader.reset_batch_offset(0)
        for itr1 in range(validation_dataset_reader.get_num_of_records() // 2):
            valid_images, _ = validation_dataset_reader.next_batch(
                FLAGS.batch_size)

            predprob, pred = sess.run([probability, pred_annotation],
                                      feed_dict={
                                          image: valid_images,
                                          keep_probability: 1.0
                                      })
            np.set_printoptions(threshold=10)
            #print("Prediction Probability:", predprob)
            #print("Preprob shape:", predprob.shape)
            pred = np.squeeze(pred)
            predprob = np.squeeze(predprob)

            # @TODO: convert np once not repeatedly
            for itr2 in range(FLAGS.batch_size):
                # if(itr1 * 2 + itr2 <= 831):
                #    continue

                # 1. run CRF
                #print("Output file: ", FLAGS.logs_dir + "crf_" + str(itr1*2+itr2))
                #crfimage, crfoutput = crf(valid_images[itr2].astype(np.uint8),pred[itr2].astype(np.uint8))
                crfwithlabeloutput = mycrf.crf_with_labels(
                    valid_images[itr2].astype(np.uint8),
                    pred[itr2].astype(np.uint8), NUM_OF_CLASSESS)
                crfwithprobsoutput = mycrf.crf_with_probs(
                    valid_images[itr2].astype(np.uint8), predprob[itr2],
                    NUM_OF_CLASSESS)
                # 2. show result display
                if False:
                    print("orig:", valid_images[itr2].dtype)
                    print("annot:", valid_annotations[itr2].dtype, " shape:",
                          valid_annotations[itr2].shape)
                    print("pred:", pred[itr2].dtype, " shape:",
                          pred[itr2].shape)
                    print("crfwithlabel:", crfwithlabeloutput.dtype)
                    print("crfwithprobs:", crfwithprobsoutput.dtype)

                orignal = valid_images[itr2].astype(np.uint8)
                fcnpred = pred[itr2].astype(np.uint8)
                crfwithlabelpred = crfwithlabeloutput.astype(np.uint8)
                crfwithprobspred = crfwithprobsoutput.astype(np.uint8)

                if False:
                    savefile = FLAGS.logs_dir + "result/error_result_" + \
                        str(itr1 * 2 + itr2) + ".png"
                    myplot.showResultImages(orignal, groundtruth, fcnpred,
                                            crfwithlabelpred,
                                            crfwithprobspred)  # , savefile)

                # 4. saving result
                if True:
                    # filenum = str(itr1 * 2 + itr2 +1) # to number the same as input
                    filenum = str(itr1 * 2 + itr2)  # now we have 0-index image

                    #print(FLAGS.logs_dir + "resultSum_" + filenum)
                    #plt.savefig(FLAGS.logs_dir + "resultSum_" + filenum)
                    utils.save_image(orignal,
                                     FLAGS.logs_dir,
                                     name="in_" + filenum)
                    #utils.save_image(fcnpred, FLAGS.logs_dir, name="pred_" + filenum)
                    #utils.save_image(crfwithlabelpred, FLAGS.logs_dir, name="crfwithlabel_" + filenum)
                    utils.save_image(crfwithprobspred,
                                     FLAGS.logs_dir,
                                     name="crf_" + filenum)

                    # ---End calculate cross matrix
                    print("Saved image: %s" % filenum)
Пример #15
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "visualize":
        max_out_itr = validation_dataset_reader.images.shape[
            0] / FLAGS.batch_size
        for out_itr in range(max_out_itr):
            valid_images, valid_annotations = validation_dataset_reader.next_batch(
                FLAGS.batch_size)
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: valid_images,
                                annotation: valid_annotations,
                                keep_probability: 1.0
                            })
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            print(pred)
            for itr in range(FLAGS.batch_size):
                pred[itr] = pred[itr] * 255
                valid_annotations[itr] = valid_annotations[itr] * 255
            for itr in range(FLAGS.batch_size):
                utils.save_image(valid_images[itr].astype(np.uint8),
                                 FLAGS.inf_dir,
                                 name="inp_" + str(out_itr) + "_" +
                                 str(5 + itr))
                utils.save_image(valid_annotations[itr].astype(np.uint8),
                                 FLAGS.inf_dir,
                                 name="gt_" + str(out_itr) + "_" +
                                 str(5 + itr))
                utils.save_image(pred[itr].astype(np.uint8),
                                 FLAGS.inf_dir,
                                 name="pred_" + str(out_itr) + "_" +
                                 str(5 + itr))
                print("Saved image: %d" % itr)
Пример #16
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    # train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    train_records, valid_records = scene_parsing.read_dataset(
        'data_test2/')  # mexi para rodar TESTE

    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:

        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...", ckpt.model_checkpoint_path)
    train_images, train_annotations = validation_dataset_reader.next_batch(
        FLAGS.batch_size)

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)

            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        # train_dataset_reader = dataset.BatchDatset(train_records, image_options)
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        # test_images, test_annotations = test_dataset_reader.get_random_batch(FLAGS.batch_size)

        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })

        valid_annotations = np.squeeze(valid_annotations, axis=3)

        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            # for itr in range(3):

            # 224 224 192 borda
            # 0   0   0   preto
            # 225 0   0   vermelho
            # Convertendo para cores

            mask_bool = valid_annotations[itr] == 1
            mask_ann = np.zeros(shape=(256, 256))
            mask_ann[mask_bool] = 255
            valid_annotations_3c = np.zeros(shape=(256, 256, 3))
            valid_annotations_3c[:, :, 0] = mask_ann

            mask_bool = pred[itr] == 1
            mask_pred = np.zeros(shape=(256, 256))
            mask_pred[mask_bool] = 255
            pred_3c = np.zeros(shape=(256, 256, 3))
            pred_3c[:, :, 0] = mask_pred

            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations_3c.astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred_3c.astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
Пример #17
0
def main(argv=None):

    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")
    c_gt = tf.placeholder(tf.int32, shape=[None, 2], name="c_groundtruth")

    print("setting up vgg initialized conv layers ...")

    # 定义好FCN的网络模型
    pred_annotation, logits, mean_pixel, c_pred = inference(
        image, keep_probability)

    print("logits1 ", np.shape(tf.squeeze(annotation, squeeze_dims=[3])))
    print("labels1 ", np.shape(logits))
    print("logits1 ", np.shape(c_gt))
    print("labels2 ", np.shape(c_pred))
    # 定义损失函数,这里使用交叉熵的平均值作为损失函数
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy"))) + tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(
                logits=c_pred, labels=c_gt, name='c_entropy'))
    print("loss", np.shape(loss))
    # 定义优化器
    trainable_var = tf.trainable_variables()

    if debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    # 加载数据集
    print("Setting up image reader...")

    train_records, valid_records = scene_parsing.read_dataset(
        data_dir, data_name)

    print("训练集的大小:", len(train_records))
    print("验证集的大小:", len(valid_records))

    print("Setting up dataset reader")

    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    min_loss = 10000
    ep = 0
    # 开始训练模型
    sess = tf.Session()
    print("Setting up Saver...")
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    for itr in xrange(MAX_ITERATION):
        train_images, train_annotations, train_c_gt = train_dataset_reader.next_batch(
            batch_size)
        print(np.shape(train_images), np.shape(train_annotations),
              np.shape(train_c_gt))
        feed_dict = {
            image: train_images,
            annotation: train_annotations,
            c_gt: train_c_gt,
            keep_probability: 0.85
        }
        sess.run(train_op, feed_dict=feed_dict)
        #print ("step:",itr)

        if itr % 10 == 0:
            train_loss = sess.run(loss, feed_dict=feed_dict)
            print("Step: %d, Train_loss:%g" % (itr, train_loss))
            print("Best Step: %d, Valid_loss:%g" % (ep, min_loss))

        if itr % 200 == 0:
            valid_images, valid_annotations, valid_c_gt = validation_dataset_reader.next_batch(
                batch_size)
            valid_loss = sess.run(loss,
                                  feed_dict={
                                      image: valid_images,
                                      annotation: valid_annotations,
                                      c_gt: valid_c_gt,
                                      keep_probability: 1.0
                                  })
            print("%s ---> Validation_loss: %g" %
                  (datetime.datetime.now(), valid_loss))
            if min_loss > valid_loss:
                min_loss = valid_loss
                ep = itr
                saver.save(sess, logs_dir + "model.ckpt", itr)
Пример #18
0
def main(argv=None):
    #Create placeholders:keep_probability, image, annotation
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    #Prediction
    pred_annotation, logits = inference(image, keep_probability)

    #Tensorboard visualization
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    trainable_var = tf.trainable_variables()  #得到所有需要优化的参数

    #Loss function#softma交叉熵损失函数
    loss = tf.reduce_mean(
        (tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                        labels=tf.squeeze(
                                                            annotation,
                                                            squeeze_dims=[3]),
                                                        name="entropy")
         ))  #+tf.contrib.layers.l2_regularizer(.5)(trainable_var)
    tf.summary.scalar("entropy", loss)
    print("loss=", loss)

    # print("trainable_var=",trainable_var)

    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()
    print("summary_op", summary_op)
    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(
        FLAGS.data_dir)  #返回数量(训练集、验证集)读

    print(len(train_records))  #6509
    print(len(valid_records))  #886

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
        print("train_dataset_reader=", train_dataset_reader)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())  #初始化模型的参数

    #    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    #    if ckpt and ckpt.model_checkpoint_path:
    #        saver.restore(sess, ckpt.model_checkpoint_path)
    #        print("Model restored...")

    #Initialize the array
    loss_train = []
    acc_train = []
    loss_valid = []
    acc_valid = []

    if FLAGS.mode == "train":

        for itr in xrange(MAX_ITERATION):
            #Every time reading FLAGS.batch_size_train pictures on the training set
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size_train)  #2
            train_annotations = train_annotations / 255  #0-1
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            sess.run(train_op, feed_dict=feed_dict)  #训练的train_op是反向反向传播
            train_acc = tf.reduce_mean(
                tf.cast(tf.equal(pred_annotation, train_annotations),
                        tf.float32))
            train_loss, summary_str, train_acc = sess.run(
                [loss, summary_op, train_acc], feed_dict=feed_dict)
            addloss = sess.run(loss, feed_dict=feed_dict)
            if itr % 1 == 0:
                #Accurary on train set

                loss_train.append(train_loss)
                acc_train.append(train_acc)

                print('Step: ' + str(itr) + ',' + ' ' + 'Train_loss: ' +
                      str(addloss) + ',' + ' ' + 'Accuracy on train: ' +
                      str(train_acc))
                summary_writer.add_summary(summary_str, itr)

            if itr % 100 == 0:
                #Randomly read FLAGS.batch_size_valid pictures on the validation set
                valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
                    FLAGS.batch_size_valid)  #7
                valid_annotations = valid_annotations / 255  #0-1
                #Accurary on valication set
                valid_acc = tf.reduce_mean(
                    tf.cast(tf.equal(pred_annotation, valid_annotations),
                            tf.float32))

                #Loss and accurary on valication set
                valid_loss, valid_acc = sess.run(
                    [loss, valid_acc],
                    feed_dict={
                        image: valid_images,
                        annotation: valid_annotations,
                        keep_probability: 1.0
                    })

                loss_valid.append(valid_loss)
                acc_valid.append(valid_acc)

                print("%s ---> " % datetime.datetime.now())
                print('--->Step: ' + str(itr) + ', ' + 'valid_loss: ' +
                      str(valid_loss) + ', ' + 'Accuracy on valid: ' +
                      str(valid_acc))

                #Save model in folder of FLAGS.logs_dir
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
Пример #19
0
def main(argv=None):

    if FLAGS.mode == "fpga":
        FLAGS.batch_size = 1
        keep_probability = tf.constant(1.0, name="keep_probabilty")
        image = tf.placeholder(tf.float32,
                               shape=[1, IMAGE_SIZE, IMAGE_SIZE, 3],
                               name="input_image")
        annotation = tf.placeholder(tf.int32,
                                    shape=[1, IMAGE_SIZE, IMAGE_SIZE, 1],
                                    name="annotation")
        pred_annotation, logits = inference(image, keep_probability)

        print("Setting up image reader...")
        train_records, valid_records = scene_parsing.read_dataset(
            FLAGS.data_dir)

        print("Setting up dataset reader")
        image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
        validation_dataset_reader = dataset.BatchDatset(
            valid_records, image_options)

        sess = tf.Session()

        print("Setting up Saver...")
        saver = tf.train.Saver()
        summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

        print("Session graph Before management")
        for w in sess.graph.get_operations():
            print(w.name, w.inputs)

        outputnames = ['inference/prediction']

        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess,  # The session is used to retrieve the weights
            tf.get_default_graph().as_graph_def(
            ),  # The graph_def is used to retrieve the nodes 
            outputnames  # The output node names are used to select the usefull nodes
        )
        inference_graph_def = tf.graph_util.remove_training_nodes(
            output_graph_def,
            outputnames  # The output node names are used to select the usefull nodes
        )

        pure_inference = tf.graph_util.extract_sub_graph(
            inference_graph_def, outputnames)
        print("pure inference")
        for w in pure_inference.node:
            print(w.name, w.op, w.input)

        def load_graph(graph_def):
            with tf.Graph().as_default() as graph:
                # The name var will prefix every op/nodes in your graph
                # Since we load everything in a new graph, this is not needed
                tf.import_graph_def(graph_def)
            return graph

        graph = load_graph(pure_inference)
        print("Session graph Before management")
        for w in graph.get_operations():
            print(w.name, w.inputs)

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

            names = [op.name for op in sess.graph.get_operations()]
            print(names)
            print(image.name)
            inputimage = None
            for op in sess.graph.get_operations():
                if op.name.find("input_image") >= 0:
                    inputimage = op.outputs[0]
            valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
                FLAGS.batch_size)
            print(inputimage)
            print(valid_images)
            feed_dict = {inputimage: valid_images}

            print("test", image
                  in [op.name for op in sess.graph.get_operations()])
            #pred = sess.run(pred_annotation, feed_dict=feed_dict)
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            #pred = np.squeeze(pred, axis=3)

            (graph, schedule) = tt.from_tfgraph_to_fpga_code(
                sess.graph,
                schedulefilename="fcn.txt",
                fpgacode="fcn.json",
                outputpng="fcn.png",
                dsp=28,
                memory=4,
                DDR=256,
                victor=True)

            tensors, blobs = dt.live_cut(graph, schedule)
            print("LIVE CUT")
            print(len(tensors), tensors)

            alive = sess.run(tensors, feed_dict=feed_dict)

            print("LIVE CUT")
            for name, (step, namet, tensor) in blobs.items():
                print(name, step, namet, tensor, alive[step].shape)

        sys.exit(0)

    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[1, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[1, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        print("trainable vars")
        for var in trainable_var:
            print(var)
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })

        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
    elif False and FLAGS.mode == "fpga":

        FLAGS.batch_size = 1
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        feed_dict = {
            image: valid_images,
            annotation: valid_annotations,
            keep_probability: 1.0
        }
        pred = sess.run(pred_annotation, feed_dict=feed_dict)

        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        with tf.sess.as_default() as sess:

            (graph, schedule) = tt.from_tfgraph_to_fpga_code(
                sess.graph,
                schedulefilename="fcn.txt",
                fpgacode="fcn.json",
                outputpng="fcn.png",
                dsp=28,
                memory=4,
                DDR=256,
                victor=True)

            tensors, blobs = dt.live_cut(graph, schedule)
            print("LIVE CUT")
            print(len(tensors), tensors)

            alive = sess.run(tensors,
                             feed_dict={
                                 image: valid_images,
                                 annotation: valid_annotations,
                                 keep_probability: 1.0
                             })

            print("LIVE CUT")
            for name, (step, namet, tensor) in blobs.items():
                print(name, step, namet, tensor, alive[step].shape)
Пример #20
0
def main(argv=None):
	keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
	image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
	annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

	pred_annotation, logits = fcn(image, keep_probability)
	# tf.summary.image("input_image", image, max_outputs=2)
	# tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2)
	# tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2)

	loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
		logits=logits,
		labels=tf.squeeze(annotation, squeeze_dims=[3]),
		name="entropy"
	)))
	tf.summary.scalar("entropy", loss)

	trainable_var = tf.trainable_variables()
	if FLAGS.debug:
		for var in trainable_var:
			utils.add_to_regularization_and_summary(var)
	train_op = train(loss, trainable_var)

	print("Setting up summary op...")
	summary_op = tf.summary.merge_all()

	print("Setting up image reader...")
	train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
	print(len(train_records))
	print(len(valid_records))

	print("Setting up dataset reader")
	image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
	if FLAGS.mode == 'train':
		train_dataset_reader = dataset.BatchDatset(train_records, image_options)
	validation_dataset_reader = dataset.BatchDatset(valid_records, image_options)

	config = tf.ConfigProto(allow_soft_placement=True,
	                        log_device_placement=False)
	config.gpu_options.allow_growth = True
	sess = tf.Session(config=config)

	print("Setting up Saver...")
	saver = tf.train.Saver()
	summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

	sess.run(tf.global_variables_initializer())
	ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
	if ckpt and ckpt.model_checkpoint_path:
		saver.restore(sess, ckpt.model_checkpoint_path)
		print("Model restored...")

	if FLAGS.mode == "train":
		for itr in xrange(MAX_ITERATION):
			train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
			feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85}

			sess.run(train_op, feed_dict=feed_dict)

			if itr % 10 == 0:
				train_loss, summary_str, train_pred, logits_eval = sess.run(
					[loss, summary_op, pred_annotation, logits], feed_dict=feed_dict)

				train_annotations = np.squeeze(train_annotations, axis=3)
				train_pred = np.squeeze(train_pred, axis=3)
				sum_annotation = 0
				sum_pred_annotation = 0
				for index in range(FLAGS.batch_size):
					sum_annotation += np.sum(train_annotations[index])

					rows, cols = np.shape(train_pred[index])
					for i in range(rows):
						for j in range(cols):
							if train_pred[index][i, j] == 1 and train_annotations[index][i, j] == 1:
								sum_pred_annotation += 1

				acc = float(sum_pred_annotation) / sum_annotation

				print("Step: %d, Train_loss: %g, ACC: %f" % (itr, train_loss, acc))
				with open(os.path.join(FLAGS.logs_dir, 'train_log.txt'), 'a') as f:
					f.write("Step: %d, Train_loss: %g, ACC: %f" % (itr, train_loss, acc) + '\n')
				summary_writer.add_summary(summary_str, itr)

			if itr % 500 == 0:
				valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
				feed_dict = {image: valid_images, annotation: valid_annotations, keep_probability: 1.0}
				valid_loss, valid_pred = sess.run(
					[loss, pred_annotation],
					feed_dict=feed_dict
				)
				valid_annotations = np.squeeze(valid_annotations, axis=3)
				valid_pred = np.squeeze(valid_pred, axis=3)

				sum_pred = 0
				sum_annotation = 0
				sum_pred_annotation = 0

				for index in range(FLAGS.batch_size):
					sum_pred += np.sum(valid_pred[index])
					sum_annotation += np.sum(valid_annotations[index])

					rows, cols = np.shape(valid_pred[index])
					for i in range(rows):
						for j in range(cols):
							if valid_pred[index][i, j] == 1 and valid_annotations[index][i, j] == 1:
								sum_pred_annotation += 1
				acc = float(sum_pred_annotation) / sum_annotation
				with open(os.path.join(FLAGS.logs_dir, 'train_log.txt'), 'a') as f:
					f.write(
						"%s ---> Validation_loss: %g     acc: %f" % (datetime.datetime.now(), valid_loss, acc) + '\n')
				print("%s ---> Validation_loss: %g     acc: %f" % (datetime.datetime.now(), valid_loss, acc))
				saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

	elif FLAGS.mode == "test":
		if FLAGS.subset == "train":
			train_dataset_reader = dataset.BatchDatset(train_records, image_options)

		num_iter = int(math.ceil(float(FLAGS.data_number) / FLAGS.batch_size))

		total_sum_annotation = 0
		total_sum_pred_annotation = 0

		for number in range(num_iter):

			sum_annotation = 0
			sum_pred_annotation = 0

			if FLAGS.subset == "validation":
				images_to_check, annotation_to_check = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
			elif FLAGS.subset == "train":
				images_to_check, annotation_to_check = train_dataset_reader.next_batch(FLAGS.batch_size)
			feed_dict = {image: images_to_check, annotation: annotation_to_check, keep_probability: 1.0}
			pred = sess.run(
				pred_annotation,
				feed_dict=feed_dict
			)
			annotation_to_check = np.squeeze(annotation_to_check, axis=3)
			pred = np.squeeze(pred, axis=3)

			for index in range(FLAGS.batch_size):
				sum_annotation += np.sum(pred[index])
				total_sum_annotation += np.sum(pred[index])
				rows, cols = np.shape(pred[index])
				for i in range(rows):
					for j in range(cols):
						if annotation_to_check[index][i, j] == 1 and pred[index][i, j] == 1:
							sum_pred_annotation += 1
							total_sum_pred_annotation += 1
			acc = float(sum_pred_annotation) / sum_annotation

			print("step:   " + str(number) + "               accuracy:    " + str(acc))

			# choose how many picture to show
			if number >= 0:
				for itr in range(FLAGS.batch_size):
					utils.save_image(
						images_to_check[itr].astype(np.uint8),
						FLAGS.logs_dir,
						name="inp_" + str(number)
					)
					utils.save_image(
						annotation_to_check[itr].astype(np.uint8) * 255.0,
						FLAGS.logs_dir,
						name="gt_" + str(number)
					)
					utils.save_image(
						pred[itr].astype(np.uint8) * 255.0,
						FLAGS.logs_dir,
						name="pred_" + str(number)
					)
					print("Saved image: %d" % number)

		total_acc = float(total_sum_pred_annotation) / total_sum_annotation

		print("total_acc:         " + str(total_acc))
		with open(os.path.join(FLAGS.logs_dir, 'eval_log.txt'), 'a') as f:
			f.write("number_data:       " + str(FLAGS.data_number) + '\n')
			f.write("test on:           " + str(FLAGS.subset) + '\n')
			f.write("total_acc:         " + str(total_acc) + '\n')
Пример #21
0
def main(argv=None):
    #是否需要申明把简单的计算放在cpu下.
    with tf.Graph().as_default(), tf.device('/cpu:0'):
        regularization = tf.Variable(0, dtype=tf.float32)

        keep_probability = tf.placeholder(tf.float32,
                                          name="keep_probabilty")  # dropout
        image = tf.placeholder(tf.float32,
                               shape=[None, None, None, 3],
                               name="input_image")  # 输入
        annotation = tf.placeholder(tf.int32,
                                    shape=[None, None, None, 1],
                                    name="annotation")  # label
        mean = tf.placeholder(tf.float32, name='mean')  # 给mean一个占位符.
        # grads = tf.placeholder(tf.float32, shape=[None])
        pred_annotation, logits, softmax = inference(image, keep_probability,
                                                     mean)  # logits=resize_F8

        tf.summary.image("input_image", image, max_outputs=2)
        tf.summary.image("ground_truth",
                         tf.cast(annotation, tf.uint8),
                         max_outputs=2)
        tf.summary.image("pred_annotation",
                         tf.cast(pred_annotation, tf.uint8),
                         max_outputs=2)

        loss = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=logits,
                labels=tf.squeeze(annotation, squeeze_dims=[3]),
                name="entropy"))
        trainable_var = tf.trainable_variables(
        )  # Variable被收集在名为tf.GraphKeys.VARIABLES的colletion中
        # opt = tf.train.AdamOptimizer(FLAGS.learning_rate)

        # with tf.device('/gpu:0'):
        train_op1 = train(loss, trainable_var)
        with tf.device('/gpu:1'):
            train_op2 = train(loss, trainable_var)

        # train_op = train(grads, opt)

        tf.summary.scalar("entropy", loss)  # train val公用一个loss节点运算.

        print("Setting up summary op...")

        print("Setting up image reader...")
        train_records, valid_records = scene_parsing.read_dataset(
            FLAGS.data_dir)  # 数据的转换输入.
        print(len(train_records))
        print(len(valid_records))

        print("Setting up dataset reader")
        image_options = {
            'resize': True,
            'resize_size': IMAGE_RESIZE
        }  # 将IMAGE_SIZE大小作为一个batch

        if FLAGS.mode == 'train':
            train_dataset_reader = dataset.BatchDatset(train_records,
                                                       image_options)
        validation_dataset_reader = dataset.BatchDatset(
            valid_records, image_options)  # 是train模式也把validation载入进来.

        sess = tf.Session()
        print("Setting up Saver...")
        saver = tf.train.Saver()  # 声明tf.train.Saver()类 用于存储模型.
        summary_op = tf.summary.merge_all()  # 汇总所有summary.
        summary_writer_train = tf.summary.FileWriter(FLAGS.logs_dir + '/train',
                                                     sess.graph)
        summary_writer_val = tf.summary.FileWriter(FLAGS.logs_dir +
                                                   '/val')  # 这里不需要再加入graph.

        sess.run(tf.global_variables_initializer())

        ckpt = tf.train.get_checkpoint_state(
            FLAGS.logs_dir)  # 生成check_point,下次可以从check_point继续训练
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("Model restored...")

        if FLAGS.mode == "train":

            mymean = [73.9524, 73.9524, 73.9524]  # 这里要改.
            for itr in xrange(MAX_ITERATION):  # 修改itr数值,接着之前的模型数量后继续训练.
                train_images, train_annotations = train_dataset_reader.next_batch(
                    FLAGS.batch_size)
                feed_dict = {
                    image: train_images,
                    annotation: train_annotations,
                    keep_probability: 0.85,
                    mean: mymean
                }
                with tf.device('/gpu:0'):
                    sess.run(train_op1, feed_dict=feed_dict)
                # gra1 = sess.run(grads1, feed_dict=feed_dict)
                # avg = average_gradients([gra0, gra1])
                # opt.apply_gradients(avg)
                # sess.run(train_op, feed_dict=avg)

                if itr % 10 == 0:
                    train_loss, summary_str = sess.run([loss, summary_op],
                                                       feed_dict=feed_dict)

                    summary_writer_train.add_summary(summary_str, itr)
                    print("Step: %d, Train_loss:%g" % (itr, train_loss))

                if itr % 100 == 0:  # 每训500次测试一下验证集.

                    valid_images, valid_annotations = validation_dataset_reader.next_batch(
                        FLAGS.batch_size)
                    valid_loss, summary_str = sess.run(
                        [loss, summary_op],
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0,
                            mean: mymean
                        })  # 计算新节点loss_valid的值.
                    summary_writer_val.add_summary(summary_str, itr)
                    print("%s ---> Validation_loss: %g" %
                          (datetime.datetime.now(), valid_loss))
                    saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
            summary_writer_val.close()
            summary_writer_train.close()

        elif FLAGS.mode == "test":
            im_2017_list = []
            global im_2017  # [5106, 15106, 3]
            global im_2017tif
            for i in range(50):
                b = im_2017tif[0:5106, i * 300:i * 300 + 300, 3]
                b = np.array([np.array([b for i in range(3)])])
                b = b.transpose(0, 2, 3, 1)
                im_2017_list.append(b)
            im_2017_list.append(
                np.array([
                    np.array(
                        [im_2017tif[0:5106, 15000:15106, 3] for i in range(3)])
                ]).transpose(0, 2, 3, 1))

            allImg = []
            mymean = [73.9524, 73.9524, 73.9524]

            for n, im_2017_part in enumerate(im_2017_list):
                feed_dict_test = {
                    image: im_2017_part,
                    keep_probability: 1.0,
                    mean: mymean
                }
                a = sess.run(pred_annotation, feed_dict=feed_dict_test)
                a = np.mean(a, axis=(0, 3))
                allImg.append(a)
            result = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
            tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/deep_1025_2w.tif',
                        result)
Пример #22
0
def main(argv=None):
    # dropout保留率
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    # 图像占坑
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    # 标签占坑
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    # 预测一个batch图像  获得预测图[b,h,w,c=1]  结果特征图[b,h,w,c=151]
    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)
    # 空间交叉熵损失函数[b,h,w,c=151]  和labels[b,h,w]    每一张图分别对比
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=tf.squeeze(annotation,
                                         axis=[3]), name="entropy")))
    tf.summary.scalar("entropy", loss)

    raw_output_up = tf.image.resize_bilinear(logits, tf.shape(image)[0:2, ])
    raw_output_up_squeeze = tf.squeeze(raw_output_up, axis=0)
    raw_output_up_squeeze = tf.nn.softmax(raw_output_up_squeeze, )
    probabilities = tf.nn.softmax(logits)

    # 返回需要训练的变量列表
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)

    # 传入损失函数和需要训练的变量列表
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    # 生成绘图数据
    summary_op = tf.summary.merge_all()

    if FLAGS.mode != "test":
        print("Setting up image reader...")
        # data_dir = Data_zoo/MIT_SceneParsing/
        # training: [{image: 图片全路径, annotation:标签全路径, filename:图片名字}] [{}][{}]
        test_records, valid_records = scene_parsing.read_dataset(
            FLAGS.data_dir)
        print(len(test_records))  # 长度
        # print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}

    if FLAGS.mode == 'train':
        # 读取图片 产生类对象 其中包含所有图片信息
        train_dataset_reader = dataset.BatchDatset(test_records, image_options)

    if FLAGS.mode != "test":
        validation_dataset_reader = dataset.BatchDatset(
            valid_records, image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)
    sess.run(tf.global_variables_initializer())

    if fine_tuning:
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)  # 训练断点回复
        if ckpt and ckpt.model_checkpoint_path:  # 如果存在checkpoint文件 则恢复sess
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("Model restored...")

    if FLAGS.mode == "train":
        for itr in range(MAX_ITERATION):
            # 读取下一batch
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            # 迭代优化需要训练的变量
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                # 迭代10次打印显示
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(
                    summary_str,
                    itr)  #调用train_writer的add_summary方法将训练过程以及训练步数保存

            if itr % 500 == 0:
                # 迭代500 次验证
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                # 保存模型
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        # 可视化
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        # pred_annotation预测结果图
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":
        file_list = []
        # ./ number_segment_2/img.jpg
        # 加入文件列表  包含所有图片文件全路径+文件名字  如 Data_zoo/MIT_SceneParsing/ADEChallengeData2016/images/training/hi.jpg
        file_glob = os.path.join(FLAGS.data_dir + 'ADEChallengeData2016/',
                                 "images/", 'validation/', '*.' + 'jpg')
        file_list.extend(glob.glob(file_glob))

        print('Start Transport')

        for f in file_list:
            filename = os.path.splitext(f.split("/")[-1])[0]
            filename = os.path.splitext(filename.split("_")[-1])[0]
            image_orignal = misc.imread(f)
            # resize_image = misc.imresize(image_orignal,
            #                              [224, 224], interp='nearest')
            image_final = np.array([image_orignal])
            pred, probabilities_np = sess.run([pred_annotation, probabilities],
                                              feed_dict={
                                                  image: image_final,
                                                  keep_probability: 1.0
                                              })
            image_pred = np.squeeze(pred)
            image1 = image_orignal
            # softmax = probabilities_np.squeeze()
            softmax = probabilities_np.transpose((2, 0, 1, 3))

            # softmax_to_unary函数的输入数据为概率值的负对数
            unary = softmax_to_unary(softmax)

            # 输入数据应该是 C-continious -- 这里采用 Cython 封装器
            unary = np.ascontiguousarray(unary)

            d = dcrf.DenseCRF(image1.shape[0] * image1.shape[1], 256)

            n_labels = len(set(image_pred.flat))
            unary = unary_from_labels(image_pred,
                                      n_labels,
                                      gt_prob=0.7,
                                      zero_unsure=0)
            d.setUnaryEnergy(unary)

            # 对空间独立的小分割区域进行潜在地惩罚 - 促使生成更多连续的分割区域.
            feats = create_pairwise_gaussian(sdims=(5, 5),
                                             shape=image1.shape[:2])

            d.addPairwiseEnergy(feats,
                                compat=3,
                                kernel=dcrf.DIAG_KERNEL,
                                normalization=dcrf.NORMALIZE_SYMMETRIC)

            # 创建与颜色相关的特征
            # 因为 CNN 的分割结果太粗糙,使用局部的颜色特征来进一步提升分割结果.
            feats = create_pairwise_bilateral(sdims=(100, 100),
                                              schan=(20, 20, 20),
                                              img=image1,
                                              chdim=2)

            d.addPairwiseEnergy(feats,
                                compat=10,
                                kernel=dcrf.DIAG_KERNEL,
                                normalization=dcrf.NORMALIZE_SYMMETRIC)
            Q = d.inference(5)

            res = np.argmax(Q, axis=0).reshape(
                (image1.shape[0], image1.shape[1]))
            utils.save_image(res.astype(np.uint8),
                             FLAGS.pic_final_dir,
                             name='img' + '_' + filename)
Пример #23
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    print("setting up vgg initialized conv layers ...")

    # 定义好FCN的网络模型
    pred_annotation, logits = inference(image, keep_probability)
    # 定义损失函数,这里使用交叉熵的平均值作为损失函数
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    # 返回需要训练的变量列表
    trainable_var = tf.trainable_variables()

    if debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    # 加载数据集
    print("Setting up image reader...")

    train_records, valid_records = scene_parsing.read_dataset(
        data_dir, data_name)

    print("训练集的大小:", len(train_records))
    print("验证集的大小:", len(valid_records))

    print("Setting up dataset reader")

    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}

    if mode == 'train':
        train_dataset_reader = dataset.BatchDatset(
            train_records, image_options)  # 读取图片 产生类对象 其中包含所有图片信息
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    # 开始训练模型
    sess = tf.Session()
    print("Setting up Saver...")
    saver = tf.train.Saver()  # 保存模型类实例化

    sess.run(tf.global_variables_initializer())  # 变量初始化
    ckpt = tf.train.get_checkpoint_state(logs_dir)
    if ckpt and ckpt.model_checkpoint_path:  # 如果存在checkpoint文件 则恢复sess
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                batch_size)
            print(np.shape(train_images), np.shape(train_annotations))
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }
            sess.run(train_op, feed_dict=feed_dict)
            print("step:", itr)

            if itr % 10 == 0:
                train_loss = sess.run(loss, feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))

                saver.save(sess, logs_dir + "model.ckpt", itr)  # 保存模型

    elif mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            batch_size)

        pred = sess.run(
            pred_annotation,
            feed_dict={
                image: valid_images,
                annotation: valid_annotations,  # 预测结果
                keep_probability: 1.0
            })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    else:  # 测试模式
        since = time.time()  # 时间模块

        test_image = misc.imread('G:\\yuantu8.jpg')
        resize_image = misc.imresize(test_image, [224, 224], interp='nearest')
        a = np.expand_dims(resize_image, axis=0)
        a = np.array(a)

        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: a,
                            keep_probability: 1.0
                        })  # 预测测试结果

        pred = np.squeeze(pred, axis=3)  # 从数组的形状中删除单维条目,即把shape中为1的维度去掉
        # utils.save_image(pred[0].astype(np.uint8), logs_dir, name="pred_" + str(5))
        utils.save_image(pred[0].astype(np.uint8),
                         'G:/2/',
                         name="pred_" + str(5))
        print("Saved image: succeed")

        time_elapsed = time.time() - since
        print('Training complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))  # 打印出来时间
Пример #24
0
def main(argv=None):
    # 定义一下regularization:
    regularization = tf.Variable(0, dtype=tf.float32)

    keep_probability = tf.placeholder(tf.float32,
                                      name="keep_probabilty")  # dropout
    image = tf.placeholder(tf.float32,
                           shape=[None, None, None, 3],
                           name="input_image")  # 输入
    annotation = tf.placeholder(tf.int32,
                                shape=[None, None, None, 1],
                                name="annotation")  # label

    mean = tf.placeholder(tf.float32, name='mean')  #给mean一个占位符.

    pred_annotation, logits = inference(image, keep_probability, mean)
    # 要对logits做一个softmax回归,就可得到预测的概率分布情况.    P75.

    loss = tf.reduce_mean(  #tf.nn.sparse_softmax_cross_entropy_with_logits()函数,第一个参数是网络前向传播不包含softmax层的计算结果.P98
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                       labels=tf.squeeze(
                                                           annotation,
                                                           squeeze_dims=[3]),
                                                       name="entropy"))
    trainable_var = tf.trainable_variables(
    )  # Variable被收集在名为tf.GraphKeys.VARIABLES的colletion中
    train_op = train(loss, trainable_var)

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(
        FLAGS.data_dir)  # 数据的转换输入.
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {
        'resize': True,
        'resize_size': IMAGE_RESIZE
    }  # 将IMAGE_SIZE大小作为一个batch
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)  # 这两个是对象定义
    # 定义了train_dataset_reader
    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()  # 声明tf.train.Saver()类 用于存储模型.

    sess.run(tf.global_variables_initializer())

    ckpt = tf.train.get_checkpoint_state(
        FLAGS.logs_dir)  # 生成check_point,下次可以从check_point继续训练
    if ckpt and ckpt.model_checkpoint_path:  # 这两行的作用是:tf.train.get_checkpoint_state()函数通过checkpoint文件(它存储所有模型的名字)找到目录下最新的模型.
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        mymean = train_dataset_reader._read_images()  # 强行运行这个函数,把mean传过来.
        #train_dataset_reader 调用._read_images()  需要在里面修改mean是否运算和retrun.
        print("mean:")
        print(mymean)
        mymean = [73.8613, 73.8613, 73.8613]
        for itr in xrange(MAX_ITERATION):
            print(mymean)
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85,
                mean: mymean
            }
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss = sess.run(loss, feed_dict=feed_dict)
                print(logits.shape)
                #print(labels.shape)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                # summary_writer.add_summary(summary_str, itr)
            if itr % 500 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        # filename = '/home/lenovo/2Tdisk/Wkyao/_/test2.jpg'
        # valid_image = misc.imread(filename)
        # valid_image = np.array([np.array([valid_image for i in range(3)])])
        # valid_image = valid_image.transpose(0, 2, 3, 1)
        # print(valid_image.shape)

        im_2017_list = []
        global im_2017  # im_2015
        for i in range(30):
            b = im_2017[0:5106, i * 500:i * 500 + 500, 3]
            b = np.array([np.array([b for i in range(3)])])
            #print(b.shape)
            b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
            # print (im_2017.shape)
        im_2017_list.append(
            np.array([
                np.array([im_2017[0:5106, 15000:15106, 3] for i in range(3)])
            ]).transpose(0, 2, 3, 1))

        mymean = [73.9524, 73.9524, 73.9524]  #这里要改.
        allImg = []
        allImg_soft = []

        for n, im_2017_part in enumerate(im_2017_list):
            feed_dict_valid = {
                image: im_2017_part,
                keep_probability: 1.0,
                mean: mymean
            }

            #不使用crf:
            a = sess.run(pred_annotation, feed_dict=feed_dict_valid)
            print(type(a))
            a = np.mean(a, axis=(0, 3))
            allImg.append(a)

            #使用crf:
            #soft = sess.run(logits, feed_dict=feed_dict_valid)
            #运行 sess.run(logits, feed_dict=feed_dict_valid) 得到logits,即网络前向运算并softmax为概率分布后的结果.
            #soft = np.mean(soft, axis=0).transpose(2, 0, 1)
            #im_2017_mean = np.mean(im_2017_list[n], axis = 0)
            #c = crf.crf(im_2017_mean, soft)
            #allImg_soft.append(c)     #保存整张soft图.

        result = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
Пример #25
0
def main(argv=None):
    #Create placeholders:keep_probability, image, annotation
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE_r, IMAGE_SIZE_c, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE_r, IMAGE_SIZE_c, 1],
                                name="annotation")

    #Prediction
    pred_annotation, logits = inference(image, keep_probability)

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)

    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': False, 'resize_size': IMAGE_SIZE}

    #read dataset of validation
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())

    #load model
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "visualize":

        #valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
        #filePath_an = "E:\\data\\Xshell\\test\\annotations\\row24col78.png"
        #filePath_im = "E:\\data\\Xshell\\test\\images\\row24col78.png"
        filePath_GF = "./logs/input/1000/row2col3-2.png"  # read GF1 groundTruth.png
        filePath_GF_gt = "./logs/input/1000/row2col3-1.png"  # read GF1 image.png

        valid_images = cv2.imread(filePath_GF, -1)
        valid_annotations = cv2.imread(filePath_GF_gt, -1)

        valid_images = valid_images[np.newaxis, :]
        valid_annotations = valid_annotations[np.newaxis, :]
        valid_annotations = valid_annotations[:, :, :, np.newaxis]

        valid_annotations = valid_annotations / 255  #0-1
        #Accuracy on validation
        valid_acc = tf.reduce_mean(
            tf.cast(tf.equal(pred_annotation, valid_annotations), tf.float32))

        pred, valid_acc = sess.run(
            [pred_annotation, valid_acc],
            feed_dict={
                image: valid_images,
                annotation: valid_annotations,
                keep_probability: 1.0
            })

        print('Accuracy on valication: ' + str(valid_acc))

        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint16),
                             FLAGS.image_logs_dir,
                             name="inp_" + str(1 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint16),
                             FLAGS.image_logs_dir,
                             name="gt_" + str(1 + itr))
            utils.save_image(pred[itr].astype(np.uint16),
                             FLAGS.image_logs_dir,
                             name="pred_" + str(1 + itr))

            # scio.savemat(FLAGS.image_logs_dir, {'data': valid_images[itr]})

            print("Saved image: %d" % itr)
Пример #26
0
def main(argv=None):
    # 定义一下regularization:
    regularization = tf.Variable(0, dtype=tf.float32)

    keep_probability = tf.placeholder(tf.float32,
                                      name="keep_probabilty")  # dropout
    image = tf.placeholder(tf.float32,
                           shape=[None, None, None, 1],
                           name="input_image")  # 输入
    annotation = tf.placeholder(tf.int32,
                                shape=[None, None, None, 1],
                                name="annotation")  # label
    mean = tf.placeholder(tf.float32, name='mean')
    pred_annotation, logits, softmax = inference(image, keep_probability,
                                                 mean)  # logits=resize_F8

    print('annotation', annotation.shape)
    print('image', image.shape)

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=logits,
            # tf.nn.sparse_softmax_cross_entropy_with_logits自动对logits(resize_F8)做了softmax处理.
            labels=tf.squeeze(annotation, squeeze_dims=[3]),
            name="entropy"))
    tf.summary.scalar("entropy", loss)  # train val公用一个loss节点运算.

    trainable_var = tf.trainable_variables(
    )  # Variable被收集在名为tf.GraphKeys.VARIABLES的colletion中

    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(
        FLAGS.data_dir)  # 数据的转换输入.
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {
        'resize': False,
        'resize_size': IMAGE_RESIZE
    }  # 将IMAGE_SIZE大小作为一个batch

    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)

    sess = tf.Session()
    print("Setting up Saver...")
    saver = tf.train.Saver()  # 声明tf.train.Saver()类 用于存储模型.
    summary_op = tf.summary.merge_all()  # 汇总所有summary.
    summary_writer_train = tf.summary.FileWriter(FLAGS.logs_dir + '/train',
                                                 sess.graph)
    summary_writer_val = tf.summary.FileWriter(FLAGS.logs_dir +
                                               '/val')  # 这里不需要再加入graph.

    sess.run(tf.global_variables_initializer())

    ckpt = tf.train.get_checkpoint_state(
        FLAGS.logs_dir)  # 生成check_point,下次可以从check_point继续训练
    if ckpt and ckpt.model_checkpoint_path:  # 这两行的作用是:tf.train.get_checkpoint_state()函数通过checkpoint文件(它存储所有模型的名字)找到目录下最新的模型.
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        # mymean = train_dataset_reader._read_images()   #强行运行这个函数,把mean传过来.
        # train_dataset_reader 调用._read_images()  需要在里面修改mean是否运算和return.
        # 生成本次数据集的mean值.
        mymean = [73.9524, 73.9524, 73.9524]
        for itr in xrange(MAX_ITERATION):  # 修改itr数值,接着之前的模型数量后继续训练.
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85,
                mean: mymean
            }
            sess.run(train_op, feed_dict=feed_dict)
            if itr % 10 == 0:
                # 这里不要运算loss
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                summary_writer_train.add_summary(summary_str, itr)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))

            if itr % 100 == 0:  # 每训100次测试一下验证集.
                # valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
                # valid_loss, summary_str = sess.run([loss, summary_op], feed_dict={image: valid_images,
                #                                                                          annotation: valid_annotations,
                #                                                                          keep_probability: 1.0,
                #                                                                          mean: mymean})    #计算新节点loss_valid的值.
                # summary_writer_val.add_summary(summary_str, itr)
                # print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))

                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

        # summary_writer_val.close()
        summary_writer_train.close()

    elif FLAGS.mode == "visualize":
        # mymean = [42.11049008, 65.75782253, 74.11216841]
        mymean = [73.9524, 73.9524, 73.9524]

        validation_dataset_reader = dataset.BatchDatset(
            valid_records, image_options)
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0,
                            mean: mymean
                        })

        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":
        im_2017_list = []
        im_2017_list2 = []
        b = []
        global im_2017  # [5106, 15106, 3]
        global new_2017  # [8106, 15106, 3]
        global new_2015
        for i in range(25):
            b = new_2017[0:4000, i * 600:i * 600 + 600,
                         3]  # 训练第四通道的话,测试的时候也要换成第四通道.
            b = np.array([np.array([b for i in range(3)])])
            b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
            print(b.shape)
            '''
            # 多通道
            b = im_2017[0:5106, i * 300 : i * 300 + 300, :]
            b = np.array([b])
            # print(b.shape)
            # b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
        # im_2017_list.append(np.array([np.array([im_2017[0:5106, 15000:15106, 3] for i in range(3)])]).transpose(0, 2, 3, 1))
        #im_2017_list.append(np.array([im_2017[0:5106, 15000:15106, :]]))     # .transpose(0, 2, 3, 1))
        im_2017_list.append(np.array([im_2017[0:5106, 15000:15106, :]]))
        '''

        im_2017_list.append(
            np.array([
                np.array([new_2017[0:4000, 15000:15106, 3] for i in range(3)])
            ]).transpose(0, 2, 3, 1))
        '''
        for i in range(50):
            b = new_2017[5106:8106, i * 300:i * 300 + 300, 3]
            b = np.array([np.array([b for i in range(3)])])
            b = b.transpose(0, 2, 3, 1)
            im_2017_list2.append(b)
        
        im_2017_list2.append(
            np.array([np.array([new_2017[5106:8106, 15000:15106, 3] for i in range(3)])]).transpose(0, 2, 3, 1))
        '''
        allImg = []
        allImg2 = []
        allImg_soft = []
        allImg_crf = []
        mymean = [73.9524, 73.9524, 73.9524]

        for n, im_2017_part in enumerate(im_2017_list):
            # print(im_2017_part.shape)
            feed_dict_test = {
                image: im_2017_part,
                keep_probability: 1.0,
                mean: mymean
            }
            a = sess.run(pred_annotation, feed_dict=feed_dict_test)
            a = np.mean(a, axis=(0, 3))
            allImg.append(a)
            '''
            # Shift + Tab
            for n, im_2017_part2 in enumerate(im_2017_list2):
                # print(im_2017_part.shape)
                feed_dict_test = {image: im_2017_part2, keep_probability: 1.0, mean: mymean}
                a = sess.run(pred_annotation, feed_dict=feed_dict_test)
                a = np.mean(a, axis=(0, 3))
                allImg2.append(a)
            '''

            # 使用crf:
            soft = sess.run(softmax, feed_dict=feed_dict_test)
            # 运行 sess.run(softmax, feed_dict=feed_dict_test) 得到softmax,即网络前向运算并softmax为概率分布后的结果.
            soft = np.mean(soft, axis=0).transpose(2, 0, 1)
            # soft = soft.transpose(2, 0, 1)

            im_2017_mean = np.mean(im_2017_list[n], axis=0)

            # print (im_2017_mean.shape)     #(5106, 300, 3)
            c = crf.crf(im_2017_mean, soft)
            # print (c.shape)    #(5106, 300)
            allImg_crf.append(c)  # 保存整张soft图.
            allImg_soft.append(soft)  # 保存整张soft图.
        Crf = np.concatenate(tuple(allImg_crf),
                             axis=1)  # axis = 1 在第二个维度上进行拼接.
        softmax = np.concatenate(tuple(allImg_soft), axis=2)
        # tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/vgg/crf_vgg_1108.tif', Crf)
        np.save('/home/lenovo/2Tdisk/Wkyao/_/2017/vgg/1116_3.npy',
                softmax)  # 保存拼接的Softmax为np
        np.save(
            '/home/lenovo/2Tdisk/Wkyao/FirstSetp/ChenJ/finall/1110/1116_3.npy',
            softmax)
        res1 = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
        tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/vgg/1116_3.tif', res1)
        open_and_close(Crf)  # 膨胀操作.
Пример #27
0
def main(argv=None):
    ##########################构建网络部分####################
    # 我们首先定义网络的输入部分
    keep_probability = tf.placeholder(tf.float32, name="keep_probability")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int64,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = fcn16s_net(image, keep_probability)

    # 计算m_iou
    re_shape = tf.stack(
        [tf.shape(pred_annotation)[0], IMAGE_SIZE * IMAGE_SIZE, 1])
    annotation_new = tf.reshape(annotation, re_shape)
    pred_annotation_new = tf.reshape(pred_annotation, re_shape)
    mean_iou, endarray = tf.metrics.mean_iou(annotation_new,
                                             pred_annotation_new,
                                             NUM_OF_CLASSES)

    # 把我们需要观察的图片和生成的结果图保存下来
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # 定义损失函数
    loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3])),
                          name="entropy")

    # 把损失保存下来
    loss_summary = tf.summary.scalar("entropy", loss)

    # 获取要训练的变量
    trainable_var = tf.trainable_variables()

    # 如果是调试运行下保存变量
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    #创建把所有要保存的调试信息集中起来的操作(以备存入文件)
    print("Setting up summary op....")
    summary_op = tf.summary.merge_all()

    #################到此我们网络构建完毕#################

    #################下面我们构建数据##########
    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    # 打印出来看看数据条数是否正确
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader...")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}

    if FLAGS.mode == "train":
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)

    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)
    #################构建数据完成####################################

    ###################构建运行对话##################
    sess = tf.Session()
    print("Setting up Saver.....")
    saver = tf.train.Saver()

    # create two summary writers to show training loss and validation loss in the same graph
    # need to create two folders 'train' and 'validation' inside FLAGS.logs_dir
    train_writer = tf.summary.FileWriter(FLAGS.logs_dir + "/train", sess.graph)
    validation_writer = tf.summary.FileWriter(FLAGS.logs_dir + "validation")

    # 首先给变量初始化进行训练验证前的的准备
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    # 判断有没有checkpoint
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored .....")

    # 开始训练或者验证
    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            # 先生成batch数据
            train_images, train_annotation = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotation,
                keep_probability: 0.5
            }

            # 运行
            sess.run(train_op, feed_dict=feed_dict)

            # miou
            m_iou, array_end = sess.run(
                [mean_iou, endarray],
                feed_dict={
                    image: train_images,
                    annotation: train_annotation,
                    keep_probability: 1.0
                })
            print(m_iou)
            print(array_end)

            # 下面是保存一些能反映训练中的过程的一些信息
            if itr % 500 == 0:
                saver.save(sess, FLAGS.checkpoint_dir + "model.ckpt", itr)
    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        # 保存结果
        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.image_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.image_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.image_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
Пример #28
0
def main(argv=None):

    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")
    FM_pl = tf.placeholder(tf.float32, [])
    total_acc_pl = tf.placeholder(tf.float32, [])
    acc_pl = tf.placeholder(tf.float32, [])
    iu_pl = tf.placeholder(tf.float32, [])
    fwavacc_pl = tf.placeholder(tf.float32, [])
    # is_traing = tf.placeholder('bool')

    vgg_fcn = fcn32_vgg.FCN32VGG()
    vgg_fcn.build(image,
                  num_classes=num_classes,
                  keep_probability=keep_probability,
                  random_init_fc8=True)

    logits = vgg_fcn.upscore
    pred_annotation = vgg_fcn.pred_up
    # loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
    #                                                                       labels=tf.squeeze(annotation, squeeze_dims=[3]),
    #                                                                       name="entropy")))
    # loss_summary = tf.summary.scalar("entropy", loss)

    # trainable_var = tf.trainable_variables()
    # S_vars = [svar for svar in tf.trainable_variables() if 'weight' in svar.name]
    # l2 = tf.add_n([tf.nn.l2_loss(var) for var in S_vars])
    # # loss = loss + l2 * FLAGS.weight_decay
    # # train_op = tf.train.MomentumOptimizer(FLAGS.learning_rate, 0.9).minimize(loss + l2 * FLAGS.weight_decay)
    # train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss + l2 * FLAGS.weight_decay)
    # # train_op = train(loss, trainable_var)
    """ median-frequency re-weighting """
    # class_weights = np.array([
    #     0.5501,
    #     5.4915
    # ])
    # loss = tf.reduce_mean((tf.nn.weighted_cross_entropy_with_logits(logits=logits,
    #                                                                 targets=tf.one_hot(tf.squeeze(annotation, squeeze_dims=[3]), depth=num_classes),
    #                                                                 pos_weight=class_weights,
    #                                                                 name="entropy")))
    loss = LOSS.loss(
        logits,
        tf.one_hot(tf.squeeze(annotation, squeeze_dims=[3]),
                   depth=num_classes))
    regularization_loss = tf.add_n(
        tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    t_loss = loss + regularization_loss

    loss_summary = tf.summary.scalar("entropy", loss)
    FM_summary = tf.summary.scalar('FM', FM_pl)
    acc_total_summary = tf.summary.scalar("total_acc", total_acc_pl)
    acc_summary = tf.summary.scalar("acc", acc_pl)
    iu_summary = tf.summary.scalar("iu", iu_pl)
    fwavacc_summary = tf.summary.scalar("fwavacc", fwavacc_pl)

    train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss)
    #train_op = tf.train.MomentumOptimizer(FLAGS.learning_rate, 0.9).minimize(t_loss)
    # train_op = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(t_loss)
    summary_op = tf.summary.merge_all()

    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': False, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session(config=config)
    saver = tf.train.Saver(max_to_keep=3)

    # create two summary writers to show training loss and validation loss in the same graph
    # need to create two folders 'train' and 'validation' inside FLAGS.logs_dir
    praph_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/graph', sess.graph)
    train_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train')
    validation_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/validation')

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in range(1, MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.5
            }

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, loss_summary],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                train_writer.add_summary(summary_str, itr)

            if itr % 210 == 0:
                valid_iamges, valid_annotations = validation_dataset_reader.get_records(
                )
                val_count = 0
                total_loss = 0
                hist = np.zeros((num_classes, num_classes))
                fm = 0
                for i in range(1, 21):
                    val_images = valid_iamges[val_count:val_count +
                                              val_batch_size]
                    val_annotations = valid_annotations[val_count:val_count +
                                                        val_batch_size]
                    val_loss, val_pred_dense = sess.run(
                        [loss, logits],
                        feed_dict={
                            image: val_images,
                            annotation: val_annotations,
                            keep_probability: 1.0
                        })
                    total_loss = total_loss + val_loss
                    val_count = val_count + val_batch_size
                    hist += get_hist(val_pred_dense, val_annotations)
                    fm += get_FM(val_pred_dense, val_annotations)

                valid_loss = total_loss / 20
                FM = fm / (20 * val_batch_size)
                acc_total = np.diag(hist).sum() / hist.sum()
                acc = np.diag(hist) / hist.sum(1)
                iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) -
                                      np.diag(hist))
                freq = hist.sum(1) / hist.sum()

                # summary_st = sess.run(summary_op,feed_dict=feed_dict)

                summary_sva = sess.run(loss_summary,
                                       feed_dict={loss: valid_loss})
                summary_FM = sess.run(FM_summary, feed_dict={FM_pl: FM})
                summary_acc_total = sess.run(
                    acc_total_summary, feed_dict={total_acc_pl: acc_total})
                summary_acc = sess.run(acc_summary,
                                       feed_dict={acc_pl: np.nanmean(acc)})
                summary_iu = sess.run(iu_summary,
                                      feed_dict={iu_pl: np.nanmean(iu)})
                summary_fwavacc = sess.run(
                    fwavacc_summary,
                    feed_dict={
                        fwavacc_pl: (freq[freq > 0] * iu[freq > 0]).sum()
                    })
                print("Step: %d, Valid_loss:%g" % (itr, valid_loss))
                print(" >>> Step: %d, f1_score:%g" % (itr, FM))
                # overall accuracy
                print(" >>> Step: %d, overall accuracy:%g" % (itr, acc_total))
                print(" >>> Step: %d, mean accuracy:%g" %
                      (itr, np.nanmean(acc)))
                print(" >>> Step: %d, mean IU:%g" % (itr, np.nanmean(iu)))
                print(" >>> Step: %d, fwavacc:%g" %
                      (itr, (freq[freq > 0] * iu[freq > 0]).sum()))

                # validation_writer.add_summary(summary_st, step)
                validation_writer.add_summary(summary_sva, itr)
                validation_writer.add_summary(summary_FM, itr)
                validation_writer.add_summary(summary_acc_total, itr)
                validation_writer.add_summary(summary_acc, itr)
                validation_writer.add_summary(summary_iu, itr)
                validation_writer.add_summary(summary_fwavacc, itr)

                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

                va_images, va_annotations = validation_dataset_reader.get_random_batch(
                    20)

                pred = sess.run(pred_annotation,
                                feed_dict={
                                    image: va_images,
                                    annotation: va_annotations,
                                    keep_probability: 1.0
                                })
                va_annotations = np.squeeze(va_annotations, axis=3)
                # pred = np.squeeze(pred, axis=3)
                pred = pred * 255
                va_annotations = va_annotations * 255
                for it in range(20):
                    utils.save_image(va_images[it].astype(np.uint8),
                                     FLAGS.logs_dir,
                                     name="inp_" + str(5 + it))
                    utils.save_image(va_annotations[it].astype(np.uint8),
                                     FLAGS.logs_dir,
                                     name="gt_" + str(5 + it))
                    utils.save_image(pred[it].astype(np.uint8),
                                     FLAGS.logs_dir,
                                     name="pred_" + str(5 + it))

    elif FLAGS.mode == "visualize":
        it = 0
        valid_iamge, val_annotation = validation_dataset_reader.get_records()
        val_annotation = np.squeeze(val_annotation, axis=3)
        val_annotation = val_annotation * 255
        for filename in valid_records:
            val_image = np.array(misc.imread(filename['image']))
            val_image = np.reshape(val_image, (1, 256, 256, 3))
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: val_image,
                                keep_probability: 1.0
                            })
            pred = pred * 255
            # pred = sess.run(pred_annotation, feed_dict={image: val_image,
            #                                         keep_probability:1.0})
            utils.save_image(pred[0].astype(np.uint8),
                             FLAGS.logs_dir + 'pred01',
                             name=os.path.splitext(
                                 filename['image'].split("/")[-1])[0])
            utils.save_image(val_annotation[it].astype(np.uint8),
                             FLAGS.logs_dir + 'gt01',
                             name=os.path.splitext(
                                 filename['annotation'].split("/")[-1])[0])
            it = it + 1
Пример #29
0
    def init_network(self, random_filenames):
        # Create DB entries.
        if self.db_logging:
            self.conn = psycopg2.connect(
                "dbname=fcn_rgb user=dnn_user password=pgpswd")
            self.cur = self.conn.cursor()
            self.cur.execute(
                "INSERT INTO experiment (name, description) VALUES (%s, %s)",
                (self.run_name, self.run_descr))
            self.cur.execute("SELECT MAX(id) FROM experiment;")
            r = self.cur.fetchone()
            self.run_id = int(r[0])
            self.conn.commit()

        # create newtork
        self.keep_probability = tf.placeholder(tf.float32,
                                               name="keep_probabilty")
        self.image = tf.placeholder(
            tf.float32,
            shape=[None, self.image_height, self.image_width, 3],
            name="input_image")
        self.annotation = tf.placeholder(
            tf.int32,
            shape=[None, self.image_height, self.image_width, 1],
            name="annotation")

        self.pred_annotation, self.logits = self.inference(
            self.image, self.keep_probability)
        tf.summary.image("input_image", self.image, max_outputs=2)
        tf.summary.image("ground_truth",
                         tf.cast(self.annotation, tf.uint8),
                         max_outputs=2)
        tf.summary.image("pred_annotation",
                         tf.cast(self.pred_annotation, tf.uint8),
                         max_outputs=2)
        self.loss = tf.reduce_mean(
            (tf.nn.sparse_softmax_cross_entropy_with_logits(
                self.logits,
                tf.squeeze(self.annotation, squeeze_dims=[3]),
                name="entropy")))
        tf.summary.scalar("training_loss", self.loss)

        # Define the train/val accuracy variables.
        # self.train_accuracy = tf.Variable(0)
        # self.val_accuracy = tf.Variable(0)

        # tf.summary.scalar("training_accuracy", self.train_accuracy)
        # tf.summary.scalar("validation_accuracy", self.val_accuracy)

        self.trainable_var = tf.trainable_variables()
        if self.FLAGS.debug:
            for var in self.trainable_var:
                utils.add_to_regularization_and_summary(var)
            self.train_op = self.train(self.loss, self.trainable_var)

        print("Setting up summary op...")
        self.summary_op = tf.summary.merge_all()

        self.val_loss_sum_op = tf.summary.scalar("validation_loss", self.loss)

        print("Setting up image reader...")
        self.train_records, self.valid_records = \
          scene_parsing.read_dataset(self.FLAGS.data_dir, random_filenames)
        print(len(self.train_records))
        print(len(self.valid_records))
        self.max_iterations = self.max_epochs * len(self.train_records) + 1

        print("Setting up dataset reader")
        image_options = {
            'resize': self.image_resize,
            'image_height': self.image_height,
            'image_width': self.image_width
        }
        if self.FLAGS.mode == 'train' or self.FLAGS.mode == 'visualize':
            self.train_dataset_reader = dataset.BatchDatset(
                self.train_records, self.FLAGS.batch_size, image_options)
            self.train_dataset_reader.start()
            # Wait for first images to load
            self.train_dataset_reader.wait_for_images()

        self.validation_dataset_reader = dataset.BatchDatset(
            self.valid_records, self.FLAGS.batch_size, image_options)
        self.validation_dataset_reader.start()
        # Wait for first images to load
        self.validation_dataset_reader.wait_for_images()

        self.sess = tf.Session()

        print("Setting up Saver...")
        self.saver = tf.train.Saver(keep_checkpoint_every_n_hours=1)
        self.summary_writer = tf.summary.FileWriter(self.FLAGS.logs_dir,
                                                    self.sess.graph)

        self.sess.run(tf.global_variables_initializer())

        print("Restoring model.")
        ckpt = tf.train.get_checkpoint_state(self.FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            self.saver.restore(self.sess, ckpt.model_checkpoint_path)
            print("Model restored...")
Пример #30
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # ignore label -1
    sa = tf.squeeze(annotation, squeeze_dims=[3])
    if ignore_label is not None:
        print("Ignore Label {}".format(ignore_label))
        mask = tf.not_equal(sa, ignore_label)
        logits_b = tf.boolean_mask(logits, mask)
        sa_b = tf.boolean_mask(sa, mask)
    else:
        logits_b = logits
        sa_b = sa

    loss = tf.reduce_mean(
        (tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits_b,
                                                        labels=sa_b,
                                                        name="entropy")))
    tf.summary.scalar("entropy", loss)

    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
def main(argv=None):

    # 1. input placeholders
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=(None, IMAGE_SIZE, IMAGE_SIZE, 3),
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=(None, IMAGE_SIZE, IMAGE_SIZE, 1),
                                name="annotation")

    # 2. construct inference network
    pred_annotation, logits, net = inference(image, keep_probability)
    tf.summary.image("input_image", image, max_outputs=2)
    tf.summary.image("ground_truth",
                     tf.cast(annotation, tf.uint8),
                     max_outputs=2)
    tf.summary.image("pred_annotation",
                     tf.cast(pred_annotation, tf.uint8),
                     max_outputs=2)

    # 3. loss measure
    loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits,
        labels=tf.squeeze(annotation, squeeze_dims=[3]),
        name="entropy")))
    tf.summary.scalar("entropy", loss)

    # 4. optimizing
    trainable_var = tf.trainable_variables()
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var, net['global_step'])

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()

    print("Setting up image reader from ", FLAGS.data_dir, "...")
    train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir)
    print("data dir:", FLAGS.data_dir)
    print("train_records length :", len(train_records))
    print("valid_records length :", len(valid_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(train_records,
                                                   image_options)
    validation_dataset_reader = dataset.BatchDatset(valid_records,
                                                    image_options)

    sess = tf.Session()

    print("Setting up Saver...")
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)

    # 5. paramter setup
    # 5.1 init params
    sess.run(tf.global_variables_initializer())
    # 5.2 restore params if possible
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    # 6. train-mode
    if FLAGS.mode == "train":

        global_step = sess.run(net['global_step'])

        for itr in xrange(global_step, MAX_ITERATION):
            # 6.1 load train and GT images
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            #print("train_image:", train_images.shape)
            #print("annotation :", train_annotations.shape)

            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }
            # 6.2 traininging

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                global_step = sess.run(net['global_step'])
                saver.save(sess,
                           FLAGS.logs_dir + "model.ckpt",
                           global_step=global_step)

    # test-mode
    elif FLAGS.mode == "visualize":

        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            annotation: valid_annotations,
                            keep_probability: 1.0
                        })
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":  # heejune added
        print(">>>>>>>>>>>>>>>>Test mode")
        validation_dataset_reader.reset_batch_offset(0)
        #print(">>>>>>>>>>>>>>>>Test mode")
        for itr1 in range(validation_dataset_reader.get_num_of_records() // 2):
            #print(">>>>>>>>>>>>>>>>Test mode")
            valid_images, valid_annotations = validation_dataset_reader.next_batch(
                FLAGS.batch_size)
            pred, logits = sess.run(
                [pred_annotation, logits],
                feed_dict={
                    image: valid_images,
                    annotation: valid_annotations,
                    keep_probability: 1.0
                })
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            logits = np.squeeze(logits)
            pred = np.squeeze(pred)
            print("logits shape:", logits.shape)
            np.set_printoptions(threshold=np.inf)
            print("logits:", logits)
            for itr2 in range(FLAGS.batch_size):
                """
                Tuan add CRF post processing
                
                import sys
                path = "D:/Python/Anaconda/envs/tf\ws/FCN-Tuan/pydensecrf/dense_crf_python/"
                sys.path.append(path)
                import pydensecrf.densecrf as dcrf
                from pydensecrf.utils import compute_unary, create_pairwise_bilateral, create_pairwise_gaussian, softmax_to_unary
                import skimage.io as io
                image = valid_images[itr2].astype(np.uint8)
                processed_probabilities = pred
                softmax = processed_probabilities.transpose((2, 0, 1))
                
                unary = softmax_to_unary(softmax)
                # The inputs should be C-continious -- we are using Cython wrapper
                unary = np.ascontiguousarray(unary)

                d = dcrf.DenseCRF(image.shape[0] * image.shape[1], 2)
                print(">>>>>>>>>>>", unary.shape)
                d.setUnaryEnergy(unary)


                # This potential penalizes small pieces of segmentation that are
                # spatially isolated -- enforces more spatially consistent segmentations
                feats = create_pairwise_gaussian(sdims=(10, 10), shape=image.shape[:2])

                d.addPairwiseEnergy(feats, compat=3,
                                    kernel=dcrf.DIAG_KERNEL,
                                    normalization=dcrf.NORMALIZE_SYMMETRIC)

                # This creates the color-dependent features --
                # because the segmentation that we get from CNN are too coarse
                # and we can use local color features to refine them
                feats = create_pairwise_bilateral(sdims=(50, 50), schan=(20, 20, 20),
                                                   img=image, chdim=2)

                d.addPairwiseEnergy(feats, compat=10,
                                     kernel=dcrf.DIAG_KERNEL,
                                     normalization=dcrf.NORMALIZE_SYMMETRIC)
                Q = d.inference(5)
                res = np.argmax(Q, axis=0).reshape((image.shape[0], image.shape[1]))

                cmap = plt.get_cmap('bwr')

                f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
                ax1.imshow(res, vmax=1.5, vmin=-0.4, cmap=cmap)
                ax1.set_title('Segmentation with CRF post-processing')
                #probability_graph = ax2.imshow(np.dstack((train_annotation,)*3)*100)
                #ax2.set_title('Ground-Truth Annotation')
                plt.show()

                
                end Tuan
                """
                print("Output file: ",
                      FLAGS.logs_dir + "crf_" + str(itr1 * 2 + itr2))
                crfimage, crfoutput = crf(valid_images[itr2].astype(np.uint8),
                                          logits[itr2])

                fig = plt.figure()
                pos = 240 + 1
                plt.subplot(pos)
                plt.imshow(valid_images[itr2].astype(np.uint8))
                plt.axis('off')
                plt.title('Original')

                pos = 240 + 2
                plt.subplot(pos)
                plt.imshow(valid_annotations[itr2].astype(np.uint8),
                           cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('GT')

                pos = 240 + 3
                plt.subplot(pos)
                plt.imshow(pred[itr2].astype(np.uint8),
                           cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('Prediction')

                pos = 240 + 4
                plt.subplot(pos)
                plt.imshow(crfoutput, cmap=plt.get_cmap('nipy_spectral'))
                plt.axis('off')
                plt.title('CRFPostProcessing')

                pos = 240 + 6
                plt.subplot(pos)
                ret, errorImage = cv2.threshold(
                    cv2.absdiff(pred[itr2].astype(np.uint8),
                                valid_annotations[itr2].astype(np.uint8)), 0.5,
                    255, cv2.THRESH_BINARY)
                plt.imshow(errorImage, cmap=plt.get_cmap('gray'))
                plt.axis('off')
                plt.title('Pred Error:' + str(np.count_nonzero(errorImage)))

                # Calculate cross matrix between gtimage and prediction image and store to file
                #pred_cross_matrix = _calcCrossMat(valid_annotations[itr2].astype(np.uint8), pred[itr2].astype(np.uint8),
                #                                  NUM_OF_CLASSESS)
                #np.save(FLAGS.logs_dir + "pred_cross_matrix_" + str(itr1*2 + itr2), pred_cross_matrix)

                # ---End calculate cross matrix

                #Convert image from float to 0->255 uint8
                #nfo = np.info(crfoutput.dtype)
                #crfoutput = crfoutput.astype(np.float64)/ info.max()
                #crfoutput = 255*crfoutput
                #crfoutput = crfoutput.astype(np.uint8)

                crfoutput = cv2.normalize(crfoutput, None, 0, 255,
                                          cv2.NORM_MINMAX)
                valid_annotations[itr2] = cv2.normalize(
                    valid_annotations[itr2], None, 0, 255, cv2.NORM_MINMAX)

                pos = 240 + 8
                plt.subplot(pos)
                ret, errorImage = cv2.threshold(
                    cv2.absdiff(crfoutput.astype(np.uint8),
                                valid_annotations[itr2].astype(np.uint8)), 10,
                    255, cv2.THRESH_BINARY)
                plt.imshow(errorImage, cmap=plt.get_cmap('gray'))
                plt.axis('off')
                plt.title('CRF Error:' + str(np.count_nonzero(errorImage)))

                # Calculate cross matrix between gtimage and CRF postprocessing of prediction image and store to file
                #crfpred_cross_matrix = _calcCrossMat(valid_annotations[itr2].astype(np.uint8), crfoutput.astype(np.uint8),
                #                                     NUM_OF_CLASSESS)
                #np.save(FLAGS.logs_dir + "crfpred_cross_matrix_" + str(itr1 * 2 + itr2), pred_cross_matrix)

                # ---End calculate cross matrix

                np.set_printoptions(threshold=np.inf)
                #print(cv2.absdiff(crfoutput.astype(np.uint8), valid_annotations[itr2].astype(np.uint8)))

                #fig.show()
                plt.show()
                #print(FLAGS.logs_dir + "resultSum_" + str(itr1 * 2 + itr2))
                plt.savefig(FLAGS.logs_dir + "resultSum_" +
                            str(itr1 * 2 + itr2))
                #---------------------------------------------
                utils.save_image(valid_images[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="inp_" + str(itr1 * 2 + itr2))
                utils.save_image(valid_annotations[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="gt_" + str(itr1 * 2 + itr2))
                utils.save_image(pred[itr2].astype(np.uint8),
                                 FLAGS.logs_dir,
                                 name="pred_" + str(itr1 * 2 + itr2))
                utils.save_image(crfoutput,
                                 FLAGS.logs_dir,
                                 name="crf_" + str(itr1 * 2 + itr2))

                #---End calculate cross matrix
                print("Saved image: %d" % (itr1 * 2 + itr2))