Exemplo n.º 1
0
    def main(self):
        MAX_ITERATION = int(1e5 + 1)
        NUM_OF_CLASSESS = 21
        IMAGE_SIZE = 224
        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 is the final result (1*224*224*1)  logits is the unnormalized score matrix (1*224*224*50)
        pred_annotation, logits = self.build(image,
                                             train=True,
                                             random_init_fc8=True,
                                             debug=True,
                                             num_classes=21)
        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)
        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()
        train_op = self.train(loss, trainable_var)
        summary_op = tf.summary.merge_all()

        print("Setting up image reader...")
        train_records, valid_records, test_records = scene_parsing.read_dataset(
            FLAGS.data_dir)
        print(len(train_records))
        print(len(valid_records))
        print('train_recors looklike')
        print(train_records[0])
        print('valid_recors looklike')
        print(valid_records[0])
        print('test_recors looklike')
        print(test_records[0])

        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)
        if FLAGS.mode == 'test':
            test_dataset_reader = test_dataset.BatchDatset(
                test_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)
        #saver = tf.train.import_meta_graph('model.ckpt-99500.meta')
        #saver.restore(sess,'./model.ckpt-99500')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print('restoring the model with checkpoint :')
            print(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
                }

                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
                                          })
                    print("%s ---> Validation_loss: %g" %
                          (datetime.datetime.now(), valid_loss))
                    pred = sess.run(pred_annotation,
                                    feed_dict={
                                        image: valid_images,
                                        annotation: valid_annotations
                                    })
                    correct_prediction = tf.equal(pred, valid_annotations)
                    accuracy = tf.reduce_mean(
                        tf.cast(correct_prediction, tf.float32))
                    print('The accuracy right now is ')
                    print(sess.run(accuracy))
                    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)
            print(valid_images.shape)
            #print(valid_images[0][0][0])
            #print(valid_images[1][0][0])
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: valid_images,
                                annotation: valid_annotations
                            })
            #print(pred[0]==pred[1])

            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            #print(pred.shape)
            #print(pred[14]==pred[15])

            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":
            test_images = test_dataset_reader.get_random_batch(1)
            pred = sess.run(pred_annotation, feed_dict={image: test_images})
            print('prediction has shape')
            print(pred.shape)
            pred = np.squeeze(pred, axis=3)
            utils.save_image(pred[0].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="test_")
Exemplo n.º 2
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)
Exemplo n.º 3
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_value, 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)
    #logits:the last layer of conv net
    #labels:the ground truth
    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()
    
    #Check if has the log file
    if not os.path.exists(FLAGS.logs_dir):
        print("The logs path '%s' is not found" % FLAGS.logs_dir)
        print("Create now..")
        os.makedirs(FLAGS.logs_dir)
        print("%s is created successfully!" % FLAGS.logs_dir)

    #Create a file to write logs.
    #filename='logs'+ FLAGS.mode + str(datatime.datatime.now()) + '.txt'
    filename="logs_%s%s.txt"%(FLAGS.mode,datetime.datetime.now())
    path_=os.path.join(FLAGS.logs_dir,filename)
    with open(path_,'w') as logs_file:
        logs_file.write("The logs file is created at %s.\n" % datetime.datetime.now())
        logs_file.write("The model is ---%s---.\n" % FLAGS.logs_dir)
        logs_file.write("The mode is %s\n"% (FLAGS.mode))
        logs_file.write("The train data batch size is %d and the validation batch size is %d\n."%(FLAGS.batch_size,FLAGS.v_batch_size))
        logs_file.write("The train data is %s.\n" % (FLAGS.data_dir))
        logs_file.write("The data size is %d and the MAX_ITERATION is %d.\n" % (IMAGE_SIZE, MAX_ITERATION))
        logs_file.write("Setting up image reader...")

    
    print("Setting up image reader...")
    train_records, valid_records = scene_parsing.my_read_dataset(FLAGS.data_dir)
    print('number of train_records',len(train_records))
    print('number of valid_records',len(valid_records))
    with open(path_, 'a') as logs_file:
        logs_file.write('number of train_records %d\n' % len(train_records))
        logs_file.write('number of valid_records %d\n' % 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 not train,restore the model trained before
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    num_=0
    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)
        logs_file = open(path_, 'a')  
        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:
            
            #Caculate the accurary at the training set.
            train_random_images, train_random_annotations = train_dataset_reader.get_random_batch_for_train(FLAGS.v_batch_size)
            train_loss,train_pred_anno = sess.run([loss,pred_annotation], feed_dict={image:train_random_images,
                                                                                        annotation:train_random_annotations,
                                                                                        keep_probability:1.0})
            accu_iou_,accu_pixel_ = accu.caculate_accurary(train_pred_anno, train_random_annotations)
            print("%s ---> Training_loss: %g" % (datetime.datetime.now(), train_loss))
            print("%s ---> Training_pixel_accuary: %g" % (datetime.datetime.now(),accu_pixel_))
            print("%s ---> Training_iou_accuary: %g" % (datetime.datetime.now(),accu_iou_))
            print("---------------------------")
            #Output the logs.
            num_ = num_ + 1
            logs_file.write("No.%d the itr number is %d.\n" % (num_, itr))
            logs_file.write("%s ---> Training_loss: %g.\n" % (datetime.datetime.now(), train_loss))
            logs_file.write("%s ---> Training_pixel_accuary: %g.\n" % (datetime.datetime.now(),accu_pixel_))
            logs_file.write("%s ---> Training_iou_accuary: %g.\n" % (datetime.datetime.now(),accu_iou_))
            logs_file.write("---------------------------\n")

            valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.v_batch_size)
            #valid_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations,
                                                       #keep_probability: 1.0})
            valid_loss,pred_anno=sess.run([loss,pred_annotation],feed_dict={image:valid_images,
                                                                                      annotation:valid_annotations,
                                                                                      keep_probability:1.0})
            accu_iou,accu_pixel=accu.caculate_accurary(pred_anno,valid_annotations)
            print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))
            print("%s ---> Validation_pixel_accuary: %g" % (datetime.datetime.now(),accu_pixel))
            print("%s ---> Validation_iou_accuary: %g" % (datetime.datetime.now(),accu_iou))

            #Output the logs.
            logs_file.write("%s ---> Validation_loss: %g.\n" % (datetime.datetime.now(), valid_loss))
            logs_file.write("%s ---> Validation_pixel_accuary: %g.\n" % (datetime.datetime.now(),accu_pixel))
            logs_file.write("%s ---> Validation_iou_accuary: %g.\n" % (datetime.datetime.now(),accu_iou))
            saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
        '''         
        if itr % 10000 == 0:
                
            count=0
            if_con=True
            accu_iou_t=0
            accu_pixel_t=0
        
            while if_con:
                count=count+1
                valid_images, valid_annotations, filenames, if_con, start, end=validation_dataset_reader.next_batch_valid(FLAGS.v_batch_size)
                valid_loss,pred_anno=sess.run([loss,pred_annotation],feed_dict={image:valid_images,
                                                                                      annotation:valid_annotations,
                                                                                      keep_probability:1.0})
                accu_iou,accu_pixel=accu.caculate_accurary(pred_anno,valid_annotations)
            
                accu_iou_t=accu_iou_t+accu_iou
                accu_pixel_t=accu_pixel_t+accu_pixel
            print("No.%d toal validation data accurary" % itr)
            print("%s ---> Total validation_pixel_accurary: %g" % (datetime.datetime.now(),accu_pixel_t/count))
            print("%s ---> Total validation_iou_accurary: %g" % (datetime.datetime.now(),accu_iou_t/count))
            #Write logs file
            logs_file.write("No.%d toal validation data accurary.\n" % itr)
            logs_file.write("%s ---> Total validation_pixel_accurary: %g.\n" % (datetime.datetime.now(),accu_pixel_t/count))
            logs_file.write("%s ---> Total validation_iou_accurary: %g.\n" % (datetime.datetime.now(),accu_iou_t/count))'''
            #End the iterator
        logs_file.close()
Exemplo n.º 4
0
def main(argv=None):
    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")

    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)
    labels = tf.squeeze(annotation, squeeze_dims=[3])
    logits = FixLogitsWithIgnoreClass(logits, labels)
    # Calculate loss
    class_weights = tf.constant([0.1, 1., 1., 0.1, 20., 0.1, 2., 0.3, 0.1])
    onehot_labels = tf.one_hot(labels, depth=9)
    weights = tf.reduce_sum(class_weights * onehot_labels, axis=3)
    unweighted_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=labels, name="entropy")
    weighted_loss = unweighted_loss * weights
    loss = tf.reduce_mean(weighted_loss)
    # loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, 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, test_records = scene_parsing.read_dataset(
        FLAGS.data_dir)
    print(len(train_records))
    print(len(valid_records))
    print(len(test_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)
    if FLAGS.mode == 'test':
        test_dataset_reader = dataset.BatchDatset(test_records, image_options)

    sess = tf.Session()
    print("Setting up Saver...")
    saver = tf.train.Saver(max_to_keep=10)
    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)

            # Debug
            # output = sess.run(weights, feed_dict=feed_dict)
            # print("weight shape : ", output.shape)
            # print("weight : ", output[0])
            # output = sess.run(weighted_loss, feed_dict=feed_dict)
            # print("weighted_loss shape: ", output.shape)
            # print("weighted_loss : ", output)
            # output = sess.run(loss, feed_dict=feed_dict)
            # print("loss shape: ", output.shape)
            # print("loss : ", output)
            # Debug

            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 % 4000 == 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, valid_files = 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.vis_dir,
                             name="inp_" + valid_files[itr])
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.vis_dir,
                             name="gt_" + valid_files[itr])
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.vis_dir,
                             name="pred_" + valid_files[itr])
            print("Saved image: %d" % itr)

    elif FLAGS.mode == "test":
        # videoWriter = cv2.VideoWriter('test.avi', cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'), 5, (IMAGE_WIDTH, IMAGE_HEIGHT), False)
        for itr in range(len(test_records)):
            # for itr in range(len(train_records)):
            test_images, test_annotations = test_dataset_reader.next_batch(1)
            # test_images, test_annotations = train_dataset_reader.next_batch(1)
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: test_images,
                                annotation: test_annotations,
                                keep_probability: 1.0
                            })
            test_annotations = np.squeeze(test_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            print(itr)
            # videoWriter.write(pred[0].astype(np.uint8))
            utils.save_image(test_images[0].astype(np.uint8),
                             FLAGS.vis_dir,
                             name="inp_" + test_records[itr]['filename'])
            utils.save_image(test_annotations[0].astype(np.uint8),
                             FLAGS.vis_dir,
                             name="gt_" + test_records[itr]['filename'])
            utils.save_image(pred[0].astype(np.uint8),
                             FLAGS.vis_dir,
                             name="pred_" + test_records[itr]['filename'])
def main(argv=None):
    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")

    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)
    labels = tf.squeeze(annotation, squeeze_dims=[3])
    # logits = FixLogitsWithIgnoreClass(logits, labels)

    # Calculate loss
    class_weights = tf.constant([0.0,  1000.0,  1000.0])
    onehot_labels = tf.one_hot(labels, depth=NUM_OF_CLASSESS)
    weights = tf.reduce_sum(class_weights * onehot_labels, axis=3)
    unweighted_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name="entropy")
    weighted_loss = unweighted_loss * weights
    loss = tf.reduce_mean(weighted_loss)
    # loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, 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, test_records = image_reader.read_dataset(FLAGS.data_dir)
    print('Train num:', len(train_records))
    print('Val num:', len(valid_records))
    print('Test num:', len(test_records))

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

    sess = tf.Session()
    print("Setting up Model Saver...")
    saver = tf.train.Saver(max_to_keep = 20)
    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("and path is : " + ckpt.model_checkpoint_path)

    if FLAGS.mode == "train":
        global fout
        fout = open(FLAGS.result_log, 'w')
        for itr in xrange(MAX_ITERATION):
            # decrease learning_rate after enough iteration
            if (itr % LR_DECREASE_ITER == 0 and itr > 0):
                FLAGS.learning_rate *= LR_DECREASE_RATE
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size, random_rotate = 1)
            feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85}
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 50 == 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 % 5000 == 0):
                valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size, random_rotate = 0)
                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)
                # run test
                cntTable = np.zeros((NUM_OF_CLASSESS, NUM_OF_CLASSESS))
                for i in range(len(test_records)):
                    test_images, test_annotations = test_dataset_reader.next_batch(1, random_rotate = 0)
                    pred = sess.run(pred_annotation, feed_dict={image: test_images, annotation: test_annotations, keep_probability: 1.0})
                    test_annotations = np.squeeze(test_annotations, axis=3)
                    pred = np.squeeze(pred, axis=3)
                    table = CalcConfusionMatrix(test_annotations[0], pred[0])
                    cntTable += table
                OutputResult(itr, cntTable)
        fout.close()


    elif FLAGS.mode == "test":
        # videoWriter = cv2.VideoWriter('test.avi', cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'), 5, (IMAGE_WIDTH, IMAGE_HEIGHT), False)
        for itr in range(len(test_records)):
        # for itr in range(len(train_records)):
            test_images, test_annotations = test_dataset_reader.next_batch(1, random_rotate = 0)
            # test_images, test_annotations = train_dataset_reader.next_batch(1, random_rotate = 0)
            pred = sess.run(pred_annotation, feed_dict={image: test_images, annotation: test_annotations,
                                                        keep_probability: 1.0})
            test_annotations = np.squeeze(test_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            print(str(itr) + '/' + str(len(test_records)))
            # videoWriter.write(pred[0].astype(np.uint8))
            utils.save_image(test_images[0].astype(np.uint8), FLAGS.vis_dir, name="inp_" + test_records[itr]['filename'])
            utils.save_image(test_annotations[0].astype(np.uint8), FLAGS.vis_dir, name="gt_" + test_records[itr]['filename'])
            utils.save_image(pred[0].astype(np.uint8), FLAGS.vis_dir, name="pred_" + test_records[itr]['filename'])
            # utils.save_image(test_images[0].astype(np.uint8), FLAGS.vis_dir, name="inp_" + train_records[itr]['filename'])
            # utils.save_image(test_annotations[0].astype(np.uint8), FLAGS.vis_dir, name="gt_" + train_records[itr]['filename'])
            # utils.save_image(pred[0].astype(np.uint8), FLAGS.vis_dir, name="pred_" + train_records[itr]['filename'])
        # videoWriter.release()

    elif FLAGS.mode == "test_trainset":
        for itr in range(len(train_records)):
            test_images, test_annotations = train_dataset_reader.next_batch(1, random_rotate = 0)
            pred = sess.run(pred_annotation, feed_dict={image: test_images, annotation: test_annotations,
                                                        keep_probability: 1.0})
            test_annotations = np.squeeze(test_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)
            print(str(itr) + '/' + str(len(train_records)))
            utils.save_image(test_images[0].astype(np.uint8), FLAGS.vis_train_dir, name="inp_" + train_records[itr]['filename'])
            utils.save_image(test_annotations[0].astype(np.uint8), FLAGS.vis_train_dir, name="gt_" + train_records[itr]['filename'])
            utils.save_image(pred[0].astype(np.uint8), FLAGS.vis_train_dir, name="pred_" + train_records[itr]['filename'])
Exemplo n.º 6
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")))
    #if set the classes num=2,sparse_softmax_cross_entropy_with_logits will be wrong!
    tf.summary.scalar("training_entropy_loss", 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) #get read lists
    print(len(train_records)) #44
    print(len(valid_records)) #10

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

    #if train mode,get datas batch by bactch
    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(max_to_keep=2)
    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 model has been trained,restore it
    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)
            #print(train_images.shape)
            #print(train_annotations.shape)
            #print(itr)
            feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85}
            sess.run(train_op, feed_dict=feed_dict)

            if itr % 15 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.validation_batch_size)
                
                train_loss, summary_str = sess.run([loss, summary_op], feed_dict=feed_dict)
                valid_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations,
                                                       keep_probability: 1.0})
                pre_train_image = sess.run(pred_annotation, feed_dict={image: train_images, keep_probability: 1.0})
                pre_valid_image = sess.run(pred_annotation, feed_dict={image: valid_images, keep_probability: 1.0})
                
                sensitivity_list_t, FPavg_list_t = computeFROC(pre_train_image,train_annotations, allowedDistance, range_threshold)
                froc_score_t = get_FROC_avg_score(sensitivity_list_t,nbr_of_thresholds)
                #f1_score = metrics.f1_score(valid_annotations_flat, pre_valid_image_flat) 
                sensitivity_list, FPavg_list = computeFROC(pre_valid_image,valid_annotations, allowedDistance, range_threshold)
                froc_score = get_FROC_avg_score(sensitivity_list,nbr_of_thresholds)
                
                #SN_score_tb = tf.Summary(value = [tf.Summary.Value(tag="f1_score", simple_value=f1_score)])
                froc_score_t_tb = tf.Summary(value = [tf.Summary.Value(tag="froc_score_training", simple_value=froc_score_t)])
                froc_score_tb = tf.Summary(value = [tf.Summary.Value(tag="froc_score_validation", simple_value=froc_score)])
                validation_loss = tf.Summary(value = [tf.Summary.Value(tag="validation_loss", simple_value=valid_loss)])
                print('froc_score_traing:',froc_score_t)
                print('froc_score:',froc_score)
                
                #summary_writer.add_summary(SN_score_tb,itr)
                summary_writer.add_summary(summary_str, itr)
                summary_writer.add_summary(froc_score_t_tb,itr)
                summary_writer.add_summary(froc_score_tb,itr)
                summary_writer.add_summary(validation_loss, itr)
                summary_writer.flush()
                
                print("Step: %d, learning_rate:%g" % (itr, FLAGS.learning_rate))
                print("Step: %d, Train_loss:%g" % (itr, train_loss))
                print("Step: %d, Validation_loss:%g" % (itr, valid_loss))
                sys.stdout.flush()
            if itr % 5000 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
                sys.stdout.flush()
            if itr == 30000:
                FLAGS.learning_rate = 1e-6
            if itr == 40000:
                FLAGS.learning_rate = 1e-7
    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.validation_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.validation_batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir+'images/', name="inp_" + str(1+itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir+'images/', name="gt_" + str(1+itr))
            utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir+'images/', name="pred_" + str(1+itr))
            print("Saved image: %d" % itr)
            sys.stdout.flush()
gen_loss_mse = tf.reduce_mean(2 * tf.nn.l2_loss(pred_image - lab_images)) / (
    IMAGE_SIZE * IMAGE_SIZE * 100 * 100)
tf.summary.scalar("HyperColumns_loss_MSE", gen_loss_mse)

train_variables = tf.trainable_variables()
for v in train_variables:
    utils.add_to_regularization_and_summary(var=v)

train_op = train(gen_loss_mse, train_variables)

print("Reading image dataset...")
train_images, testing_images, validation_images = flowers.read_dataset(
    FLAGS.data_dir)
image_options = {"resize": True, "resize_size": IMAGE_SIZE, "color": "LAB"}
batch_reader_train = dataset.BatchDatset(train_images, image_options)
batch_reader_validate = dataset.BatchDatset(validation_images, image_options)
batch_reader_testing = dataset.BatchDatset(testing_images, image_options)

print("Setting up session")
sess = tf.Session()
summary_op = tf.summary.merge_all()
saver = tf.train.Saver()
train_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train', sess.graph)
validate_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/validate')

sess.run(tf.global_variables_initializer())
if restore_model == True:
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
Exemplo n.º 8
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(
        tf.float32,
        shape=[FLAGS.batch_size, IMAGE_SIZE_H, IMAGE_SIZE_W, 3],
        name="input_image")
    annotation = tf.placeholder(
        tf.int32,
        shape=[FLAGS.batch_size, IMAGE_SIZE_H, IMAGE_SIZE_W, 1],
        name="annotation")
    # 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")

    precessed_image = (1.0 / 255.0) * tf.to_float(image)  # to [0, 1] range

    feed1, feed2, conv5 = shufflenet(precessed_image,
                                     is_training=True,
                                     num_classes=1000,
                                     depth_multiplier='1.0')

    pred_annotation, logits = decode(feed1, feed2, conv5)

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

    # loss function by junghun "https://stackoverflow.com/questions/44560549/unbalanced-data-and-weighted-cross-entropy"
    # class_weights = tf.constant([1.0,100.0])
    # weights = tf.reduce_sum(class_weights * onehot_labels, axis=1)
    # unweighted_losses = tf.nn.softmax_cross_entropy_with_logits(onehot_labels, logits)
    # weighted_losses = unweighted_losses * weights
    # loss = tf.reduce_mean(weighted_losses)

    # loss function by junghun "https://stackoverflow.com/questions/40198364/how-can-i-implement-a-weighted-cross-entropy-loss-in-tensorflow-using-sparse-sof/46984951#46984951"
    class_weights = tf.constant([1.0, 100.0])
    weights = tf.gather(class_weights, annotation)
    # unweighted_losses = tf.nn.softmax_cross_entropy_with_logits(onehot_labels, logits)
    # weighted_losses = unweighted_losses * weights

    # ######
    #     loss = tf.reduce_mean(tf.losses.sparse_softmax_cross_entropy(annotation, logits, weights))
    # #######

    # original loss function (no class weight)
    # 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)
    # # ########

    #train_op = tf.train.AdamOptimizer(0.001).minimize(loss)

    ###########
    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_h': IMAGE_SIZE_H,
        'resize_size_w': IMAGE_SIZE_W
    }
    # image_options = {'resize': True, 'resize_size': IMAGE_SIZE}

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

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

    # ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    #if ckpt and ckpt.model_checkpoint_path:

    ##########
    #
    ##########

    # reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    # reuse_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="ShuffleNetV2")
    # for var in reuse_vars:
    #     if "Conv1" not in var.name :
    #         print(var.name)

    #imagenet pretrained model load
    # variable_restore_op = slim.assign_from_checkpoint_fn(FLAGS.model_dir + "model.ckpt-1661328",
    #                                                          slim.get_trainable_variables(),
    #                                                          ignore_missing_vars=True)
    # #imagenet pretrained model load

    variable_restore_op = slim.assign_from_checkpoint_fn(
        FLAGS.pupil_seg_model_dir + "model.ckpt-100000",
        slim.get_trainable_variables(),
        ignore_missing_vars=True)
    # reuse_vars_dict = dict([(var.name, var) for var in reuse_vars])
    # reuse_vars_dict = dict([(var.name, var) for var in reuse_vars if "batch_norm" not in var.name])

    #print(reuse_vars_dict)

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

    # saver.restore(sess, ckpt.model_checkpoint_path)
    # saver.restore(sess, "")

    sess.run(tf.global_variables_initializer())
    # print(FLAGS.model_dir + "model.ckpt-1661328")

    variable_restore_op(sess)

    #saver = tf.train.import_meta_graph(FLAGS.model_dir + "model.ckpt-1661328.meta")

    #saver.restore(sess, FLAGS.model_dir + "model.ckpt-1661328")
    print(FLAGS.model_dir + "model.ckpt-1661328")

    # new_saver = tf.train.Saver()

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

    ######
    loss = tf.reduce_mean(
        tf.losses.sparse_softmax_cross_entropy(annotation, logits, weights))
    #######

    #########
    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("traaaauubausbdusabduabdub",train_op)
    ########

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

    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}
            # 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)
                # train_loss = sess.run([loss], 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
                                                   })
                # 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
                        })
        # 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)
Exemplo n.º 9
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)
Exemplo n.º 10
0
import numpy as np
import tensorflow as tf
from sys import getsizeof
import scipy.io
import TensorflowUtils as utils
import vgg_inference
import datetime

# 1. load data
# read datasets
image_dir = "E:\pythontest\FCN\ADEChallengeData2016"
image_options = {'resize': True, 'resize_size': 224}
train_records, valid_records = getrecords.create_image_lists(image_dir)

# get batches
train_dataset_reader = dataset.BatchDatset(train_records, image_options)	# class BatchDatset
valid_dataset_reader = dataset.BatchDatset(valid_records, image_options)
#train_images, train_annotations = train_dataset_reader.next_batch(50)	# get ndarray data of batches

## read VGG16 model
#model_data = scipy.io.loadmat("imagenet-vgg-verydeep-19")

# 2. define basical parameters
IMAGE_SIZE = 224
NUM_OF_CLASSES = 151
MAX_ITERATION = 50
learning_rate = 1.0e-4
batch_size = 10

# 3. Construct networks
keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
Exemplo n.º 11
0
def main(argv=None):
    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)
    # class_weight = tf.constant([0.007479654948, 0.9925203451])
    # 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=32.,
                                                  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 = {}
    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...")
    import pdb

    pdb.set_trace()
    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)

            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)
Exemplo n.º 12
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")
    soft_annotation = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 2], name="soft_annotation")
    hard_annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="hard_annotation")

    pred_annotation_value, pred_annotation, logits = inference(image, keep_probability)
    #tf.summary.image("input_image", image, max_outputs=2)
    #tf.summary.image("ground_truth", tf.cast(soft_annotation, tf.uint8), max_outputs=2)
    #tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2)
    #logits:the last layer of conv net
    soft_logits = tf.nn.softmax(logits/FLAGS.temperature)
    soft_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits/FLAGS.temperature,
                                                                        labels = soft_annotation,
                                                                        name = "entropy_soft"))
    hard_loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                                  labels=tf.squeeze(hard_annotation, squeeze_dims=[3]),
                                                                  name="entropy_hard")))

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

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

    print("Setting up summary op...")
    summary_op = tf.summary.merge_all()
    
    #Check if has the log file
    if not os.path.exists(FLAGS.logs_dir):
        print("The logs path '%s' is not found" % FLAGS.logs_dir)
        print("Create now..")
        os.makedirs(FLAGS.logs_dir)
        print("%s is created successfully!" % FLAGS.logs_dir)

    #Create a file to write logs.
    filename="logs_%s%s.txt"%(FLAGS.mode,datetime.datetime.now())
    path_=os.path.join(FLAGS.logs_dir,filename)
    with open(path_,'w') as logs_file:
        logs_file.write("The logs file is created at %s.\n" % datetime.datetime.now())
        logs_file.write("The model is ---%s---.\n" % FLAGS.logs_dir)
        logs_file.write("The mode is %s\n"% (FLAGS.mode))
        logs_file.write("The train data batch size is %d and the validation batch size is %d\n."%(FLAGS.batch_size,FLAGS.v_batch_size))
        logs_file.write("The train data is %s.\n" % (FLAGS.data_dir))
        logs_file.write("The data size is %d and the MAX_ITERATION is %d.\n" % (IMAGE_SIZE, MAX_ITERATION))
        logs_file.write("Setting up image reader...")

    
    print("Setting up image reader...")
    train_records, valid_records, test_records = scene_parsing.read_dataset_tvt(FLAGS.data_dir)
    print('number of train_records',len(train_records))
    print('number of valid_records',len(valid_records))
    print('number of test records', len(test_records))
    with open(path_, 'a') as logs_file:
        logs_file.write('number of train_records %d\n' % len(train_records))
        logs_file.write('number of valid_records %d\n' % len(valid_records))
        logs_file.write('number of test_records %d\n' % len(test_records))

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset_soft.BatchDatset(train_records, image_options)
        validation_dataset_reader = dataset_soft.BatchDatset(valid_records, image_options)
    test_dataset_reader = dataset.BatchDatset(test_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 not train,restore the model trained before
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    num_=0
    for itr in xrange(MAX_ITERATION):
        
        train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
        train_annotations_n = train_annotations/FLAGS.normal
        feed_dict = {image: train_images, soft_annotation: train_annotations_n, keep_probability: 0.85}

        sess.run(train_op, feed_dict=feed_dict)
        logs_file = open(path_, 'a')  
        if itr % 10 == 0:
            soft_train_logits, train_logits = sess.run([soft_logits, logits], feed_dict=feed_dict)
            train_logits = np.array(train_logits)
            soft_train_logits = np.array(soft_train_logits)

            train_loss, summary_str = sess.run([soft_loss, summary_op], feed_dict=feed_dict)
            print("Step: %d, Train_loss:%g" % (itr, train_loss))
            logs_file.write("Step: %d, Train_loss:%g\n" % (itr, train_loss))
            summary_writer.add_summary(summary_str, itr)

        if itr % 500 == 0:
            
            #Caculate the accurary at the validation set.
            valid_images, valid_annotations = valid_dataset_reader.get_records()
            valid_logits,valid_loss,valid_pred_anno = sess.run([soft_logits,soft_loss,pred_annotation], feed_dict={image:valid_images,
                                                                                        soft_annotation:valid_annotations/FLAGS.normal,
                                                                                        keep_probability:1.0})
            print('shape of train_random_annotations', train_random_annotations.shape)
            #Caculate accurary
            choose_len = math.ceil((FLAGS.soft_thresh_upper - FLAGS.soft_thresh_lower)/0.01)
            test_thresh = FLAGS.soft_thresh_lower
            accu_sum_max = -1
            accu_iou_max = 0
            accu_pixel_max = 0
            choose_thresh = FLAGS.soft_thresh_lower
            choose_thresh = test_thresh
            print(choose_len)
            for i in range(choose_len):
                accu_iou_,accu_pixel_ = accu.caculate_soft_accurary(valid_logits, valid_annotations/FLAGS.normal, test_thresh)
                accu_sum = accu_iou_*FLAGS.w_iou + accu_pixel_*(1-FLAGS.w_iou)
                print('accu_sum: %g' % accu_sum)
                if accu_sum>accu_sum_max:
                    choose_thresh = test_thresh
                    accu_sum_max = accu_sum
                    accu_iou_max = accu_iou_
                    accu_pixel_max = accu_pixel_
                print('%d accu_iou:%g, accu_pixel:%g, accu_sum:%g, test_thresh:%g' % (i,accu_iou_max, accu_pixel_max,accu_sum_max,test_thresh))
                test_thresh += FLAGS.interval
            FLAGS.soft_thresh = choose_thresh
            print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), train_loss))
            print("%s ---> The chosen soft_threshhold is %g." % (datetime.datetime.now(),FLAGS.soft_thresh))
            print("%s ---> Validation_pixel_accuary: %g" % (datetime.datetime.now(),accu_pixel_max))
            print("%s ---> Validation_iou_accuary: %g" % (datetime.datetime.now(),accu_iou_max))

            print("---------------------------")
            #Output the logs.
            num_ = num_ + 1
            logs_file.write("No.%d the itr number is %d.\n" % (num_, itr))
            logs_file.write("%s ---> Validation_loss: %g.\n" % (datetime.datetime.now(), train_loss))
            logs_file.write("%s ---> The chosen soft_threshhold is %g.\n" % (datetime.datetime.now(),FLAGS.soft_thresh))
            logs_file.write("%s ---> Validation_pixel_accuary: %g.\n" % (datetime.datetime.now(),accu_pixel_))
            logs_file.write("%s ---> Validation_iou_accuary: %g.\n" % (datetime.datetime.now(),accu_iou_))
            logs_file.write("---------------------------\n")
            
            #test dataset
            test_images, test_annotations = test_dataset_reader.next_batch(FLAGS.v_batch_size)
            test_loss,pred_anno=sess.run([hard_loss,pred_annotation],feed_dict={image:test_images,
                                                                                      hard_annotation:test_annotations,
                                                                                      keep_probability:1.0})
            accu_iou,accu_pixel=accu.caculate_soft_accurary(pred_anno, test_annotations, FLAGS.soft_thresh)
            print("%s ---> test_loss: %g" % (datetime.datetime.now(), valid_loss))
            print("%s ---> test_pixel_accuary: %g" % (datetime.datetime.now(),accu_pixel))
            print("%s ---> test_iou_accuary: %g" % (datetime.datetime.now(),accu_iou))

            #Output the logs.
            logs_file.write("%s ---> test_loss: %g.\n" % (datetime.datetime.now(), valid_loss))
            logs_file.write("%s ---> test_pixel_accuary: %g.\n" % (datetime.datetime.now(),accu_pixel))
            logs_file.write("%s ---> test_iou_accuary: %g.\n" % (datetime.datetime.now(),accu_iou))
            saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
            #End the iterator
        logs_file.close()
Exemplo n.º 13
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE[1], IMAGE_SIZE[0], 1],
                           name="input_image")
    annotation = tf.placeholder(tf.float32,
                                shape=[None, 1, IMAGE_SIZE[0], 1],
                                name="annotation")
    pred_annotation, logits = inference(image, keep_probability)
    # logits = tf.squeeze(logits, squeeze_dims=[1, 3])
    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")))
    alpha = 0.9
    belta = 0.1
    # one sample: -ylog(y)+-(1-y)log(1-y),    n samples: mean(one sample)
    loss = tf.reduce_mean(
        tf.add(
            -alpha * tf.reduce_sum(annotation * tf.log(logits + 1e-9), 1),
            -belta * tf.reduce_sum(
                (1 - annotation) * tf.log(1 - logits + 1e-9), 1)))
    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 dataset reader")
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(FLAGS.data_dir)
    validation_dataset_reader = dataset.BatchDatset(
        FLAGS.test_data_dir, dataset_file='dataset_test.txt')

    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)
    ckpt.model_checkpoint_path = 'logs/model.ckpt-100000'
    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 == "inference":
        path = './real_test_imgs/'
        fnames = os.listdir(path)
        imgs = np.array(
            [_transform(os.path.join(path, elem)) for elem in fnames])
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: imgs,
                            keep_probability: 1.0
                        })  # [80, 1,1024,1]
        pred = np.squeeze(pred, axis=3)
        pred = np.squeeze(pred, axis=1)
        pred = np.asarray(pred, np.int)
        res = []
        for itr in range(len(imgs)):
            im = imgs[itr]
            pre = pred[itr]
            im = 255 * np.squeeze(im, axis=2)
            im = Image.fromarray(im)
            # make sure images are of shape(h,w,3)
            img = im.convert('RGB')
            img.save('result/source_%s.jpg' % str(itr))
            res.append(['source_%s.jpg'] + list(pre))
            img_d = ImageDraw.Draw(img)
            x_len, y_len = img.size
            for x in range(x_len):
                if pre[x] == 1:
                    img_d.line(((x, 0), (x, y_len)), (250, 0, 0))
            img.save('result/pred_%s.jpg' % str(itr))
            # utils.save_image(im.astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5 + itr))
        np.savetxt('res.txt', res, fmt='%s')
    elif FLAGS.mode == "test":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        pred = sess.run(pred_annotation,
                        feed_dict={
                            image: valid_images,
                            keep_probability: 1.0
                        })  # [80, 1,1024,1]
        pred = np.squeeze(pred, axis=3)
        pred = np.squeeze(pred, axis=1)
        pred = np.asarray(pred, np.int)
        for itr in range(FLAGS.batch_size):
            im = valid_images[itr]
            pre = pred[itr]
            im = 255 * np.squeeze(im, axis=2)
            im = Image.fromarray(im)
            # make sure images are of shape(h,w,3)
            img = im.convert('RGB')

            img_d = ImageDraw.Draw(img)
            x_len, y_len = img.size
            for x in range(x_len):
                if pre[x] == 1:
                    img_d.line(((x, 0), (x, y_len)), (250, 0, 0))
            img.save('result/pred_%s.jpg' % str(itr))
def main(argv=None):
    print("Setting up network...")
    train_phase = tf.placeholder(tf.bool, name="train_phase")
    images = tf.placeholder(tf.float32, shape=[None, None, None, 1], name='L_image')
    lab_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name="LAB_image")

    pred_image = generator(images, train_phase)

    gen_loss_mse = tf.reduce_mean(2 * tf.nn.l2_loss(pred_image - lab_images)) / (IMAGE_SIZE * IMAGE_SIZE * 100 * 100)
    #    tf.scalar_summary("Generator_loss_MSE", gen_loss_mse)
    tf.summary.scalar("Generator_loss_MSE", gen_loss_mse)

    train_variables = tf.trainable_variables()
    for v in train_variables:
        utils.add_to_regularization_and_summary(var=v)

    train_op = train(gen_loss_mse, train_variables)

    print("Reading image dataset...")
    # train_images, testing_images, validation_images = flowers.read_dataset(FLAGS.data_dir)
    train_images = lamem.read_dataset(FLAGS.data_dir)
    image_options = {"resize": True, "resize_size": IMAGE_SIZE, "color": "LAB"}
    batch_reader = dataset.BatchDatset(train_images, image_options)

    print("Setting up session")
    sess = tf.Session()
    summary_op = tf.summary.merge_all()
    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):
            l_image, color_images = batch_reader.next_batch(FLAGS.batch_size)
            feed_dict = {images: l_image, lab_images: color_images, train_phase: True}

            if itr % 10 == 0:
                mse, summary_str = sess.run([gen_loss_mse, summary_op], feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, itr)
                print("Step: %d, MSE: %g" % (itr, mse))
                if FLAGS.MSE_stop>mse:
                    print("MSE is reachin stop value, training interrupted")
                    break

            if itr % 100 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
                pred = sess.run(pred_image, feed_dict=feed_dict)
                idx = np.random.randint(0, FLAGS.batch_size)
                save_dir = os.path.join(FLAGS.logs_dir, "image_checkpoints")
                utils.save_image(color_images[idx], save_dir, "gt" + str(itr // 100))
                utils.save_image(pred[idx].astype(np.float64), save_dir, "pred" + str(itr // 100))
                print("%s --> Model saved" % datetime.datetime.now())

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10000 == 0:
                FLAGS.learning_rate /= 2

    elif FLAGS.mode == "test":
        count = 15
        l_image, color_images = batch_reader.get_random_batch(count)
        print("/!\ TEST:", l_image.shape, color_images.shape)
        feed_dict = {images: l_image, lab_images: color_images, train_phase: False}
        save_dir = os.path.join(FLAGS.logs_dir, "image_pred")
        pred = sess.run(pred_image, feed_dict=feed_dict)
        for itr in range(count):
            utils.save_image(color_images[itr], save_dir, "gt" + str(itr))
            utils.save_image(pred[itr].astype(np.float64), save_dir, "pred" + str(itr))
        print("--- Images saved on test run ---")

    elif FLAGS.mode == "custom":
        l_image = PIL_to_lab(FLAGS.filename)
        color_image = Image.open(FLAGS.filename)
        color_image = rgb2lab(color_image)
        color_image = np.expand_dims(color_image, axis=0)
        feed_dict = {images: l_image, lab_images: color_image, train_phase: False}
        save_dir = os.path.join(FLAGS.logs_dir, "image_pred")
        pred = sess.run(pred_image, feed_dict=feed_dict)
        for itr in range(1):
            utils.save_image(color_image[itr], save_dir, "gt" + str(itr)) 
            utils.save_image(pred[itr].astype(np.float64), save_dir, "pred" + str(itr))
        print("--- Images saved on custom run ---")
Exemplo n.º 15
0
def main(_):
    # FCN 그래프를 선언합니다.
    FCN_model = FCN(rate=0.15)

    # 최적화를 위한 Adam 옵티마이저를 정의합니다.
    optimizer = tf.optimizers.Adam(FLAGS.learning_rate)

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

    # 학습된 파라미터를 저장하기 위한 tf.train.CheckpointManager와
    # tensorboard summary들을 저장하기 위한 FileWriter를 선언합니다.
    print("Setting up Saver...")
    ckpt = tf.train.Checkpoint(model=FCN_model)
    ckpt_manager = tf.train.CheckpointManager(ckpt,
                                              directory=FLAGS.logs_dir,
                                              max_to_keep=5)
    summary_writer = tf.summary.create_file_writer(FLAGS.logs_dir)

    # 저장된 ckpt 파일이 있으면 저장된 파라미터를 불러옵니다.
    latest_ckpt = tf.train.latest_checkpoint(FLAGS.logs_dir)
    if latest_ckpt:
        ckpt.restore(latest_ckpt)
        print("Model restored...")

    if FLAGS.mode == "train":
        for itr in range(MAX_ITERATION):
            # 학습 데이터를 불러옵니다.
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            # 이미지를 float32 타입으로 변환합니다.
            train_images = train_images.astype('float32')

            # train_step을 실행해서 파라미터를 한 스텝 업데이트합니다.
            train_step(FCN_model, train_images, train_annotations, optimizer)

            # 10회 반복마다 training 데이터 손실 함수를 출력합니다.
            if itr % 10 == 0:
                pred_annotation, logits = FCN_model(train_images)
                train_loss = sparse_cross_entropy_loss(logits,
                                                       train_annotations)
                print("반복(Step): %d, Training 손실함수(Train_loss):%g" %
                      (itr, train_loss))
                with summary_writer.as_default():
                    tf.summary.scalar('train_loss', train_loss, step=itr)
                    tf.summary.image("input_image",
                                     tf.cast(train_images, tf.uint8),
                                     step=itr,
                                     max_outputs=2)
                    tf.summary.image("ground_truth",
                                     tf.cast(train_annotations, tf.uint8),
                                     step=itr,
                                     max_outputs=2)
                    tf.summary.image("pred_annotation",
                                     tf.cast(pred_annotation, tf.uint8),
                                     step=itr,
                                     max_outputs=2)

            # 500회 반복마다 validation 데이터 손실 함수를 출력하고 학습된 모델의 파라미터를 ckpt 파일로 저장합니다.
            if itr % 500 == 0:
                valid_images, valid_annotations = validation_dataset_reader.next_batch(
                    FLAGS.batch_size)
                # 이미지를 float32 타입으로 변환합니다.
                valid_images = valid_images.astype('float32')
                _, logits = FCN_model(valid_images, is_training=False)
                valid_loss = sparse_cross_entropy_loss(logits,
                                                       valid_annotations)
                print("%s ---> Validation 손실함수(Validation_loss): %g" %
                      (datetime.datetime.now(), valid_loss))
                ckpt_manager.save(checkpoint_number=itr)
                with summary_writer.as_default():
                    tf.summary.scalar('valid_loss', valid_loss, step=itr)

    elif FLAGS.mode == "visualize":
        # validation data로 prediction을 진행합니다.
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            FLAGS.batch_size)
        # 이미지를 float32 타입으로 변환합니다.
        valid_images = valid_images.astype('float32')
        pred, _ = FCN_model(valid_images, is_training=False)
        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)
Exemplo n.º 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")))
    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":
        #for i in range(21):
        cap = cv2.VideoCapture(0)
        start = time.time()

        while (True):
            ret, frame = cap.read()
            #valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
            #valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)

            pic = np.zeros((1, 224, 224, 3), np.uint8)
            pic[0][:][:][:] = cv2.resize(frame, (IMAGE_SIZE, IMAGE_SIZE),
                                         interpolation=cv2.INTER_CUBIC)
            valid_images = pic
            valid_annotations = np.zeros((1, 224, 224, 1), np.uint8)

            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)
            pred = pred[FLAGS.batch_size - 1].astype(np.uint8)
            valid = valid_images[FLAGS.batch_size - 1].astype(np.uint8)
            #pre_wigh=road_red(pred,valid)
            pre_wigh = find_counters(pred, valid)
            px = cv2.resize(pre_wigh, (640, 480),
                            interpolation=cv2.INTER_CUBIC)
            cv2.imshow("pre", px)
            cv2.waitKey(5)
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    # In MRI image, there are four layer, which are t1, t1ce, t2 and flair
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 4],
                           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':
        print("reading training dataset... wait a moment...")
        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":
        with open('./visualize_set.json') as f:
            brain_to_visualize = json.load(f)

        for brain_name in brain_to_visualize:
            z_idx = brain_to_visualize[brain_name]

            valid_images, valid_annotations = validation_dataset_reader.get_single_brain_by_name(
                brain_name)
            feed_image = valid_images[z_idx, ...]
            feed_image = feed_image[None, ...]
            feed_annotation = valid_annotations[z_idx, ...]
            feed_annotation = feed_annotation[None, ...]

            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: feed_image,
                                annotation: feed_annotation,
                                keep_probability: 1.0
                            })
            feed_annotation = np.squeeze(feed_annotation, axis=3)
            pred = np.squeeze(pred, axis=3)

            utils.save_image(feed_image[0][..., 1].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + f"{brain_name}_{z_idx}")
            utils.save_image((feed_annotation[0] * 100).astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + f"{brain_name}_{z_idx}")
            utils.save_image((pred[0] * 100).astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + f"{brain_name}_{z_idx}")
            print(f"Saved image: {brain_name}_{z_idx}")

    elif FLAGS.mode == "evaluate":
        gt_tumor, seg_tumor, overlapped = 0, 0, 0
        true_positive, true_negative, false_positive, false_negative = 0, 0, 0, 0

        for i in range(len(valid_records)):
            valid_images, valid_annotations = validation_dataset_reader.get_single_brain(
                i)
            # Divide tensors depth wise, due to memory issue
            z_limit = valid_images.shape[0]
            for z in range(z_limit):
                feed_image = valid_images[z, ...]
                feed_image = feed_image[None, ...]
                feed_annotation = valid_annotations[z, ...]
                feed_annotation = feed_annotation[None, ...]

                pred = sess.run(pred_annotation,
                                feed_dict={
                                    image: feed_image,
                                    annotation: feed_annotation,
                                    keep_probability: 1.0
                                })
                feed_annotation = np.squeeze(feed_annotation, axis=3)
                pred = np.squeeze(pred, axis=3)

                gt = np.asarray(feed_annotation[0]).astype(np.bool)
                if (FLAGS.eval_range == "tumor_only" and gt.sum() == 0):
                    # case which has no tumor on the slice
                    continue

                seg = np.asarray(pred[0]).astype(np.bool)
                pixels = len(gt) * len(gt)

                gt_tumor += gt.sum()
                seg_tumor += seg.sum()
                overlapped += np.logical_and(gt, seg).sum()

                tp = np.bitwise_and(gt, seg)
                true_positive += tp.sum()
                fn = np.bitwise_and(gt, np.invert(seg))
                false_negative += fn.sum()
                fp = np.bitwise_and(np.invert(gt), seg)
                false_positive += fp.sum()
                tn = np.bitwise_and(np.invert(gt), np.invert(seg))
                true_negative += tn.sum()

        dice = 2 * overlapped / (gt_tumor + seg_tumor)
        sensitivity = true_positive / (true_positive + false_negative)
        specificity = true_negative / (true_negative + false_positive)
        ppv = true_positive / (true_positive + false_positive)
        npv = true_negative / (true_negative + false_negative)
        print(f"DICE COEFFICIENT: {dice}")
        print(f"SENSITIVITY: {sensitivity}, SPECIFICITY: {specificity}")
        print(f"PPV: {ppv}, NPV: {npv}")
        print(
            f"raw TRUE POSITIVE: {true_positive}, TRUE NEGATIVE: {true_negative}"
        )
        print(
            f"raw FALSE POSITIVE: {false_positive}, FALSE NEGATIVE: {false_negative}"
        )
Exemplo n.º 18
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')
    is_training = False
    if FLAGS.mode == "train":
        is_training = True

    image075 = tf.image.resize_images(
        image, [int(IMAGE_SIZE * 0.75), int(IMAGE_SIZE * 0.75)])
    image050 = tf.image.resize_images(
        image, [int(IMAGE_SIZE * 0.5), int(IMAGE_SIZE * 0.5)])
    image125 = tf.image.resize_images(
        image, [int(IMAGE_SIZE * 1.25), int(IMAGE_SIZE * 1.25)])

    annotation075 = tf.cast(tf.image.resize_images(
        annotation, [int(IMAGE_SIZE * 0.75), int(IMAGE_SIZE * 0.75)]), tf.int32)
    annotation050 = tf.cast(tf.image.resize_images(
        annotation, [int(IMAGE_SIZE * 0.5), int(IMAGE_SIZE * 0.5)]), tf.int32)
    annotation125 = tf.cast(tf.image.resize_images(
        annotation, [int(IMAGE_SIZE * 1.25), int(IMAGE_SIZE * 1.25)]), tf.int32)

    # 2. construct inference network
    reuse1 = False
    reuse2 = True

    with tf.variable_scope('', reuse=reuse1):
        pred_annotation100, logits100, net100, conv5_1_weight100 = unetinference(
            image, keep_probability, is_training=is_training)
    with tf.variable_scope('', reuse=reuse2):
        pred_annotation075, logits075, net075, conv5_1_weight075 = unetinference(
            image075, keep_probability, is_training=is_training)
    with tf.variable_scope('', reuse=reuse2):
        pred_annotation050, logits050, net050, conv5_1_weight050 = unetinference(
            image050, keep_probability, is_training=is_training)
    with tf.variable_scope('', reuse=reuse2):
        pred_annotation125, logits125, net125, conv5_1_weight125 = unetinference(
            image125, keep_probability, is_training=is_training)

    logits_train = tf.reduce_mean(tf.stack([logits100,
                                            tf.image.resize_images(logits075,
                                                                   tf.shape(logits100)[1:3, ]),
                                            tf.image.resize_images(logits050,
                                                                   tf.shape(logits100)[1:3, ])]),
                                  axis=0)

    pred_annotation_train = tf.reduce_mean(tf.stack([tf.cast(pred_annotation100, tf.float32),
                                                     tf.image.resize_images(pred_annotation075,
                                                                            tf.shape(pred_annotation100)[1:3, ]),
                                                     tf.image.resize_images(pred_annotation050,
                                                                            tf.shape(pred_annotation100)[1:3, ])]),
                                           axis=0)

    pred_annotation_test = tf.reduce_mean(tf.stack([tf.cast(pred_annotation100, tf.float32),
                                                    tf.image.resize_images(pred_annotation075,
                                                                           tf.shape(pred_annotation100)[1:3, ]),
                                                    tf.image.resize_images(pred_annotation125,
                                                                           tf.shape(pred_annotation100)[1:3, ])]),
                                          axis=0)

    logits_test = tf.reduce_mean(tf.stack([logits100,
                                           tf.image.resize_images(logits075,
                                                                  tf.shape(logits100)[1:3, ]),
                                           tf.image.resize_images(logits125,
                                                                  tf.shape(logits100)[1:3, ])]),
                                 axis=0)

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

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

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

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

    reduced_loss = loss + loss100 + loss075 + loss050

    # 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(reduced_loss, trainable_var, net100['global_step'])

    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_annotation100,
            tf.uint8),
        max_outputs=3)

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

    print("Setting up image reader from ", FLAGS.data_dir, "...")
    print("data dir:", FLAGS.data_dir)

    train_records, valid_records = fashion_parsing.read_dataset(FLAGS.data_dir)
    test_records = None
    if DATA_SET == "CFPD":
        train_records, valid_records, test_records = ClothingParsing.read_dataset(
            FLAGS.data_dir)
        print("test_records length :", len(test_records))
    if DATA_SET == "LIP":
        train_records, valid_records = HumanParsing.read_dataset(
            FLAGS.data_dir)

    print("train_records length :", len(train_records))
    print("valid_records length :", len(valid_records))

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

    if FLAGS.mode == 'train':
        train_dataset_reader = DataSetReader.BatchDatset(
            train_records, image_options)
        validation_dataset_reader = DataSetReader.BatchDatset(
            valid_records, image_options)
        if DATA_SET == "CFPD":
            test_dataset_reader = DataSetReader.BatchDatset(
                test_records, image_options)
    if FLAGS.mode == 'visualize':
        validation_dataset_reader = DataSetReader.BatchDatset(
            valid_records, image_options)
    if FLAGS.mode == 'test' or FLAGS.mode == 'crftest' or FLAGS.mode == 'predonly' or FLAGS.mode == "fulltest":
        if DATA_SET == "CFPD":
            test_dataset_reader = DataSetReader.BatchDatset(
                test_records, image_options)
        else:
            test_dataset_reader = DataSetReader.BatchDatset(
                valid_records, image_options)
            test_records = valid_records

    sess = tf.Session()

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

    # 5. parameter 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, net100, train_dataset_reader, validation_dataset_reader,
                      image, annotation, keep_probability, train_op, reduced_loss, summary_op, summary_writer,
                      saver)

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

        fd.mode_visualize_scales(sess, FLAGS, VIS_DIR, validation_dataset_reader,
                                 conv5_1_weight100, pred_annotation_test, pred_annotation100, pred_annotation075, pred_annotation125, image, annotation, keep_probability, NUM_OF_CLASSES)

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

        fd.mode_test(sess, FLAGS, TEST_DIR, test_dataset_reader,
                     pred_annotation_test, image, annotation, keep_probability, logits_test, NUM_OF_CLASSES)

    sess.close()
Exemplo n.º 19
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 + 1):
            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:  # 测试模式
        for sx in range(1, 201):
            since = time.time()  # 时间模块

            #  test_image = misc.imread('D://FMCW_Interference/test/inp_'+str(sx)+'.jpg')
            test_image = misc.imread(
                'D:/Data/deeplearning/FCN/Data_zoo/MIT_SceneParsing/ADEChallengeData2016/images/FMCW_Interference/test/inp_'
                + str(sx) + '.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),'D://FMCW_Interference/test/', name="pred_" + str(sx))
            utils.save_image(
                pred[0].astype(np.uint8),
                'D:/Data/deeplearning/FCN/Data_zoo/MIT_SceneParsing/ADEChallengeData2016/images/FMCW_Interference/test/',
                name="pred_" + str(sx))
            print("Saved image: succeed")

            time_elapsed = time.time() - since
            print('Training complete in {:.0f}m {:.0f}s'.format(
                time_elapsed // 60, time_elapsed % 60))  # 打印出来时间
def main(argv=None):
    MSE = []
    ITR = []
    ERROR = {}
    print("Setting up network...")
    train_phase = tf.placeholder(tf.bool, name="train_phase")
    images = tf.placeholder(tf.float32,
                            shape=[None, None, None, 1],
                            name='L_image')
    lab_images = tf.placeholder(tf.float32,
                                shape=[None, None, None, 3],
                                name="LAB_image")

    pred_image = generator(images, train_phase)

    #gen_loss_mse = tf.reduce_mean(2 * tf.nn.l2_loss(pred_image - lab_images)) / (IMAGE_SIZE * IMAGE_SIZE * 100 * 100)
    gen_loss_mse = error_func(pred_image, lab_images)

    tf.summary.scalar("Generator_loss_MSE", gen_loss_mse)

    train_variables = tf.trainable_variables()
    for v in train_variables:
        utils.add_to_regularization_and_summary(var=v)

    train_op = train(gen_loss_mse, train_variables)

    print("Reading image dataset...")
    # train_images, testing_images, validation_images = flowers.read_dataset(FLAGS.data_dir)
    # train_images = lamem.read_dataset(FLAGS.data_dir)
    file_list = []
    file_glob = os.path.join('/home/shikhar/Desktop/full run/lamem', "images",
                             '*.' + 'jpg')
    file_list.extend(glob.glob(file_glob))
    train_images = file_list

    image_options = {"resize": True, "resize_size": IMAGE_SIZE, "color": "LAB"}
    batch_reader = dataset.BatchDatset(train_images, image_options)

    print("Setting up session")
    sess = tf.Session()
    summary_op = tf.summary.merge_all()
    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)
    sess.run(tf.initialize_all_variables())

    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):
            l_image, color_images = batch_reader.next_batch(FLAGS.batch_size)
            feed_dict = {
                images: l_image,
                lab_images: color_images,
                train_phase: True
            }

            if itr % 10 == 0:
                mse, summary_str = sess.run([gen_loss_mse, summary_op],
                                            feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, itr)
                MSE.append(mse)
                ITR.append(itr // 10)
                print("Step: %d, MSE: %g" % (itr, mse))

            if itr % 10 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
                pred = sess.run(pred_image, feed_dict=feed_dict)
                idx = np.random.randint(0, FLAGS.batch_size)
                save_dir = os.path.join(FLAGS.logs_dir, "image_checkpoints")
                print("shape of color images ", len(color_images))
                utils.save_image(color_images[idx], save_dir,
                                 "gt" + str(itr // 10))
                utils.save_image(pred[idx].astype(np.float64), save_dir,
                                 "pred" + str(itr // 10))
                print("%s --> Model saved" % datetime.datetime.now())

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 10000 == 0:
                FLAGS.learning_rate /= 2
        ERROR["mse"] = MSE
        ERROR["itr"] = ITR
        #with open(pickle_filepath, 'wb') as f:
        #pickle.dump(ERROR, f, pickle.HIGHEST_PROTOCOL)
    elif FLAGS.mode == "test":
        count = 10
        l_image, color_images = batch_reader.get_random_batch(count)
        feed_dict = {
            images: l_image,
            lab_images: color_images,
            train_phase: False
        }
        save_dir = os.path.join(FLAGS.logs_dir, "image_pred")
        pred = sess.run(pred_image, feed_dict=feed_dict)
        for itr in range(count):
            utils.save_image(color_images[itr], save_dir, "gt" + str(itr))
            utils.save_image(pred[itr].astype(np.float64), save_dir,
                             "pred" + str(itr))
        print("--- Images saved on test run ---")
Exemplo n.º 21
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)
Exemplo n.º 22
0
def main(argv=None):

    with tf.Graph().as_default(), tf.device('/cpu:0'):

        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,
                                            reuse=tf.AUTO_REUSE)

        if FLAGS.mode == 'train':
            labels = tf.squeeze(annotation, squeeze_dims=[3])
            class_weight = tf.constant([
                0.0013, 0.0012, 0.0305, 0.0705, 0.8689, 0.2045, 0.0937, 0.8034,
                0.0046, 0.1884, 0.1517, 0.5828, 0.0695, 0.0019
            ])  #1/freq^(1/3)
            weights = tf.gather(class_weight, labels)
            loss = tf.reduce_mean(
                (tf.losses.sparse_softmax_cross_entropy(logits=logits,
                                                        labels=labels,
                                                        weights=weights)))
            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)
            tf.summary.scalar("entropy", loss)
            print("Setting up summary op...")
            summary_op = tf.summary.merge_all()

            print("Setting up dataset reader")
            image_options = {'resize': False, 'resize_size': IMAGE_SIZE}
            train_records, valid_records, test_records = scene_parsing.read_dataset(
                FLAGS.patch_dir)
            print(len(train_records))
            print(len(valid_records))
            train_dataset_reader = dataset.BatchDatset(train_records,
                                                       image_options)
            validation_dataset_reader = dataset.BatchDatset(
                valid_records, image_options)
            test_dataset_reader = dataset.BatchDatset(test_records,
                                                      image_options)

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

            # Calculate the gradients for each model tower.
            tower_grads = []
            reuse_vars = tf.AUTO_REUSE
            tf.get_variable_scope().reuse_variables()
            optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate)
            for i in xrange(FLAGS.num_gpus):
                with tf.device(
                        assign_to_device('/gpu:{}'.format(i),
                                         ps_device='/cpu:0')):

                    # Split data between GPUs
                    _x = image[i * FLAGS.batch_size:(i + 1) * FLAGS.batch_size]
                    _y = annotation[i * FLAGS.batch_size:(i + 1) *
                                    FLAGS.batch_size]
                    print(i + 100)
                    pred_annotation, logits = inference(_x,
                                                        keep_probability,
                                                        reuse=reuse_vars)
                    labels = tf.squeeze(_y, squeeze_dims=[3])
                    weights = tf.gather(class_weight, labels)

                    loss = tf.reduce_mean(
                        (tf.losses.sparse_softmax_cross_entropy(
                            logits=logits, labels=labels, weights=weights)))
                    grads = optimizer.compute_gradients(loss)
                    reuse_vars = True
                    tower_grads.append(grads)

            # We must calculate the mean of each gradient. Note that this is the
            # synchronization point across all towers.
            print('average_gradients')
            #print(tower_grads)
            grads = average_gradients(tower_grads)
            print(
                '*******************************************************************'
            )
            print('apply_gradients')
            # Apply the gradients to adjust the shared variables.
            train_op = optimizer.apply_gradients(grads)

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

        print("Setting up global_variables_initializer...")
        init = tf.global_variables_initializer()
        print("Done initial")
        # read old model
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        model_name = [f for f in os.listdir(FLAGS.logs_dir) if ("ckpt" in f)]
        print(model_name)
        config = tf.ConfigProto(
            allow_soft_placement=True)  #, log_device_placement=True)
        with tf.Session(config=config) as sess:
            print('run init sess')
            sess.run(init)
            print('done init sess')
            # read old model
            if len(model_name) > 0:
                model_name = [f for f in model_name if ("meta" in f)]
                saver.restore(
                    sess, FLAGS.logs_dir +
                    model_name[0][:model_name[0].find("meta") - 1])
                print("Model restored..." +
                      model_name[0][:model_name[0].find("meta") - 1])

            if FLAGS.mode == "train":
                for itr in xrange(MAX_ITERATION):
                    # Get a batch for each GPU
                    train_images, train_annotations = train_dataset_reader.next_batch(
                        FLAGS.batch_size * FLAGS.num_gpus)
                    feed_dict = {
                        image: train_images,
                        annotation: train_annotations,
                        keep_probability: 0.85
                    }

                    sess.run(train_op, feed_dict=feed_dict)

                    if itr % 50 == 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 * FLAGS.num_gpus)
                        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 == "evaluate":
                test_images, test_annotations = load_val_data()
                number_patches = test_images.shape[0]
                test_images1 = np.ndarray((1, patch_size, patch_size, 3),
                                          dtype=np.uint8)
                pred_800 = np.ndarray((number_patches, patch_size, patch_size),
                                      dtype=np.uint8)

                print("Shapes of images:")
                print(np.shape(test_images))
                print(np.shape(test_annotations))

                for i in range(0, number_patches):
                    test_images1[0, :, :, :] = test_images[i, :, :, :]
                    pred = sess.run(pred_annotation,
                                    feed_dict={
                                        image: test_images1,
                                        keep_probability: 1.0
                                    })
                    pred = np.squeeze(pred, axis=3)
                    # print(i, np.max(pred))
                    pred_800[i] = ndi.zoom(pred[0],
                                           patch_size / IMAGE_SIZE,
                                           order=0)

                np.save(FLAGS.result_dir + "pred_800" + ".npy", pred_800)
Exemplo n.º 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.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(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)
            xx = random.randint(0, 100)
            xy = random.randint(0, 100)
            h = random.randint(50, 100)
            w = random.randint(50, 100)
            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(20):
                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 = random.randint(50, 100)
                xy = random.randint(50, 100)
                h = random.randint(50, 100)
                w = random.randint(50, 100)
                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, FLAGS.logs_dir + "model_z.ckpt", 500)

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(
            2)
        xx = random.randint(50, 100)
        xy = random.randint(50, 100)
        h = random.randint(50, 100)
        w = random.randint(50, 100)
        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(20):
            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)
Exemplo n.º 24
0
dataset_train = ShapesDataset()
dataset_train.load_shapes(train_images_num, IMAGE_SIZE, IMAGE_SIZE)
train_records = dataset_train.image_info

dataset_val = ShapesDataset()
dataset_val.load_shapes(val_images_num, IMAGE_SIZE, IMAGE_SIZE)
valid_records = dataset_val.image_info

# ---------------------------------------------#
print(len(train_records))
print(len(valid_records))

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

with tf.Session() as sess:
    images = tf.placeholder("float", [None, IMAGE_SIZE, IMAGE_SIZE, 3])
    labels = tf.placeholder(tf.int32, [None, IMAGE_SIZE, IMAGE_SIZE, 1])
    # feed_dict = {images: img1,labels:}
    # batch_images = tf.expand_dims(images, 0)
    batch_images = images
    if train == 1:
        vgg_fcn = fcn32_vgg.FCN32VGG()
        with tf.name_scope("content_vgg"):
            vgg_fcn.build(batch_images,
                          train=True,
                          num_classes=NUM_CLASS,
                          debug=True)
Exemplo n.º 25
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)  # [n,224,224,1] , [n,224,224,151]
    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 == "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)
Exemplo n.º 26
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_value, pred_annotation, logits, pred_prob = 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)
    #logits:the last layer of conv net
    #labels:the ground truth
    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()

    #Create a file to write logs.
    #filename='logs'+ FLAGS.mode + str(datetime.datetime.now()) + '.txt'
    filename = "logs_%s%s.txt" % (FLAGS.mode, datetime.datetime.now())
    path_ = os.path.join(FLAGS.logs_dir, filename)
    logs_file = open(path_, 'w')
    logs_file.write("The logs file is created at %s\n" %
                    datetime.datetime.now())
    logs_file.write("The mode is %s\n" % (FLAGS.mode))
    logs_file.write(
        "The train data batch size is %d and the validation batch size is %d.\n"
        % (FLAGS.batch_size, FLAGS.v_batch_size))
    logs_file.write("The train data is %s.\n" % (FLAGS.data_dir))
    logs_file.write("The model is ---%s---.\n" % FLAGS.logs_dir)

    print("Setting up image reader...")
    logs_file.write("Setting up image reader...\n")
    train_records, valid_records = scene_parsing.my_read_dataset(
        FLAGS.data_dir)
    print('number of train_records', len(train_records))
    print('number of valid_records', len(valid_records))
    logs_file.write('number of train_records %d\n' % len(train_records))
    logs_file.write('number of valid_records %d\n' % 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 not train,restore the model trained before
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "visualize":
        #if need image_name to compare
        valid_images, valid_annotations, valid_filename = validation_dataset_reader.get_random_batch(
            FLAGS.v_batch_size)
        #valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)

        t_start = time.time()
        pred_value, pred, logits_, pred_prob_ = sess.run(
            [pred_annotation_value, pred_annotation, logits, pred_prob],
            feed_dict={
                image: valid_images,
                annotation: valid_annotations,
                keep_probability: 1.0
            })

        print('the shape of pred_value', pred_value.shape)
        print('the shape of pred', pred.shape)

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

        print('the shape of pred_value after squeeze', pred_value.shape)
        print('the shape of pred after squeeze', pred.shape)

        t_elapsed_s = time.time() - t_start
        speed_s = len(pred) / t_elapsed_s
        print('the num of picture', len(pred))
        print('speed_neddle', speed_s)

        re_save_dir = "%s%s" % (FLAGS.result_dir, datetime.datetime.now())
        logs_file.write("The result is save at file'%s'.\n" % re_save_dir)
        logs_file.write("The number of part visualization is %d.\n" %
                        FLAGS.v_batch_size)

        #Check the result path if exists.
        if not os.path.exists(re_save_dir):
            print("The path '%s' is not found." % re_save_dir)
            print("Create now ...")
            os.makedirs(re_save_dir)
            print("Create '%s' successfully." % re_save_dir)
            logs_file.write("Create '%s' successfully.\n" % re_save_dir)

        #label_predict_pixel
        for itr in range(FLAGS.v_batch_size):
            t_fit = time.time()
            filename = valid_filename[itr]['filename']

            valid_images_ = pred_visualize(valid_images[itr], pred[itr])
            valid_images_ = anno_visualize(valid_images_,
                                           valid_annotations[itr])

            #valid_images_=pred_visualize(valid_images[itr],pred[itr])
            #valid_images_=fit_ellipse_findContours(valid_images[itr],np.expand_dims(pred[itr],axis=2).astype(np.uint8))
            #valid_images_=fit_ellipse(valid_images[itr],np.expand_dims(pred[itr],axis=2).astype(np.uint8))

            utils.save_image(valid_images_.astype(np.uint8),
                             re_save_dir,
                             name="inp_" + filename)
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             re_save_dir,
                             name="gt_" + filename)
            utils.save_image(pred[itr].astype(np.uint8),
                             re_save_dir,
                             name="pred_" + filename)
            utils.save_image(pred_value[itr].astype(np.uint8),
                             re_save_dir,
                             name="heat_" + filename)
            print("Saved image: %d" % itr)

        t_elapsed_ = time.time() - t_start
        speed = len(pred) / t_elapsed_
        print('the num of picture', len(pred))
        print('speed_neddle', speed)

    elif FLAGS.mode == "accurary":
        count = 0
        if_con = True
        accu_iou_t = 0
        accu_pixel_t = 0

        while if_con:
            count = count + 1
            valid_images, valid_annotations, valid_filenames, if_con, start, end = validation_dataset_reader.next_batch_valid(
                FLAGS.v_batch_size)
            valid_loss, pred_anno = sess.run(
                [loss, pred_annotation],
                feed_dict={
                    image: valid_images,
                    annotation: valid_annotations,
                    keep_probability: 1.0
                })
            accu_iou, accu_pixel = accu.caculate_accurary(
                pred_anno, valid_annotations)
            print("Ture %d ---> the data from %d to %d" % (count, start, end))
            print("%s ---> Validation_pixel_accuary: %g" %
                  (datetime.datetime.now(), accu_pixel))
            print("%s ---> Validation_iou_accuary: %g" %
                  (datetime.datetime.now(), accu_iou))
            #Output logs.
            logs_file.write("Ture %d ---> the data from %d to %d\n" %
                            (count, start, end))
            logs_file.write("%s ---> Validation_pixel_accuary: %g\n" %
                            (datetime.datetime.now(), accu_pixel))
            logs_file.write("%s ---> Validation_iou_accuary: %g\n" %
                            (datetime.datetime.now(), accu_iou))

            accu_iou_t = accu_iou_t + accu_iou
            accu_pixel_t = accu_pixel_t + accu_pixel
        print("%s ---> Total validation_pixel_accuary: %g" %
              (datetime.datetime.now(), accu_pixel_t / count))
        print("%s ---> Total validation_iou_accuary: %g" %
              (datetime.datetime.now(), accu_iou_t / count))
        #Output logs
        logs_file.write("%s ---> Total validation_pixel_accurary: %g\n" %
                        (datetime.datetime.now(), accu_pixel_t / count))
        logs_file.write("%s ---> Total validation_iou_accurary: %g\n" %
                        (datetime.datetime.now(), accu_iou_t / count))

    elif FLAGS.mode == "all_visualize":

        re_save_dir = "%s%s" % (FLAGS.result_dir, datetime.datetime.now())
        logs_file.write("The result is save at file'%s'.\n" % re_save_dir)
        logs_file.write("The number of part visualization is %d.\n" %
                        FLAGS.v_batch_size)

        #Check the result path if exists.
        if not os.path.exists(re_save_dir):
            print("The path '%s' is not found." % re_save_dir)
            print("Create now ...")
            os.makedirs(re_save_dir)
            print("Create '%s' successfully." % re_save_dir)
            logs_file.write("Create '%s' successfully.\n" % re_save_dir)
        re_save_dir_im = os.path.join(re_save_dir, 'images')
        re_save_dir_heat = os.path.join(re_save_dir, 'heatmap')
        re_save_dir_ellip = os.path.join(re_save_dir, 'ellip')
        re_save_dir_transheat = os.path.join(re_save_dir, 'transheat')
        if not os.path.exists(re_save_dir_im):
            os.makedirs(re_save_dir_im)
        if not os.path.exists(re_save_dir_heat):
            os.makedirs(re_save_dir_heat)
        if not os.path.exists(re_save_dir_ellip):
            os.makedirs(re_save_dir_ellip)
        if not os.path.exists(re_save_dir_transheat):
            os.makedirs(re_save_dir_transheat)
        count = 0
        if_con = True
        accu_iou_t = 0
        accu_pixel_t = 0

        while if_con:
            count = count + 1
            valid_images, valid_annotations, valid_filename, if_con, start, end = validation_dataset_reader.next_batch_valid(
                FLAGS.v_batch_size)
            pred_value, pred, logits_, pred_prob_ = sess.run(
                [pred_annotation_value, pred_annotation, logits, pred_prob],
                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)
            pred_value = np.squeeze(pred_value, axis=3)

            #label_predict_pixel
            for itr in range(len(pred)):
                filename = valid_filename[itr]['filename']

                valid_images_ = anno_visualize(valid_images[itr].copy(),
                                               pred[itr])
                valid_images_ = pred_visualize(valid_images_,
                                               valid_annotations[itr])

                #save result

                utils.save_image(valid_images_.astype(np.uint8),
                                 re_save_dir_im,
                                 name="inp_" + filename)
                if FLAGS.fit_ellip:
                    valid_images_ellip = fit_ellipse_findContours(
                        valid_images[itr].copy(),
                        np.expand_dims(pred[itr], axis=2).astype(np.uint8))
                    #valid_images_=fit_ellipse(valid_images[itr],np.expand_dims(pred[itr],axis=2).astype(np.uint8))
                    utils.save_image(valid_images_ellip.astype(np.uint8),
                                     re_save_dir_ellip,
                                     name="ellip_" + filename)
                if FLAGS.heatmap:
                    heat_map = density_heatmap(pred_prob_[itr, :, :, 1])
                    utils.save_image(heat_map.astype(np.uint8),
                                     re_save_dir_heat,
                                     name="heat_" + filename)
                if FLAGS.trans_heat:
                    trans_heat_map = translucent_heatmap(
                        valid_images[itr],
                        heat_map.astype(np.uint8).copy())
                    utils.save_image(trans_heat_map,
                                     re_save_dir_transheat,
                                     name="trans_heat_" + filename)

    logs_file.close()
    if FLAGS.mode == "visualize" or FLAGS.mode == "all_visualize":
        result_logs_file = os.path.join(re_save_dir, filename)
        shutil.copyfile(path_, result_logs_file)
Exemplo n.º 27
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, 1], name="annotation")

    #logits = inference(image, keep_probability)
    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)
    #logits:the last layer of conv net
    #labels:the ground truth

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
                                                       labels=tf.squeeze(
                                                           annotation,
                                                           squeeze_dims=[1]),
                                                       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, test_records = scene_parsing.my_read_dataset(
        FLAGS.data_dir)
    print('number of train_records', len(train_records))
    print('number of valid_records', len(valid_records))
    print('number of test_records', len(test_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 not train,restore the model trained before
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")

    if FLAGS.mode == "train":
        current_itr = FLAGS.train_itr
        for itr in xrange(current_itr, MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict_train = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.5
            }
            sess.run(train_op, feed_dict=feed_dict_train)
            if itr % 10 == 0:
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict_train)

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

            if itr % 10 == 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
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)
Exemplo n.º 28
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=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)
    train_records, valid_records, test_records = fashion_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("test_records length :", len(test_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)
        test_dataset_reader = dataset.BatchDatset(test_records, image_options)
    if FLAGS.mode == 'visualize':
        validation_dataset_reader = dataset.BatchDatset(
            valid_records, image_options)
    if FLAGS.mode == 'test':
        test_dataset_reader = dataset.BatchDatset(test_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)

        fd.mode_test(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
                     pred_annotation, image, annotation, keep_probability,
                     logits, NUM_OF_CLASSES)

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

        fd.mode_visualize(sess, FLAGS, VIS_DIR, validation_dataset_reader,
                          pred_annotation, image, annotation, keep_probability,
                          NUM_OF_CLASSES)

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

        fd.mode_test(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
                     pred_annotation, image, annotation, keep_probability,
                     logits, NUM_OF_CLASSES)

    sess.close()
Exemplo n.º 29
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]))
        cv2.imwrite(segmented_dir + 'color.png', scaledImage2)
        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)
Exemplo n.º 30
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

    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(
    )  # 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)  # 这两个是对象定义

    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":
        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 = sess.run(loss, feed_dict=feed_dict)
                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]  # im_2015
            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))  # im_2015

        allImg = []

        for n, im_2017_part in enumerate(im_2017_list):
            feed_dict_valid = {image: im_2017_part, keep_probability: 1.0}
            a = sess.run(pred_annotation, feed_dict=feed_dict_valid)
            a = np.mean(a, axis=(0, 3))
            print(a.shape)
            allImg.append(a)
            # tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/2017_%d.tif' % n, a)  # 保存分割后的图片

        result = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
        tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/2017_result_1012.tif',
                    result)