text
stringlengths
1
93.6k
avg_precision: (float) average precision
avd_recall: (float) avg_recall
'''
def get_offset_error(x_pred, y_pred, x_true, y_true, output_downscale, max_dist=16):
if max_dist is None:
max_dist = 16
n = len(x_true)
m = len(x_pred)
if m == 0 or n == 0:
return 0
x_true *= output_downscale
y_true *= output_downscale
x_pred *= output_downscale
y_pred *= output_downscale
dx = np.expand_dims(x_true, 1) - x_pred
dy = np.expand_dims(y_true, 1) - y_pred
d = np.sqrt(dx ** 2 + dy ** 2)
assert d.shape == (n, m)
sorted_idx = np.asarray(np.unravel_index(np.argsort(d.ravel()), d.shape))
# Need to divide by n for average error
hit_thresholds = np.arange(12, -1, -1)
off_err, num_hits, fn = offset_sum(sorted_idx, d, n, m, max_dist, hit_thresholds, len(hit_thresholds))
off_err /= n
precisions = np.asarray(num_hits, dtype='float32') / m
recall = np.asarray(num_hits, dtype='float32') / ( np.asarray(num_hits, dtype='float32') + np.asarray(fn, dtype='float32'))
avg_precision = precisions.mean()
avg_recall = recall.mean()
return off_err, avg_precision, avg_recall
'''
Draws bounding box on predictions of LSC-CNN
Parameters
----------
image: (ndarray:HXWX3) input image
h_map: (HXW) map denoting height of the box
w_map: (HXW) map denoting width of the box
gt_pred_map: (HXW) binary map denoting points of prediction
prediction_downscale: (int) scale in which LSC-CNN predicts.
thickness: (int) thickness of bounding box
multi_colours: (bool) If True, plots different colours for different scales
Returns
----------
boxed_img: image with bounding boxes plotted
'''
def get_boxed_img(image, h_map, w_map, gt_pred_map, prediction_downscale, thickness=1, multi_colours=False):
if multi_colours:
colours = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)] # colours for [1/8, 1/4, 1/2] scales
if image.shape[2] != 3:
boxed_img = image.astype(np.uint8).transpose((1, 2, 0)).copy()
else:
boxed_img = image.astype(np.uint8).copy()
head_idx = np.where(gt_pred_map > 0)
H, W = boxed_img.shape[:2]
Y, X = head_idx[-2] , head_idx[-1]
for y, x in zip(Y, X):
h, w = h_map[y, x]*prediction_downscale, w_map[y, x]*prediction_downscale
if multi_colours:
selected_colour = colours[(BOX_SIZE_BINS.index(h // prediction_downscale)) // 3]
else:
selected_colour = (0, 255, 0)
if h//2 in BOXES[3] or h//2 in BOXES[2]:
t = 1
else:
t = thickness
cv2.rectangle(boxed_img, (max(int(prediction_downscale * x - w / 2), 0), max(int(prediction_downscale * y - h / 2), 0)),
(min(int(prediction_downscale * x + w - w / 2), W), min(int(prediction_downscale * y + h - h / 2), H)), selected_colour, t)
return boxed_img.transpose((2, 0, 1))
'''
Testing function for LSC-CNN.
Parameters
-----------
test_funcs: (python function) function to test the images
(returns 4 channel output [b_1, b_2, b_3, z] for gt and prediction)
dataset: (Object) DataReader Object
set_name: (string) sets the name for dataset to test on - either test or train
print_output: (bool) Dumps gt and predictions if True
Returns
----------
metrics_test: (dict) Dictionary of metrics
txt: (string) metrics in string format to log
'''
def test_lsccnn(test_funcs, dataset, set_name, network, print_output=False, thresh=0.2):
test_functions = []
global test_loss
global counter
test_loss = 0.
counter = 0.
metrics_test = {}
metrics_ = ['new_mae', 'mle', 'mse', 'loss1']
for k in metrics_:
metrics_test[k] = 0.0