-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
273 lines (220 loc) · 8.16 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
from __future__ import print_function
import numpy as np
import torch
import torch.distributed as dist
import tarfile
import errno
import os
from torch.nn import functional as F
import scipy.io
import cv2 as cv
from sklearn.model_selection import train_test_split
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def softmax_cross_entropy_criterion(logit, truth, is_average=True):
loss = F.cross_entropy(logit, truth, reduce=is_average)
return loss
def softmax(X, theta=1.0, axis=None):
"""
Compute the softmax of each element along an axis of X.
Parameters
----------
X: ND-Array. Probably should be floats.
theta (optional): float parameter, used as a multiplier
prior to exponentiation. Default = 1.0
axis (optional): axis to compute values along. Default is the
first non-singleton axis.
Returns an array the same size as X. The result will sum to 1
along the specified axis.
"""
# make X at least 2d
y = np.atleast_2d(X)
# find axis
if axis is None:
axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)
# multiply y against the theta parameter,
y = y * float(theta)
# subtract the max for numerical stability
y = y - np.expand_dims(np.max(y, axis=axis), axis)
# exponentiate y
y = np.exp(y)
# take the sum along the specified axis
ax_sum = np.expand_dims(np.sum(y, axis=axis), axis)
# finally: divide elementwise
p = y / ax_sum
# flatten if X was 1D
if len(X.shape) == 1: p = p.flatten()
return p
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def mkdir(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def setup_for_distributed(is_master):
"""
This function disables printing when not in master process
"""
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop('force', False)
if is_master or force:
builtin_print(*args, **kwargs)
__builtin__.print = print
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def init_distributed_mode(args):
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
args.rank = int(os.environ["RANK"])
args.world_size = int(os.environ['WORLD_SIZE'])
args.gpu = int(os.environ['LOCAL_RANK'])
elif 'SLURM_PROCID' in os.environ:
args.rank = int(os.environ['SLURM_PROCID'])
args.gpu = args.rank % torch.cuda.device_count()
elif hasattr(args, "rank"):
pass
else:
print('Not using distributed mode')
args.distributed = False
return
args.distributed = True
torch.cuda.set_device(args.gpu)
args.dist_backend = 'nccl'
print('| distributed init (rank {}): {}'.format(
args.rank, args.dist_url), flush=True)
torch.distributed.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
setup_for_distributed(args.rank == 0)
###################### DATA PROCESSING PART #################################
def exact_file(args ):
#utils.mkdir()
print('Extracting car_devkit.tgz...')
if not os.path.exists('devkit'):
with tarfile.open(args.download_path+'/car_devkit.tgz', "r:gz") as tar:
tar.extractall(args.data_dir)
print('Extracting cars_train.tgz...')
if not os.path.exists('cars_train'):
with tarfile.open(args.download_path+'/cars_train.tgz', "r:gz") as tar:
tar.extractall(args.data_dir)
print('Extracting cars_test.tgz...')
if not os.path.exists('cars_test'):
with tarfile.open(args.download_path+'/cars_test.tgz', "r:gz") as tar:
tar.extractall(args.data_dir)
def split_train_valid_set(args):
print("Processing train data...")
cars_annos = scipy.io.loadmat(args.data_dir+'/devkit/cars_train_annos.mat')
annotations = cars_annos['annotations']
annotations = np.transpose(annotations)
fnames = []
class_ids = []
bboxes = []
labels = []
for annotation in annotations:
bbox_x1 = annotation[0][0][0][0]
bbox_y1 = annotation[0][1][0][0]
bbox_x2 = annotation[0][2][0][0]
bbox_y2 = annotation[0][3][0][0]
class_id = annotation[0][4][0][0]
labels.append('%04d' % (class_id,))
fname = annotation[0][5][0]
bboxes.append((bbox_x1, bbox_y1, bbox_x2, bbox_y2))
class_ids.append(class_id)
fnames.append(fname)
labels_count = np.unique(class_ids).shape[0]
print(np.unique(class_ids))
print('The number of different cars is %d' % labels_count)
index=range(len(fnames))
index_train, index_test,fnames_train, fnames_test, label_train, label_test = train_test_split(index,fnames, labels,
stratify=labels,
test_size=0.1)
src_folder =args.data_dir+'/cars_train'
for i in range(len(fnames)):
fname = fnames[i]
label = labels[i]
(x1, y1, x2, y2) = bboxes[i]
src_path = os.path.join(src_folder, fname)
src_image = cv.imread(src_path)
height, width = src_image.shape[:2]
# margins of 16 pixels
margin = 16
x1 = max(0, x1 - margin)
y1 = max(0, y1 - margin)
x2 = min(x2 + margin, width)
y2 = min(y2 + margin, height)
if i in index_train:
dst_folder = args.data_dir + '/train/'
else:
dst_folder = args.data_dir + '/valid/'
dst_path = os.path.join(dst_folder, label)
if not os.path.exists(dst_path):
os.makedirs(dst_path)
dst_path = os.path.join(dst_path, fname)
crop_image = src_image[y1:y2, x1:x2]
cv.imwrite(dst_path, crop_image)
def process_test_data(args):
print("Processing test data...")
cars_annos = scipy.io.loadmat(args.data_dir+'/devkit/cars_test_annos.mat')
annotations = cars_annos['annotations']
annotations = np.transpose(annotations)
fnames = []
bboxes = []
for annotation in annotations:
bbox_x1 = annotation[0][0][0][0]
bbox_y1 = annotation[0][1][0][0]
bbox_x2 = annotation[0][2][0][0]
bbox_y2 = annotation[0][3][0][0]
fname = annotation[0][4][0]
bboxes.append((bbox_x1, bbox_y1, bbox_x2, bbox_y2))
fnames.append(fname)
src_folder =args.data_dir+'/cars_test'
dst_folder = args.data_dir + '/test/'
num_samples = len(fnames)
if not os.path.exists(dst_folder):
os.makedirs(dst_folder)
for i in range(num_samples):
fname = fnames[i]
(x1, y1, x2, y2) = bboxes[i]
src_path = os.path.join(src_folder, fname)
src_image = cv.imread(src_path)
height, width = src_image.shape[:2]
# margins of 16 pixels
margin = 16
x1 = max(0, x1 - margin)
y1 = max(0, y1 - margin)
x2 = min(x2 + margin, width)
y2 = min(y2 + margin, height)
# print(fname)
dst_path = os.path.join(dst_folder, fname)
crop_image = src_image[y1:y2, x1:x2]
#dst_img = cv.resize(src=crop_image, dsize=(img_height, img_width))
cv.imwrite(dst_path, crop_image)