-
Notifications
You must be signed in to change notification settings - Fork 21
/
load_dataset.py
146 lines (103 loc) · 3.81 KB
/
load_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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
PyTorch Dataset Handling. The dataset folder should comprise of two subfolders namely "train" and "test" where both folders has subfolders that named
according to their class names.
'''
import os
import glob
import cv2
import torch
from torch.utils import data
from torch.utils.data import Dataset, dataset
class LoadDataset(Dataset):
'''Loads the dataset from the given path.
'''
def __init__(self, dataset_folder_path, image_size=224, image_depth=3, train=True, transform=None):
'''Parameter Init.
'''
assert not dataset_folder_path is None, "Path to the dataset folder must be provided!"
self.dataset_folder_path = dataset_folder_path
self.transform = transform
self.image_size = image_size
self.image_depth = image_depth
self.train = train
self.classes = sorted(self.get_classnames())
self.image_path_label = self.read_folder()
def get_classnames(self):
'''Returns the name of the classes in the dataset.
'''
return os.listdir(f"{self.dataset_folder_path.rstrip('/')}/train/" )
def read_folder(self):
'''Reads the folder for the images with their corresponding label (foldername).
'''
image_path_label = []
if self.train:
folder_path = f"{self.dataset_folder_path.rstrip('/')}/train/"
else:
folder_path = f"{self.dataset_folder_path.rstrip('/')}/test/"
for x in glob.glob(folder_path + "**", recursive=True):
if not x.endswith('jpg'):
continue
class_idx = self.classes.index(x.split('/')[-2])
image_path_label.append((x, int(class_idx)))
return image_path_label
def __len__(self):
'''Returns the total size of the data.
'''
return len(self.image_path_label)
def __getitem__(self, idx):
'''Returns a single image and its corresponding label.
'''
if torch.is_tensor(idx):
idx = idx.tolist()
image, label = self.image_path_label[idx]
if self.image_depth == 1:
image = cv2.imread(image, 0)
else:
image = cv2.imread(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (self.image_size, self.image_size))
if self.transform:
image = self.transform(image)
return {
'image': image,
'label': label
}
class LoadInputImages(Dataset):
'''Loads the dataset for visualization.
'''
def __init__(self, input_folder, image_size, image_depth, transform=None):
'''Param init.
'''
self.input_folder = input_folder.rstrip('/') + '/'
self.image_size = image_size
self.image_depth = image_depth
self.transform = transform
self.image_paths = self.read_folder()
def read_folder(self):
'''Reads all the image paths in the given folder.
'''
image_paths = []
for x in glob.glob(self.input_folder + '**'):
if not x.endswith('jpg'):
continue
image_paths.append(x)
return image_paths
def __len__(self):
'''Returns the total number of images in the folder.
'''
return len(self.image_paths)
def __getitem__(self, idx):
'''Returns a single image array.
'''
if torch.is_tensor(idx):
idx = idx.tolist()
image = self.image_paths[idx]
if self.image_depth == 1:
image = cv2.imread(image, 0)
else:
image = cv2.imread(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (self.image_size, self.image_size))
if self.transform:
image = self.transform(image)
return image