-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_ltce.py
475 lines (409 loc) · 17.6 KB
/
utils_ltce.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import numpy as np
import torch.nn.functional as F
import math
from torchvision import transforms
import torch
import cv2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
#matplotlib.use('agg')
MAPS = ['map3','map4']
Scales = [0.9, 1.1]
MIN_HW = 384
MAX_HW = 1584
IM_NORM_MEAN = [0.485, 0.456, 0.406]
IM_NORM_STD = [0.229, 0.224, 0.225]
def select_exemplar_rois(image):
all_rois = []
print("Press 'q' or Esc to quit. Press 'n' and then use mouse drag to draw a new examplar, 'space' to save.")
while True:
key = cv2.waitKey(1) & 0xFF
if key == 27 or key == ord('q'):
break
elif key == ord('n') or key == '\r':
rect = cv2.selectROI("image", image, False, False)
x1 = rect[0]
y1 = rect[1]
x2 = x1 + rect[2] - 1
y2 = y1 + rect[3] - 1
all_rois.append([y1, x1, y2, x2])
for rect in all_rois:
y1, x1, y2, x2 = rect
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
print("Press q or Esc to quit. Press 'n' and then use mouse drag to draw a new examplar")
return all_rois
def matlab_style_gauss2D(shape=(3,3),sigma=0.5):
"""
2D gaussian mask - should give the same result as MATLAB's
fspecial('gaussian',[shape],[sigma])
"""
m,n = [(ss-1.)/2. for ss in shape]
y,x = np.ogrid[-m:m+1,-n:n+1]
h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
sumh = h.sum()
if sumh != 0:
h /= sumh
return h
def PerturbationLoss(output,boxes,sigma=8, use_gpu=False):
Loss = 0.
if boxes.shape[1] > 1:
boxes = boxes.squeeze()
for tempBoxes in boxes.squeeze():
y1 = int(tempBoxes[1])
y2 = int(tempBoxes[3])
x1 = int(tempBoxes[2])
x2 = int(tempBoxes[4])
out = output[:,:,y1:y2,x1:x2]
GaussKernel = matlab_style_gauss2D(shape=(out.shape[2],out.shape[3]),sigma=sigma)
GaussKernel = torch.from_numpy(GaussKernel).float()
if use_gpu: GaussKernel = GaussKernel.cuda()
Loss += F.mse_loss(out.squeeze(),GaussKernel)
else:
boxes = boxes.squeeze()
y1 = int(boxes[1])
y2 = int(boxes[3])
x1 = int(boxes[2])
x2 = int(boxes[4])
out = output[:,:,y1:y2,x1:x2]
Gauss = matlab_style_gauss2D(shape=(out.shape[2],out.shape[3]),sigma=sigma)
GaussKernel = torch.from_numpy(Gauss).float()
if use_gpu: GaussKernel = GaussKernel.cuda()
Loss += F.mse_loss(out.squeeze(),GaussKernel)
return Loss
def MincountLoss(output,boxes, use_gpu=False):
ones = torch.ones(1)
if use_gpu: ones = ones.cuda()
Loss = 0.
if boxes.shape[1] > 1:
boxes = boxes.squeeze()
for tempBoxes in boxes.squeeze():
y1 = int(tempBoxes[1])
y2 = int(tempBoxes[3])
x1 = int(tempBoxes[2])
x2 = int(tempBoxes[4])
X = output[:,:,y1:y2,x1:x2].sum()
if X.item() <= 1:
Loss += F.mse_loss(X,ones)
else:
boxes = boxes.squeeze()
y1 = int(boxes[1])
y2 = int(boxes[3])
x1 = int(boxes[2])
x2 = int(boxes[4])
X = output[:,:,y1:y2,x1:x2].sum()
if X.item() <= 1:
Loss += F.mse_loss(X,ones)
return Loss
def pad_to_size(feat, desire_h, desire_w):
""" zero-padding a four dim feature matrix: N*C*H*W so that the new Height and Width are the desired ones
desire_h and desire_w should be largers than the current height and weight
"""
cur_h = feat.shape[-2]
cur_w = feat.shape[-1]
left_pad = (desire_w - cur_w + 1) // 2
right_pad = (desire_w - cur_w) - left_pad
top_pad = (desire_h - cur_h + 1) // 2
bottom_pad =(desire_h - cur_h) - top_pad
return F.pad(feat, (left_pad, right_pad, top_pad, bottom_pad))
#changed
def extract_features(feature_model, image, boxes,feat_map_keys=['map3','map4'], exemplar_scales=[0.9, 1.1],model_type='vgg16'):
# N =1 -> 1 image for each iter
#M = 3 -> 3 boxes
#
N, M = image.shape[0], boxes.shape[2]
"""
Getting features for the image N * C * H * W
"""
Image_features = feature_model(image)
#print(Image_features)
#print(Image_features['map3'].shape)
#print(Image_features['map4'].shape)
"""
Getting features for the examples (N*M) * C * h * w
"""
for ix in range(0,N):
# boxes = boxes.squeeze(0)
boxes = boxes[ix][0]
cnter = 0
Cnter1 = 0
for keys in feat_map_keys:
image_features = Image_features[keys][ix].unsqueeze(0)
print('Image Map: ', image_features.size)
if model_type=="resnet":
if keys == 'map1' or keys == 'map2':
Scaling = 4.0
elif keys == 'map3':
Scaling = 8.0
elif keys == 'map4':
Scaling = 16.0
else:
Scaling = 32.0
elif model_type=="vgg16":
if keys == 'map1' or keys == 'map2':
Scaling = 16.0
elif keys == 'map3':
Scaling = 32.0
elif keys == 'map4':
Scaling = 64.0
else:
Scaling = 128.0
boxes_scaled = boxes / Scaling
boxes_scaled[:, 1:3] = torch.floor(boxes_scaled[:, 1:3])
boxes_scaled[:, 3:5] = torch.ceil(boxes_scaled[:, 3:5])
boxes_scaled[:, 3:5] = boxes_scaled[:, 3:5] + 1 # make the end indices exclusive
feat_h, feat_w = image_features.shape[-2], image_features.shape[-1]
# make sure exemplars don't go out of bound
boxes_scaled[:, 1:3] = torch.clamp_min(boxes_scaled[:, 1:3], 0)
boxes_scaled[:, 3] = torch.clamp_max(boxes_scaled[:, 3], feat_h)
boxes_scaled[:, 4] = torch.clamp_max(boxes_scaled[:, 4], feat_w)
box_hs = boxes_scaled[:, 3] - boxes_scaled[:, 1]
box_ws = boxes_scaled[:, 4] - boxes_scaled[:, 2]
max_h = math.ceil(max(box_hs))
max_w = math.ceil(max(box_ws))
for j in range(0,M):
y1, x1 = int(boxes_scaled[j,1]), int(boxes_scaled[j,2])
y2, x2 = int(boxes_scaled[j,3]), int(boxes_scaled[j,4])
#print(y1,y2,x1,x2,max_h,max_w)
try:
if j == 0:
examples_features = image_features[:,:,y1:y2, x1:x2]
if examples_features.shape[2] != max_h or examples_features.shape[3] != max_w:
#examples_features = pad_to_size(examples_features, max_h, max_w)
examples_features = F.interpolate(examples_features, size=(max_h,max_w),mode='bilinear')
else:
feat = image_features[:,:,y1:y2, x1:x2]
if feat.shape[2] != max_h or feat.shape[3] != max_w:
feat = F.interpolate(feat, size=(max_h,max_w),mode='bilinear')
#feat = pad_to_size(feat, max_h, max_w)
examples_features = torch.cat((examples_features,feat),dim=0)
except:
continue
"""
Convolving example features over image features
"""
h, w = examples_features.shape[2], examples_features.shape[3]
features = F.conv2d(
F.pad(image_features, ((int(w/2)), int((w-1)/2), int(h/2), int((h-1)/2))),
examples_features
)
combined = features.permute([1,0,2,3])
# computing features for scales 0.9 and 1.1
for scale in exemplar_scales:
h1 = math.ceil(h * scale)
w1 = math.ceil(w * scale)
if h1 < 1: # use original size if scaled size is too small
h1 = h
if w1 < 1:
w1 = w
examples_features_scaled = F.interpolate(examples_features, size=(h1,w1),mode='bilinear')
features_scaled = F.conv2d(F.pad(image_features, ((int(w1/2)), int((w1-1)/2), int(h1/2), int((h1-1)/2))),
examples_features_scaled)
features_scaled = features_scaled.permute([1,0,2,3])
combined = torch.cat((combined,features_scaled),dim=1)
#print("combined shape",combined.shape)
if cnter == 0:
Combined = 1.0 * combined
else:
if Combined.shape[2] != combined.shape[2] or Combined.shape[3] != combined.shape[3]:
combined = F.interpolate(combined, size=(Combined.shape[2],Combined.shape[3]),mode='bilinear')
Combined = torch.cat((Combined,combined),dim=1)
cnter += 1
if ix == 0:
All_feat = 1.0 * Combined.unsqueeze(0)
else:
All_feat = torch.cat((All_feat,Combined.unsqueeze(0)),dim=0)
return All_feat
class resizeImage(object):
"""
If either the width or height of an image exceed a specified value, resize the image so that:
1. The maximum of the new height and new width does not exceed a specified value
2. The new height and new width are divisible by 8
3. The aspect ratio is preserved
No resizing is done if both height and width are smaller than the specified value
By: Minh Hoai Nguyen ([email protected])
"""
def __init__(self, MAX_HW=1504):
self.max_hw = MAX_HW
def __call__(self, sample):
image,lines_boxes = sample['image'], sample['lines_boxes']
W, H = image.size
if W > self.max_hw or H > self.max_hw:
scale_factor = float(self.max_hw)/ max(H, W)
new_H = 8*int(H*scale_factor/8)
new_W = 8*int(W*scale_factor/8)
resized_image = transforms.Resize((new_H, new_W))(image)
else:
scale_factor = 1
resized_image = image
boxes = list()
for box in lines_boxes:
box2 = [int(k*scale_factor) for k in box]
y1, x1, y2, x2 = box2[0], box2[1], box2[2], box2[3]
boxes.append([0, y1,x1,y2,x2])
boxes = torch.Tensor(boxes).unsqueeze(0)
resized_image = Normalize(resized_image)
sample = {'image':resized_image,'boxes':boxes}
return sample
class resizeImageWithGT(object):
"""
If either the width or height of an image exceed a specified value, resize the image so that:
1. The maximum of the new height and new width does not exceed a specified value
2. The new height and new width are divisible by 8
3. The aspect ratio is preserved
No resizing is done if both height and width are smaller than the specified value
By: Minh Hoai Nguyen ([email protected])
Modified by: Viresh
"""
def __init__(self, MAX_HW=1504):
self.max_hw = MAX_HW
def __call__(self, sample):
image,lines_boxes,density = sample['image'], sample['lines_boxes'],sample['gt_density']
W, H = image.size
if W > self.max_hw or H > self.max_hw:
scale_factor = float(self.max_hw)/ max(H, W)
new_H = 8*int(H*scale_factor/8)
new_W = 8*int(W*scale_factor/8)
resized_image = transforms.Resize((new_H, new_W))(image)
resized_density = cv2.resize(density, (new_W, new_H))
orig_count = np.sum(density)
new_count = np.sum(resized_density)
if new_count > 0: resized_density = resized_density * (orig_count / new_count)
else:
scale_factor = 1
resized_image = image
resized_density = density
boxes = list()
for box in lines_boxes:
box2 = [int(k*scale_factor) for k in box]
y1, x1, y2, x2 = box2[0], box2[1], box2[2], box2[3]
boxes.append([0, y1,x1,y2,x2])
boxes = torch.Tensor(boxes).unsqueeze(0)
resized_image = Normalize(resized_image)
resized_density = torch.from_numpy(resized_density).unsqueeze(0).unsqueeze(0)
sample = {'image':resized_image,'boxes':boxes,'gt_density':resized_density}
return sample
Normalize = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=IM_NORM_MEAN, std=IM_NORM_STD)])
Transform = transforms.Compose([resizeImage( MAX_HW)])
TransformTrain = transforms.Compose([resizeImageWithGT(MAX_HW)])
def denormalize(tensor, means=IM_NORM_MEAN, stds=IM_NORM_STD):
"""Reverses the normalisation on a tensor.
Performs a reverse operation on a tensor, so the pixel value range is
between 0 and 1. Useful for when plotting a tensor into an image.
Normalisation: (image - mean) / std
Denormalisation: image * std + mean
Args:
tensor (torch.Tensor, dtype=torch.float32): Normalized image tensor
Shape:
Input: :math:`(N, C, H, W)`
Output: :math:`(N, C, H, W)` (same shape as input)
Return:
torch.Tensor (torch.float32): Demornalised image tensor with pixel
values between [0, 1]
Note:
Symbols used to describe dimensions:
- N: number of images in a batch
- C: number of channels
- H: height of the image
- W: width of the image
"""
denormalized = tensor.clone()
for channel, mean, std in zip(denormalized, means, stds):
channel.mul_(std).add_(mean)
return denormalized
def scale_and_clip(val, scale_factor, min_val, max_val):
"Helper function to scale a value and clip it within range"
new_val = int(round(val*scale_factor))
new_val = max(new_val, min_val)
new_val = min(new_val, max_val)
return new_val
def visualize_output_and_save(input_, output, boxes, save_path, figsize=(20, 12), dots=None):
"""
dots: Nx2 numpy array for the ground truth locations of the dot annotation
if dots is None, this information is not available
"""
# get the total count
pred_cnt = output.sum().item()
boxes = boxes.squeeze(0)
boxes2 = []
for i in range(0, boxes.shape[0]):
y1, x1, y2, x2 = int(boxes[i, 1].item()), int(boxes[i, 2].item()), int(boxes[i, 3].item()), int(
boxes[i, 4].item())
roi_cnt = output[0,0,y1:y2, x1:x2].sum().item()
boxes2.append([y1, x1, y2, x2, roi_cnt])
img1 = format_for_plotting(denormalize(input_))
output = format_for_plotting(output)
fig = plt.figure(figsize=figsize)
# display the input image
ax = fig.add_subplot(2, 2, 1)
ax.set_axis_off()
ax.imshow(img1)
for bbox in boxes2:
y1, x1, y2, x2 = bbox[0], bbox[1], bbox[2], bbox[3]
rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=3, edgecolor='y', facecolor='none')
rect2 = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=1, edgecolor='k', linestyle='--', facecolor='none')
ax.add_patch(rect)
ax.add_patch(rect2)
if dots is not None:
ax.scatter(dots[:, 0], dots[:, 1], c='red', edgecolors='blue')
# ax.scatter(dots[:,0], dots[:,1], c='black', marker='+')
ax.set_title("Input image, gt count: {}".format(dots.shape[0]))
else:
ax.set_title("Input image")
ax = fig.add_subplot(2, 2, 2)
ax.set_axis_off()
ax.set_title("Overlaid result, predicted count: {:.2f}".format(pred_cnt))
img2 = 0.2989*img1[:,:,0] + 0.5870*img1[:,:,1] + 0.1140*img1[:,:,2]
ax.imshow(img2, cmap='gray')
ax.imshow(output, cmap=plt.cm.viridis, alpha=0.5)
# display the density map
ax = fig.add_subplot(2, 2, 3)
ax.set_axis_off()
ax.set_title("Density map, predicted count: {:.2f}".format(pred_cnt))
ax.imshow(output)
# plt.colorbar()
ax = fig.add_subplot(2, 2, 4)
ax.set_axis_off()
ax.set_title("Density map, predicted count: {:.2f}".format(pred_cnt))
ret_fig = ax.imshow(output)
for bbox in boxes2:
y1, x1, y2, x2, roi_cnt = bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]
rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=3, edgecolor='y', facecolor='none')
rect2 = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=1, edgecolor='k', linestyle='--',
facecolor='none')
ax.add_patch(rect)
ax.add_patch(rect2)
ax.text(x1, y1, '{:.2f}'.format(roi_cnt), backgroundcolor='y')
fig.colorbar(ret_fig, ax=ax)
fig.savefig(save_path, bbox_inches="tight")
plt.close()
def format_for_plotting(tensor):
"""Formats the shape of tensor for plotting.
Tensors typically have a shape of :math:`(N, C, H, W)` or :math:`(C, H, W)`
which is not suitable for plotting as images. This function formats an
input tensor :math:`(H, W, C)` for RGB and :math:`(H, W)` for mono-channel
data.
Args:
tensor (torch.Tensor, torch.float32): Image tensor
Shape:
Input: :math:`(N, C, H, W)` or :math:`(C, H, W)`
Output: :math:`(H, W, C)` or :math:`(H, W)`, respectively
Return:
torch.Tensor (torch.float32): Formatted image tensor (detached)
Note:
Symbols used to describe dimensions:
- N: number of images in a batch
- C: number of channels
- H: height of the image
- W: width of the image
"""
has_batch_dimension = len(tensor.shape) == 4
formatted = tensor.clone()
if has_batch_dimension:
formatted = tensor.squeeze(0)
if formatted.shape[0] == 1:
return formatted.squeeze(0).detach()
else:
return formatted.permute(1, 2, 0).detach()