def validation_IOU(self, sess, pred, X, Y, keep_prob, is_training): img_loader2 = data_load.ImgLoader('pascal') img_loader2.run('val') # train or val batch_num = 10 input_batch, label_batch = img_loader2.nextbatch(self.input_size, 0) IOU_sum = 0 for i in range(batch_num): input_batch, label_batch = img_loader2.nextbatch( self.input_size, i) pred_ = sess.run(pred, feed_dict={ X: input_batch, Y: label_batch, keep_prob: 1.0, is_training: False }) IOU = util.intersection_over_union(pred_, label_batch) IOU_sum += IOU return IOU_sum / batch_num
import data_load import FCN import tensorflow as tf import util from matplotlib.pyplot import imshow, hist, show, figure, subplot, subplots_adjust, setp batch_size = 3 image_idx = 2 masker = util.masker() img_loader = data_load.ImgLoader('pascal') FCN = FCN.FCN(batch_size, 0.001) img_loader.run('val') # train or val input_batch, label_batch = img_loader.nextbatch_for_inference( batch_size, image_idx) _, label_batch_cal_loss = img_loader.nextbatch(batch_size, image_idx) ########################################## ########################################## X = tf.placeholder( tf.float32, [None, input_batch.shape[1], input_batch.shape[2], input_batch.shape[3] ]) # h * w * 3 Y = tf.placeholder(tf.float32, [ None, label_batch.shape[1], label_batch.shape[2], len(masker.class_color_list) ]) # h * w * (class+1) <= 배경포함(+1) keep_prob = tf.placeholder(tf.float32) is_training = tf.placeholder(tf.bool)
def run(self, max_iter, already_done): ######already_done = 이미 에폭 돌아간 횟수######## img_loader = data_load.ImgLoader() img_loader.run('train') # train or val data_size = len(img_loader.class_path) batch_num = data_size // self.input_size # input_batch, label_batch = img_loader.nextbatch(self.input_size, iter) input_batch, label_batch = img_loader.nextbatch(self.input_size, 0, stochastic=True) X = tf.placeholder(tf.float32, [ None, input_batch.shape[1], input_batch.shape[2], input_batch.shape[3] ]) # h * w * 3 Y = tf.placeholder(tf.float32, [ None, label_batch.shape[1], label_batch.shape[2], label_batch.shape[3] ]) # h * w * (class+1) <= 배경포함(+1) keep_prob = tf.placeholder(tf.float32) is_training = tf.placeholder(tf.bool) train_op, pred, loss, logit = self.train(X, Y, keep_prob, is_training) sess = tf.Session() saver = tf.train.Saver(tf.global_variables()) ckpt = tf.train.get_checkpoint_state('./model') if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): saver.restore(sess, ckpt.model_checkpoint_path) else: sess.run(tf.global_variables_initializer()) for epoch in range(max_iter): for itr in range(batch_num): input_batch, label_batch = img_loader.nextbatch( self.input_size, itr, stochastic=True) _, loss_, pred_ = sess.run( [train_op, loss, pred], feed_dict={ X: input_batch, Y: label_batch, keep_prob: 0.5, is_training: True }) print('iteration :', itr, ' loss :', loss_) if (epoch + 1) % 2 == 0: IOU_this_batch = util.intersection_over_union( pred_, label_batch) print('########################\n', 'intersection over union for this batch :', IOU_this_batch, '\n########################') IOU_val = self.validation_IOU(sess, pred, X, Y, keep_prob, is_training) print('########################\n', 'intersection over union for validation set:', IOU_val, '\n########################') model_dir = './model' + str(epoch + 1 + already_done) + '/model.ckpt' with open('loss.txt', 'a') as wf: loss_info = '\nepoch: ' + '%7d' % ( epoch + 1 + already_done ) + ' batch loss: ' + '%7.6f' % loss_ + ' batch IOU: ' + '%7.6f' % IOU_this_batch + ' valiation IOU: ' + '%7.6f' % IOU_val wf.write(loss_info) saver.save(sess, model_dir) else: model_dir = './model/model.ckpt' saver.save(sess, model_dir)