-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
81 lines (60 loc) · 2.32 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
import pathlib
from torch.utils.data import Dataset
import numpy as np
import os
from PIL import Image
from torchvision import transforms as T
class SketchDataset(Dataset):
def __init__(self, data_dir, train=True, class_list=None, first_k=2000):
super().__init__()
n_classes = len(class_list)
if class_list is None:
class_list = os.listdir(data_dir)
self.img_paths = []
self.labels = []
self.class_list = class_list
for k, classname in enumerate(class_list):
img_dir = os.path.join(data_dir, classname)
n_imgs = len(os.listdir(img_dir))
split = int(n_imgs * 0.9)
order = np.random.permutation(n_imgs)
if train:
for i in order[:first_k]:
img_path = os.path.join(img_dir, str(i) + '.png').replace('\\', '/')
self.img_paths.append(img_path)
self.labels.append(k)
else:
for i in order[split:split + first_k]:
img_path = os.path.join(img_dir, str(i) + '.png').replace('\\', '/')
self.img_paths.append(img_path)
self.labels.append(k)
self.len = len(self.img_paths)
print('Loaded {0} images paths from {1} classes.'.format(self.len, n_classes))
def __len__(self):
return self.len
def __getitem__(self, index):
path = self.img_paths[index]
img = Image.open(path).convert('L')
img = T.ToTensor()(img)
label = self.labels[index]
return img, label, path
class TestDataset(Dataset):
def __init__(self, data_dir):
super().__init__()
self.class_list = os.listdir(data_dir)
self.img_paths = []
self.labels = []
for k, classname in enumerate(self.class_list):
img_dir = os.path.join(data_dir, classname)
self.img_paths.append(img_dir)
self.labels.append(-1)
self.len = len(self.img_paths)
print('Loaded {0} images.'.format(self.len))
def __len__(self):
return self.len
def __getitem__(self, index):
path = self.img_paths[index]
img = Image.open(path).convert('L')
img = T.ToTensor()(img)
label = self.labels[index]
return img, label, path