def main(argv=None):
    print "Reading notMNIST data..."
    train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = \
        read_notMNIST.get_notMNISTData(FLAGS.data_dir)

    print "Setting up tf model..."
    dataset = tf.placeholder(tf.float32, shape=(None, IMAGE_SIZE * IMAGE_SIZE))

    labels = tf.placeholder(tf.float32, shape=(None, NUMBER_OF_CLASSES))

    global_step = tf.Variable(0, trainable=False)

    logits = inference_fully_convolutional(dataset)

    for var in tf.trainable_variables():
        utils.add_to_regularization_and_summary(var)

    loss_val = loss(logits, labels)
    train_op = train(loss_val, global_step)
    summary_op = tf.merge_all_summaries()
    with tf.Session() as sess:
        print "Setting up summary and saver..."
        sess.run(tf.initialize_all_variables())
        summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
        saver = tf.train.Saver()
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print "Model restored!"

        if FLAGS.mode == "train":
            for step in xrange(MAX_ITERATIONS):
                offset = (step * BATCH_SIZE) % (train_labels.shape[0] - BATCH_SIZE)

                batch_data = train_dataset[offset:(offset + BATCH_SIZE), :]
                batch_labels = train_labels[offset:(offset + BATCH_SIZE), :]

                feed_dict = {dataset: batch_data, labels: batch_labels}
                if step % 100 == 0:
                    l, summary_str = sess.run([loss_val, summary_op], feed_dict=feed_dict)
                    print "Step: %d Mini batch loss: %g"%(step, l)
                    summary_writer.add_summary(summary_str, step)

                if step % 1000 == 0:
                    valid_loss = sess.run(loss_val, feed_dict={dataset:valid_dataset, labels:valid_labels})
                    print "-- Validation loss %g" % valid_loss
                    saver.save(sess, FLAGS.logs_dir +"model.ckpt", global_step=step)

                sess.run(train_op, feed_dict=feed_dict)

        test_loss = sess.run(loss_val, feed_dict={dataset:test_dataset, labels:test_labels})
        print "Test loss: %g" % test_loss
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

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

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

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

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

    print("... main >> 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("... main >> 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("... main >> Model restored...")

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

	    print("itr = %d" % (itr) )	
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
            feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85}

            sess.run(train_op, feed_dict=feed_dict)

            if itr % 2 == 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 % 4 == 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)
Esempio n. 3
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)
Esempio n. 4
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE[0], IMAGE_SIZE[1], 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE[0], IMAGE_SIZE[1], 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 = task2_data.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...")
    #saver.restore(sess, '/home/cqiuac/FCN.tensorflow-master/logs/model.ckpt-3000')

    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)
                if valid_loss < 0.4:
                    print('Goal reached, program ends.')
                    break
                    return 0

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

            for itr in range(FLAGS.batch_size):
                corrRecord = valid_records[2 * runtime + itr]
                storePath = '/home/cqiuac/FCN.tensorflow-master/Data_zoo/comp5421_TASK2/val/preLabel/'
                oriPath = '/home/cqiuac/FCN.tensorflow-master/Data_zoo/comp5421_TASK2/val/labels/'
                #utils.save_image(valid_images[itr].astype(np.uint8), storePath, name=corrRecord['filename'] + 'Image')
                #utils.save_image(40*valid_annotations[itr].astype(np.uint8), storePath, name=corrRecord['filename'] + 'Label')
                utils.save_image(pred[itr].astype(np.uint8), storePath, name=corrRecord['filename'])
                imgOri = Image.open(oriPath + corrRecord['filename']+'.png')
                OriSize = imgOri.size
                imgPre = Image.open(storePath + corrRecord['filename']+'.png')
                imgPre = imgPre.resize((OriSize[0], OriSize[1]), Image.ANTIALIAS)
                imgPre.save(storePath + corrRecord['filename']+'.png')
                print("Saved image: %s" % corrRecord['filename'])

    else:
        test_image = misc.imread('./Data_zoo/test/1.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)
        utils.save_image(pred[0].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5))
        print("Saved image: succeed")
Esempio n. 5
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)
Esempio 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, 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)
Esempio n. 7
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)
    print('loss calculatio')
    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)

    #Redefine train_records and valid_records here
    #print(len(train_images),'length of train_records')
    #print(len(valid_images),'length of valid_records')

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    #if FLAGS.mode == 'train': #Training Mode
    #train_dataset_reader = dataset.BatchDatset(train_records, image_options)
    #validation_dataset_reader = dataset.BatchDatset(valid_records, image_options)
    #All of them are 4D Tensors
    #train_labels =np.array(train_labels)
    #train_data = np.array(train_data)
    #valid_data = np.array(valid_data)
    #valid_labels = np.array(valid_labels)
    #Input image is of size idx*966*1225*3 -> idx*224*224*3
    print(train_data.shape[0], 'number of train images')
    resized_train_data = []
    resized_train_labels = []
    for idx in range(train_data.shape[0]):
        resized = scipy.ndimage.interpolation.zoom(
            train_data[idx],
            (224 / 966, 224 / 1225, 1.0))  #966 to 224 factor of
        resized_train_data.append(resized)
        #train_data[idx] = misc.imresize(train_data[idx],[IMAGE_SIZE, IMAGE_SIZE], interp='bilinear')
        #train_labels[idx] = misc.imresize(train_labels[idx],[IMAGE_SIZE, IMAGE_SIZE], interp='bilinear')

        resized_labels = scipy.ndimage.interpolation.zoom(
            train_labels[idx], (224 / 966, 224 / 1225))  #966 to 224 factor of
        resized_labels = resized_labels[:, :, np.newaxis]
        resized_train_labels.append(resized_labels)

    resized_train_data = np.asarray(resized_train_data)
    print(resized_train_data.shape, 'resized array shape')
    np.save('training_images.npy', resized_train_data)
    resized_train_labels = np.asarray(resized_train_labels)
    print(resized_train_labels.shape, 'size of training labels')
    np.save('training_annotations.npy', resized_train_labels)

    resized_valid_data = []
    resized_valid_labels = []
    #resized_labels= np.zeros(224,224,1)
    for idx in range(valid_data.shape[0]):
        valid_resized = scipy.ndimage.interpolation.zoom(
            valid_data[idx],
            (224 / 966, 224 / 1225, 1.0))  #966 to 224 factor of
        resized_valid_data.append(valid_resized)
        resized_labels_array = scipy.ndimage.interpolation.zoom(
            valid_labels[idx], (224 / 966, 224 / 1225))  #966 to 224 factor of
        #convert labels to 3D array with 1 channel
        #resized_valid_labels = np.reshape(resized_labels,(resized_labels.shape[0],resized_labels.shape[1],resized_labels.shape[2],1))
        resized_labels_array = resized_labels_array[:, :, np.newaxis]
        resized_valid_labels.append(resized_labels_array)

        #valid_data[idx] = misc.imresize(valid_data[idx],[IMAGE_SIZE, IMAGE_SIZE], interp='bilinear')
        #valid_labels[idx] = misc.imresize(valid_labels[idx],[IMAGE_SIZE, IMAGE_SIZE], interp='bilinear')
    resized_valid_data = np.asarray(resized_valid_data)  #convert to np.array
    np.save('validation_images.npy', resized_valid_data)
    resized_valid_labels = np.asarray(
        resized_valid_labels)  #convert to np.array
    np.save('validation_annotations.npy', resized_valid_labels)
    print(resized_valid_labels.shape, 'Size of validation labels')
    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 = resized_train_data
            train_annotations = resized_train_labels
            #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 = resized_valid_data
                valid_annotations = resized_valid_labels
                #valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
                valid_loss = sess.run(loss,
                                      feed_dict={
                                          image: valid_images,
                                          annotation: valid_annotations,
                                          keep_probability: 1.0
                                      })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

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

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

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="inp_" + str(5 + itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
            utils.save_image(pred[itr].astype(np.uint8),
                             FLAGS.logs_dir,
                             name="pred_" + str(5 + itr))
            print("Saved image: %d" % itr)
def main(argv=None):
    input_data = tf.placeholder(tf.float32, [None, 784])
    truth_labels = tf.placeholder(tf.float32, [None, 10])
    dataset = mnist.input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
    pred_labels = inference(input_data)
    entropy = -tf.reduce_sum(truth_labels * tf.log(pred_labels))
    tf.scalar_summary('Cross_entropy', entropy)
    train_vars = tf.trainable_variables()
    for v in train_vars:
        utils.add_to_regularization_and_summary(v)
    train_op = train(entropy, train_vars)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred_labels, 1), tf.argmax(truth_labels, 1)), tf.float32))

    # Session start
    sess = tf.InteractiveSession()
    sess.run(tf.initialize_all_variables())
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
    summary_op = tf.merge_all_summaries()

    def test():
        test_accuracy = accuracy.eval(feed_dict={input_data: dataset.test.images, truth_labels: dataset.test.labels})
        return test_accuracy

    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
    else:
        for i in xrange(MAX_ITERATIONS):
            batch = dataset.train.next_batch(FLAGS.batch_size)
            feed_dict = {input_data: batch[0], truth_labels: batch[1]}
            train_op.run(feed_dict=feed_dict)

            if i % 10 == 0:
                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, i)

            if i % 100 == 0:
                train_accuracy = accuracy.eval(feed_dict=feed_dict)
                print("step: %d, training accuracy: %g" % (i, train_accuracy))

            if i % 5000 == 0:
                saver.save(sess, FLAGS.logs_dir + 'model.ckpt', i)

    train_vars_copy = sess.run([tf.identity(var) for var in train_vars])
    print ('Variables Perecent: %d, Test accuracy: %g' % (100, test()))

    k = tf.placeholder(tf.int32)

    def scatter_add(variables):
        shape = utils.get_tensor_size(variables)
        values, indices = tf.nn.top_k(-1 * variables, tf.cast(k * shape / 100, tf.int32))
        return tf.scatter_add(variables, indices, values)

    def scatter_subtract(variables1, variables2):
        shape = utils.get_tensor_size(variables2)
        values, indices = tf.nn.top_k(-1 * variables1, tf.cast(k * shape / 100, tf.int32))
        return tf.scatter_sub(variables2, indices, values)

    scatter_add_op = [scatter_add(var) for var in train_vars]
    scatter_sub_op = [scatter_subtract(var1, var2) for var1, var2 in zip(train_vars_copy, train_vars)]

    for count in range(1, 20):
        sess.run(scatter_add_op, feed_dict={k: count})
        print ('Variables Perecent: %d, Test accuracy: %g' % ((100 - count), test()))
        sess.run(scatter_sub_op, feed_dict={k: count})
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

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

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

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

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

    print("Setting up dataset reader")
    image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
    if FLAGS.mode == 'train':
        train_dataset_reader = dataset.BatchDatset(image_options)
    #validation_dataset_reader = dataset.BatchDatset (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":
        valid_images, valid_annotations = train_dataset_reader.make_val()
        valid_annotations = createGround(valid_annotations,NUM_OF_CLASSESS)
        for itr in xrange(MAX_ITERATION):
            train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
            train_annotations = createGround(train_annotations,NUM_OF_CLASSESS)
            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_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations,
                                                       keep_probability: 1.0})
                print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr)

    elif FLAGS.mode == "visualize":
        train_dataset_reader = dataset.BatchDatset(image_options)
        valid_images, valid_annotations = train_dataset_reader.get_random_batch(15)

        valid_annotations = createGround(valid_annotations, NUM_OF_CLASSESS)
        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(15):
            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)

        img1 = np.load('../data/x_trn_%d.npy' % NUM_OF_CLASSESS)
        img = np.zeros((224,224,3))
        img[:,:,:] = img1[2000:2224,2000:2224,:]
        img= np.expand_dims(img, axis=0)
        msk1 = np.load('../data/y_trn_%d.npy' % NUM_OF_CLASSESS)
        msk = np.zeros((224, 224, 10))
        msk[:, :, :] = msk1[2000:2224, 2000:2224, :]

        msk = np.expand_dims(msk, axis =0)
        #msk = np.expand_dims(msk, axis=3)
        msk = createGround(msk, NUM_OF_CLASSESS)
        #img = np.reshape(img, (-1, IMAGE_SIZE,IMAGE_SIZE, 3))
        #msk = np.reshape(msk, (-1, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CLASSESS))
        pred_all = sess.run(pred_annotation, feed_dict={image: img, annotation: msk,
                                                        keep_probability: 1.0})
        utils.save_image(img[0].astype(np.uint8), FLAGS.logs_dir, name="input_all3" )
        msk = np.squeeze(msk, axis=3)
        pred_all = np.squeeze(pred_all, axis=3)
        utils.save_image(msk[0].astype(np.uint8), FLAGS.logs_dir, name="gt_all3")
        utils.save_image(pred_all[0].astype(np.uint8), FLAGS.logs_dir, name="pred_all3")
        print("Saved image all tiles")
Esempio n. 10
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()
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}"
        )
Esempio n. 12
0
 def __init__(self, flags):
     """
     Initialize:
         placeholder,
         train_op,
         summary,
         session,
         saver and file_writer
     """
     
     self.flags = flags
     image_size = int(self.flags.image_size)
     num_class = int(self.flags.num_class)
     
     # Place holder
     self.keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
     self.image = tf.placeholder(tf.float32, shape=[None, image_size, image_size, 3], name="input_image")
     self.phase_train = tf.placeholder(tf.bool, name='phase_train')
     self.neighbor_indeces = tf.placeholder(tf.int64, name="neighbor_indeces")
     self.neighbor_vals = tf.placeholder(tf.float32, name="neighbor_vals")
     self.neighbor_shape = tf.placeholder(tf.int64, name="neighbor_shape")
     neighbor_filter = (self.neighbor_indeces, self.neighbor_vals, self.neighbor_shape)
     _image_weights = brightness_weight(self.image, neighbor_filter, sigma_I = 0.05)
     image_weights = convert_to_batchTensor(*_image_weights)
     
     # Prediction and loss
     self.pred_annotation, self.image_segment_logits, self.reconstruct_image = \
         self.inference(self.image, self.keep_probability, self.phase_train, self.flags)
     image_segment = tf.nn.softmax(self.image_segment_logits)
     self.colorized_pred_annotation = utils.batch_colorize(
                                 self.pred_annotation, 0, num_class, self.flags.cmap)
     self.reconstruct_loss = tf.reduce_mean(tf.reshape(
                                 (self.image - self.reconstruct_image)**2, shape=[-1]))        
     batch_soft_ncut = soft_ncut(self.image, image_segment, image_weights)
     self.soft_ncut = tf.reduce_mean(batch_soft_ncut)
     self.loss = self.reconstruct_loss + self.soft_ncut
     
     # Train var and op
     trainable_var = tf.trainable_variables()
     encode_trainable_var = tf.trainable_variables("infer_encode")
     if self.flags.debug:
         for var in trainable_var:
             utils.add_to_regularization_and_summary(var)
     self.reconst_learning_rate, self.train_reconst_op = \
         self.train(self.reconstruct_loss, trainable_var, self.flags)
     self.softNcut_learning_rate, self.train_softNcut_op = \
         self.train(self.soft_ncut, encode_trainable_var, self.flags)    
     self.reconst_learning_rate_summary = tf.summary.scalar("reconst_learning_rate", self.reconst_learning_rate)
     self.softNcut_learning_rate_summary = tf.summary.scalar("softNcut_learning_rate", self.softNcut_learning_rate)
     
     # Summary
     tf.summary.image("input_image", self.image, max_outputs=2)
     tf.summary.image("reconstruct_image", self.reconstruct_image, max_outputs=2)
     tf.summary.image("pred_annotation", self.colorized_pred_annotation, max_outputs=2)   
     reconstLoss_summary = tf.summary.scalar("reconstruct_loss", self.reconstruct_loss)
     softNcutLoss_summary = tf.summary.scalar("soft_ncut_loss", self.soft_ncut)
     totLoss_summary = tf.summary.scalar("total_loss", self.loss)
     self.loss_summary = tf.summary.merge([reconstLoss_summary, softNcutLoss_summary, totLoss_summary])
     self.summary_op = tf.summary.merge_all()
     
     # Session ,saver, and writer
     print("Setting up Session and Saver...")
     self.sess = tf.Session()
     self.saver = tf.train.Saver(max_to_keep=2)
     # 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
     self.train_writer = tf.summary.FileWriter(os.path.join(self.flags.logs_dir, 'train'), self.sess.graph)
     self.validation_writer = tf.summary.FileWriter(os.path.join(self.flags.logs_dir, 'validation'))
     
     print("Initialize tf variables")
     self.sess.run(tf.global_variables_initializer())
     ckpt = tf.train.get_checkpoint_state(self.flags.logs_dir)
     if ckpt and ckpt.model_checkpoint_path:
         self.saver.restore(self.sess, ckpt.model_checkpoint_path)
         print("Model restored...") 
     return
Esempio n. 13
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
    print('annotation', annotation.shape)
    print('image', image.shape)
    # mean = tf.placeholder(tf.float32, name='mean')
    pred_annotation, logits, softmax = inference(
        image, keep_probability)  # logits=resize_F8
    print('logits', logits.shape)
    # 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,
            # tf.nn.sparse_softmax_cross_entropy_with_logits自动对logits(resize_F8)做了softmax处理.
            labels=tf.squeeze(annotation, squeeze_dims=[3]),
            name="entropy"))
    tf.summary.scalar("entropy", loss)  # train val公用一个loss节点运算.

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

    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...")

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

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

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

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

    sess.run(tf.global_variables_initializer())

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

    if FLAGS.mode == "train":
        # mymean = train_dataset_reader._read_images()   #强行运行这个函数,把mean传过来.

        # train_dataset_reader 调用._read_images()  需要在里面修改mean是否运算和return.
        # 生成本次数据集的mean值.
        # mymean = [42.11049008, 65.75782253, 74.11216841]    #这里要根据数据集更新.
        # mymean = [73.9524, 73.9524, 73.9524]
        for itr in xrange(30000, 40000):  # 修改itr数值,接着之前的模型数量后继续训练.
            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85
            }
            #print ('###', annotation.shape)  (???1)
            #print ('$$$', train_annotations.shape)  #(8, 256, 256, 1)
            sess.run(train_op, feed_dict=feed_dict)
            if itr % 10 == 0:
                # 这里不要运算loss
                train_loss, summary_str = sess.run([loss, summary_op],
                                                   feed_dict=feed_dict)
                summary_writer_train.add_summary(summary_str, itr)
                print("Step: %d, Train_loss:%g" % (itr, train_loss))

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

        # summary_writer_val.close()
        summary_writer_train.close()

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

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

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

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

    elif FLAGS.mode == "test":
        im_2017_list = []
        im_2017_list2 = []
        b = []
        global im_2017  # [5106, 15106, 3]
        global new_2017  # [8106, 15106, 3]
        global new_2015
        for i in range(25):
            b = sub[0:3000, i * 600:i * 600 + 600]
            b = np.array([np.array([b for i in range(3)])])
            b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
            print(b.shape)
            '''
            # 多通道
            b = im_2017[0:5106, i * 300 : i * 300 + 300, :]
            b = np.array([b])
            # print(b.shape)
            # b = b.transpose(0, 2, 3, 1)
            im_2017_list.append(b)
        # im_2017_list.append(np.array([np.array([im_2017[0:5106, 15000:15106, 3] for i in range(3)])]).transpose(0, 2, 3, 1))
        #im_2017_list.append(np.array([im_2017[0:5106, 15000:15106, :]]))     # .transpose(0, 2, 3, 1))
        im_2017_list.append(np.array([im_2017[0:5106, 15000:15106, :]]))
        '''

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

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

        #     # 使用crf:
        #     soft = sess.run(softmax, feed_dict=feed_dict_test)
        #     # 运行 sess.run(softmax, feed_dict=feed_dict_test) 得到softmax,即网络前向运算并softmax为概率分布后的结果.
        #     soft = np.mean(soft, axis=0).transpose(2, 0, 1)
        #     # soft = soft.transpose(2, 0, 1)
        #
        #     im_2017_mean = np.mean(im_2017_list[n], axis=0)
        #
        #     # print (im_2017_mean.shape)     #(5106, 300, 3)
        #     c = crf.crf(im_2017_mean, soft)
        #     # print (c.shape)    #(5106, 300)
        #     allImg_soft.append(c)  # 保存整张soft图.
        # Crf = np.concatenate(tuple(allImg_soft), axis=1)  # axis = 1 在第二个维度上进行拼接.
        # tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/crf_sub_1031.tif', Crf)

        res1 = np.concatenate(tuple(allImg), axis=1).astype(np.uint8)
        # res2 = np.concatenate(tuple(allImg2), axis=1).astype(np.uint8)
        # result = np.concatenate((res1, res2), axis=0)
        print(type(res1))
        tiff.imsave('/home/lenovo/2Tdisk/Wkyao/_/2017/1105.tif', res1)
Esempio n. 14
0
def main(argv=None):
    # 1. input placeholders
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=(None, IMAGE_SIZE, IMAGE_SIZE, 3),
                           name="input_image")
    annotation = tf.placeholder(tf.int32,
                                shape=(None, IMAGE_SIZE, IMAGE_SIZE, 1),
                                name="annotation")
    # 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()
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 ---")
Esempio n. 16
0
def main(argv=None):
    data = mnist.input_data.read_data_sets("MNIST_data", one_hot=False)
    images = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE * IMAGE_SIZE], name="input_image")
    tf.image_summary("Input", tf.reshape(images, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]), max_images=2)

    mu, log_var = encoder_fc(images)
    epsilon = tf.random_normal(tf.shape(mu), name="epsilon")
    z = mu + tf.mul(tf.exp(log_var * 0.5), epsilon)

    pred_image = decoder_fc(z)
    entropy_loss = tf.reduce_sum(
        tf.nn.sigmoid_cross_entropy_with_logits(pred_image, images, name="entropy_loss"), reduction_indices=1)
    tf.histogram_summary("Entropy_loss", entropy_loss)
    pred_image_sigmoid = tf.nn.sigmoid(pred_image)
    tf.image_summary("Output", tf.reshape(pred_image_sigmoid, [-1, IMAGE_SIZE, IMAGE_SIZE, 1]), max_images=2)

    KL_loss = -0.5 * tf.reduce_sum(1 + log_var - tf.pow(mu, 2) - tf.exp(log_var), reduction_indices=1)
    tf.histogram_summary("KL_Divergence", KL_loss)

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

    reg_loss = tf.add_n(tf.get_collection("reg_loss"))
    tf.scalar_summary("Reg_loss", reg_loss)
    total_loss = tf.reduce_mean(KL_loss + entropy_loss) + FLAGS.regularization * reg_loss
    tf.scalar_summary("total_loss", total_loss)
    train_op = train(total_loss, train_variables)

    sess = tf.Session()
    summary_op = tf.merge_all_summaries()
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(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...")

    for itr in xrange(MAX_ITERATIONS):
        batch_images, batch_labels = data.train.next_batch(FLAGS.batch_size)
        sess.run(train_op, feed_dict={images: batch_images})

        if itr % 500 == 0:
            entr_loss, KL_div, tot_loss, summary_str = sess.run([entropy_loss, KL_loss, total_loss, summary_op],
                                                                feed_dict={images: batch_images})
            print (
                "Step: %d, Entropy loss: %g, KL Divergence: %g, Total loss: %g" % (itr, np.mean(entr_loss), np.mean(KL_div), tot_loss))
            summary_writer.add_summary(summary_str, itr)

        if itr % 1000 == 0:
            saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=itr)

    def test():
        z_vec = sess.run(z, feed_dict={images: data.test.images})
        write_array = np.hstack((z_vec, np.reshape(data.test.labels, (-1, 1))))
        df = pd.DataFrame(write_array)
        df.to_csv("z_vae_output.csv", header=False, index=False)

    test()
Esempio n. 17
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")

    if FLAGS.optimization == "cross_entropy":
        annotation = tf.placeholder(tf.int32,
                                    shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                    name="annotation")  # For cross entropy
        pred_annotation, logits = inference(image, keep_probability)
        print("pred_annotation, logits shape",
              pred_annotation.get_shape().as_list(),
              logits.get_shape().as_list())

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

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

    elif FLAGS.optimization == "dice":
        annotation = tf.placeholder(tf.int32,
                                    shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1],
                                    name="annotation")  # For DICE
        pred_annotation, logits = inference(image, keep_probability)

        # pred_annotation (argmax) is not differentiable so it cannot be optimized. So in loss, we need to use logits instead of pred_annotation!
        logits = tf.nn.softmax(logits)  # axis = -1 default

        logits2 = tf.slice(logits, [0, 0, 0, 1],
                           [-1, IMAGE_SIZE, IMAGE_SIZE, 1])

        loss = 1 - tl.cost.dice_coe(logits2,
                                    tf.cast(annotation, dtype=tf.float32))

    total_var = tf.trainable_variables()
    # ========================================
    # To limit the training range
    # scope_name = 'inference'
    # trainable_var = [var for var in total_var if scope_name in var.name]
    # ========================================

    # Train all model
    trainable_var = total_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()

    #    for variable in trainable_var:
    #        print(variable)

    #Way to count the number of variables + print variable names
    """
    total_parameters = 0
    for variable in trainable_var:
        # shape is an array of tf.Dimension
        print(variable)
        shape = variable.get_shape()
        print(shape)
        print(len(shape))
        variable_parameters = 1
        for dim in shape:
            print(dim)
            variable_parameters *= dim.value
        print(variable_parameters)
        total_parameters += variable_parameters
    print("Total # of parameters : ", total_parameters)

    """
    # All the variables defined HERE -------------------------------
    #dir_name = 'DICOM_data/mandible/'
    #contour_name = 'brainstem'

    dir_name = 'DICOM_data/cord/'
    contour_name = 'cord'

    batch_size = 3

    opt_crop = True
    crop_shape = (224, 224)
    opt_resize = False
    resize_shape = (224, 224)
    rotation = True
    rotation_angle = [-5, 5]
    bitsampling = False
    bitsampling_bit = [4, 8]
    # --------------------------------------------------------------

    sess = tf.Session()
    #sess = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0})) # CPU ONLY

    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":
        print("Setting up training data...")
        dicom_records = dicom_batch.read_DICOM(dir_name=dir_name +
                                               'training_set',
                                               dir_image=dir_image,
                                               dir_mask=dir_mask,
                                               contour_name=contour_name,
                                               opt_resize=opt_resize,
                                               resize_shape=resize_shape,
                                               opt_crop=opt_crop,
                                               crop_shape=crop_shape,
                                               rotation=rotation,
                                               rotation_angle=rotation_angle,
                                               bitsampling=bitsampling,
                                               bitsampling_bit=bitsampling_bit)

        print("Setting up validation data...")
        validation_records = dicom_batch.read_DICOM(
            dir_name=dir_name + 'validation_set',
            dir_image=dir_image,
            dir_mask=dir_mask,
            contour_name=contour_name,
            opt_resize=opt_resize,
            resize_shape=resize_shape,
            opt_crop=opt_crop,
            crop_shape=crop_shape,
            rotation=False,
            rotation_angle=rotation_angle,
            bitsampling=False,
            bitsampling_bit=bitsampling_bit)

        print("Start training")
        start = time.time()
        train_loss_list = []
        x_train = []
        validation_loss_list = []
        x_validation = []
        # for itr in xrange(MAX_ITERATION):
        for itr in xrange(MAX_ITERATION):  # about 12 hours of work / 2000
            train_images, train_annotations = dicom_records.next_batch(
                batch_size=batch_size)
            """
            ##################################################################################
            if FLAGS.optimization == "dice":
                train_annotations = np.repeat(train_annotations,2,axis=3)
                train_annotations[:,:,:,0] = 1 - train_annotations[:,:,:,1]
            ###################################################################################
            """
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.75
            }
            sess.run(train_op, feed_dict=feed_dict)

            if (itr + 1) % 20 == 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_loss_list.append(train_loss)
                x_train.append(itr + 1)
                #summary_writer.add_summary(summary_str, itr)

            if (itr + 1) % 50 == 0:
                valid_images, valid_annotations = validation_records.next_batch(
                    batch_size=batch_size)
                """
                ##################################################################################
                if FLAGS.optimization == "dice":
                    valid_annotations = np.repeat(valid_annotations, 2, axis=3)
                    valid_annotations[:, :, :, 0] = 1 - valid_annotations[:, :, :, 1]
                ###################################################################################
                """
                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))
                validation_loss_list.append(valid_loss)
                x_validation.append(itr + 1)

            if (itr + 1) % 2000 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", itr + 1)

            end = time.time()
            print("Iteration #", itr + 1, ",", np.int32(end - start), "s")

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

        # Draw loss functions
        #print("train_loss_list : ", train_loss_list)
        #print("validation_loss_list : ", validation_loss_list)
        plt.plot(x_train, train_loss_list, label='train')
        plt.plot(x_validation, validation_loss_list, label='validation')
        plt.title("loss functions")
        plt.xlabel("epoch")
        plt.ylabel("loss")
        plt.ylim(ymin=min(train_loss_list))
        plt.ylim(ymax=max(train_loss_list) * 1.1)
        plt.legend()
        plt.savefig("loss_functions.png")
        #plt.show()

    # Need to add another mode to draw the contour based on image only.
    elif FLAGS.mode == "test":
        print("Setting up test data...")
        img_dir_name = '..\H&N_CTONLY'
        test_batch_size = 10
        test_index = 5
        ind = 0
        test_records = dicom_batchImage.read_DICOMbatchImage(
            dir_name=img_dir_name,
            opt_resize=opt_resize,
            resize_shape=resize_shape,
            opt_crop=opt_crop,
            crop_shape=crop_shape)

        test_annotations = np.zeros([test_batch_size, 224, 224,
                                     1])  # fake input

        for index in range(test_index):
            print("Start creating data")
            test_images = test_records.next_batch(batch_size=test_batch_size)
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: test_images,
                                annotation: test_annotations,
                                keep_probability: 1.0
                            })
            pred = np.squeeze(pred, axis=3)

            print("Start saving data")
            for itr in range(test_batch_size):
                plt.subplot(121)
                plt.imshow(test_images[itr, :, :, 0], cmap='gray')
                plt.title("image")
                plt.subplot(122)
                plt.imshow(pred[itr], cmap='gray')
                plt.title("pred mask")
                plt.savefig(FLAGS.logs_dir + "/Prediction_test" + str(ind) +
                            ".png")
                print("Test iteration : ", ind)
                ind += 1
                # plt.show()
            """
            for itr in range(10):
                test_images = test_records.next_batch(batch_size=test_batch_size)
                pred = sess.run(pred_annotation, feed_dict={image: test_images, annotation: test_annotations, keep_probability: 1.0})
                pred = np.squeeze(pred, axis=3)

                plt.subplot(121)
                plt.imshow(test_images[0,:,:,0], cmap='gray')
                plt.title("image")
                plt.subplot(122)
                plt.imshow(pred[0], cmap='gray')
                plt.title("pred mask")
                plt.savefig(FLAGS.logs_dir + "/Prediction_test" + str(itr) + ".png")
                print("Test iteration : ", itr)
                #plt.show()
            """

    elif FLAGS.mode == "visualize":
        print("Setting up validation data...")
        validation_records = dicom_batch.read_DICOM(
            dir_name=dir_name + 'validation_set',
            dir_image=dir_image,
            dir_mask=dir_mask,
            contour_name=contour_name,
            opt_resize=opt_resize,
            resize_shape=resize_shape,
            opt_crop=opt_crop,
            crop_shape=crop_shape,
            rotation=False,
            rotation_angle=rotation_angle,
            bitsampling=False,
            bitsampling_bit=bitsampling_bit)

        dice_array = []
        bins = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
        # Save the image for display. Use matplotlib to draw this.
        for itr in range(20):
            valid_images, valid_annotations = validation_records.next_batch(
                batch_size=1)
            """
            ## In order to used 'None' Class and mask class at the same time for DICE objective function
            if FLAGS.optimization == "dice":
                valid_annotations = np.repeat(valid_annotations,2,axis=3)
                valid_annotations[:,:,:,0] = 1 - valid_annotations[:,:,:,1]
                print(valid_annotations.shape)
            else:
                valid_annotations = np.squeeze(valid_annotations, axis=3)
            ###################################################################################
            """

            #valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = sess.run(pred_annotation,
                            feed_dict={
                                image: valid_images,
                                annotation: valid_annotations,
                                keep_probability: 1.0
                            })
            pred = np.squeeze(pred, axis=3)
            """
            #################
            logits2 = sess.run(logits, feed_dict={image: valid_images, annotation: valid_annotations, keep_probability: 1.0})

            print(type(logits2), logits2.shape, pred.shape)
            plt.subplot(121)
            plt.imshow(logits2[0, :, :, 0], cmap='gray')
            plt.subplot(122)
            plt.imshow(pred[0, :, :], cmap='gray')
            plt.savefig(FLAGS.logs_dir + "/logits_pred" + str(itr) + ".png")
            #plt.show()
            print(logits2.min(), logits2.max(), np.mean(logits2))
            print(pred[0].min(), pred[0].max(), np.mean(pred[0]))
            print(valid_annotations[0].min(), valid_annotations[0].max(), np.mean(valid_annotations[0]))
            ######################
            """

            print(valid_images.shape, valid_annotations.shape, pred.shape)
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            dice_coeff = dice(valid_annotations[0], pred[0])

            dice_array.append(dice_coeff)
            print("min max of prediction : ",
                  pred.flatten().min(),
                  pred.flatten().max())
            print("min max of validation : ",
                  valid_annotations.flatten().min(),
                  valid_annotations.flatten().max())
            print("DICE : ", dice_coeff)
            print(valid_annotations.shape)

            # Save images
            plt.subplot(131)
            plt.imshow(valid_images[0, :, :, 0], cmap='gray')
            plt.title("image")
            plt.subplot(132)
            plt.imshow(valid_annotations[0, :, :], cmap='gray')
            plt.title("mask original")
            plt.subplot(133)
            plt.imshow(pred[0], cmap='gray')
            plt.title("mask predicted")
            plt.suptitle("DICE : " + str(dice_coeff))

            plt.savefig(FLAGS.logs_dir + "/Prediction_validation" + str(itr) +
                        ".png")
            # plt.show()

        plt.figure()
        plt.hist(dice_array, bins)
        plt.xlabel('Dice')
        plt.ylabel('frequency')
        plt.title('Dice coefficient distribution of validation dataset')
        plt.savefig(FLAGS.logs_dir + "/dice histogram" + ".png")
Esempio n. 18
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))  # 打印出来时间
Esempio n. 19
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)
train_phase = tf.placeholder(tf.bool, name="train_phase")
images = tf.placeholder(tf.float32,
                        shape=[None, None, None, 1],
                        name='L_images')
lab_images = tf.placeholder(tf.float32,
                            shape=[None, None, None, 3],
                            name="LAB_images")
pred_image = HyperColumns(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.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()
def main(argv=None):
    if not os.path.exists(FLAGS.logs_dir):
        os.makedirs(FLAGS.logs_dir)

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

    pred_annotation, logits = inference(image, keep_probability)

    # tf.image_summary("input_image", image, max_images=2)
    # tf.image_summary("ground_truth", tf.cast(annotation, tf.uint8), max_images=2)
    # tf.image_summary("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_images=2)
    loss = tf.reduce_mean(
        (tf.nn.sparse_softmax_cross_entropy_with_logits(logits, annotation)))
    #loss_bg = tf.reduce_mean((tf.contrib.losses.mean_squared_error(logits[:, :, :, 0], annotation[:, :, :, 0])))
    #loss_fg = tf.reduce_mean((tf.contrib.losses.mean_squared_error(logits[:, :, :, 1], annotation[:, :, :, 1])))
    #loss_new = 0.25 * loss_bg + loss_fg
    # tf.scalar_summary("entropy", loss)

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

    # TODO new loss
    train_op = train(loss, trainable_var)

    # TODO next batch file name suffle
    data = sorted(glob(os.path.join(FLAGS.data_dir, "*.png")))
    data2 = sorted(glob(os.path.join(FLAGS.data2_dir, "*.png")))
    data += data2
    train_size = len(data)

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

    print("Setting up Saver...")
    saver = tf.train.Saver(max_to_keep=4)
    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...{}".format(ckpt.model_checkpoint_path))
    else:
        print('No model found')

    if FLAGS.mode == "train":
        step = 0
        for epoch in xrange(MAX_ITERATION):
            np.random.shuffle(data)
            for batch_itr in xrange(train_size / FLAGS.batch_size):
                print('[{:d}/{:d}] [{:d}/{:d}]'.format(
                    epoch, MAX_ITERATION, batch_itr,
                    train_size / FLAGS.batch_size))
                train_images_name = data[batch_itr *
                                         FLAGS.batch_size:(batch_itr + 1) *
                                         FLAGS.batch_size]

                train_images = [
                    scipy.misc.imread(train_image_name).astype(np.uint8)
                    for train_image_name in train_images_name
                ]

                # Is this the normal way?
                train_annotations = [
                    scipy.misc.imread(
                        os.path.join(
                            FLAGS.label_dir,
                            train_annotation_name.split('/')[-1])).astype(
                                np.uint8) / 255
                    for train_annotation_name in train_images_name
                ]

                # train_images, train_annotations = traitn_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 step % 5 == 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" % (step, train_loss))
                    # summary_writer.add_summary(summary_str, step)

                if step % 100 == 0:
                    # train_loss, summary_str = sess.run([loss, summary_op], feed_dict=feed_dict)
                    scipy.misc.imsave(
                        'logs/{:d}_image.png'.format(step),
                        utils.merge(np.array(train_images), SAMPLE_SHAPE))
                    scipy.misc.imsave(
                        'logs/{:d}_gt.png'.format(step),
                        utils.heatmap_visualize(
                            utils.merge(np.array(train_annotations),
                                        SAMPLE_SHAPE,
                                        is_gray=True)))

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

                    pred = np.squeeze(pred, axis=3)
                    scipy.misc.imsave(
                        'logs/{:d}_pred.png'.format(step),
                        utils.heatmap_visualize(
                            utils.merge(pred, SAMPLE_SHAPE, is_gray=True)))
                    # summary_writer.add_summary(summary_str, step)

                if step % 300 == 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))
                    print('checkpoint')
                    saver.save(sess, FLAGS.logs_dir + "model.ckpt", step)

                step += 1

    elif FLAGS.mode == "visualize":
        # valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
        train_images = scipy.misc.imread(
            '/data/vllab1/Github/streetview_synthesize/aachen_000009_000019_leftImg8bit.png'
        ).astype(np.uint8)
        train_annotations = scipy.misc.imread(
            '/data/vllab1/Github/streetview_synthesize/aachen_000009_000019_gtFine_labelIds.png'
        ).astype(np.uint8)
        valid_images = [train_images]
        #valid_annotations = [np.expand_dims(train_annotations, axis=3)]
        valid_annotations = [train_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):
            scipy.misc.imsave("inp_" + str(5 + itr) + '.png',
                              valid_images[itr].astype(np.uint8))
            scipy.misc.imsave("gt_" + str(5 + itr) + '.png',
                              valid_annotations[itr].astype(np.uint8))
            scipy.misc.imsave("pred_" + str(5 + itr) + '.png',
                              pred[itr].astype(np.uint8) * 255)
            #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":
        data, gt = [], []
        dataset_dir = '../../../dataset/CITYSCAPES/leftImg8bit_trainvaltest/leftImg8bit/val'
        for folder in os.listdir(dataset_dir):
            path = os.path.join(dataset_dir, folder, "*_leftImg8bit.png")
            data.extend(glob(path))

        #np.random.shuffle(data)

        data_size = len(data)
        for batch_itr in range(0, data_size / FLAGS.batch_size):
            print('[{:d}/{:d}]'.format(batch_itr,
                                       data_size / FLAGS.batch_size))
            val_images_name = data[batch_itr *
                                   FLAGS.batch_size:(batch_itr + 1) *
                                   FLAGS.batch_size]

            val_images = [
                scipy.misc.imresize(
                    scipy.misc.imread(val_image_name).astype(np.uint8),
                    0.25,
                    interp='bilinear',
                    mode=None) for val_image_name in val_images_name
            ]

            pred, heatmap = sess.run([pred_annotation, logits],
                                     feed_dict={
                                         image: val_images,
                                         keep_probability: 1.0
                                     })
            heatmap_s = heatmap[:, :, :, 1]
            smin = heatmap_s.min()
            heatmap_s -= smin
            heatmap_s = heatmap_s * heatmap_s
            #valid_annotations = [np.squeeze(valid_annotations, axis=3)]
            pred = np.squeeze(pred, axis=3)
            #heatmap = np.copy(logits[:,:,:,1])

            scipy.misc.imsave('test/{:d}_image.png'.format(batch_itr),
                              utils.merge(np.array(val_images), SAMPLE_SHAPE))
            scipy.misc.imsave(
                'test/{:d}_pred.png'.format(batch_itr),
                utils.heatmap_visualize(
                    utils.merge(pred, SAMPLE_SHAPE, is_gray=True)))
            import math
            scipy.misc.imsave(
                'test/{:d}_logit.png'.format(batch_itr),
                utils.heatmap_visualize(
                    utils.merge(heatmap_s * heatmap_s,
                                SAMPLE_SHAPE,
                                is_gray=True)))
Esempio n. 22
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)
Esempio 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.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()
def main(argv=None):
    input_data = tf.placeholder(tf.float32, [None, 784])
    truth_labels = tf.placeholder(tf.float32, [None, 10])
    dataset = mnist.input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
    pred_labels = inference(input_data)
    entropy = -tf.reduce_sum(truth_labels * tf.log(pred_labels))
    tf.scalar_summary('Cross_entropy', entropy)
    train_vars = tf.trainable_variables()
    for v in train_vars:
        utils.add_to_regularization_and_summary(v)
    train_op = train(entropy, train_vars)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred_labels, 1), tf.argmax(truth_labels, 1)), tf.float32))

    # Session start
    sess = tf.InteractiveSession()
    sess.run(tf.initialize_all_variables())
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)
    summary_op = tf.merge_all_summaries()

    def test():
        test_accuracy = accuracy.eval(feed_dict={input_data: dataset.test.images, truth_labels: dataset.test.labels})
        return test_accuracy

    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
    else:
        for i in xrange(MAX_ITERATIONS):
            batch = dataset.train.next_batch(FLAGS.batch_size)
            feed_dict = {input_data: batch[0], truth_labels: batch[1]}
            train_op.run(feed_dict=feed_dict)

            if i % 10 == 0:
                summary_str = sess.run(summary_op, feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, i)

            if i % 100 == 0:
                train_accuracy = accuracy.eval(feed_dict=feed_dict)
                print("step: %d, training accuracy: %g" % (i, train_accuracy))

            if i % 5000 == 0:
                saver.save(sess, FLAGS.logs_dir + 'model.ckpt', i)
    print(len(train_vars))
    train_vars_copy = sess.run([tf.identity(var) for var in train_vars])
    print('Variables Perecent: %d, Test accuracy: %g' % (100, test()))

    k = tf.placeholder(tf.int32)

    def scatter_add(variables):
        shape = utils.get_tensor_size(variables)
        values, indices = tf.nn.top_k(-1 * variables, tf.cast(k * shape / 100, tf.int32))
        return tf.scatter_add(variables, indices, values)

    def scatter_subtract(variables1, variables2):
        shape = utils.get_tensor_size(variables2)
        values, indices = tf.nn.top_k(-1 * variables1, tf.cast(k * shape / 100, tf.int32))
        return tf.scatter_sub(variables2, indices, values)

    scatter_add_op = [scatter_add(var) for var in train_vars]
    scatter_sub_op = [scatter_subtract(var1, var2) for var1, var2 in zip(train_vars_copy, train_vars)]
    for count in range(1, 20):
        sess.run(scatter_add_op, feed_dict={k: count})
        print('Variables Perecent: %d, Test accuracy: %g' % ((100 - count), test()))
        sess.run(scatter_sub_op, feed_dict={k: count})
Esempio 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")
    c_gt = tf.placeholder(tf.int32, shape=[None, 2], name="c_groundtruth")

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

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

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

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

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

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

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

    print("Setting up dataset reader")

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

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

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

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

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

        if itr % 200 == 0:
            valid_images, valid_annotations, valid_c_gt = validation_dataset_reader.next_batch(
                batch_size)
            valid_loss = sess.run(loss,
                                  feed_dict={
                                      image: valid_images,
                                      annotation: valid_annotations,
                                      c_gt: valid_c_gt,
                                      keep_probability: 1.0
                                  })
            print("%s ---> Validation_loss: %g" %
                  (datetime.datetime.now(), valid_loss))
            if min_loss > valid_loss:
                min_loss = valid_loss
                ep = itr
                saver.save(sess, logs_dir + "model.ckpt", itr)
Esempio n. 26
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'])
Esempio n. 27
0
def main(argv=None):
    indexList = tf.constant(index)
    neighbourList = tf.constant(neighbour)

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

    pred_annotation, pred_annotation_no0, 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])
    fixedLogits = FixLogitsWithIgnoreClass(logits, labels)
    # Calculate loss
    class_weights = tf.constant([0.1, 1., 1., 0.1, 20., 0.1, 8., 8., 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=fixedLogits, labels=labels, name="entropy")
    weighted_loss = unweighted_loss * weights
    loss = tf.reduce_mean(weighted_loss)
    loss_constraint = GetConstraintLoss(indexList, neighbourList, logits, image)
    loss = tf.add(loss, tf.reduce_mean(loss_constraint))

    # 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 = 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...")

    if FLAGS.timeline == True:
        options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
        run_metadata = tf.RunMetadata()

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

            if FLAGS.timeline == True:
                sess.run(train_op, options=options, run_metadata=run_metadata, feed_dict=feed_dict)
                fetched_timeline = tf_timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open('timeline/timeline_step_%d.json' % itr, 'w') as f:
                    f.write(chrome_trace)
            else:
                sess.run(train_op, feed_dict=feed_dict)

            # Debug
            if (itr < 10):
                nowTime = datetime.datetime.now()
                print((nowTime - lastTime).seconds,'sec, ', itr)
                lastTime = nowTime
            # output = sess.run(logits, feed_dict=feed_dict)
            # print("logits shape : ", output.shape)
            # print("logits : ", output)
            # output = sess.run(loss_constraint, feed_dict=feed_dict)
            # print("loss shape: ", output.shape)
            # print("logits : ", logits[0][0][0])
            # 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 % 100 == 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 % 3000 == 0):
                valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size)
                valid_images, valid_annotations = RandomCrop(valid_images, valid_annotations)
                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_newloss.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)
            pred = np.zeros_like(np.squeeze(test_annotations, axis=3))
            for sub in range(IMAGE_WIDTH / IMAGE_WIDTH_CROP):
                sub_images, sub_annotations = SubCrop(test_images, test_annotations, sub)
                sub_pred = sess.run(pred_annotation, feed_dict={image: sub_images, annotation: sub_annotations,
                                                        keep_probability: 1.0})
                sub_pred = np.squeeze(sub_pred, axis=3)
                pred[:, :, sub * IMAGE_WIDTH_CROP:(sub + 1) * IMAGE_WIDTH_CROP] = sub_pred
            print(itr)
            test_annotations = np.squeeze(test_annotations, axis=3)
            # videoWriter.write(pred[0].astype(np.uint8))
            # 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'])
            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'])
Esempio n. 28
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32,
                           shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3],
                           name="input_image")
    annotation = tf.placeholder(tf.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)
Esempio n. 29
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...")

    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 = sess.run(
                [pred_annotation, 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)
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'])
Esempio n. 31
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, 1],
                           name="input_image")
    annotation = tf.placeholder(tf.float32,
                                shape=[None, IMAGE_SIZE, IMAGE_SIZE, 2],
                                name="annotation")
    z = tf.placeholder(tf.float32, shape=[None, 1, 1, 50], name="z")
    #  mask = tf.placeholder(tf.float32, shape=[None, 64, 64, 1], name="mask")
    #  mask2 = tf.placeholder(tf.float32, shape=[None, 64, 64, 1], name="mask2")
    z_new = tf.placeholder(tf.float32, shape=[None, 1, 1, 50], name="z_new")
    istrain = tf.placeholder(tf.bool)
    #z_lip = tf.placeholder(tf.float32, shape=[None, 1, 1, 10], name="z_lip")
    #z_lip_inv = tf.placeholder(tf.float32, shape=[None, 1, 1, 10], name="z_lip_inv")
    e = tf.placeholder(tf.float32, shape=[None, 1, 1, 4050], name="e")
    e_p = tf.placeholder(tf.float32, shape=[None, 1, 1, 4050], name="e_p")

    save_itr = 0

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

    # mask_ = tf.ones([FLAGS.batch_size,32,64,3])
    # mask = tf.pad(mask_, [[0,0],[0,32],[0,0],[0,0]])

    #   mask2__ = tf.ones([FLAGS.batch_size,78,78,3])
    #  mask2_ = tf.pad(mask2__, [[0,0],[25,25],[25,25],[0,0]])
    # mask2 = mask2_ - mask
    zero = tf.zeros([FLAGS.batch_size, 1, 1, 4050])
    logits_a, logits_b, h = inference(image, keep_probability, z, 0.0, istrain)
    logits_ae, logits_be, h_e = inference(image, keep_probability, z, e,
                                          istrain)

    logits = tf.concat([logits_a, logits_b], axis=3)
    logits_e = tf.concat([logits_ae, logits_be], axis=3)
    #logits_lip,_  = inference((1-mask)*image + mask*0.0, keep_probability,z_lip,istrain   )
    #logits_lip_inv,_  = inference((1-mask)*image + mask*0.0, keep_probability,z_lip_inv,istrain   )

    z_pred = predictor(h, z, zero, istrain)
    z_pred_e = predictor(h, z, e_p, istrain)
    #  z_pred_lip =  predictor(h,z_lip,istrain)
    #  z_pred_lip_inv =  predictor(h,z_lip_inv,istrain)
    # logits = inference(image, keep_probability,z,istrain)
    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)

    # lossz = 0.1 * tf.reduce_mean(tf.reduce_sum(tf.abs(z),[1,2,3]))
    #  lossz = 0.1 * tf.reduce_mean(tf.abs(z))
    # loss_all = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square((image - logits)),[1,2,3])))
    # loss_all = tf.reduce_mean(tf.reduce_sum(tf.contrib.layers.flatten(tf.abs(image - logits)),1))

    #  loss_mask = 0.8*tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square((image - logits)*mask),[1,2,3])))
    loss_ = tf.reduce_mean(
        tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((annotation - logits))),
                      1))

    # loss_ =  0.4*loss_mask + loss_mask2
    #  loss = tf.reduce_mean(tf.squared_difference(logits ,annotation ))
    loss_summary = tf.summary.scalar("entropy", loss_)
    # zloss = tf.reduce_mean(tf.losses.cosine_distance(tf.contrib.layers.flatten(z_new) ,tf.contrib.layers.flatten(z_pred),axis =1))
    zloss_ = tf.reduce_mean(
        tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((z_pred - z_new))), 1))
    #   zloss_lip = tf.reduce_mean(tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((z_pred - z_pred_lip))),1))
    #    zloss_lip_inv = -tf.reduce_mean(tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((z_pred - z_pred_lip_inv))),1))

    #    z_loss = zloss_ + 0.1* zloss_lip# + zloss_lip_inv

    lip_loss_dec = tf.reduce_mean(
        tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((logits - logits_e))),
                      1))
    loss = loss_ + 0.1 * lip_loss_dec

    lip_loss_pred = tf.reduce_mean(
        tf.reduce_sum(tf.contrib.layers.flatten(tf.abs((z_pred - z_pred_e))),
                      1))
    zloss = zloss_ + 0.1 * lip_loss_pred

    grads = train_z(loss_, z)

    trainable_var = tf.trainable_variables()
    trainable_z_pred_var = tf.trainable_variables(scope="predictor")
    trainable_d_pred_var = tf.trainable_variables(scope="decoder")

    print(trainable_z_pred_var)
    if FLAGS.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)
    train_op = train(loss, trainable_var)
    train_pred = train_predictor(zloss, trainable_z_pred_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...")

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

            train_images, train_annotations = train_dataset_reader.next_batch(
                FLAGS.batch_size)
            print("$$$$$$$$$$$$$$$$$$$$")
            print(train_images.shape)
            #  z_ = np.reshape(signal.gaussian(200, std=1),(FLAGS.batch_size,1,1,10))-0.5
            z_ = np.random.uniform(low=-1.0,
                                   high=1.0,
                                   size=(FLAGS.batch_size, 1, 1, 50))
            #           train_images[train_images < 0.] = -1.
            #          train_annotations[train_annotations < 0.] = -1.
            #         train_images[train_images >= 0.] = 1.0
            #        train_annotations[train_annotations >= 0.] = 1.0

            x1 = random.randint(0, 10)
            w1 = random.randint(30, 54)
            y1 = random.randint(0, 10)
            h1 = random.randint(30, 54)

            cond = random.randint(0, 10)
            # saved = True
            if False:
                saved = False
                train_images_m, train_annotations_m = train_dataset_reader.get_random_batch(
                    FLAGS.batch_size)
                train_images_m[train_images_m < 0.] = -1.
                train_annotations_m[train_annotations_m < 0.] = -1.
                train_images_m[train_images_m >= 0.] = 1.0
                train_annotations_m[train_annotations_m >= 0.] = 1.0

                train_images = (train_images + 1.) / 2.0 * 255.0
                train_annotations = (train_annotations + 1.) / 2.0 * 255.0
                train_images_m = (train_images_m + 1.) / 2.0 * 255.0
                train_annotations_m = (train_annotations_m + 1.) / 2.0 * 255.0

                train_images_m[:, 32:, :, :] = 0
                train_annotations_m[:, 32:, :, :] = 0
                train_images = np.clip((train_images + train_images_m), 0.0,
                                       255.0)
                train_annotations = np.clip(
                    (train_annotations + train_annotations_m), 0.0, 255.0)
                '''
                train_images[train_images < 0.] = -1.
                train_annotations[train_annotations < 0.] = -1.
                train_images[train_images >= 0.] = 1.0
                train_annotations[train_annotations >= 0.] = 1.0
                '''

                train_annotations_ = np.squeeze(train_annotations, axis=3)
                train_images_ = train_images

                train_images = train_images / 127.5 - 1.0
                train_annotations = train_annotations / 127.5 - 1.0

            # for itr_ in range(FLAGS.batch_size):
            #    utils.save_image(train_images_[itr_].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr_) )
            #   utils.save_image(train_annotations_[itr_].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr_) )
    #        train_images[:,x1:w1,y1:h1,:] = 0

    #  print(train_images)
            r_m, r_m2 = random_mask(64)
            #feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85, z: z_,mask:r_m, istrain:True }
            #train_images[:,50:100,50:100,:] =0
            v = 0
            # print(train_images)
            error_dec = np.random.normal(0.0, 0.001,
                                         (FLAGS.batch_size, 1, 1, 4050))
            error_dec_ = np.random.normal(0.0, 0.001,
                                          (FLAGS.batch_size, 1, 1, 4050))
            # z_l_inv = z_ + np.random.normal(0.0,0.1)
            #  feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.85, z: z_, e:error_dec, mask:r_m, istrain:True }
            #  z_l = z_ + np.random.normal(0.0,0.001)
            #     lloss,_ = sess.run([lip_loss, train_lip ], feed_dict=feed_dict)
            # z_l = z_ + np.random.normal(0.0,0.001)
            # print("Step: %d, lip_loss:%g" % (itr,lloss))

            for p in range(20):
                z_ol = np.copy(z_)
                #    z_l = z_ol + np.random.normal(0.0,0.001)
                # print("666666666666666666666666666666666666666")
                feed_dict = {
                    image: train_images,
                    annotation: train_annotations,
                    keep_probability: 0.85,
                    z: z_,
                    e: error_dec,
                    istrain: True
                }
                # lloss,_ = sess.run([lip_loss, train_lip ], feed_dict=feed_dict)
                # print("Step: %d, z_step: %d, lip_loss:%g" % (itr,p,lloss))
                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_, -10.0, 10.0)
                '''
                m = interp1d([-10.0,10.0],[-1.0,1.0])
                print(np.max(z_))
                print(np.min(z_))
                z_ol_interp = m(z_ol)
                z_interp = m(z_)
                _,z_pred_loss =sess.run([train_pred,zloss],feed_dict={image: train_images,mask:r_m,z:z_ol_interp,z_new:z_interp,e_p:error_dec_,istrain:True,keep_probability: 0.85})
                print("Step: %d, z_step: %d, z_pred_loss:%g" % (itr,p,z_pred_loss))
                '''

            # _,z_pred_loss =sess.run([train_pred,zloss],feed_dict={image: train_images,mask:r_m,z:z_ol,z_new:z_,istrain:True,keep_probability: 0.85})
            # print("Step: %d, z_step: %d, z_pred_loss:%g" % (itr,p,z_pred_loss))
            # z_ = np.clip(z_, -1.0, 1.0)
            # print(v.shape)
            # print(z_.shape)
            feed_dict = {
                image: train_images,
                annotation: train_annotations,
                keep_probability: 0.85,
                e: error_dec,
                z: z_,
                istrain: True
            }
            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_annotations[valid_annotations < 0.] = -1.
                #          valid_images[valid_images < 0.] = -1.
                #         valid_annotations[valid_annotations >= 0.] = 1.0
                #        valid_images[valid_images >= 0.] = 1.0

                x1 = random.randint(0, 10)
                w1 = random.randint(30, 54)
                y1 = random.randint(0, 10)
                h1 = random.randint(30, 54)

                #           valid_images[:,x1:w1,y1:h1,:] = 0

                valid_loss, summary_sva = sess.run(
                    [loss, loss_summary],
                    feed_dict={
                        image: valid_images,
                        annotation: valid_annotations,
                        keep_probability: 1.0,
                        z: z_,
                        e: error_dec,
                        istrain: False
                    })
                print("%s ---> Validation_loss: %g" %
                      (datetime.datetime.now(), valid_loss))

                # add validation loss to TensorBoard
                validation_writer.add_summary(summary_sva, itr)
                if itr % 3000 == 0:
                    save_itr = save_itr + 3000

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

    elif FLAGS.mode == "visualize":
        valid_images, valid_annotations = train_dataset_reader.next_batch(
            FLAGS.batch_size)
        #  valid_annotations[valid_annotations < 0.] = -1.0
        #  valid_images[valid_images < 0.] = -1.0
        #  valid_annotations[valid_annotations >= 0.] = 1.0
        #  valid_images[valid_images >= 0.] = 1.0

        x1 = random.randint(0, 10)
        w1 = random.randint(30, 54)
        y1 = random.randint(0, 10)
        h1 = random.randint(30, 54)

        #  valid_images[:,x1:w1,y1:h1,:] = 0
        r_m, r_m2 = random_mask(64)
        # z_ = np.zeros(low=-1.0, high=1.0, size=(FLAGS.batch_size,1,1,10))
        # z_ = np.reshape(signal.gaussian(200, std=1),(FLAGS.batch_size,1,1,10))-0.5
        z_ = np.random.uniform(low=-1.0,
                               high=1.0,
                               size=(FLAGS.batch_size, 1, 1, 50))
        feed_dict = {
            image: valid_images,
            annotation: valid_annotations,
            keep_probability: 0.85,
            z: z_,
            istrain: False
        }
        v = 0
        m__ = interp1d([-10.0, 10.0], [-1.0, 1.0])
        z_ = m__(z_)
        #      feed_dict = {image: valid_images, annotation: valid_annotations, keep_probability: 0.85, z: z_, istrain:False,mask:r_m }
        for p in range(20):
            z_ol = np.copy(z_)
            # print("666666666666666666666666666666666666666")
            #                print(z_)
            # feed_dict = {image: valid_images, annotation: valid_annotations, keep_probability: 0.85, z: z_, istrain:False,mask:r_m }
            #  z_loss, summ = sess.run([loss,loss_summary], feed_dict=feed_dict)
            #  print("z_step: %d, Train_loss:%g" % (p,z_loss))
            # z_, z_pred_loss = sess.run(z_pred,zlossfeed_dict = {image: valid_images, annotation: valid_annotations, keep_probability: 1.0, z:z_ol, istrain:False,mask:r_m})

            #                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_ = z_ol + 0.001 * v_prev + (1 + 0.001) * v
        #  z_ = z_ol + 0.001 * v_prev + (1+0.001)*v
        # print("z_____________")
        # print(z__)
        # print("z_")
        # print(z_)
        #    m__ = interp1d([-10.0,10.0],[-1.0,1.0])
        #     z_ol = m__(z_ol)
        #       z_ = sess.run(z_pred,feed_dict = {image: valid_images, annotation: valid_annotations, keep_probability: 0.85, z:z_ol, istrain:False,mask:r_m})
        #   m_ = interp1d([-1.0,1.0],[-10.0,10.0])
        #  z_ = m_(z_)
        # z_ = np.clip(z_, -1.0, 1.0)
        # print(z_pred_loss)
    # m_ = interp1d([-1.0,1.0],[-10.0,10.0])
    # z_ = m_(z_)

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

        valid_images = (valid_images + 1.) / 2.0 * 100.0
        # predicted_patch = sess.run(mask) * pred
        # pred = valid_images_masked + predicted_patch
        pred_ = pred * 128.0
        #        pred = pred + 1./2.0*255
        print(np.max(pred_))
        print(np.min(pred_))
        pred = np.reshape(np.concatenate((valid_images, pred_), axis=3),
                          (-1, 64, 64, 3))

        valid_annotations_ = valid_annotations * 128.0
        valid_annotations = np.reshape(
            np.concatenate((valid_images, valid_annotations_), axis=3),
            (-1, 64, 64, 3))

        # for itr in range(FLAGS.batch_size):
        # utils.save_image(valid_images_masked[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="predz_" + str(5+itr))
        #        utils.save_image(valid_images_masked[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr)+'_' + str(p) )
        #       utils.save_image(valid_annotations_[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr)+'_' + str(p)  )
        #  utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="predz_" + str(5+itr)+'_' + str(p)  )
        #   print("Saved image: %d" % itr)

        for itr in range(FLAGS.batch_size):
            #    utils.save_image(valid_images_masked[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr) )
            utils.save_image(color.lab2rgb(pred[itr]),
                             FLAGS.logs_dir,
                             name="predz_" + str(5 + itr))
            utils.save_image(color.lab2rgb(valid_annotations[itr]),
                             FLAGS.logs_dir,
                             name="gt_" + str(5 + itr))
Esempio n. 32
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, 7],
                           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 cfgs.debug:
        for var in trainable_var:
            utils.add_to_regularization_and_summary(var)

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

    #Create a file to write logs.
    #filename='logs'+ cfgs.mode + str(datetime.datetime.now()) + '.txt'
    filename = "logs_%s%s.txt" % (cfgs.mode, datetime.datetime.now())
    path_ = os.path.join(cfgs.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" % (cfgs.mode))
    logs_file.write(
        "The train data batch size is %d and the validation batch size is %d.\n"
        % (cfgs.batch_size, cfgs.v_batch_size))
    logs_file.write("The train data is %s.\n" % (cfgs.data_dir))
    logs_file.write("The model is ---%s---.\n" % cfgs.logs_dir)

    print("Setting up image reader...")
    logs_file.write("Setting up image reader...\n")
    train_records, valid_records = scene_parsing.my_read_video_dataset(
        cfgs.seq_list_path, cfgs.anno_path)
    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")
    vis = True if cfgs.mode == 'all_visualize' else False
    image_options = {
        'resize': True,
        'resize_size': IMAGE_SIZE,
        'visualize': vis
    }

    if cfgs.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(cfgs.logs_dir, sess.graph)

    sess.run(tf.global_variables_initializer())
    ckpt = tf.train.get_checkpoint_state(cfgs.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 cfgs.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(
                cfgs.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 cfgs.mode == "all_visualize":

        re_save_dir = "%s%s" % (cfgs.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" %
                        cfgs.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_filename, valid_cur_images, if_con, start, end = validation_dataset_reader.next_batch_video_valid(
                cfgs.v_batch_size)
            pred_value, pred, logits_, pred_prob_ = sess.run(
                [pred_annotation_value, pred_annotation, logits, pred_prob],
                feed_dict={
                    image: valid_images,
                    keep_probability: 1.0
                })
            print("Turn %d :----start from %d ------- to %d" %
                  (count, start, end))
            pred = np.squeeze(pred, axis=3)
            pred_value = np.squeeze(pred_value, axis=3)

            for itr in range(len(pred)):
                filename = valid_filename[itr]['filename']
                valid_images_ = pred_visualize(valid_cur_images[itr].copy(),
                                               pred[itr])
                utils.save_image(valid_images_.astype(np.uint8),
                                 re_save_dir_im,
                                 name="inp_" + filename)

                if cfgs.fit_ellip:
                    #valid_images_ellip=fit_ellipse_findContours_ori(valid_images[itr].copy(),np.expand_dims(pred[itr],axis=2).astype(np.uint8))
                    valid_images_ellip = fit_ellipse_findContours(
                        valid_cur_images[itr].copy(),
                        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 cfgs.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 cfgs.trans_heat:
                    trans_heat_map = translucent_heatmap(
                        valid_cur_images[itr],
                        heat_map.astype(np.uint8).copy())
                    utils.save_image(trans_heat_map,
                                     re_save_dir_transheat,
                                     name="trans_heat_" + filename)
Esempio n. 33
0
def main(argv=None):
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation")

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

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

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

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

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

    sess = tf.Session()

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

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

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

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

            sess.run(train_op, feed_dict=feed_dict)

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

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

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

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

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr))
            utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5+itr))
            print("Saved image: %d" % itr)
Esempio n. 34
0
def main(argv=None):
    with tf.device('/device:GPU:1'):
        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=FLAGS.batch_size)
        tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=FLAGS.batch_size)
        tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=FLAGS.batch_size)
        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)
    if FLAGS.mode == "train":
        print("No. train records: ", len(train_records))
        print("No. validation records: ", len(valid_records))

        print("Setting up dataset reader")
        image_options_train = {'resize': True, 'resize_width': IMAGE_WIDTH, 'resize_height': IMAGE_HEIGHT, 'image_augmentation':FLAGS.image_augmentation}
        image_options_val = {'resize': True, 'resize_width': IMAGE_WIDTH, 'resize_height': IMAGE_HEIGHT}
        train_val_dataset = dataset.TrainVal.from_records(
            train_records, valid_records, image_options_train, image_options_val, FLAGS.batch_size, FLAGS.batch_size)
    #validation_dataset_reader = dataset.BatchDatset(valid_records, image_options_val)

    with tf.device('/device:GPU:1'):
        config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
        config.gpu_options.allow_growth = True
        sess = tf.Session(config= config)

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

        # 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
        if FLAGS.mode == 'train':
            train_writer = tf.summary.FileWriter(os.path.join(FLAGS.logs_dir, 'train'), sess.graph)
            validation_writer = tf.summary.FileWriter(os.path.join(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":
            it_train, it_val = train_val_dataset.get_iterators()
            # get_next = iterator.get_next()
            #training_init_op, val_init_op = train_val_dataset.get_ops()
            if FLAGS.dropout <=0 or FLAGS.dropout > 1:
                raise ValueError("Dropout value not in range (0,1]")
            #sess.run(training_init_op)

            #Ignore filename from reader
            next_train_images, next_train_annotations, next_train_name = it_train.get_next()
            next_val_images, next_val_annotations, next_val_name = it_val.get_next()
            for i in xrange(MAX_ITERATION):
                train_images, train_annotations = sess.run([next_train_images, next_train_annotations])
                feed_dict = {image: train_images, annotation: train_annotations, keep_probability: (1 - FLAGS.dropout)}

                sess.run(train_op, feed_dict=feed_dict)

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

                if i % 500 == 0:
                    #sess.run(val_init_op)

                    valid_images, valid_annotations = sess.run([next_val_images, next_val_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, i)
                    saver.save(sess, os.path.join(FLAGS.logs_dir, "model.ckpt"), i)
                    #sess.run(training_init_op)


        elif FLAGS.mode == "visualize":
            iterator = train_val_dataset.get_iterator()
            get_next = iterator.get_next()
            training_init_op, val_init_op = train_val_dataset.get_ops()
            sess.run(val_init_op)
            valid_images, valid_annotations = sess.run(get_next)
            pred = sess.run(pred_annotation, feed_dict={image: valid_images, annotation: valid_annotations,
                                                        keep_probability: 1.0})
            valid_annotations = np.squeeze(valid_annotations, axis=3)
            pred = np.squeeze(pred, axis=3)

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

        elif FLAGS.mode == "predict":
            predict_records = scene_parsing.read_prediction_set(FLAGS.data_dir)
            no_predict_images = len(predict_records)
            print ("No. of predict records {}".format(no_predict_images))
            predict_image_options = {'resize': True, 'resize_width': IMAGE_WIDTH, 'resize_height': IMAGE_HEIGHT, 'predict_dataset': True}
            test_dataset_reader = dataset.SingleDataset.from_records(predict_records, predict_image_options)
            next_test_image = test_dataset_reader.get_iterator().get_next()
            if not os.path.exists(os.path.join(FLAGS.logs_dir, "predictions")):
                os.makedirs(os.path.join(FLAGS.logs_dir, "predictions"))
            for i in range(no_predict_images):
                if ((i + 1) % 10 == 0 or (i + 1) == no_predict_images):
                    print("Predicted {}/{} images".format(i + 1, no_predict_images))
                predict_images, predict_names = sess.run(next_test_image)
                pred = sess.run(pred_annotation, feed_dict={image: predict_images,
                                                            keep_probability: 1.0})
                pred = np.squeeze(pred, axis=3)
                utils.save_image((pred[0] * 255).astype(np.uint8), os.path.join(FLAGS.logs_dir, "predictions"),
                                 name= predict_names[0].decode('UTF-8'))
Esempio n. 35
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)
    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)
    '''
    train_dataset_reader = BatchDatset('data/trainlist.mat')

    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":
    itr = 0
    train_images, train_annotations = train_dataset_reader.next_batch()
    while len(train_annotations) > 0:
        #train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)
        #print('==> batch data: ', train_images[0][100][100], '===', train_annotations[0][100][100])
        feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.5}

        sess.run(train_op, feed_dict=feed_dict)

        if itr % 100 == 0:
            train_loss, summary_str, rpred = sess.run([loss, summary_op, pred_annotation], feed_dict=feed_dict)
            print("Step: %d, Train_loss:%g" % (itr, train_loss))
            summary_writer.add_summary(summary_str, itr)
            print(np.sum(rpred))
            print('=============')
            print(np.sum(train_annotations))
            print('------------>>>')

        #if itr % 10000 == 0 and itr > 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))'''

        itr += 1
        train_images, train_annotations = train_dataset_reader.next_batch()

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

    '''elif FLAGS.mode == "visualize":
Esempio n. 36
0
def main(argv=None):
    print("Setting up image reader...")
    train_images, valid_images, test_images = flowers.read_dataset(FLAGS.data_dir)
    # image_options = {"crop": True, "crop_size": MODEL_IMAGE_SIZE, "resize": True, "resize_size": IMAGE_SIZE}
    # dataset_reader = dataset.BatchDatset(train_images, image_options)
    # images = tf.placeholder(tf.float32, [FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CHANNELS])
    filename_queue = tf.train.string_input_producer(train_images)
    images = read_input_queue(filename_queue)

    train_phase = tf.placeholder(tf.bool)
    z_vec = tf.placeholder(tf.float32, [None, FLAGS.z_dim], name="z")

    print("Setting up network model...")
    tf.histogram_summary("z", z_vec)
    tf.image_summary("image_real", images, max_images=1)
    gen_images = generator(z_vec, train_phase)
    tf.image_summary("image_generated", gen_images, max_images=3)

    with tf.variable_scope("discriminator") as scope:
        discriminator_real_prob, logits_real, feature_real = discriminator(images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_real_prob, name='disc_real_prob'))
        scope.reuse_variables()
        discriminator_fake_prob, logits_fake, feature_fake = discriminator(gen_images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_fake_prob, name='disc_fake_prob'))

    discriminator_loss_real = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_real, tf.ones_like(logits_real)))
    discrimintator_loss_fake = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.zeros_like(logits_fake)))
    discriminator_loss = discrimintator_loss_fake + discriminator_loss_real
    gen_loss_1 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.ones_like(logits_fake)))
    gen_loss_2 = tf.reduce_mean(tf.nn.l2_loss(feature_real - feature_fake)) / (IMAGE_SIZE * IMAGE_SIZE)
    gen_loss = gen_loss_1 + 0.1 * gen_loss_2

    tf.scalar_summary("Discriminator_loss_real", discriminator_loss_real)
    tf.scalar_summary("Discrimintator_loss_fake", discrimintator_loss_fake)
    tf.scalar_summary("Discriminator_loss", discriminator_loss)
    tf.scalar_summary("Generator_loss", gen_loss)

    train_variables = tf.trainable_variables()
    generator_variables = [v for v in train_variables if v.name.startswith("generator")]
    # print(map(lambda x: x.op.name, generator_variables))
    discriminator_variables = [v for v in train_variables if v.name.startswith("discriminator")]
    # print(map(lambda x: x.op.name, discriminator_variables))
    generator_train_op = train(gen_loss, generator_variables)
    discriminator_train_op = train(discriminator_loss, discriminator_variables)

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

    def visualize():
        count = 20
        z_feed = np.random.uniform(-1.0, 1.0, size=(count, FLAGS.z_dim)).astype(np.float32)
        # z_feed = np.tile(np.random.uniform(-1.0, 1.0, size=(1, FLAGS.z_dim)).astype(np.float32), (count, 1))
        # z_feed[:, 25] = sorted(10.0 * np.random.randn(count))
        image = sess.run(gen_images, feed_dict={z_vec: z_feed, train_phase: False})

        for iii in xrange(count):
            print(image.shape)
            utils.save_image(image[iii, :, :, :], IMAGE_SIZE, FLAGS.logs_dir, name=str(iii))
            print("Saving image" + str(iii))

    sess = tf.Session()
    summary_op = tf.merge_all_summaries()
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(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...")
        visualize()
        return

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess, coord)
    try:
        for itr in xrange(MAX_ITERATIONS):
            batch_z = np.random.uniform(-1.0, 1.0, size=[FLAGS.batch_size, FLAGS.z_dim]).astype(np.float32)
            # feed_dict = {images: dataset_reader.next_batch(FLAGS.batch_size), z_vec: batch_z, train_phase: True}
            feed_dict = {z_vec: batch_z, train_phase: True}

            sess.run(discriminator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                g_loss_val, d_loss_val, summary_str = sess.run([gen_loss, discriminator_loss, summary_op],
                                                               feed_dict=feed_dict)
                print("Step: %d, generator loss: %g, discriminator_loss: %g" % (itr, g_loss_val, d_loss_val))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=itr)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    except KeyboardInterrupt:
        print("Ending Training...")
    finally:
        coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)