psnr = utils.eval_rec(points.numpy(), colors.numpy(), torch.cat((DC_coef, CT_q), 0).numpy(), Qstep, depth, inv_haar3D)
时间: 2023-06-12 14:06:33 浏览: 113
这段代码中,首先将点云数据和颜色数据转换为numpy数组,然后将DC系数和CT系数拼接成一个数组,再将Qstep、depth和inv_haar3D作为参数传入utils.eval_rec函数中进行重建,并将重建结果计算PSNR值。其中DC系数是离散余弦变换(DCT)的低频系数,CT系数是离散小波变换(DWT)的高频系数,Qstep是量化步长,depth是小波分解的层数,inv_haar3D是三维小波反变换的函数。
相关问题
from bm3d import bm3d_rgb from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr from PIL import Image import argparse import os import torch import numpy as np from torchvision.utils import save_image def main(): imagename = './test_image1/(1271).jpg' save_dir = 'test_result' save_path = 'noise' y = np.array(Image.open(imagename)) / 255 noise_type = 'g3' noise_var = 0.02 seed = 0 noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape) z = np.atleast_3d(y) + np.atleast_3d(noise) y_est = bm3d_rgb(z, psd) psnr = get_psnr(y, y_est) print("PSNR:", psnr) y_est = np.minimum(np.maximum(y_est, 0), 1) z_rang = np.minimum(np.maximum(z, 0), 1) z_rang = torch.from_numpy(np.transpose(z_rang, (2, 0, 1))).float() y_est = torch.from_numpy(np.transpose(y_est, (2, 0, 1))).float() denoise_img_path = os.path.join(save_dir, 'denoised.jpg') save_image(y_est, denoise_img_path) noise_img_path = os.path.join(save_path, 'noise.jpg') save_image(z_rang, noise_img_path) if __name__ == '__main__': main()改为对灰度图处理
可以将代码中的`bm3d_rgb`函数改为`bm3d`函数,同时将读入图像的方式改为读入灰度图像即可。具体代码如下:
```
from bm3d import bm3d
from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr
from PIL import Image
import argparse
import os
import torch
import numpy as np
from torchvision.utils import save_image
def main():
imagename = './test_image1/(1271).jpg'
save_dir = 'test_result'
save_path = 'noise'
y = np.array(Image.open(imagename).convert('L')) / 255
noise_type = 'g3'
noise_var = 0.02
seed = 0
noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape)
z = y + noise
y_est = bm3d(z, psd)
psnr = get_psnr(y, y_est)
print("PSNR:", psnr)
y_est = np.minimum(np.maximum(y_est, 0), 1)
z_rang = np.minimum(np.maximum(z, 0), 1)
z_rang = torch.from_numpy(z_rang).unsqueeze(0).float()
y_est = torch.from_numpy(y_est).unsqueeze(0).float()
denoise_img_path = os.path.join(save_dir, 'denoised.jpg')
save_image(y_est, denoise_img_path)
noise_img_path = os.path.join(save_path, 'noise.jpg')
save_image(z_rang, noise_img_path)
if __name__ == '__main__':
main()
```
def eval_psnr(loader, model, eval_type=None): model.eval() if eval_type == 'f1': metric_fn = utils.calc_f1 metric1, metric2, metric3, metric4 = 'f1', 'auc', 'none', 'none' elif eval_type == 'building': metric_fn = utils.calc_fmeasure metric1, metric2, metric3, metric4 = 'build', 'non_build', 'none', 'none' elif eval_type == 'ber': metric_fn = utils.calc_ber metric1, metric2, metric3, metric4 = 'shadow', 'non_shadow', 'ber', 'none' elif eval_type == 'cod': metric_fn = utils.calc_cod metric1, metric2, metric3, metric4 = 'sm', 'em', 'wfm', 'mae' if local_rank == 0: pbar = tqdm(total=len(loader), leave=False, desc='val') else: pbar = None pred_list = [] gt_list = [] for batch in loader: for k, v in batch.items(): batch[k] = v.cuda() inp = batch['inp'] pred = torch.sigmoid(model.infer(inp)) batch_pred = [torch.zeros_like(pred) for _ in range(dist.get_world_size())] batch_gt = [torch.zeros_like(batch['gt']) for _ in range(dist.get_world_size())] dist.all_gather(batch_pred, pred) pred_list.extend(batch_pred) dist.all_gather(batch_gt, batch['gt']) gt_list.extend(batch_gt) if pbar is not None: pbar.update(1) if pbar is not None: pbar.close() pred_list = torch.cat(pred_list, 1) gt_list = torch.cat(gt_list, 1) result1, result2, result3, result4 = metric_fn(pred_list, gt_list) return result1, result2, result3, result4, metric1, metric2, metric3, metric4
这是一个用于评估模型性能的函数,其输入参数包括一个数据集加载器(loader)、一个模型(model)和一个评估类型(eval_type)。函数根据评估类型选择不同的指标(metric_fn)来评估模型的性能,并返回四个评估结果(result1, result2, result3, result4)和四个指标(metric1, metric2, metric3, metric4)。函数的实现过程中,使用了分布式训练和异步数据加载的技术,以提高计算效率。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)