def visual_results(self, dataset_type="TEST", images_index=3): image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] with self.sess as sess: # Restore saved session saver = tf.train.Saver() saver.restore(self.sess, os.path.join(self.saved_dir, 'model.ckpt-19')) _, _, prediction = cal_loss(logits=self.logits, labels=self.labels_pl) if (dataset_type == 'TRAIN'): test_type_path = self.config["TRAIN_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(367), images_index) elif (dataset_type == 'VAL'): test_type_path = self.config["VAL_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(101), images_index) elif (dataset_type == 'TEST'): test_type_path = self.config["TEST_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(233), images_index) # Load images image_filename, label_filename = get_filename_list( test_type_path, self.config) images, labels = get_all_test_data(image_filename, label_filename) # Keep images subset of length images_index images = [images[i] for i in indexes] labels = [labels[i] for i in indexes] pred_tot = [] for image_batch, label_batch in zip(images, labels): image_batch = np.reshape(image_batch, [1, image_h, image_w, image_c]) label_batch = np.reshape(label_batch, [1, image_h, image_w, 1]) fetches = [prediction] feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.batch_size_pl: 1 } pred = sess.run(fetches=fetches, feed_dict=feed_dict) pred = np.reshape(pred, [image_h, image_w]) pred_tot.append(pred) draw_plots(images, labels, pred_tot)
def test(self): image_filename, label_filename = get_filename_list( self.test_file, self.config) with self.graph.as_default(): with self.sess as sess: loss, accuracy, prediction = normal_loss( self.logits, self.labels_pl, self.num_classes) images, labels = get_all_test_data(image_filename, label_filename) #acc_final = [] #iu_final = [] #iu_mean_final = [] loss_tot = [] acc_tot = [] pred_tot = [] #hist = np.zeros((self.num_classes, self.num_classes)) step = 0 for image_batch, label_batch in zip(images, labels): image_batch = np.reshape( image_batch, [1, self.input_h, self.input_w, self.input_c]) label_batch = np.reshape( label_batch, [1, self.input_h, self.input_w, 1]) feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.batch_size_pl: 1 } fetches = [loss, accuracy, self.logits, prediction] loss_per, acc_per, logit, pred = sess.run( fetches=fetches, feed_dict=feed_dict) loss_tot.append(loss_per) acc_tot.append(acc_per) pred_tot.append(pred) print( "Image Index {}: TEST Loss{:6.3f}, TEST Accu {:6.3f}". format(step, loss_tot[-1], acc_tot[-1])) step = step + 1 #per_class_acc(logit, label_batch, self.num_classes) #hist += get_hist(logit, label_batch) #acc_tot = np.diag(hist).sum() / hist.sum() #iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) #print("Total Accuracy for test image: ", acc_tot) #print("Total MoI for test images: ", iu) #print("mean MoI for test images: ", np.nanmean(iu)) #acc_final.append(acc_tot) #iu_final.append(iu) #iu_mean_final.append(np.nanmean(iu)) return pred_tot
def load_data(manifest, indices=None, n=None): image_filenames, label_filenames = get_filename_list(manifest) if indices is None: indices = range( 0, len(label_filenames)) if n is None else np.random.choice( range(0, len(label_filenames)), n) image_filenames = [image_filenames[i] for i in indices] label_filenames = [label_filenames[i] for i in indices] images, labels = get_all_test_data(image_filenames, label_filenames) return indices, np.array(images), np.array(labels)
def test(self): image_filename, label_filename = get_filename_list( self.test_file, self.config) with self.graph.as_default(): with self.sess as sess: saver = tf.train.Saver() saver.restore(self.sess, os.path.join(self.saved_dir, 'model.ckpt-19')) loss, accuracy, prediction = normal_loss( self.logits, self.labels_pl, self.num_classes) images, labels = get_all_test_data(image_filename, label_filename) loss_tot = [] acc_tot = [] pred_tot = [] step = 0 for image_batch, label_batch in zip(images, labels): image_batch = np.reshape( image_batch, [1, self.input_h, self.input_w, self.input_c]) label_batch = np.reshape( label_batch, [1, self.input_h, self.input_w, 1]) feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.batch_size_pl: 1 } fetches = [loss, accuracy, self.logits, prediction] loss_per, acc_per, logit, pred = sess.run( fetches=fetches, feed_dict=feed_dict) loss_tot.append(loss_per) acc_tot.append(acc_per) pred_tot.append(pred) print( "Image Index {}: TEST Loss{:6.3f}, TEST Accu {:6.3f}". format(step, loss_tot[-1], acc_tot[-1])) step = step + 1
def test(self): image_filename, label_filename = get_filename_list(self.test_file, self.config) with self.graph.as_default(): with self.sess as sess: loss, accuracy, prediction = normal_loss(self.logits, self.labels_pl, self.num_classes) prob = tf.nn.softmax(self.logits, dim=-1) prob = tf.reshape(prob, [self.input_h, self.input_w, self.num_classes]) images, labels = get_all_test_data(image_filename, label_filename) NUM_SAMPLE = [] for i in range(30): NUM_SAMPLE.append(2 * i + 1) acc_final = [] iu_final = [] iu_mean_final = [] # uncomment the line below to only run for two times. # NUM_SAMPLE = [1, 30] NUM_SAMPLE = [1] for num_sample_generate in NUM_SAMPLE: loss_tot = [] acc_tot = [] pred_tot = [] var_tot = [] hist = np.zeros((self.num_classes, self.num_classes)) step = 0 for image_batch, label_batch in zip(images, labels): image_batch = np.reshape(image_batch, [1, self.input_h, self.input_w, self.input_c]) label_batch = np.reshape(label_batch, [1, self.input_h, self.input_w, 1]) # comment the code below to apply the dropout for all the samples if num_sample_generate == 1: feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: False, self.batch_size_pl: 1} else: feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1} # uncomment this code below to run the dropout for all the samples # feed_dict = {test_data_tensor: image_batch, test_label_tensor:label_batch, phase_train: False, keep_prob:0.5, phase_train_dropout:True} fetches = [loss, accuracy, self.logits, prediction] if self.bayes is False: loss_per, acc_per, logit, pred = sess.run(fetches=fetches, feed_dict=feed_dict) var_one = [] else: logit_iter_tot = [] loss_iter_tot = [] acc_iter_tot = [] prob_iter_tot = [] logit_iter_temp = [] for iter_step in range(num_sample_generate): loss_iter_step, acc_iter_step, logit_iter_step, prob_iter_step = sess.run( fetches=[loss, accuracy, self.logits, prob], feed_dict=feed_dict) loss_iter_tot.append(loss_iter_step) acc_iter_tot.append(acc_iter_step) logit_iter_tot.append(logit_iter_step) prob_iter_tot.append(prob_iter_step) logit_iter_temp.append( np.reshape(logit_iter_step, [self.input_h, self.input_w, self.num_classes])) loss_per = np.nanmean(loss_iter_tot) acc_per = np.nanmean(acc_iter_tot) logit = np.nanmean(logit_iter_tot, axis=0) print(np.shape(prob_iter_tot)) prob_mean = np.nanmean(prob_iter_tot, axis=0) prob_variance = np.var(prob_iter_tot, axis=0) logit_variance = np.var(logit_iter_temp, axis=0) # THIS TIME I DIDN'T INCLUDE TAU pred = np.reshape(np.argmax(prob_mean, axis=-1), [-1]) # pred is the predicted label var_sep = [] # var_sep is the corresponding variance if this pixel choose label k length_cur = 0 # length_cur represent how many pixels has been read for one images for row in np.reshape(prob_variance, [self.input_h * self.input_w, self.num_classes]): temp = row[pred[length_cur]] length_cur += 1 var_sep.append(temp) var_one = np.reshape(var_sep, [self.input_h, self.input_w]) # var_one is the corresponding variance in terms of the "optimal" label pred = np.reshape(pred, [self.input_h, self.input_w]) loss_tot.append(loss_per) acc_tot.append(acc_per) pred_tot.append(pred) var_tot.append(var_one) print("Image Index {}: TEST Loss{:6.3f}, TEST Accu {:6.3f}".format(step, loss_tot[-1], acc_tot[-1])) step = step + 1 per_class_acc(logit, label_batch, self.num_classes) hist += get_hist(logit, label_batch) acc_tot = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("Total Accuracy for test image: ", acc_tot) print("Total MoI for test images: ", iu) print("mean MoI for test images: ", np.nanmean(iu)) acc_final.append(acc_tot) iu_final.append(iu) iu_mean_final.append(np.nanmean(iu)) return acc_final, iu_final, iu_mean_final, prob_variance, logit_variance, pred_tot, var_tot
def visual_results(self, dataset_type = "TEST", images_index = 3, FLAG_MAX_VOTE = False): image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] train_dir = self.config["SAVE_MODEL_DIR"] FLAG_BAYES = self.config["BAYES"] print(FLAG_BAYES) with self.sess as sess: # Restore saved session saver = tf.train.Saver() saver.restore(sess, train_dir) _, _, prediction = cal_loss(logits=self.logits, labels=self.labels_pl,number_class=self.num_classes) prob = tf.nn.softmax(self.logits,dim = -1) if (dataset_type=='TRAIN'): test_type_path = self.config["TRAIN_FILE"] if type(images_index) == list: indexes = images_index else: '''CHANGE IT BACK''' #indexes = random.sample(range(367),images_index) indexes = random.sample(range(6),images_index) #indexes = [0,75,150,225,300] elif (dataset_type=='VAL'): test_type_path = self.config["VAL_FILE"] if type(images_index) == list: indexes = images_index else: #indexes = random.sample(range(101),images_index) indexes = random.sample(range(10),images_index) #indexes = [0,25,50,75,100] elif (dataset_type=='TEST'): test_type_path = self.config["TEST_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(5),images_index) #indexes = random.sample(range(233),images_index) #indexes = [0,50,100,150,200] # Load images image_filename,label_filename = get_filename_list(test_type_path, self.config) images, labels = get_all_test_data(image_filename,label_filename) # Keep images subset of length images_index images = [images[i] for i in indexes] labels = [labels[i] for i in indexes] num_sample_generate = 30 pred_tot = [] var_tot = [] print(image_c) for image_batch, label_batch in zip(images,labels): print(image_batch.shape) image_batch = np.reshape(image_batch,[1,image_h,image_w,image_c]) label_batch = np.reshape(label_batch,[1,image_h,image_w,1]) if FLAG_BAYES is False: print("NON BAYES") fetches = [prediction] feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.batch_size_pl: 1} pred = sess.run(fetches = fetches, feed_dict = feed_dict) pred = np.reshape(pred,[image_h,image_w]) var_one = [] else: feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1} prob_iter_tot = [] pred_iter_tot = [] for iter_step in range(num_sample_generate): prob_iter_step = sess.run(fetches = [prob], feed_dict = feed_dict) prob_iter_tot.append(prob_iter_step) pred_iter_tot.append(np.reshape(np.argmax(prob_iter_step,axis = -1),[-1])) if FLAG_MAX_VOTE is True: prob_variance,pred = MAX_VOTE(pred_iter_tot,prob_iter_tot,self.config["NUM_CLASSES"]) #acc_per = np.mean(np.equal(pred,np.reshape(label_batch,[-1]))) var_one = var_calculate(pred,prob_variance) pred = np.reshape(pred,[image_h,image_w]) else: prob_mean = np.nanmean(prob_iter_tot,axis = 0) prob_variance = np.var(prob_iter_tot, axis = 0) pred = np.reshape(np.argmax(prob_mean,axis = -1),[-1]) #pred is the predicted label with the mean of generated samples #THIS TIME I DIDN'T INCLUDE TAU var_one = var_calculate(pred,prob_variance) pred = np.reshape(pred,[image_h,image_w]) pred_tot.append(pred) var_tot.append(var_one) draw_plots_bayes(images, labels, pred_tot, var_tot) return (images,labels,pred_tot,var_tot)
def transfer_output(self,rotation=[0],dataset_type = "TRAIN"): image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] train_dir = self.config["SAVE_MODEL_DIR"] datagen = ImageDataGenerator() with self.sess as sess: saver=tf.train.Saver() saver.restore(sess, train_dir) #self.logits=tf.nn.bias_add(self.conv, self.biases, name=scope.name) #_, _, prediction = cal_loss(logits=self.logits,labels=self.labels_pl,number_class=self.num_classes) #prob = tf.nn.softmax(self.logits,dim = -1) if(dataset_type == "TRAIN"): test_type_path = self.config["TRAIN_FILE"] else: test_type_path = self.config["TEST_FILE"] image_filename,label_filename = get_filename_list(test_type_path, self.config) images, labels = get_all_test_data(image_filename,label_filename) print(len(images)) image_batch=np.reshape(images,[len(images),image_h,image_w,image_c]) label_batch=np.reshape(labels,[len(labels),image_h,image_w,1]) #pred_tot = [] #var_tot = [] logit_aug=[] image_aug=[] label_aug=[] for deg in rotation: logit_tot=[] image_tot=[] label_tot=[] for image_batch, label_batch in zip(images,labels): #print(image_batch.shape) #to just convert the image into the standard format #for example: 1(batch size),64,64,3 image_batch=datagen.apply_transform(image_batch,{'theta':deg}) label_batch=datagen.apply_transform(label_batch,{'theta':deg}) image_batch = np.reshape(image_batch,[1,image_h,image_w,image_c]) label_batch = np.reshape(label_batch,[1,image_h,image_w,1]) image_tot.append(image_batch) label_tot.append(label_batch) #non baysien model #fetches = [prediction] feed_dict = {self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1} #pred = sess.run(fetches = fetches, feed_dict = feed_dict) logit= sess.run([self.logits],feed_dict = feed_dict) #原本是images_h* images_w, with each element the label of it #pred = np.reshape(pred,[image_h,image_w]) #pred_tot.append(pred) logit_tot.append(logit) logit_aug.append(logit_tot) image_aug.append(image_tot) label_aug.append(label_tot) return logit_aug,label_aug,image_aug#logit_tot,label_tot,image_tot
def visual_results(self, dataset_type="TRAIN", NUM_IMAGES=3): #train_dir = "./saved_models/segnet_vgg_bayes/segnet_vgg_bayes_30000/model.ckpt-30000" #train_dir = "./saved_models/segnet_scratch/segnet_scratch_30000/model.ckpt-30000" image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] train_dir = self.config["SAVE_MODEL_DIR"] FLAG_BAYES = self.config["BAYES"] with self.sess as sess: # Restore saved session saver = tf.train.Saver() saver.restore(sess, train_dir) _, _, prediction = normal_loss(logits=self.logits, labels=self.labels_pl, number_class=self.num_classes) prob = tf.nn.softmax(self.logits, dim=-1) if (dataset_type == 'TRAIN'): test_type_path = self.config["TRAIN_FILE"] indexes = random.sample(range(367), NUM_IMAGES) #indexes = [0,75,150,225,300] elif (dataset_type == 'VAL'): test_type_path = self.config["VAL_FILE"] indexes = random.sample(range(101), NUM_IMAGES) #indexes = [0,25,50,75,100] elif (dataset_type == 'TEST'): test_type_path = self.config["TEST_FILE"] indexes = random.sample(range(233), NUM_IMAGES) #indexes = [0,50,100,150,200] # Load images image_filename, label_filename = get_filename_list( test_type_path, self.config) images, labels = get_all_test_data(image_filename, label_filename) # Keep images subset of length NUM_IMAGES images = [images[i] for i in indexes[0:NUM_IMAGES]] labels = [labels[i] for i in indexes[0:NUM_IMAGES]] num_sample_generate = 30 pred_tot = [] var_tot = [] for image_batch, label_batch in zip(images, labels): image_batch = np.reshape(image_batch, [1, image_h, image_w, image_c]) label_batch = np.reshape(label_batch, [1, image_h, image_w, 1]) if FLAG_BAYES is False: fetches = [prediction] feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5 } pred = sess.run(fetches=fetches, feed_dict=feed_dict) pred = np.reshape(pred, [image_h, image_w]) var_one = [] else: feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: False } prob_iter_tot = [] for iter_step in range(num_sample_generate): prob_iter_step = sess.run(fetches=[prob], feed_dict=feed_dict) prob_iter_tot.append(prob_iter_step) prob_mean = np.nanmean(prob_iter_tot, axis=0) prob_variance = np.var(prob_iter_tot, axis=0) #THIS TIME I DIDN'T INCLUDE TAU pred = np.reshape(np.argmax(prob_mean, axis=-1), [-1]) #pred is the predicted label var_sep = [ ] #var_sep is the corresponding variance if this pixel choose label k length_cur = 0 #length_cur represent how many pixels has been read for one images for row in np.reshape(prob_variance, [image_h * image_w, 12]): temp = row[pred[length_cur]] length_cur += 1 var_sep.append(temp) var_one = np.reshape( var_sep, [image_h, image_w] ) #var_one is the corresponding variance in terms of the "optimal" label pred = np.reshape(pred, [image_h, image_w]) pred_tot.append(pred) var_tot.append(var_one) draw_plots(images, labels, pred_tot)
def visual_results(self, dataset_type="TEST", images_index=3, FLAG_MAX_VOTE=False): image_w = self.config["INPUT_WIDTH"] image_h = self.config["INPUT_HEIGHT"] image_c = self.config["INPUT_CHANNELS"] train_dir = self.config["SAVE_MODEL_DIR"] FLAG_BAYES = self.config["BAYES"] with self.sess as sess: # Restore saved session saver = tf.train.Saver() saver.restore(sess, train_dir) kernel = variable_with_weight_decay('weights', initializer=initialization( 1, 64), shape=[1, 1, 64, 3], wd=False) conv = tf.nn.conv2d(self.deconv1_3, kernel, [1, 1, 1, 1], padding='SAME') biases = variable_with_weight_decay('biases', tf.constant_initializer(0.0), shape=[3], wd=False) logits = tf.nn.bias_add(conv, biases, name="scope.name") #exit() sess.run(tf.global_variables_initializer()) #sess.run(logits) _, _, prediction = cal_loss(logits=logits, labels=self.labels_pl) prob = tf.nn.softmax(logits, dim=-1) print( "===================================================================================" ) print(prediction) #exit() if (dataset_type == 'TRAIN'): test_type_path = self.config["TRAIN_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(367), images_index) #indexes = [0,75,150,225,300] elif (dataset_type == 'VAL'): test_type_path = self.config["VAL_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(101), images_index) #indexes = [0,25,50,75,100] elif (dataset_type == 'TEST'): test_type_path = self.config["TEST_FILE"] if type(images_index) == list: indexes = images_index else: indexes = random.sample(range(233), images_index) #indexes = [0,50,100,150,200] # Load images image_filename, label_filename = get_filename_list( test_type_path, self.config) images, labels = get_all_test_data(image_filename, label_filename) # Keep images subset of length images_index images = [images[i] for i in indexes] labels = [labels[i] for i in indexes] num_sample_generate = 30 pred_tot = [] var_tot = [] for image_batch, label_batch in zip(images, labels): image_batch = np.reshape(image_batch, [1, image_h, image_w, image_c]) label_batch = np.reshape(label_batch, [1, image_h, image_w, 1]) if FLAG_BAYES is False: fetches = [prediction] feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.batch_size_pl: 1 } pred = sess.run(fetches=fetches, feed_dict=feed_dict) pred = np.reshape(pred, [image_h, image_w]) var_one = [] else: feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1 } prob_iter_tot = [] pred_iter_tot = [] for iter_step in range(num_sample_generate): prob_iter_step = sess.run(fetches=[prob], feed_dict=feed_dict) prob_iter_tot.append(prob_iter_step) pred_iter_tot.append( np.reshape(np.argmax(prob_iter_step, axis=-1), [-1])) if FLAG_MAX_VOTE is True: prob_variance, pred = MAX_VOTE( pred_iter_tot, prob_iter_tot, self.config["NUM_CLASSES"]) #acc_per = np.mean(np.equal(pred,np.reshape(label_batch,[-1]))) var_one = var_calculate(pred, prob_variance) pred = np.reshape(pred, [image_h, image_w]) else: prob_mean = np.nanmean(prob_iter_tot, axis=0) prob_variance = np.var(prob_iter_tot, axis=0) pred = np.reshape( np.argmax(prob_mean, axis=-1), [-1] ) #pred is the predicted label with the mean of generated samples #THIS TIME I DIDN'T INCLUDE TAU var_one = var_calculate(pred, prob_variance) pred = np.reshape(pred, [image_h, image_w]) pred_tot.append(pred) var_tot.append(var_one) draw_plots_bayes(images, labels, pred_tot, var_tot)
def test(self): image_filename, label_filename = get_filename_list(self.test_file) with tf.Session() as sess: # Restore saved session saver = tf.train.Saver() saver.restore(sess, tf.train.latest_checkpoint(FLAGS.runtime_dir)) loss, accuracy, prediction = normal_loss(self.logits, self.labels_pl, self.n_classes) images, labels = get_all_test_data(image_filename, label_filename) NUM_SAMPLE = [] for i in range(30): NUM_SAMPLE.append(2 * i + 1) acc_final = [] iu_final = [] iu_mean_final = [] # uncomment the line below to only run for two times. # NUM_SAMPLE = [1, 30] NUM_SAMPLE = [1] for num_sample_generate in NUM_SAMPLE: loss_tot = [] acc_tot = [] hist = np.zeros((self.n_classes, self.n_classes)) step = 0 for image_batch, label_batch in zip(images, labels): image_batch = np.reshape( image_batch, [1, self.input_h, self.input_w, self.input_c]) label_batch = np.reshape( label_batch, [1, self.input_h, self.input_w, 1]) # comment the code below to apply the dropout for all the samples if num_sample_generate == 1: feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: False, self.batch_size_pl: 1 } else: feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.with_dropout_pl: True, self.batch_size_pl: 1 } loss_per, acc_per, logit, pred = sess.run( [loss, accuracy, self.logits, prediction], feed_dict=feed_dict) loss_tot.append(loss_per) acc_tot.append(acc_per) print( "Image Index {}: TEST Loss{:6.3f}, TEST Accu {:6.3f}". format(step, loss_tot[-1], acc_tot[-1])) step = step + 1 per_class_acc(logit, label_batch, self.n_classes) hist += get_hist(logit, label_batch) acc_tot = np.diag(hist).sum() / hist.sum() iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print("Total Accuracy for test image: ", acc_tot) print("Total MoI for test images: ", iu) print("mean MoI for test images: ", np.nanmean(iu)) acc_final.append(acc_tot) iu_final.append(iu) iu_mean_final.append(np.nanmean(iu)) return acc_final, iu_final, iu_mean_final
def visual_results(self, dataset_type="TEST", indices=None, n_samples=3, model_file=None): with tf.Session() as sess: # Restore saved session saver = tf.train.Saver() if model_file is None: saver.restore(sess, tf.train.latest_checkpoint(FLAGS.runtime_dir)) else: saver.restore(sess, os.path.join(FLAGS.runtime_dir, model_file)) _, _, prediction = cal_loss(logits=self.logits, labels=self.labels_pl, n_classes=self.n_classes) test_type_path = None if dataset_type == 'TRAIN': test_type_path = self.train_file elif dataset_type == 'VAL': test_type_path = self.val_file elif dataset_type == 'TEST': test_type_path = self.test_file # Load images image_filenames, label_filenames = get_filename_list( test_type_path) images, labels = get_all_test_data(image_filenames, label_filenames) if not indices: indices = random.sample(range(len(images)), n_samples) # Keep images subset of length images_index images = [images[i] for i in indices] labels = [labels[i] for i in indices] pred_tot = [] for image_batch, label_batch in zip(images, labels): image_batch = np.reshape( image_batch, [1, self.input_h, self.input_w, self.input_c]) label_batch = np.reshape(label_batch, [1, self.input_h, self.input_w, 1]) fetches = [prediction] feed_dict = { self.inputs_pl: image_batch, self.labels_pl: label_batch, self.is_training_pl: False, self.keep_prob_pl: 0.5, self.batch_size_pl: 1 } pred = sess.run(fetches=fetches, feed_dict=feed_dict) pred = np.reshape(pred, [self.input_h, self.input_w]) pred_tot.append(pred) draw_plots_bayes(images, labels, pred_tot)