|
1 | 1 | import torch |
2 | 2 | from torch import optim |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | +from skimage.metrics import peak_signal_noise_ratio, structural_similarity |
| 6 | +from tqdm import tqdm |
| 7 | + |
3 | 8 |
|
4 | 9 | from src.esrgan.model import Generator |
5 | 10 | from src.utils import config |
6 | 11 | from src.utils.utils import load_checkpoint, plot_examples |
7 | 12 | from src.utils.utils import seed_torch |
| 13 | +from src.utils.data_loaders import get_loaders |
| 14 | + |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import numpy as np |
| 17 | +from skimage.metrics import peak_signal_noise_ratio, structural_similarity |
| 18 | + |
| 19 | + |
| 20 | +def evaluate_and_plot(test_loader, gen, num_samples=3): |
| 21 | + psnr_list = [] |
| 22 | + ssim_list = [] |
| 23 | + selected_samples = [] |
| 24 | + |
| 25 | + loop = tqdm(test_loader, desc="Evaluating", leave=False) |
| 26 | + with torch.no_grad(): |
| 27 | + for idx, (low_res, high_res) in enumerate(loop): |
| 28 | + low_res = low_res.to(config.DEVICE) |
| 29 | + high_res = high_res.to(config.DEVICE) |
| 30 | + |
| 31 | + # Generator ile yüksek çözünürlüklü görüntü üret |
| 32 | + fake_high_res = gen(low_res) |
| 33 | + |
| 34 | + # PSNR ve SSIM hesapla |
| 35 | + for i in range(low_res.shape[0]): |
| 36 | + sr_img = fake_high_res[i].cpu().numpy().transpose(1, 2, 0) |
| 37 | + hr_img = high_res[i].cpu().numpy().transpose(1, 2, 0) |
| 38 | + lr_img = low_res[i].cpu().numpy().transpose(1, 2, 0) |
| 39 | + |
| 40 | + # Normalize görüntüler (0-1 aralığına) |
| 41 | + sr_img = np.clip(sr_img, 0, 1) |
| 42 | + hr_img = np.clip(hr_img, 0, 1) |
| 43 | + lr_img = np.clip(lr_img, 0, 1) |
| 44 | + |
| 45 | + # PSNR ve SSIM hesapla |
| 46 | + psnr = peak_signal_noise_ratio(hr_img, sr_img, data_range=1.0) |
| 47 | + ssim = structural_similarity( |
| 48 | + hr_img, sr_img, channel_axis=2, data_range=1.0 |
| 49 | + ) |
| 50 | + psnr_list.append(psnr) |
| 51 | + ssim_list.append(ssim) |
| 52 | + |
| 53 | + # İlk num_samples kadar örneği seç |
| 54 | + if len(selected_samples) < num_samples: |
| 55 | + selected_samples.append((lr_img, hr_img, sr_img, psnr, ssim)) |
| 56 | + |
| 57 | + # Ortalama PSNR ve SSIM değerlerini yazdır |
| 58 | + print(f"Average PSNR: {np.mean(psnr_list):.4f}") |
| 59 | + print(f"Average SSIM: {np.mean(ssim_list):.4f}") |
| 60 | + |
| 61 | + # Seçilen örnekleri çiz |
| 62 | + fig, axes = plt.subplots(num_samples, 3, figsize=(10, 3 * num_samples)) |
| 63 | + fig.suptitle( |
| 64 | + f"Super-Resolution Evaluation Samples\nPSNR: {np.mean(psnr_list):.4f} SSIM{np.mean(ssim_list):.4f}", |
| 65 | + fontsize=16, |
| 66 | + ) |
| 67 | + for i, (lr, hr, sr, psnr, ssim) in enumerate(selected_samples): |
| 68 | + axes[i, 0].imshow(lr) |
| 69 | + axes[i, 0].set_title(f"LR") |
| 70 | + axes[i, 0].axis("off") |
| 71 | + |
| 72 | + axes[i, 1].imshow(sr) |
| 73 | + axes[i, 1].set_title(f"SR PSNR: {psnr:.2f} SSIM: {ssim:.4f}") |
| 74 | + axes[i, 1].axis("off") |
| 75 | + |
| 76 | + axes[i, 2].imshow(hr) |
| 77 | + axes[i, 2].set_title(f"HR") |
| 78 | + axes[i, 2].axis("off") |
| 79 | + |
| 80 | + plt.tight_layout() |
| 81 | + plt.savefig( |
| 82 | + f"{config.SAVE_PATH}/evaluation_samples.png", dpi=300, bbox_inches="tight" |
| 83 | + ) |
| 84 | + plt.show() |
| 85 | + |
8 | 86 |
|
9 | 87 | def test(): |
10 | 88 | seed_torch(config.SEED) |
| 89 | + _, _, test_loader = get_loaders() |
11 | 90 | gen = Generator(in_channels=3).to(config.DEVICE) |
12 | | - opt_gen = optim.Adam(gen.parameters(), lr=config.LEARNING_RATE, betas=(0.0, 0.9)) |
13 | | - load_checkpoint( |
14 | | - config.CHECKPOINT_GEN, |
15 | | - gen, |
16 | | - opt_gen, |
17 | | - config.LEARNING_RATE, |
18 | | - ) |
19 | | - plot_examples(config.TEST_IMAGE_DIR, gen) |
| 91 | + gen.eval() |
| 92 | + load_checkpoint(config.CHECKPOINT_GEN, gen) |
| 93 | + print("Testing the model...") |
| 94 | + evaluate_and_plot(test_loader, gen, num_samples=12) |
0 commit comments