-
Notifications
You must be signed in to change notification settings - Fork 118
/
dataset.py
98 lines (74 loc) · 2.44 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
87
88
89
90
91
92
93
94
95
96
97
98
import os
import random
import numpy as np
from PIL import Image
from skimage import io
from skimage.color import gray2rgb
import torch
from torch.utils.data import Dataset
import warnings
class AEI_Dataset(Dataset):
def __init__(self, root, transform=None):
super(AEI_Dataset, self).__init__()
self.root = root
self.files = [
os.path.join(path, filename)
for path, dirs, files in os.walk(root)
for filename in files
if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".jpeg")
]
self.transform = transform
def __getitem__(self, index):
l = len(self.files)
s_idx = index%l
if index >= 4*l:
f_idx = s_idx
else:
f_idx = random.randrange(l)
# if f_idx == 0:
# f_idx = s_idx
# else:
# f_idx = random.randrange(l)
if f_idx == s_idx:
same = torch.ones(1)
else:
same = torch.zeros(1)
f_img = Image.open(self.files[f_idx])
s_img = Image.open(self.files[s_idx])
f_img = f_img.convert('RGB')
s_img = s_img.convert('RGB')
if self.transform is not None:
f_img = self.transform(f_img)
s_img = self.transform(s_img)
return f_img, s_img, same
def __len__(self):
return len(self.files) * 5
class AEI_Val_Dataset(Dataset):
def __init__(self, root, transform=None):
super(AEI_Val_Dataset, self).__init__()
self.root = root
self.files = [
os.path.join(path, filename)
for path, dirs, files in os.walk(root)
for filename in files
if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".jpeg")
]
self.transfrom = transform
def __getitem__(self, index):
l = len(self.files)
f_idx = index // l
s_idx = index % l
if f_idx == s_idx:
same = torch.ones(1)
else:
same = torch.zeros(1)
f_img = Image.open(self.files[f_idx])
s_img = Image.open(self.files[s_idx])
f_img = f_img.convert('RGB')
s_img = s_img.convert('RGB')
if self.transfrom is not None:
f_img = self.transfrom(f_img)
s_img = self.transfrom(s_img)
return f_img, s_img, same
def __len__(self):
return len(self.files) * len(self.files)