def eval(self): print('Evaluating ...') if not hasattr(self, 'sess'): sess = tf.Session() self.load(sess, self.save_dir) else: sess = self.sess border=8 in_h,in_w=self.eval_in_size out_h = in_h*self.scale #512 out_w = in_w*self.scale #960 bd=border//self.scale eval_gt = tf.placeholder(tf.float32, [None, self.num_frames, out_h, out_w, 3]) eval_inp=DownSample(eval_gt, BLUR, scale=self.scale) filenames=open(self.eval_dir, 'rt').read().splitlines() # sorted(glob.glob(join(self.eval_dir,'*'))) # gt_list=[sorted(glob.glob(join(f,'truth','*.png'))) for f in filenames] gt_list=[sorted(glob.glob(join('H:/AI4K/data/frame_data/training/HR', f, '*.png'))) for f in filenames] gt_list = gt_list[:3] center=15 batch_gt = [] batch_cnt=0 mse_acc=None for gtlist in gt_list: max_frame=len(gtlist) for idx0 in range(center, max_frame, 32): index=np.array([i for i in range(idx0-self.num_frames//2,idx0+self.num_frames//2+1)]) index=np.clip(index,0,max_frame-1).tolist() gt=[cv2_imread(gtlist[i]) for i in index] gt = [i[border:out_h+border, border:out_w+border, :].astype(np.float32) / 255.0 for i in gt] batch_gt.append(np.stack(gt, axis=0)) if len(batch_gt) == self.eval_basz: batch_gt = np.stack(batch_gt, 0) batch_lr=sess.run(eval_inp,feed_dict={eval_gt:batch_gt}) mse_val=sess.run(self.eval_mse,feed_dict={self.L_eval:batch_lr, self.H:batch_gt[:,self.num_frames//2:self.num_frames//2+1]}) if mse_acc is None: mse_acc = mse_val else: mse_acc = np.concatenate([mse_acc, mse_val], axis=0) batch_gt = [] print('\tEval batch {} - {} ...'.format(batch_cnt, batch_cnt + self.eval_basz)) batch_cnt+=self.eval_basz psnr_acc = 10 * np.log10(1.0 / mse_acc) mse_avg = np.mean(mse_acc, axis=0) psnr_avg = np.mean(psnr_acc, axis=0) for i in range(mse_avg.shape[0]): tf.summary.scalar('val_mse{}'.format(i), tf.convert_to_tensor(mse_avg[i], dtype=tf.float32)) print('Eval PSNR: {}, MSE: {}'.format(psnr_avg, mse_avg)) # write to log file with open(self.log_dir, 'a+') as f: mse_avg=(mse_avg*1e6).astype(np.int64)/(1e6) psnr_avg=(psnr_avg*1e6).astype(np.int64)/(1e6) f.write('{'+'"Iter": {} , "PSNR": {}, "MSE": {}'.format(sess.run(self.global_step), psnr_avg.tolist(), mse_avg.tolist())+'}\n')
def __init__(self, in_channels, n_classes, bilinear=True): super(Unet, self).__init__() self.in_channels = in_channels self.n_classes = n_classes self.bilinear = bilinear self.conv1 = Conv_BN_Relu(self.in_channels, 64) self.down1 = DownSample(in_channels=64, out_channels=128) self.down2 = DownSample(in_channels=128, out_channels=256) self.down3 = DownSample(in_channels=256, out_channels=512) self.down4 = DownSample(in_channels=512, out_channels=512) self.up1 = UpSample(in_channels=1024, out_channels=256) self.up2 = UpSample(in_channels=512, out_channels=128) self.up3 = UpSample(in_channels=256, out_channels=64) self.up4 = UpSample(in_channels=128, out_channels=64) self.output = Output_Layer_Conv(64, self.n_classes)
def test_video_truth(self, path, name='result', reuse=False, part=8): save_path = join(path, name) automkdir(save_path) imgs = sorted(glob.glob(join(path, 'truth', '*.png'))) imgs = [cv2_imread(i) / 255. for i in imgs] test_gt = tf.placeholder(tf.float32, [None, self.num_frames, None, None, 3]) test_inp = DownSample(test_gt, BLUR, scale=self.scale) if not reuse: self.build() sess = tf.Session() self.sess = sess sess.run(tf.global_variables_initializer()) self.saver = tf.train.Saver(max_to_keep=100, keep_checkpoint_every_n_hours=1) self.load(sess, self.save_dir) gt_list = [] max_frame = len(imgs) for i in range(max_frame): index = np.array([ i for i in range(i - self.num_frames // 2, i + self.num_frames // 2 + 1) ]) index = np.clip(index, 0, max_frame - 1).tolist() gt = np.array([imgs[j] for j in index]) gt_list.append(gt) gt_list = np.array(gt_list) lr_list = self.sess.run(test_inp, feed_dict={test_gt: gt_list}) print('Save at {}'.format(save_path)) print('{} Inputs With Shape {}'.format(lr_list.shape[0], lr_list.shape[1:])) part = min(part, max_frame) if max_frame % part == 0: num_once = max_frame // part else: num_once = max_frame // part + 1 all_time = 0 for i in trange(part): st_time = time.time() sr = self.sess.run(self.SR, feed_dict={ self.L: lr_list[i * num_once:(i + 1) * num_once], self.is_train: False }) onece_time = time.time() - st_time if i > 0: all_time += onece_time for j in range(sr.shape[0]): img = sr[j][0] * 255. img = np.clip(img, 0, 255).astype(np.uint8) imgname = '{:0>4}.png'.format(i * num_once + j) cv2_imsave(join(save_path, imgname), img) print('spent {} s in total and {} s in average'.format( all_time, all_time / (max_frame - 1)))
def gkern(kernlen=13, nsig=1.6): import scipy.ndimage.filters as fi # create nxn zeros inp = np.zeros((kernlen, kernlen)) # set element at the middle to one, a dirac delta inp[kernlen // 2, kernlen // 2] = 1 # gaussian-smooth the dirac, resulting in a gaussian filter mask return fi.gaussian_filter(inp, nsig) h = gkern(13, 1.6) # 13 and 1.6 for x4 h = h[:, :, np.newaxis, np.newaxis].astype(np.float32) # Network H = tf.placeholder(tf.float32, shape=[None, T_in, None, None, 3]) L_ = DownSample(H, h, R) L = L_[:, :, 2:-2, 2:-2, :] # To minimize boundary artifact is_train = tf.placeholder(tf.bool, shape=[]) # Phase ,scalar with tf.variable_scope('G') as scope: GH = G(L, is_train) params_G = [v for v in tf.global_variables() if v.name.startswith('G/')] # Session config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: tf.global_variables_initializer().run()