-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_iou.py
203 lines (126 loc) · 4.95 KB
/
compute_iou.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
import PIL
from torchvision.transforms import RandomResizedCrop
import torchvision.transforms as transforms
import random
import numpy as np
from config import cfg
import cv2
import os
from tqdm import tqdm
"""
This script is used to compute the mean IoU of the localized and random crops
"""
transform = RandomResizedCrop(224)
def LocalizedRandomResizedCrop(
image,
xo, yo, Wo, Ho,
size: tuple,
THR: float,
scale: tuple = (0.08, 1.0),
ratio: tuple = (3. / 4., 4. / 3.),
):
"""
Args :
image (PIL Image): Image to be cropped.
bbox (tuple): Bounding box coordinates (x, y, w, h)
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
should be a multiple of patch_size
alpha (float): 0: centers are equal, 1 : centers are shifted
scale (tuple): Range of the random size of the cropped image compared to
the original image size.
ratio (tuple): Range of aspect ratio of the origin aspect ratio cropped
image compared to the original image ratio. Default value is
(3. / 4., 4. / 3.).
Returns:
PIL Image: Cropped image.
"""
area_image = image.size[0] * image.size[1]
Ao = max(Wo, Ho)**2
scale = (max(scale[0], (THR*Ao)/area_image),
scale[1])
effective_scale = random.uniform(*scale)
log_ratio = tuple(np.log(r) for r in ratio)
effective_ratio = np.exp(random.uniform(*log_ratio))
side = area_image**0.5
crop_side = side * effective_scale**0.5
if effective_ratio > 1:
Wc = effective_ratio * crop_side
Hc = crop_side
else:
Wc = crop_side
Hc = crop_side / effective_ratio
Hc = min(Hc, float(image.size[1]))
Wc = min(Wc, float(image.size[0]))
alpha = 1 - THR**0.5
range_x = alpha * (Wc + Wo)/2
range_y = alpha * (Hc + Ho)/2
effective_x = random.uniform(-range_x, range_x)
effective_y = random.uniform(-range_y, range_y)
center_x = xo + Wo/2 + effective_x
center_y = yo + Ho/2 + effective_y
crop_bbox = [
max(0, center_y - Hc/2),
max(0, center_x - Wc/2),
Hc,
Wc,
]
if crop_bbox[0] + crop_bbox[2] > image.size[1]:
crop_bbox[2] = image.size[1] - crop_bbox[0]
if crop_bbox[1] + crop_bbox[3] > image.size[0]:
crop_bbox[3] = image.size[0] - crop_bbox[1]
crop_bbox = [int(x) for x in crop_bbox]
return crop_bbox
def compute_niou(bbox,gt,):
"""
Args:
y, x, h, w
warning: not the true iou, but intersection over area of the gt
"""
x1, y1, w1, h1 = bbox
x3, y3, w3, h3 = gt
x2 = x1 + w1
y2 = y1 + h1
x4 = x3 + w3
y4 = y3 + h3
# x1,y1,x2,y2,x3,y3,x4,y4
inter_width = min(x2, x4) - max(x1, x3)
inter_height = min(y2, y4) - max(y1, y3)
if inter_width <= 0 or inter_height <= 0:
return 0
areaIntersection = inter_width * inter_height
return areaIntersection / (w3 * h3)
def get_miou(THR):
with open(cfg.dataset.box_file) as f:
temp = f.readlines()
# format i x y w h
boxes = {int(line.split()[0]):list(map(lambda x : int(float(x)), line.split()[1:])) for line in temp}
with open(cfg.dataset.img_file) as f:
temp = f.readlines()
# format i path
images_name = {int(line.split()[0]):line.split()[1] for line in temp}
miou_localized = []
miou_random = []
size = 224
total = len(images_name)
assert list(boxes.keys()) == list(images_name.keys())
for i in range(total):
name = images_name[i+1]
box = boxes[i+1]
img = PIL.Image.open(os.path.join(cfg.dataset.img_dir,name))
#img = transforms.ToPILImage()(img)
localised_box = LocalizedRandomResizedCrop(img, *box, size = (size,size), THR = THR)
random_box = transform.get_params(img, transform.scale, transform.ratio)
box = [box[1], box[0], box[3], box[2]] # x,y,w,h -> y,x,h,w
iou_random = compute_niou(random_box,box)
iou_localised = compute_niou(localised_box,box)
miou_random.append(iou_random)
miou_localized.append(iou_localised)
print(f"{np.mean(miou_localized):.3f} | {np.mean(miou_random):.3f} | {str(i).zfill(len(str(total)))}/{total}", end="\r")
return miou_localized, miou_random
if __name__ == "__main__":
miou_localized, miou_random = get_miou(THR = 0.0955)
print()
print(f"miou_localized : {np.mean(miou_localized):.3f}")
print(f"miou_random : {np.mean(miou_random):.3f}")