-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdata_loader.py
130 lines (102 loc) · 3.94 KB
/
data_loader.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#-*-coding:utf-8-*-
from pathlib import Path
from itertools import chain
import os
from munch import Munch
from PIL import Image
import torch
from torch.utils import data
from torchvision import transforms
def listdir(dname):
fnames = list(chain(*[list(Path(dname).rglob('*.' + ext))
for ext in ['png', 'jpg', 'jpeg', 'JPG']]))
return fnames
class DefaultDataset(data.Dataset):
def __init__(self, root, transform=None):
self.samples = listdir(root)
self.samples.sort()
self.transform = transform
self.targets = None
def __getitem__(self, index):
fname = self.samples[index]
img = Image.open(fname).convert('RGB')
if self.transform is not None:
img = self.transform(img)
return img
def __len__(self):
return len(self.samples)
class ReferenceDataset(data.Dataset):
def __init__(self, root, transform=None):
self.samples = self._make_dataset(root)
self.transform = transform
def _make_dataset(self, root):
domains = os.listdir(root)
fnames, fnames2 = [], []
for idx, domain in enumerate(sorted(domains)):
class_dir = os.path.join(root, domain)
cls_fnames = listdir(class_dir)
if idx == 0:
fnames += cls_fnames
elif idx == 1:
fnames2 += cls_fnames
return list(zip(fnames, fnames2))
def __getitem__(self, index):
fname, fname2 = self.samples[index]
name = str(fname2)
img_name, _ = name.split('.', 1)
_, img_name = img_name.rsplit('/', 1)
img = Image.open(fname).convert('RGB')
img2 = Image.open(fname2).convert('RGB')
if self.transform is not None:
img = self.transform(img)
img2 = self.transform(img2)
return img, img2, img_name
def __len__(self):
return len(self.samples)
def get_train_loader(root, img_size=512, resize_size=256, batch_size=8, shuffle=True, num_workers=8, drop_last=True):
transform = transforms.Compose([
transforms.RandomCrop(img_size),
transforms.Resize([resize_size, resize_size]),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
dataset = ReferenceDataset(root, transform)
return data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=True,
drop_last=drop_last)
def get_test_loader(root, img_size=512, batch_size=8, shuffle=False, num_workers=4):
transform = transforms.Compose([
# transforms.CenterCrop(img_size),
transforms.Resize([img_size, img_size]),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
dataset = ReferenceDataset(root, transform)
return data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=True)
class InputFetcher:
def __init__(self, loader):
self.loader = loader
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def _fetch_refs(self):
try:
x, y, name = next(self.iter)
except (AttributeError, StopIteration):
self.iter = iter(self.loader)
x, y, name = next(self.iter)
return x, y, name
def __next__(self):
x, y, img_name = self._fetch_refs()
x, y = x.to(self.device), y.to(self.device)
inputs = Munch(img_exp=x, img_raw=y, img_name=img_name)
return inputs