ilhamdev's picture
Update RealESRGAN/model.py
37286f1 verified
Raw History Blame Contribute Delete
3.02 kB
import os
import torch
from PIL import Image
import numpy as np
from huggingface_hub import hf_hub_download
from .rrdbnet_arch import RRDBNet
from .utils import pad_reflect, split_image_into_overlapping_patches, stich_together, unpad_image
HF_MODELS = {
2: dict(repo_id='ai-forever/Real-ESRGAN', filename='RealESRGAN_x2.pth'),
4: dict(repo_id='ai-forever/Real-ESRGAN', filename='RealESRGAN_x4.pth'),
8: dict(repo_id='ai-forever/Real-ESRGAN', filename='RealESRGAN_x8.pth'),
}
class RealESRGAN:
def __init__(self, device, scale=4):
self.device = device
self.scale = scale
self.model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=scale)
def load_weights(self, model_path, download=True):
if not os.path.exists(model_path) and download:
config = HF_MODELS[self.scale]
cache_dir = os.path.dirname(model_path) or 'weights'
os.makedirs(cache_dir, exist_ok=True)
hf_hub_download(repo_id=config['repo_id'], filename=config['filename'], local_dir=cache_dir)
print('Weights downloaded to:', model_path)
loadnet = torch.load(model_path, map_location=self.device)
if 'params' in loadnet:
self.model.load_state_dict(loadnet['params'], strict=True)
elif 'params_ema' in loadnet:
self.model.load_state_dict(loadnet['params_ema'], strict=True)
else:
self.model.load_state_dict(loadnet, strict=True)
self.model.eval().to(self.device)
def predict(self, lr_image, batch_size=4, patches_size=192, padding=24, pad_size=15):
lr_image = np.array(lr_image)
lr_image = pad_reflect(lr_image, pad_size)
# PERBAIKAN 3: Terima n_patches_per_row dari fungsi split
patches, p_shape, n_patches_per_row = split_image_into_overlapping_patches(lr_image, patch_size=patches_size, padding_size=padding)
img = torch.FloatTensor(patches/255).permute((0,3,1,2)).to(self.device).detach()
with torch.no_grad():
res = self.model(img[0:batch_size])
for i in range(batch_size, img.shape[0], batch_size):
res = torch.cat((res, self.model(img[i:i+batch_size])), 0)
sr_image = res.permute((0,2,3,1)).cpu().clamp_(0, 1).numpy()
padded_size_scaled = tuple(np.multiply(p_shape[0:2], self.scale)) + (3,)
scaled_image_shape = tuple(np.multiply(lr_image.shape[0:2], self.scale)) + (3,)
# PERBAIKAN 4: Kirim n_patches_per_row ke fungsi stich_together
np_sr_image = stich_together(
sr_image,
padded_image_shape=padded_size_scaled,
target_shape=scaled_image_shape,
padding_size=padding * self.scale,
n_patches_per_row=n_patches_per_row
)
sr_img = (np_sr_image * 255).astype(np.uint8)
sr_img = unpad_image(sr_img, pad_size * self.scale)
return Image.fromarray(sr_img)