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) costProb = tf.nn.softmax(logits) labels = tf.squeeze(annotation, squeeze_dims=[3]) # merge label 1,3 to label 1 labels = tf.where(tf.not_equal(labels, 3), labels, tf.ones_like(labels) * 1) annotation_BGR = Gray2BGR(annotation) pred_annotation_BGR = Gray2BGR(pred_annotation) input_sum = tf.summary.image("input_image", image, max_outputs=4) gt_sum = tf.summary.image("ground_truth", tf.cast(annotation_BGR, tf.uint8), max_outputs=4) pred_sum = tf.summary.image("pred_annotation", tf.cast(pred_annotation_BGR, tf.uint8), max_outputs=4) # Calculate loss class_weights = tf.constant([0.0, 1.0, 3.0]) onehot_labels = tf.one_hot(labels, depth=NUM_OF_CLASSESS) weights = tf.reduce_sum(class_weights * onehot_labels, axis=3) # loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits, name="entropy"))) unweighted_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits, name="entropy") weighted_loss = unweighted_loss * weights loss = tf.reduce_mean(weighted_loss) loss_sum = tf.summary.scalar("loss", loss) lr_sum = tf.summary.scalar("learning_rate", tf.Variable(FLAGS.learning_rate, tf.float64)) 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([input_sum, gt_sum, pred_sum, lr_sum]) 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) summary_train_writer = tf.summary.FileWriter(FLAGS.logs_dir + "/train/", sess.graph) summary_val_writer = tf.summary.FileWriter(FLAGS.logs_dir + "/val/", sess.graph) summary_test_writer = tf.summary.FileWriter(FLAGS.logs_dir + "/test/", 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_weak_annotations = train_dataset_reader.next_batch_weak( FLAGS.batch_size, random_rotate=1) feed_dict = { image: train_images, annotation: train_weak_annotations, keep_probability: 0.85 } sess.run(train_op, feed_dict=feed_dict) if itr % 50 == 0: train_loss, summary_str, summary_loss = sess.run( [loss, summary_op, loss_sum], feed_dict=feed_dict) print("Step: %d, Train_loss:%g" % (itr, train_loss)) summary_writer.add_summary(summary_str, itr) summary_train_writer.add_summary(summary_loss, itr) if (itr % 500 == 0): valid_images, valid_annotations, valid_weak_annotations = validation_dataset_reader.next_batch_weak( FLAGS.batch_size, random_rotate=0) valid_loss, summary_loss = sess.run( [loss, loss_sum], 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) summary_val_writer.add_summary(summary_loss, itr) # run test for evaluation result test_images, test_annotations, test_weak_annotations = test_dataset_reader.next_batch_weak( FLAGS.batch_size, random_rotate=0) test_loss, summary_loss = sess.run( [loss, loss_sum], feed_dict={ image: test_images, annotation: test_annotations, keep_probability: 1.0 }) summary_test_writer.add_summary(summary_loss, itr) cntTable = np.zeros((NUM_OF_CLASSESS, NUM_OF_CLASSESS)) for i in range(len(test_records)): test_images, test_annotations, test_weak_annotations = test_dataset_reader.next_batch_weak( 1, random_rotate=0) pred = sess.run(pred_annotation, feed_dict={ image: test_images, annotation: test_annotations, keep_probability: 1.0 }) # merge label 1,3 to label 1 test_annotations = np.where(test_annotations != 3, test_annotations, 1) 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": ftest = open(prob_log, 'w') for itr in range(len(test_records)): test_images, test_annotations, test_weak_annotations = test_dataset_reader.next_batch_weak( 1, random_rotate=0) pred = sess.run(pred_annotation, feed_dict={ image: test_images, annotation: test_annotations, keep_probability: 1.0 }) costMap = sess.run(costProb, feed_dict={ image: test_images, annotation: test_annotations, keep_probability: 1.0 }) test_annotations = np.squeeze(test_annotations, axis=3) test_weak_annotations = np.squeeze(test_weak_annotations, axis=3) pred = np.squeeze(pred, axis=3) costImg = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH)) ftest.write(test_records[itr]['filename']) ftest.write("\n") for i in range(IMAGE_HEIGHT): for j in range(IMAGE_WIDTH): ftest.write("%.10f %.10f\n" % (float( costMap[0, i, j, 1]), float(costMap[0, i, j, 2]))) if (test_images[0, i, j, 0] != 0): costImg[i, j] = costMap[0, i, j, 1] / ( costMap[0, i, j, 1] + costMap[0, i, j, 2]) * 255 print(str(itr) + '/' + str(len(test_records))) 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(test_weak_annotations[0].astype(np.uint8), FLAGS.vis_dir, name="wgt_" + test_records[itr]['filename']) utils.save_image(pred[0].astype(np.uint8), FLAGS.vis_dir, name="pred_" + test_records[itr]['filename']) utils.save_image(costImg.astype(np.uint8), FLAGS.vis_dir, name="cost_" + test_records[itr]['filename']) ftest.close() 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'])
if self.batch_offset > self.images.shape[0]: # Finished epoch self.epochs_completed += 1 print("****************** Epochs completed: " + str(self.epochs_completed) + "******************") # Shuffle the data perm = np.arange( self.images.shape[0]) #array([0,1,.,images.shape[0]-1]) np.random.shuffle(perm) self.images = self.images[perm] self.annotations = self.annotations[perm] # Start next epoch start = 0 self.batch_offset = batch_size end = self.batch_offset return self.images[start:end], self.annotations[start:end] def get_random_batch(self, batch_size): indexes = np.random.randint(0, self.images.shape[0], size=[batch_size]).tolist() #to list return self.images[indexes], self.annotations[indexes] if __name__ == '__main__': data_dir = '/home/hongyvsvxinlang/git/human_seg/data' image_options = {'resize': True, 'resize_size': 256} train_records, valid_records = scene_parsing.read_dataset(data_dir) train_images, train_annotations = BatchDatset( train_records, image_options).next_batch(256)
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([1.0, 1.0, 1.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): train_images, train_annotations = train_dataset_reader.next_batch( FLAGS.batch_size, random_rotate=0) 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'])
sess = tf.Session() summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph) # Initialize variables sess.run(tf.global_variables_initializer()) # Initialize summary print("Setting up Model Saver...") saver = tf.train.Saver(max_to_keep = 10) 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) # Get data train_records, valid_records, test_records = image_reader.read_dataset(FLAGS.data_dir) print('Train num:', len(train_records)) print('Test num:', len(test_records)) print("Setting up dataset reader") image_options = {'resize': False, 'resize_size': 300} train_dataset_reader = dataset.BatchDatset(train_records, image_options) test_dataset_reader = dataset.BatchDatset(test_records, image_options) if (FLAGS.mode == "train"): # Training loop loss_vec = [] train_accuracy = [] test_accuracy = [] for i in xrange(MAX_ITERATION): train_vector, train_annotations, trains_images = train_dataset_reader.next_batch(FLAGS.batch_size, random_rotate = 0) sess.run(train_op, feed_dict={x_data: train_vector, y_label: train_annotations})
def main(argv=None): keep_probability = tf.placeholder(tf.float32, name="keep_probabilty") image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image") annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 1], name="annotation") pred_annotation, logits = inference(image, keep_probability) tf.summary.image("input_image", image, max_outputs=2) tf.summary.image("ground_truth", tf.cast(annotation, tf.uint8), max_outputs=2) tf.summary.image("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_outputs=2) loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=tf.squeeze(annotation, squeeze_dims=[3]), name="entropy"))) tf.summary.scalar("entropy", loss) trainable_var = tf.trainable_variables() if FLAGS.debug: for var in trainable_var: utils.add_to_regularization_and_summary(var) train_op = train(loss, trainable_var) print("Setting up summary op...") summary_op = tf.summary.merge_all() print("Setting up image reader...") train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir) print(len(train_records)) print(len(valid_records)) print("Setting up dataset reader") image_options = {'resize': True, 'resize_size': IMAGE_SIZE} if FLAGS.mode == 'train': train_dataset_reader = dataset.BatchDatset(train_records, image_options) validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) sess = tf.Session() print("Setting up Saver...") saver = tf.train.Saver(max_to_keep=10) summary_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/train', sess.graph) # tval_summary_writer = tf.summary.FileWriter(FLAGS.logs_dir + '/val', 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(itr)) utils.save_image((valid_annotations[itr] * 255.).astype(np.uint8), FLAGS.logs_dir, name="annot_" + str(itr)) utils.save_image((pred[itr] * 255.).astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(itr)) print("Saved image: %d" % itr)