-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
48 lines (44 loc) · 1.63 KB
/
Copy pathdata.py
File metadata and controls
48 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# --- data.py ---------------------------------------------------------------
import glob, os, random
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as T
import torch
from sr3 import *
def identity(x):
return x
class SRFolderDataset(Dataset):
"""
Expects a directory full of HR PNG/JPG images.
LR is generated on-the-fly with bicubic ↓.
"""
def __init__(self,
root_dir: str,
hr_size: int = HR_IMG_SIZE,
lr_size: int = LR_IMG_SIZE,
training: bool = True):
super().__init__()
self.paths = sorted(
sum([glob.glob(os.path.join(root_dir, ext))
for ext in ("*.png", "*.jpg", "*.jpeg")], [])
)
self.training = training
self.hr_tf = T.Compose([
# random crops / flips only during training
T.RandomCrop(hr_size) if training else T.CenterCrop(hr_size),
T.RandomHorizontalFlip() if training else T.Lambda(identity),
T.ToTensor(), # (0,1)
T.Normalize(0.5, 0.5) # (-1,1)
])
self.lr_tf = T.Compose([
T.Resize(lr_size, interpolation=T.InterpolationMode.BICUBIC),
T.ToTensor(),
T.Normalize(0.5, 0.5)
])
def __len__(self): return len(self.paths)
def __getitem__(self, idx):
path = self.paths[idx]
img_hr = Image.open(path).convert("RGB")
img_hr = self.hr_tf(img_hr)
img_lr = self.lr_tf(T.ToPILImage()( (img_hr * 0.5 + 0.5).clamp(0,1) ))
return img_lr, img_hr