forked from yuzhoucw/230pix2pix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
87 lines (70 loc) · 3.36 KB
/
dataloader.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
import os
import numpy as np
import random
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from PIL import Image
class GANDataset(Dataset):
# Initial logic here, including reading the image files and transform the data
def __init__(self, rootA, rootB, transform=None, unaligned=False, device=None, test=False):
# initialize image path and transformation
sortedA = sorted(os.listdir(rootA), key=lambda name: int(name.split('_')[0]))
sortedB = sorted(os.listdir(rootB), key=lambda name: int(name.split('_')[0]))
self.image_pathsA = list(map(lambda x: os.path.join(rootA, x), sortedA))
self.image_pathsB = list(map(lambda x: os.path.join(rootB, x), sortedB))
self.transform = transform
self.unaligned = unaligned
self.device = device
self.test = test
# override to support indexing
def __getitem__(self, index):
image_pathA = self.image_pathsA[index]
imageA = Image.open(image_pathA).convert('RGB')
# unaligned the paired images if needed
if self.unaligned:
image_pathB = self.image_pathsB[random.randint(0, len(self.image_pathsB)-1)]
else:
image_pathB = self.image_pathsB[index]
imageB = Image.open(image_pathB).convert('RGB')
# transform the images if needed
if self.transform is not None:
if self.test:
imageA = self.transform(imageA)
imageB = self.transform(imageB)
else: # if test is False, need to crop and flip the same way
# setting the same seed for input and target tansformations
seed = np.random.randint(2147483647)
random.seed(seed)
imageA = self.transform(imageA)
random.seed(seed)
imageB = self.transform(imageB)
# convert to GPU tensor
if self.device is not None:
imageA = imageA.to(self.device)
imageB = imageB.to(self.device)
return imageA, imageB, index+1
# returns the number of examples we read
def __len__(self):
# print(len(self.image_pathsA))
return max(len(self.image_pathsA), len(self.image_pathsB)) # of how many examples we have
## return - DataLoader
def get_dataloader(image_pathA, image_pathB, batch_size, resize, crop, unaligned=False, device=None, shuffle=True, test=False):
if test:
transform = transforms.Compose([
transforms.Resize(crop, Image.BICUBIC), # resize to crop size directly
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])
else:
transform = transforms.Compose([
# resize PIL image to given size
transforms.Resize(resize, Image.BICUBIC),
# crop image at randomn location
transforms.RandomCrop(crop),
# flip images randomly
transforms.RandomHorizontalFlip(),
# convert image input into torch tensor
transforms.ToTensor(),
# normalize image with mean and standard deviation
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
batch_dataset = GANDataset(image_pathA, image_pathB, transform, unaligned, device, test)
return DataLoader(dataset=batch_dataset, batch_size=batch_size, shuffle=shuffle)