-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
312 lines (249 loc) · 8.4 KB
/
utils.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
import cv2
import torch
import numpy as np
from PIL import Image
from PIL import ImageFile
from tqdm import tqdm
import csv
import glob
import os
import time
from multiprocessing import Pool
# import smote_variants as sv
ImageFile.LOAD_TRUNCATED_IMAGES = True
weather_classes = ['雨凇', '雾凇', '雾霾', '霜', '露', '结冰', '降雨', '降雪', '冰雹']
# 1 2 3 4 5 6 7 8 9
def classes_num(filename):
labels = [0]*9
with open(filename) as f:
f_csv = csv.reader(f)
f_csv.__next__()
for row in f_csv:
labels[int(row[1])-1] += 1
return labels
def image_info(dir):
min_w, min_h = 1000, 1000
max_w, max_h = 0, 0
w, h = [], []
ex = []
for img_path in glob.glob(dir+'/*'):
try:
img = Image.open(img_path)
if img.size[0] < min_w:
min_w = img.size[0]
elif img.size[0] > max_w:
max_w = img.size[0]
if img.size[1] < min_h:
min_h = img.size[1]
elif img.size[1] > max_h:
max_h = img.size[1]
w.append(img.size[0])
h.append(img.size[1])
except(OSError, NameError):
ex.append(img_path)
img = cv2.imread(img_path)
print(img.shape)
print("max_w:{}, max_h:{}".format(max_w, max_h))
print("min_w:{}, min_h:{}".format(min_w, min_h))
print('len:{}, mean_w:{}, mean_h:{}'.format(len(w), np.mean(w), np.mean(h)))
print(ex)
def read_test_data(fdir):
images = []
im_names =[]
i=0
for img_path in tqdm(glob.glob(fdir+'/*')):
im = load_image(os.path.join(fdir, img_path))
im_names.append(img_path.split('/')[-1])
images.append(im)
i+=1
if i>10000:
break
return images, im_names
def read_non_ice_snow_data(fdir, filename):
images = []
labels = []
with open(filename) as f:
f_csv = csv.reader(f)
f_csv.__next__()
print('loading image...')
i=0
for row in tqdm(f_csv):
if row[0] == 'cad097b0899f45bcba277adf5344097e.png':
continue
if int(row[1]) in [6, 8]:
continue
elif int(row[1]) in [7]:
labels.append(5)
elif int(row[1]) in [9]:
labels.append(6)
# labels.append(int(row[1]) - 3)
else:
labels.append(int(row[1])-1)
images.append(os.path.join(fdir, row[0]))
i+=1
if i>7000:
break
return images, labels
def read_ice_snow_data(fdir, filename):
images = []
labels = []
# names = []
# path = os.path.join(fdir, filename)
with open(filename) as f:
f_csv = csv.reader(f)
f_csv.__next__()
print('loading image...')
i=0
for row in tqdm(f_csv):
if row[0] == 'cad097b0899f45bcba277adf5344097e.png':
continue
if int(row[1]) not in [6, 8]:
continue
# img = load_image(os.path.join(fdir, row[0]))
images.append(os.path.join(fdir, row[0]))
labels.append(int(int(row[1])-1==5))
# names.append(row[0])
i+=1
if i>7000:
break
return images, labels
def read_data(fdir, filename, train_less=False, clean_data=False):
images = []
labels = []
# names = []
# path = os.path.join(fdir, filename)
need_cleans = []
if clean_data:
with open('./err.csv', 'r') as f:
f_csv = csv.reader(f)
for row in tqdm(f_csv):
need_cleans.append(row[0])
with open(filename) as f:
f_csv = csv.reader(f)
f_csv.__next__()
print('loading image...')
i=0
for row in tqdm(f_csv):
if row[0] == 'cad097b0899f45bcba277adf5344097e.png':
continue
if clean_data:
if row[0] in need_cleans:
continue
if train_less:
images.append(os.path.join(fdir, row[0]))
if int(row[1]) in [6, 8]:
labels.append(0)
# elif int(row[1]) in [8, 9]:
# labels.append(int(row[1])-2)
else:
# labels.append(int(row[1])-1)
labels.append(1)
continue
# img = load_image(os.path.join(fdir, row[0]))
images.append(os.path.join(fdir, row[0]))
labels.append(int(row[1])-1)
# names.append(row[0])
i+=1
# if i>7000:
# break
return images, labels
def read_smote_data(fdir, filename, val_num=500):
images = []
labels = []
# path = os.path.join(fdir, filename)
with open(filename) as f:
f_csv = csv.reader(f)
f_csv.__next__()
print('loading image...')
for row in tqdm(f_csv):
if row[0] == 'cad097b0899f45bcba277adf5344097e.png':
continue
images.append(os.path.join(fdir, row[0]))
labels.append(int(row[1])-1)
train_data = images[val_num:], labels[val_num:]
val_data = images[:val_num], labels[:val_num]
return train_data, val_data
# def smote_data(images, labels):
# # images = images[:50]
# # labels = labels[:50]
# shape = np.shape(images)
# nums = shape[0] // 2
# oversampler = sv.MulticlassOversampling(sv.Borderline_SMOTE2(proportion=0.7, n_neighbors=3, k_neighbors=3, n_jobs=12)) # MDO
# X, y = oversampler.sample(np.reshape(images, (len(images), -1)), labels)
# X = X.reshape((len(y), shape[1], shape[2], shape[3])).astype(np.uint8)
# mkdir('new_train')
# with open('new_train_label.csv', 'a', encoding='utf-8') as f:
# f_csv = csv.writer(f)
# for i, x in enumerate(X):
# im = Image.fromarray(x)
# im.save('./new_train/'+str(i)+'.jpg', 'jpeg')
# f_csv.writerow([str(i)+'.jpg', y[i]+1])
#
# print('org: %d -> x: %d' % (len(labels), len(y)))
# ys = [0]*10
# for i in y:
# ys[i+1] += 1
# print(ys)
def load_image(filename):
try:
img = Image.open(filename)
except(OSError, NameError):
print('cv opened image')
cv_img = cv2.imread(filename)
img = Image.fromarray(cv_img)
img = img.convert("RGB")
# print(filename)
return img
def load_image_label(params, resize=600):
try:
img = Image.open(params[0])
except(OSError, NameError):
print('cv opened image')
cv_img = cv2.imread(params[0])
img = Image.fromarray(cv_img)
img = img.convert("RGB")
img = img.resize((resize, resize), Image.ANTIALIAS)
# print(filename)
return np.array(img), params[1]
def to_tensor(data, dtype=torch.float16, device=None):
return torch.as_tensor(data, dtype=dtype, device=device)
def mkdir(path):
# give a path, create the folder
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
def save_image(img, name):
cv2.imwrite(name, img)
def add_weight_decay(net, l2_value, skip_list=()):
decay, no_decay = [], []
for name, param in net.named_parameters():
if not param.requires_grad: continue # frozen weights
if len(param.shape) == 1 or name.endswith(".bias") or name in skip_list: no_decay.append(param)
else: decay.append(param)
return [{'params': no_decay, 'weight_decay': 0.}, {'params': decay, 'weight_decay': l2_value}]
if __name__ == "__main__":
file_dir = "/home/lzw/datasets/air"
filename = "Train_label.csv"
label_file = os.path.join(file_dir, filename)
nums = classes_num(label_file)
for i, cl in enumerate(weather_classes):
print('{}:{}'.format(cl, nums[i]))
print('nums:', np.sum(nums))
image_info(os.path.join(file_dir,'train'))
# train_data, val_data = read_smote_data(
# os.path.join(file_dir,'train'), os.path.join(file_dir,filename), val_num=0)
# images, labels = train_data
# tic = time.time()
# pool = Pool(48)
# img_names = pool.map(load_image_label, list(zip(images, labels)))
# pool.close()
# pool.join()
# toc = time.time()
# imgs = []
# labs = []
# for im, name in img_names:
# imgs.append(im)
# labs.append(name)
# print('load image: ', toc-tic)
# print(imgs[0].shape)
# smote_data(imgs, labs)