| import os |
| import cv2 |
| import torch |
| import argparse |
| import numpy as np |
| import torch.nn as nn |
| import torch.nn.functional as F |
| from skimage import img_as_ubyte |
| from basicsr.models.archs.restormer_arch import Restormer |
| import yaml |
|
|
| def load_img(filepath): |
| return cv2.cvtColor(cv2.imread(filepath), cv2.COLOR_BGR2RGB) |
|
|
| def save_img(filepath, img): |
| cv2.imwrite(filepath, cv2.cvtColor(img, cv2.COLOR_RGB2BGR)) |
|
|
| def load_model(yaml_path, weight_path): |
| """加载 Restormer 模型""" |
| try: |
| from yaml import CLoader as Loader |
| except ImportError: |
| from yaml import Loader |
|
|
| with open(yaml_path, mode='r') as f: |
| config = yaml.load(f, Loader=Loader) |
|
|
| net_config = config['network_g'] |
| net_type = net_config.pop('type', None) |
| model = Restormer(**net_config) |
| checkpoint = torch.load(weight_path) |
| model.load_state_dict(checkpoint['params']) |
| model.cuda() |
| model.eval() |
| return model |
|
|
| def restore_image(model, input_image_path, output_image_path, factor=8): |
| """用模型复原单张图片""" |
| img = np.float32(load_img(input_image_path)) / 255.0 |
| img_t = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0).cuda() |
|
|
| |
| h, w = img_t.shape[2], img_t.shape[3] |
| H, W = ((h + factor) // factor) * factor, ((w + factor) // factor) * factor |
| padh, padw = H - h, W - w |
| if padh != 0 or padw != 0: |
| img_t = F.pad(img_t, (0, padw, 0, padh), 'reflect') |
|
|
| with torch.no_grad(): |
| restored = model(img_t) |
| restored = restored[:, :, :h, :w] |
| restored = torch.clamp(restored, 0, 1).cpu().permute(0, 2, 3, 1).squeeze(0).numpy() |
|
|
| save_img(output_image_path, img_as_ubyte(restored)) |
| print(f"✅ Saved restored image to: {output_image_path}") |
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Single image restoration using Restormer") |
| parser.add_argument("--input", required=True, type=str, help="Path to input image") |
| parser.add_argument("--output", required=True, type=str, help="Path to save output image") |
| parser.add_argument("--model", required=True, choices=['restormer.gaussian_denoise_15', 'restormer.gaussian_denoise_25', 'restormer.gaussian_denoise_50', 'restormer.real_denoise', 'restormer.derain', 'restormer.defocus_deblur', 'restormer.motion_deblur'], |
| help="Model type to use") |
| args = parser.parse_args() |
|
|
| |
| model_configs = { |
| "restormer.gaussian_denoise_15": { |
| "yaml": "/hdd/Restoration/Restormer/Denoising/Options/GaussianColorDenoising_RestormerSigma15.yml", |
| "weights": "/hdd/Restoration/Inference/Restormer/Denoising/pretrained_models/gaussian_color_denoising_sigma15.pth" |
| }, |
| "restormer.gaussian_denoise_25": { |
| "yaml": "/hdd/Restoration/Restormer/Denoising/Options/GaussianColorDenoising_RestormerSigma25.yml", |
| "weights": "/hdd/Restoration/Restormer/Denoising/pretrained_models/gaussian_color_denoising_sigma25.pth" |
| }, |
| "restormer.gaussian_denoise_50": { |
| "yaml": "/hdd/Restoration/Restormer/Denoising/Options/GaussianColorDenoising_RestormerSigma50.yml", |
| "weights": "/hdd/Restoration/Restormer/Denoising/pretrained_models/gaussian_color_denoising_sigma50.pth" |
| }, |
| "restormer.real_denoise": { |
| "yaml": "/hdd/Restoration/Inference/Restormer/Denoising/Options/RealDenoising_Restormer.yml", |
| "weights": "/hdd/Restoration/Inference/Restormer/Denoising/pretrained_models/real_denoising.pth" |
| }, |
| "restormer.derain": { |
| "yaml": "/hdd/Restoration/Restormer/Deraining/Options/Deraining_Restormer.yml", |
| "weights": "/hdd/Restoration/Restormer/Deraining/pretrained_models/deraining.pth" |
| }, |
| "restormer.defocus_deblur": { |
| "yaml": "/hdd/Restoration/Restormer/Defocus_Deblurring/Options/DefocusDeblur_Single_8bit_Restormer.yml", |
| "weights": "/hdd/Restoration/Restormer/Defocus_Deblurring/pretrained_models/single_image_defocus_deblurring.pth" |
| }, |
| "restormer.motion_deblur": { |
| "yaml": "/hdd/Restoration/Inference/Restormer/Motion_Deblurring/Options/Deblurring_Restormer.yml", |
| "weights": "/hdd/Restoration/Inference/Restormer/Motion_Deblurring/pretrained_models/motion_deblurring.pth" |
| } |
| } |
|
|
| config = model_configs[args.model] |
| model = load_model(config["yaml"], config["weights"]) |
| restore_image(model, args.input, args.output) |
|
|