-
Notifications
You must be signed in to change notification settings - Fork 70
/
dataset.py
86 lines (72 loc) · 2.48 KB
/
dataset.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 torch.utils.data as data
import torchvision.transforms as transforms
from PIL import Image
import utils
def pil_loader(path):
# open path as file to avoid ResourceWarning
# (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def accimage_loader(path):
import accimage
try:
return accimage.Image(path)
except IOError:
# Potentially a decoding problem, fall back to PIL.Image
return pil_loader(path)
def default_loader(path):
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader(path)
else:
return pil_loader(path)
class ImageDataset(data.Dataset):
def __init__(self,
root_dir,
meta_file,
transform=None,
image_size=128,
normalize=True):
self.root_dir = root_dir
if transform is not None:
self.transform = transform
else:
norm_mean = [0.5, 0.5, 0.5]
norm_std = [0.5, 0.5, 0.5]
if normalize:
self.transform = transforms.Compose([
utils.CenterCropLongEdge(),
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize(norm_mean, norm_std)
])
else:
self.transform = transforms.Compose([
utils.CenterCropLongEdge(),
transforms.Resize(image_size),
transforms.ToTensor()
])
with open(meta_file) as f:
lines = f.readlines()
print("building dataset from %s" % meta_file)
self.num = len(lines)
self.metas = []
self.classifier = None
for line in lines:
line_split = line.rstrip().split()
if len(line_split) == 2:
self.metas.append((line_split[0], int(line_split[1])))
else:
self.metas.append((line_split[0], -1))
print("read meta done")
def __len__(self):
return self.num
def __getitem__(self, idx):
filename = self.root_dir + '/' + self.metas[idx][0]
cls = self.metas[idx][1]
img = default_loader(filename)
# transform
if self.transform is not None:
img = self.transform(img)
return img, cls, self.metas[idx][0]