-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
187 lines (153 loc) · 6.26 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import torch
import cv2
import torch.utils.data
from torch.utils.data import Dataset
"""ProtegO dataset"""
def up_dataset(up_path):
up = cv2.imread(up_path)
up = cv2.resize(up, (100, 32))
up = cv2.cvtColor(up, cv2.COLOR_BGR2RGB)
up = torch.FloatTensor(up)
up = up / 255 # normalization to [0,1]
up = up.permute(2,0,1) # [C, H, W]
return up
class train_dataset_builder(Dataset):
def __init__(self, height, width, img_path):
'''
height: input height to model
width: input width to model
total_img_path: path with all images
seq_len: sequence length
'''
self.height = height
self.width = width
self.img_path = img_path
self.dataset = []
img = []
for i,j,k in os.walk(self.img_path):
for file in k:
file_name = os.path.join(i ,file)
img.append(file_name)
self.total_img_name = img
for img_name in self.total_img_name:
_, label, _ = img_name.split('_')
self.dataset.append([img_name, label])
def __getitem__(self, index):
img_name, label = self.dataset[index]
IMG = cv2.imread(img_name)
IMG = cv2.resize(IMG, (self.width, self.height)) # resize
# binarization processing
gray = cv2.cvtColor(IMG, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
IMG = cv2.cvtColor(binary, cv2.COLOR_GRAY2RGB)
IMG = torch.FloatTensor(IMG) # [H, W, C]
IMG = IMG / 255 # normalization to [0,1]
IMG = IMG.permute(2,0,1) # [C, H, W]
return IMG, label
def __len__(self):
return len(self.dataset)
class test_dataset_builder(Dataset):
def __init__(self, height, width, img_path):
self.height = height
self.width = width
self.img_path = img_path
self.dataset = []
img = []
for i,j,k in os.walk(self.img_path):
for file in k:
file_name = os.path.join(i ,file)
img.append(file_name)
self.total_img_name = img
for img_name in self.total_img_name:
img_index, label, img_adv = img_name.split('_')
img_adv = img_adv.split('.')
index_or_advlogo = img_adv[0]
self.dataset.append([img_name, label, img_index, index_or_advlogo])
self.dataset = sorted(self.dataset)
def __getitem__(self, index):
img_name, label, img_index, index_or_advlogo = self.dataset[index]
IMG = cv2.imread(img_name)
ORG = cv2.resize(IMG, (self.width, self.height))
IMG = cv2.cvtColor(ORG, cv2.COLOR_BGR2RGB)
IMG = torch.FloatTensor(IMG) # convert to tensor [H, W, C]
IMG = IMG / 255
IMG = IMG.permute(2,0,1) # [C, H, W]
gray = cv2.cvtColor(ORG, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
mask = cv2.cvtColor(binary, cv2.COLOR_GRAY2RGB)
mask = torch.FloatTensor(mask) # [H, W, C]
mask = mask / 255 # normalization to [0,1]
mask = mask.permute(2,0,1) # [C, H, W]
return IMG, label, img_index, index_or_advlogo, img_name, mask
def __len__(self):
return len(self.dataset)
class test_adv_dataset(Dataset):
def __init__(self, height, width, img_path):
self.height = height
self.width = width
self.img_path = img_path
self.dataset = []
img = []
for i,j,k in os.walk(self.img_path):
for file in k:
file_name = os.path.join(i ,file)
img.append(file_name)
self.total_img_name = img
for img_name in self.total_img_name:
img_index, label, img_adv = img_name.split('_')
img_adv = img_adv.split('.')
index_or_advlogo = img_adv[0]
self.dataset.append([img_name, label, img_index, index_or_advlogo])
self.dataset = sorted(self.dataset)
def __getitem__(self, index):
img_name, label, img_index, index_or_advlogo = self.dataset[index]
IMG = cv2.imread(img_name)
IMG = cv2.resize(IMG, (self.width, self.height))
# binarization processing
gray = cv2.cvtColor(IMG, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
img_b = cv2.cvtColor(binary, cv2.COLOR_GRAY2RGB)
img_b = torch.FloatTensor(img_b)
img_b = img_b / 255 # normalization to [0,1]
img_b = img_b.permute(2,0,1) # [C, H, W]
img = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)
img = torch.FloatTensor(img)
img = img /255 # normalization to [0,1]
img = img.permute(2,0,1) # [C, H, W]
return img_b, img, label, img_index, index_or_advlogo, img_name
def __len__(self):
return len(self.dataset)
"""STR models dataset"""
class strdataset(Dataset):
def __init__(self, height, width, total_img_path):
'''
height: input height to model
width: input width to model
total_img_path: path with all images
seq_len: sequence length
'''
self.total_img_path = total_img_path
self.height = height
self.width = width
img = []
self.dataset = []
for i,_,k in os.walk(total_img_path):
for file in k:
file_name = os.path.join(i ,file)
img.append(file_name)
self.total_img_name = img
for img_name in self.total_img_name:
_, label, _ = img_name.split('_')
self.dataset.append([img_name, label])
def __getitem__(self, index):
img_name, label = self.dataset[index]
IMG = cv2.imread(img_name)
IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)
IMG = cv2.resize(IMG, (self.width, self.height)) # resize
IMG = (IMG - 127.5)/127.5 # normalization to [-1,1]
IMG = torch.FloatTensor(IMG) # convert to tensor [H, W, C]
IMG = IMG.permute(2,0,1) # [C, H, W]
return IMG, label
def __len__(self):
return len(self.dataset)