Create dress_enhancer.py
Browse files- model/dress_enhancer.py +36 -0
model/dress_enhancer.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from basicsr.archs.swinir_arch import SwinIR
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision.transforms.functional import to_tensor, to_pil_image
|
| 7 |
+
|
| 8 |
+
# Load pretrained SwinIR model
|
| 9 |
+
def load_swinir_model():
|
| 10 |
+
model = SwinIR(
|
| 11 |
+
upscale=2,
|
| 12 |
+
in_chans=3,
|
| 13 |
+
img_size=64,
|
| 14 |
+
window_size=8,
|
| 15 |
+
img_range=1.0,
|
| 16 |
+
depths=[6, 6, 6, 6],
|
| 17 |
+
embed_dim=180,
|
| 18 |
+
num_heads=[6, 6, 6, 6],
|
| 19 |
+
mlp_ratio=2,
|
| 20 |
+
upsampler='nearest+conv',
|
| 21 |
+
resi_connection='1conv'
|
| 22 |
+
)
|
| 23 |
+
model_path = 'https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/swinir_sr_classical_patch64_x2_48.pth'
|
| 24 |
+
ckpt = torch.hub.load_state_dict_from_url(model_path, map_location='cpu')
|
| 25 |
+
model.load_state_dict(ckpt['params'], strict=True)
|
| 26 |
+
model.eval()
|
| 27 |
+
return model
|
| 28 |
+
|
| 29 |
+
swinir = load_swinir_model()
|
| 30 |
+
|
| 31 |
+
@torch.no_grad()
|
| 32 |
+
def enhance_clothes(image_pil):
|
| 33 |
+
image = to_tensor(image_pil).unsqueeze(0)
|
| 34 |
+
output = swinir(image)
|
| 35 |
+
output_image = output.squeeze().clamp(0, 1)
|
| 36 |
+
return to_pil_image(output_image)
|