-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
325 lines (269 loc) · 12.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import os
import time
import cv2
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from transform import get_transfrom, test_transfrom
from utils import train_val_test_index
class MetalDataset(Dataset):
def __init__(self, mode='train', transform = None, val_split = 0.3, test_spilt = 0.1,
image_size = 256, seed = 42, cluster_img=False, combine=False):
assert mode in ['train', 'test', 'val']
super().__init__()
assert os.getcwd() in ['/home/rico-li/Job/Metal', '/home/aiuser/Job/MetalClassification'], 'in the wrong working directory'
if os.getcwd() == '/home/rico-li/Job/Metal':
print('In local machine.')
else:
print('In server.')
path = os.getcwd()+'/Image'
class_names = os.listdir(path)
class_names.sort()
label = []
image_path = []
img_names = []
image_path_merge = []
np.random.seed(seed) # fix the train val test set.
for i, class_name in enumerate(class_names):
image_names = os.listdir(f'{path}/{class_name}')
img_names += image_names
image_path += [f'{path}/{class_name}/{image_name}' for image_name in image_names]
if cluster_img == False:
if combine == True:
if i == 13:
label += [11] * len(image_names) # TODO: class 13 are re-label to class 11
elif i == 14:
label += [13] * len(image_names) # TODO: class 14 are re-label to class 13
else:
label += [i] * len(image_names)
else:
label += [i] * len(image_names)
self.index_list = train_val_test_index(label, mode, val_split, test_spilt)
self.label = [label[i] for i in self.index_list]
self.data_path = [image_path[i] for i in self.index_list]
self.image_names = [img_names[i] for i in self.index_list]
else:
# cluster label: for training data only
assert mode == 'train', 'for training data only'
# ----- notice here -----
# the file is created with train_val_test_index distribution, no need to use
# [label[i] for i in self.index_list] anymore!
# their images name is in oneDfea_imgname__1113merge.txt
# ----- notice here -----
label = torch.load('oneDfea_train_label36')
label = label.type(torch.LongTensor).tolist()
self.label = label
imgnamefile = open('oneDfea_imgname_1113merge.txt')
imgName = imgnamefile.read()
imgName = imgName.splitlines() # list
self.image_names = imgName
name_length = len(class_name)
if class_name == 'TTA':
image_path_merge += [f'{path}/{class_name}/{image_name}' for image_name in imgName if class_name == image_name[:name_length]]
image_path_merge += [f'{path}/TTP/{image_name}' for image_name in imgName if 'TTP' == image_name[:name_length]]
elif class_name == 'TTP':
pass
else:
image_path_merge += [f'{path}/{class_name}/{image_name}' for image_name in imgName if class_name == image_name[:name_length]]
self.data_path = image_path_merge
self.mode = mode
self.transform = transform
self.image_size = image_size
def __len__(self):
return len(self.data_path)
def __getitem__(self, idx):
label = torch.tensor(self.label[idx])
path_i = self.data_path[idx]
image = cv2.imread(path_i, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_name = self.image_names[idx]
if self.transform:
image = get_transfrom(image, crop_size=self.image_size) # tensor
else:
image = test_transfrom(image, size=self.image_size) # tensor
return image, label, image_name
class BinaryDataset(Dataset):
def __init__(self, mode='train', transform = None, val_split = 0.3, test_spilt = 0.1,
image_size = 256, seed = 42, task='1113others'):
assert mode in ['train', 'test', 'val']
assert task in ['1113others', '1113'], 'no such task'
super().__init__()
path = os.getcwd()+'/Image'
class_names = os.listdir(path)
class_names.sort()
if task == '1113':
print('Dealing with 1113 task now')
class_names = [name for name in class_names if (name == 'TTA') or (name == 'TTP')]
else:
print('Dealing with 1113others task now')
pass
label = []
image_path = []
img_names = []
np.random.seed(seed) # fix the train val test set.
for i, class_name in enumerate(class_names):
image_names = os.listdir(f'{path}/{class_name}')
img_names += image_names
image_path += [f'{path}/{class_name}/{image_name}' for image_name in image_names]
if task == '1113others':
if (class_name == 'TTA') or (class_name == 'TTP'):
label += [0] * len(image_names)
else:
label += [1] * len(image_names)
else:
if class_name == 'TTA':
label += [0] * len(image_names)
else: # TTP class
label += [1] * len(image_names)
self.index_list = train_val_test_index(label, mode, val_split, test_spilt)
self.label = [label[i] for i in self.index_list]
self.data_path = [image_path[i] for i in self.index_list]
self.image_names = [img_names[i] for i in self.index_list]
self.mode = mode
self.transform = transform
self.image_size = image_size
def __len__(self):
return len(self.data_path)
def classWeight(self):
class0 = [i for i in self.label if i == 0] # for 1113 task:'TTA', for 1113others:'TTA' and 'TTP'
class1 = [i for i in self.label if i == 1] # for 1113 task:'TTP', for 1113others: others
p_class0 = len(class0)/self.__len__()
p_class1 = len(class1)/self.__len__()
return 1/p_class0, 1/p_class1
def __getitem__(self, idx):
label = torch.tensor(self.label[idx])
path_i = self.data_path[idx]
image = cv2.imread(path_i, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_name = self.image_names[idx]
if self.transform:
image = get_transfrom(image, crop_size=self.image_size)
else:
image = test_transfrom(image, size=self.image_size)
return image, label, image_name
class EncoderDataset(Dataset):
def __init__(self, mode, val_en_spilt = 0.2, val_an_spilt = 0.25, image_size = 256, seed = 42):
super().__init__()
assert mode in ['training', 'val_en', 'threshold','val_an']
self.image_size = image_size
self.mode = mode
self.seed = seed
path = os.getcwd()+'/Image'
class_names = os.listdir(path)
class_names.sort()
label = []
image_path = []
img_names = []
# for label 10
label_10 = []
image_path_10 = []
img_names_10 = []
for i, class_name in enumerate(class_names):
if class_name != 'TPI':
image_names = os.listdir(f'{path}/{class_name}')
img_names += image_names
image_path += [f'{path}/{class_name}/{image_name}' for image_name in image_names]
label += [i] * len(image_names)
else:
image_names_10 = os.listdir(f'{path}/{class_name}')
img_names_10 += image_names_10
image_path_10 += [f'{path}/{class_name}/{image_name}' for image_name in image_names_10]
label_10 += [i] * len(image_names_10)
if mode in ['training','val_en']:
self.index_list = self.setSpilt(label, val_en_spilt, mode)
self.data_path = [image_path[i] for i in self.index_list]
self.label = [label[i] for i in self.index_list]
self.image_names = [img_names[i] for i in self.index_list]
elif mode in ['threshold','val_an']:
self.index_list_10 = self.setSpilt(label_10, val_an_spilt, mode)
self.data_path_10 = [image_path_10[i] for i in self.index_list_10]
self.label_10 = [label_10[i] for i in self.index_list_10]
self.image_names_10 = [img_names_10[i] for i in self.index_list_10]
def __len__(self):
if self.mode in ['training','val_en']:
return len(self.image_names)
elif self.mode in ['threshold','val_an']:
return len(self.image_names_10)
def __getitem__(self, idx):
if self.mode in ['training','val_en']:
label = torch.tensor(self.label[idx])
path_i = self.data_path[idx]
image = cv2.imread(path_i, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_name = self.image_names[idx]
image = test_transfrom(image, size=self.image_size)
return image, label, image_name
elif self.mode in ['threshold','val_an']:
label = torch.tensor(self.label_10[idx])
path_i = self.data_path_10[idx]
image = cv2.imread(path_i, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_name = self.image_names_10[idx]
image = test_transfrom(image, size=self.image_size)
return image, label, image_name
def setSpilt(self, label, spilt, whichindex):
label = np.array(label)
index_list_uni = np.unique(label)
train_index = []
val_index = []
np.random.seed(self.seed)
for i in index_list_uni:
index = np.where(label == i)
class_index = np.random.permutation(index[0]).tolist()
train_index += class_index[:round(len(class_index)*(1-spilt))]
val_index += class_index[round(len(class_index)*(1-spilt)): ]
if whichindex in ['training', 'threshold']:
return train_index
elif whichindex in ['val_en','val_an']:
return val_index
if __name__ == '__main__':
import matplotlib.pyplot as plt
# train_dataset = MetalDataset(mode='test', transform=True, cluster_img=False, image_size=256, val_split=0.3, test_spilt=0.1, seed=42)
# train_dataloader = DataLoader(train_dataset, pin_memory=True, num_workers=os.cpu_count(), batch_size=16, shuffle=True)
# labels = torch.Tensor().type(torch.long)
# for i, (image, label, image_name) in enumerate(train_dataloader):
# labels = torch.cat((labels, label))
# print(label)
# print(f"{i}-batch")
# labels = labels.numpy()
# plt.hist(labels, bins=100, alpha=0.75)
# plt.show()
# from matplotlib import pyplot as plt
# from torchvision.utils import make_grid
# from torch.utils.tensorboard import SummaryWriter
# writer = SummaryWriter(f'runs/test_10')
# dataset = EncoderDataset(mode='training')
# dataloader = DataLoader(dataset, batch_size=6, shuffle=False)
# start_time = time.time()
# for image, label, image_name in dataloader:
# image = image[:6, ...] # (B, C, H, W)
# mean =[0.3835, 0.3737, 0.3698]
# std= [1.0265, 1.0440, 1.0499]
# for i in range(3):
# image[:,i,...] = 255 - ((image[:,i,...] * std[i] + mean[i])*255)
# image = image.type(torch.int8)
# image = make_grid(image, nrow=3) # (C, H, W)
# img_np = image.numpy().transpose(1,2,0) # (H, W, C)
# plt.imshow(img_np)
# plt.show()
# writer.add_image("Image", image)
# break
# # origi_img = input[:n,...].clone().detach() #(n, C, H, W)
# # decor_img = model(origi_img) #(n, C, H, W)
# # img = torch.cat((origi_img, decor_img), dim=0) #(n, C, H, W)
# # img = make_grid(img, nrow=n)
# # writer.add_image(f"Original-Up, decor-Down in epoch: {epoch+1}", img, dataformats='CHW')
# writer.close()
train_dataset = BinaryDataset(mode='train', transform=True, image_size=256,
val_split=0.3, test_spilt=0.1, seed=42, task='1113')
print(train_dataset.classWeight())
# train_dataloader = DataLoader(train_dataset, pin_memory=True, num_workers=os.cpu_count(), batch_size=16, shuffle=True)
# labels = torch.Tensor().type(torch.long)
# for i, (image, label, image_name) in enumerate(train_dataloader):
# labels = torch.cat((labels, label))
# print(label)
# print(f"{i}-batch")
# labels = labels.numpy()
# plt.hist(labels, bins=100, alpha=0.75)
# plt.show()