def test(FLAGS): max_steps = FLAGS.max_steps batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 360, 480, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, batch_size, phase_train) pred = tf.argmax(logits, dimension=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) with tf.Session() as sess: # Load checkpoint saver.restore(sess, test_ckpt) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((NUM_CLASSES, NUM_CLASSES)) for image_batch, label_batch in zip(images, labels): feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): writeImage(im[0], 'testing_image.png') hist += get_hist(dense_prediction, label_batch) acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu))
def test_model(two): global max_steps, batch_size, image_w, image_h, image_c, image_dir, log_dir count=0 #test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = '/home/shantams/deep-learning/tf/logs/Test/run1/model.ckpt-9000' # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(image_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 240,320,1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, phase_train) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint saver.restore(sess, test_ckpt) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((3, 3)) #number of classes, number of classes for image_batch, label_batch in zip(images, labels): #print image_batch.shape feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (True): #writeImage(im[0], 'testing_image.png') writeImage(im[0], str(image_filenames[count]).split('/')[-1]) hist += get_hist(dense_prediction, label_batch) count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu))
def test(): max_steps = config.max_steps batch_size = config.batch_size test_dir = config.test_dir test_ckpt = config.testing image_w = config.image_w image_h = config.image_h image_c = config.image_c # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 360, 480, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, batch_size, phase_train) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint saver.restore(sess, test_ckpt) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((NUM_CLASSES, NUM_CLASSES)) for image_batch, label_batch in zip(images, labels): feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (config.save_image): writeImage(im[0], '%s/testing_image.png' % config.log_dir) print('Testing image saved.') hist += get_hist(dense_prediction, label_batch) # count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu))
def test(FLAGS): max_steps = FLAGS.max_steps batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 360, 480, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, batch_size, phase_train) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint saver.restore(sess, test_ckpt ) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((NUM_CLASSES, NUM_CLASSES)) for image_batch, label_batch in zip(images, labels): feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): writeImage(im[0], 'testing_image.png') # writeImage(im[0], 'out_image/'+str(image_filenames[count]).split('/')[-1]) hist += get_hist(dense_prediction, label_batch) # count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu))
def test(FLAGS): max_steps = FLAGS.max_steps batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 360, 480, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, batch_size, phase_train) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.05) #config=tf.ConfigProto(gpu_options=gpu_options) count = 0 with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: # Load checkpoint saver.restore(sess, test_ckpt) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((NUM_CLASSES, NUM_CLASSES)) for image_batch, label_batch in zip(images, labels): #image_batch = cv2.resize(image_batch, (480, 360)) #print(image_batch.shape) #label_batch = image_batch feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): #writeImage(im[0], 'testing_image0.png') #writeImage(im[1], 'testing_image1.png') #writeImage(im[2], 'testing_image2.png') writeImage( im[0], 'out_image/' + str(image_filenames[count]).split('/')[-1]) hist += get_hist(dense_prediction, label_batch) count += 1
def test(FLAGS): iterations = FLAGS.iterations batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_image_list = FLAGS.test_image_list # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.ckpt_for_test image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c # test should set batch_size = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_image_list) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, image_h, image_w, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(FLAGS, test_data_node, test_labels_node, batch_size, phase_train) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint saver.restore(sess, test_ckpt) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((FLAGS.num_classes, FLAGS.num_classes)) i = -1 for image_batch, label_batch in zip(images, labels): i += 1 feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): writeImage( FLAGS, im[0], str(image_filenames[i]).rstrip('.png') + '_hanteikekka.png') hist += get_hist(dense_prediction, label_batch) # count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu))
def test(FLAGS): max_steps = FLAGS.max_steps batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c # testing should set BATCH_SIZE = 1 batch_size = 1 image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c], name = 'input') test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, 90, 120, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(test_data_node, test_labels_node, batch_size, phase_train) logits = tf.identity(logits, name = 'output') pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint count = 0 saver.restore(sess, test_ckpt ) images, labels = get_all_test_data(image_filenames, label_filenames) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((NUM_CLASSES, NUM_CLASSES)) for image_batch, label_batch in zip(images, labels): feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): writeImage(im[0], 'testing_image.png') writeImage(im[0], 'out_image/'+str(image_filenames[count]).split('/')[-1]) hist += get_hist(dense_prediction, label_batch) count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu)) graph_def = sess.graph.as_graph_def() for node in graph_def.node: p#rint(node) if node.op == 'RefSwitch': node.op = 'Switch' for index in range(len(node.input)): if 'moving_' in node.input[index]: node.input[index] = node.input[index] + '/read' elif node.op == 'AssignSub': node.op = 'Sub' if 'use_locking' in node.attr: del node.attr['use_locking'] elif node.op == 'AssignAdd': node.op = 'Add' if 'use_locking' in node.attr: del node.attr['use_locking'] constant_graph = graph_util.convert_variables_to_constants(sess, graph_def, ["output"]) graph_io.write_graph(constant_graph, './', "FoosballBallTrackerHeatmap.pb", as_text=False)
def test(FLAGS): max_steps = FLAGS.max_steps batch_size = FLAGS.batch_size train_dir = FLAGS.log_dir # /tmp3/first350/TensorFlow/Logs test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c datadir = FLAGS.datadir outdir = FLAGS.output_dir dataset = FLAGS.dataset use_weights = FLAGS.use_weights # testing should set BATCH_SIZE = 1 batch_size = 1 # select weights # class weight calculation used in segnet # weights for dataset de_top14 # "detop15", "eutop25", "worldtiny2k", "kaggle_dstl", "vaihingen", # "detop15_nores", "eutop25_nores", "worldtiny2k_nores" if not use_weights: print('dont use weights during evaluation') use_weights = tf.constant([1.0 for i in range(FLAGS.num_class)]) image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) test_labels_node = tf.placeholder(tf.int64, shape=[batch_size, image_h, image_w, 1]) phase_train = tf.placeholder(tf.bool, name='phase_train') loss, logits = inference(FLAGS.num_class, test_data_node, test_labels_node, batch_size, phase_train, use_weights) pred = tf.argmax(logits, axis=3) # get moving avg variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(test_ckpt) # Load checkpoint vars_to_restore = get_tensors_in_checkpoint_file( file_name=ckpt.model_checkpoint_path) # print(vars_to_restore) # print([v.name for v in tf.global_variables()]) vars_to_restore = [ v for v in tf.global_variables() if (not 'conv_classifier' in v.name or not FLAGS.not_restore_last) and v.name[:-2] in vars_to_restore ] # saver = tf.train.Saver(variables_to_restore) saver = tf.train.Saver(vars_to_restore) saver.restore(sess, ckpt.model_checkpoint_path) images, labels = get_all_test_data(image_filenames, label_filenames, datadir) threads = tf.train.start_queue_runners(sess=sess) hist = np.zeros((FLAGS.num_class, FLAGS.num_class)) eval_file = open(FLAGS.dataset, 'wt+') for image_batch, label_batch in tqdm(zip(images, labels), desc="Testing", leave=True, total=len(images)): feed_dict = { test_data_node: image_batch, test_labels_node: label_batch, phase_train: False } dense_prediction, im = sess.run([logits, pred], feed_dict=feed_dict) # output_image to verify if (FLAGS.save_image): if not os.path.exists(outdir): os.makedirs(outdir) writeImage(im[0], outdir + '/testing_image.png', dataset=dataset) # writeImage(im[0], 'out_image/'+str(image_filenames[count]).split('/')[-1]) # calc iou and acc values for each image (for significance test) lhist = get_hist(dense_prediction, label_batch) lacc = np.diag(lhist).sum() / lhist.sum() liu = np.diag(lhist) / (lhist.sum(1) + lhist.sum(0) - np.diag(lhist)) acc_line = "acc" iou_line = "iou" for cls in range(lhist.shape[0]): iou = liu[cls] if float(lhist.sum(1)[cls]) == 0: local_acc = 0.0 else: local_acc = np.diag(lhist)[cls] / float(lhist.sum(1)[cls]) acc_line = acc_line + ", " + str(local_acc) iou_line = iou_line + ", " + str(iou) eval_file.write(acc_line + "\n") eval_file.write(iou_line + "\n") eval_file.flush() hist += lhist # count+=1 acc_total = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) eval_file.close() print("acc: ", acc_total) print("mean IU: ", np.nanmean(iu)) cl_name = get_dataset_classnames(dataset) for cls in range(hist.shape[0]): iou = iu[cls] if float(hist.sum(1)[cls]) == 0: acc = 0.0 else: acc = np.diag(hist)[cls] / float(hist.sum(1)[cls]) print(" class %s accuracy = %f, IoU = %f" % (cl_name[cls].ljust(12), acc, iou))
def infere(FLAGS): test_dir = FLAGS.test_dir # /tmp3/first350/SegNet-Tutorial/CamVid/train.txt test_ckpt = FLAGS.testing image_w = FLAGS.image_w image_h = FLAGS.image_h image_c = FLAGS.image_c datadir = FLAGS.datadir outdir = FLAGS.output_dir max_infere = FLAGS.max_infere dataset = FLAGS.dataset batch_size = 1 use_weights = FLAGS.use_weights image_filenames, label_filenames = get_filename_list(test_dir) test_data_node = tf.placeholder( tf.float32, shape=[batch_size, image_h, image_w, image_c]) phase_train = tf.placeholder(tf.bool, name='phase_train') # select weights # class weight calculation used in segnet # weights for dataset de_top14 # "detop15", "eutop25", "worldtiny2k", "kaggle_dstl", "vaihingen", # "detop15_nores", "eutop25_nores", "worldtiny2k_nores" print('dont use weights during inference') use_weights = tf.constant([1.0 for i in range(FLAGS.num_class)]) # feed only sat images to the net logits = inference(FLAGS.num_class, test_data_node, None, batch_size, phase_train, use_weights) # calc certainity grey scale certainity = get_certainity(logits, [image_h, image_w]) pred = tf.argmax(logits, axis=3) # get moving avg # variable_averages = tf.train.ExponentialMovingAverage( # MOVING_AVERAGE_DECAY) # variables_to_restore = variable_averages.variables_to_restore() ckpt = tf.train.get_checkpoint_state(test_ckpt) vars_to_restore = get_tensors_in_checkpoint_file( file_name=ckpt.model_checkpoint_path) # print(vars_to_restore) # print([v.name for v in tf.global_variables()]) vars_to_restore = [ v for v in tf.global_variables() if (not 'conv_classifier' in v.name or not FLAGS.not_restore_last) and v.name[:-2] in vars_to_restore ] # loader = tf.train.Saver(variables_to_restore) loader = tf.train.Saver(vars_to_restore) # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0001) with tf.Session() as sess: # Load checkpoint loader.restore(sess, ckpt.model_checkpoint_path) images, labels = get_all_test_data(image_filenames, label_filenames, datadir, max_infere) hist = np.zeros((FLAGS.num_class, FLAGS.num_class)) # create out dir if not exist if not os.path.exists(outdir): os.makedirs(outdir) print( "Warning! No Softmax Output before Argmax in Segnet! Certainity does not display probability! \n" "(Neither does a ConvNet with Softmax Output by the way ^^)") for i in trange(0, min(len(images), max_infere), desc="Infering", leave=True): image_fname = image_filenames[i] image = images[i] label = labels[ i] # not used currently, but will later be needed to save gt data along with pred print(image[0].shape) print(label[0].shape) feed_dict = {test_data_node: image, phase_train: False} dense_prediction, im, cert_img = sess.run( [logits, pred, certainity], feed_dict=feed_dict) # output_image to verify if not os.path.exists(outdir): os.makedirs(outdir) save_dir_name = os.path.dirname(outdir + '/' + image_fname) base_name = os.path.basename(outdir + '/' + image_fname) subdir = os.path.join(save_dir_name, str(i)) if not os.path.exists(subdir): os.makedirs(subdir) print(subdir) # writeImage(image, subdir + '/' + 'sat' + base_name) # deprec function TODO replace imsave with imageio.imwrite! misc.imsave(subdir + '/' + 'cert_' + base_name, cert_img[0]) misc.imsave(subdir + '/' + 'sat_' + base_name, image[0].astype(int)) writeImage(im[0], subdir + '/' + 'pred_' + base_name, dataset=dataset) writeImage(np.squeeze(label[0]), subdir + '/' + 'gt_' + base_name, dataset=dataset)