-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
142 lines (115 loc) · 4.11 KB
/
dataset.py
File metadata and controls
142 lines (115 loc) · 4.11 KB
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
import os
import sys
import numpy
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
import conf
from utils import image_preprocess
def get_imagefolder_train_loader():
train_dir = './data/ImageFolder/train'
normalize = transforms.Normalize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
train_dataset = datasets.ImageFolder(
train_dir,
transforms.Compose([
transforms.RandomResizedCrop(conf.IMAGE_SIZE),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size = conf.TRAINING_BATCH_SIZE,
shuffle = True,
num_workers = conf.NUM_WORKERS,
pin_memory = True
)
return train_loader
def get_imagefoler_val_loader():
val_dir = './data/ImageFolder/test'
normalize = transforms.Normalize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
val_dataset = datasets.ImageFolder(
val_dir,
transforms.Compose([
transforms.Resize(int(conf.IMAGE_SIZE / 0.875)),
transforms.CenterCrop(conf.IMAGE_SIZE),
transforms.ToTensor(),
normalize,
])
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size = conf.VAL_BATCH_SIZE,
shuffle = False,
num_workers = conf.NUM_WORKERS,
pin_memory = True
)
return val_loader
class MyDataset(Dataset):
def __init__(self, filename, image_dir, resize_height = 256, resize_width = 256, repeat = 1):
self.image_label_list = self.read_file(filename)
self.image_dir = image_dir
self.len = len(self.image_label_list)
self.repeat = repeat
self.resize_height = resize_height
self.resize_width = resize_width
self.toTensor = transforms.ToTensor()
def __getitem__(self, i):
index = i % self.len
image_name, label = self.image_label_list[index]
image_path = os.path.join(self.image_dir, image_name)
img = self.load_data(image_path, self.resize_height, self.resize_width, normalization = False)
img = self.data_preproccess(img)
label = numpy.array(label)
return img, label.squeeze()
def __len__(self):
if self.repeat == None:
data_len = 10000000
else:
data_len = len(self.image_label_list) * self.repeat
return data_len
def read_file(self, filename):
image_label_list = []
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
content = line.rstrip().split(' ')
name = content[0]
labels = []
for value in content[1:]:
labels.append(int(value))
image_label_list.append((name, labels))
return image_label_list
def load_data(self, path, resize_height, resize_width, normalization):
image = image_preprocess(path, resize_height, resize_width, normalization)
return image
def data_preproccess(self, data):
data = self.toTensor(data)
return data
def get_custom_train_loader():
data_dir = './data/Custom/train/images/'
txt_dir = './data/Custom/train/metadata.txt'
my_dataset = MyDataset(txt_dir, data_dir)
train_loader = torch.utils.data.DataLoader(
my_dataset,
batch_size = conf.TRAINING_BATCH_SIZE,
shuffle = True,
num_workers = conf.NUM_WORKERS
)
return train_loader
def get_custom_val_loader():
data_dir = './data/Custom/test/images/'
txt_dir = './data/Custom/test/metadata.txt'
my_dataset = MyDataset(txt_dir, data_dir)
val_loader = torch.utils.data.DataLoader(
my_dataset,
batch_size = conf.VAL_BATCH_SIZE,
shuffle = False,
num_workers = conf.NUM_WORKERS
)
return val_loader