(argv=None)
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)
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 image reader ... ) train_records valid_records = scene_parsing . read_dataset ( FLAGS . data_dir ) print ( len ( train_records )) print ( len ( valid_records ))
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":
        valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size)
        pred = sess.run(pred_annotation, feed_dict={image: valid_images, annotation: valid_annotations,
                                                    keep_probability: 1.0})
        valid_annotations = np.squeeze(valid_annotations, axis=3)
        pred = np.squeeze(pred, axis=3)

        for itr in range(FLAGS.batch_size):
            utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr))
            utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr))
            utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5+itr))
            print("Saved image: %d" % itr)'''
def main ( argv = None ) : keep_probability = tf . placeholder ( tf . float32 , name = "keep_probabilty" ) image = tf . placeholder ( tf . float32 , shape = [ None , IMAGE_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 ( ) 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":
        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)'''