def run_test(self): test_data, test_label = test_input_setup(self) print("Testing...") start_time = time.time() result = np.clip( self.pred.eval({ self.images: test_data, self.labels: test_label, self.batch: 1 }), 0, 1) passed = time.time() - start_time img1 = tf.convert_to_tensor(test_label, dtype=tf.float32) img2 = tf.convert_to_tensor(result, dtype=tf.float32) psnr = self.sess.run(tf.image.psnr(img1, img2, 1)) ssim = self.sess.run(tf.image.ssim(img1, img2, 1)) print("Took %.3f seconds, PSNR: %.6f, SSIM: %.6f" % (passed, psnr, ssim)) result = merge(self, result) image_path = os.path.join(os.getcwd(), self.output_dir) image_path = os.path.join(image_path, "test_image.png") array_image_save(result, image_path)
def run_test(self): nx, ny = test_input_setup(self) data_dir = os.path.join('{}'.format(self.checkpoint_dir), "test.h5") test_data, test_label = read_data(data_dir) test_image = merge(test_data, [nx, ny]).squeeze() print("nx =", nx, "\nny =", ny) print(test_image.shape) print(test_data.shape) # print(test_data) print("Testing...") start_time = time.time() result = self.pred.eval({ self.images: test_data, self.labels: test_label, self.batch: nx * ny }) print("Took %.3f seconds" % (time.time() - start_time)) result = merge(result, [nx, ny]) result = result.squeeze() image_path = os.path.join(os.getcwd(), self.output_dir) # image_path = os.path.join(image_path, "test_image.png") image_path = os.path.join(image_path, "test_image.png") array_image_save(result * 255, image_path)
def run_test(self): print("[INFO] Testing . . .") # NOTE only outputting 15 test examples for k in range(0, 15): np.random.seed(k) test_data, test_label = test_input_setup(self, np.random.randint(0,200)) print("Testing...") start_time = time.time() result = np.clip(self.pred.eval({self.images: test_data, self.labels: test_label, self.batch: 1}), 0, 1) print(" RESULT ", result.shape) print(" test_data ", test_data.shape) print(" test_label ", test_label.shape) passed = time.time() - start_time img1 = tf.convert_to_tensor(test_label, dtype=tf.float32) img2 = tf.convert_to_tensor(result, dtype=tf.float32) psnr = self.sess.run(tf.image.psnr(img1, img2, 1)) ssim = self.sess.run(tf.image.ssim(img1, img2, 1)) print("Took %.3f seconds, PSNR: %.6f, SSIM: %.6f" % (passed, psnr, ssim)) image_path = os.path.join(os.getcwd(), self.output_dir) image_path = os.path.join(image_path, "test_image.png") import matplotlib.pyplot as plt fig = plt.figure(figsize=(11,11)) ax1 = fig.add_subplot(131); ax1.imshow(test_data.reshape(128,128)); ax1.axis('off'); ax1.set_title('LR') ax2 = fig.add_subplot(132); ax2.imshow(test_label.reshape(384,384)); ax2.axis('off'); ax2.set_title('HR') ax3 = fig.add_subplot(133); ax3.imshow(result.reshape(384,384)); ax3.axis('off'); ax3.set_title('Predicted') plt.savefig('./result/' + str(k) + '_test.png') # Calculate Test MSE all = mse_models(self) mse_all = [] for img in all: test_data = img[0] test_label = img[1] result = np.clip(self.pred.eval({self.images: test_data, self.labels: test_label, self.batch: 1}), 0, 1) result = result.reshape(384,384) test_label = test_label.reshape(384,384) mse = np.sum((np.abs(result.astype("float") - test_label.astype("float"))) ** 2) mse /= float(result.shape[0] * result.shape[1]) mse_all.append(mse) print("##########################") print("MSE IS: ", np.mean(mse_all))
def run_test(self): test_data, test_label, nx, ny = test_input_setup(self) print("Testing...") start_time = time.time() result = self.pred.eval({self.images: test_data, self.labels: test_label, self.batch: nx * ny}) print("Took %.3f seconds" % (time.time() - start_time)) result = merge(result, [nx, ny, self.c_dim]) result = result.squeeze() image_path = os.path.join(os.getcwd(), self.output_dir) image_path = os.path.join(image_path, "test_image.png") array_image_save(result * 255, image_path)
def run_test(self): nx, ny = test_input_setup(self) data_dir = os.path.join('./{}'.format(self.checkpoint_dir), 'test.h5') test_data, test_label = read_data(data_dir) print( '................................Testing..................................' ) result = self.pred.eval({ self.images: test_data, self.labels: test_label, self.batch: nx * ny }) result = merge(result, [nx, ny]) result = result.squeeze() image_path = os.path.join(os.getcwd(), self.output_dir) image_path = os.path.join(image_path, "test_image.png") array_image_save(result * 255, image_path)
def run_test(self): # 在test_input_setup中提取出image的Y通道数据下采样后归一化,进行reshape和padding=4 test_data_label_arr = test_input_setup(self) print("Testing...") i = 0 for test_data, test_label in test_data_label_arr: start_time = time.time() result = np.clip(self.pred.eval({self.images: test_data, self.labels: test_label, self.batch: 1}), 0, 1) passed = time.time() - start_time img1 = tf.convert_to_tensor(test_label, dtype=tf.float32) img2 = tf.convert_to_tensor(result, dtype=tf.float32) psnr = self.sess.run(tf.image.psnr(img1, img2, 1)) ssim = self.sess.run(tf.image.ssim(img1, img2, 1)) print("Took %.3f seconds, PSNR: %.6f, SSIM: %.6f" % (passed, psnr, ssim)) # 将Y通道处理后合并到原图的CbCr result = merge(self, result, i) image_path = os.path.join(os.getcwd(), self.output_dir) image_path = os.path.join(image_path, "test_image_" + str(i) + ".png") i = i + 1 array_image_save(result, image_path)
def run_test(self): # 读取测试数据集图片 data_dir = os.path.join(os.sep, (os.path.join(os.getcwd(), self.TestData_dir)), "Set5") data = sorted(glob.glob(os.path.join(data_dir, "*.bmp"))) data_img = prepare_data(self, dataset=self.TestData_dir) # 生成列表记录每次的psnr,准备用于psnr的平均值求取 all_psnr_bic, all_psnr_fsr = [], [] # 遍历路径下的图片,并进行测试 for i in range(len(data)): # 制作测试用的h5文件,并读取测试图和各类输入图 nx, ny, im_y_hr_same, im_y_bic_same, im_cb_bic_same, im_cr_bic_same, color_im = test_input_setup( self, i, data_img) data_dir = os.path.join('./{}'.format(self.checkpoint_dir), "test.h5") test_data, test_label = read_data(data_dir) # 开始测试 print("Testing...") start_time = time.time() result = self.SRpred.eval({ self.LRs: test_data, self.HRs: test_label, self.batch: nx * ny }) print("Took %.3f seconds" % (time.time() - start_time)) # 对输出结果图片转换size储存形式 result = merge(result, [nx, ny]) result = result.squeeze() # 从数组的形状中删除单维度条目,即把shape中为1的维度去掉 # 对输出结果、bicubic、原图去除边界,提取干净数据 height, weight = result.shape[0], result.shape[1] result = result[2:height - 2, 2:weight - 2] # result = result[1:height-3, 1:weight-3] # matlab官方在*3上的做法,但此处效果不对,应该是卷积核的区别 im_y_hr_same = im_y_hr_same[2:height - 2, 2:weight - 2] im_y_bic_same = im_y_bic_same[2:height - 2, 2:weight - 2] # 对输出图片进行批量格式化命名 dst00 = os.path.join(os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_BIC.bmp') dst10 = os.path.join(os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_FSR.bmp') dst20 = os.path.join(os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_HR.bmp') # 保存各类测试结果图 array_image_save(im_y_bic_same * 255, dst00, color_im) # 保存bicubic输出灰度图 array_image_save(im_y_hr_same * 255, dst20, color_im) # 保存hr输出灰度图 array_image_save(result * 255, dst10, color_im) # 保存fsrcnn输出灰度图 # 计算每次bicubic和fsrcnn测试结果的psnr psnr_bic = self.psnr_computer(im_y_hr_same, im_y_bic_same) psnr_fsr = self.psnr_computer(im_y_hr_same, result) all_psnr_bic.append(psnr_bic) all_psnr_fsr.append(psnr_fsr) print('PSNR for Bicubic:', psnr_bic, 'dB\n' 'PSNR for FSRCNN:', psnr_fsr, 'dB\n') if color_im == 1: # 对cb,cr通道去除边界,提取干净数据 im_cb_bic_same = im_cb_bic_same[2:height - 2, 2:weight - 2] im_cr_bic_same = im_cr_bic_same[2:height - 2, 2:weight - 2] # 将y,cb,cr通道转为rgb通道,并对溢出值进行处理 bic_color = color_image_save(im_y_bic_same, im_cb_bic_same, im_cr_bic_same) fsr_color = color_image_save(result, im_cb_bic_same, im_cr_bic_same) hr_color = color_image_save(im_y_hr_same, im_cb_bic_same, im_cr_bic_same) # 对输出图片进行批量格式化命名 dst01 = os.path.join( os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_BIC_color.bmp') dst11 = os.path.join( os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_FSR_color.bmp') dst21 = os.path.join( os.path.abspath(self.output_dir), '00' + format(str(i), '0>3s') + '_HR_color.bmp') # 保存各类测试结果图 skio.imsave(dst01, bic_color) # 保存bicubic输出彩色图 skio.imsave(dst11, fsr_color) # 保存hr输出彩色图 skio.imsave(dst21, hr_color) # 保存fsrcnn输出彩色图 # 计算总的bicubic和fsrcnn测试结果的平均psnr mean_psnr_bic, mean_psnr_fsr = np.mean(all_psnr_bic), np.mean( all_psnr_fsr) print('Mean PSNR for Bicubic:', mean_psnr_bic, 'dB\n' 'Mean PSNR for FSRCNN:', mean_psnr_fsr, 'dB\n')