text
stringlengths
1
93.6k
def find_class_threshold(f, dataset, iters, test_funcs, network, splits=10, beg=0.0, end=0.3):
for li_idx in range(iters):
avg_errors = []
threshold = list(np.arange(beg, end, (end - beg) / splits))
log(f, 'threshold:'+str(threshold))
for class_threshold in threshold:
avg_error = test_lsccnn(test_funcs, dataset, 'test_valid', network, True, thresh=class_threshold)
avg_errors.append(avg_error[0]['new_mae'])
log(f, "class threshold: %f, avg_error: %f" % (class_threshold, avg_error[0]['new_mae']))
mid = np.asarray(avg_errors).argmin()
beg = threshold[max(mid - 2, 0)]
end = threshold[min(mid + 2, splits - 1)]
log(f, "Best threshold: %f" % threshold[mid])
optimal_threshold = threshold[mid]
return optimal_threshold
'''
This function performs box NMS on the predictions of the net.
Parameters
----------
predictions: multiscale predictions - list of numpy maps
each map is of size 4 x H x W
Returns
----------
nms_out: Binary map of where the prediction person is
box_out: Size of the box at the predicted dot
NOTE: count(nms_out) == count(box_out)
'''
def box_NMS(predictions, thresh):
Scores = []
Boxes = []
for k in range(len(BOXES)):
scores = np.max(predictions[k], axis=0)
boxes = np.argmax(predictions[k], axis=0)
# index the boxes with BOXES to get h_map and w_map (both are the same for us)
mask = (boxes<3) # removing Z
boxes = (boxes+1) * mask
scores = (scores * mask) # + 100 # added 100 since we take logsoftmax and it's negative!!
boxes = (boxes==1)*BOXES[k][0] + (boxes==2)*BOXES[k][1] + (boxes==3)*BOXES[k][2]
Scores.append(scores)
Boxes.append(boxes)
x, y, h, w, scores = apply_nms.apply_nms(Scores, Boxes, Boxes, 0.5, thresh=thresh)
nms_out = np.zeros((predictions[0].shape[1], predictions[0].shape[2])) # since predictions[0] is of size 4 x H x W
box_out = np.zeros((predictions[0].shape[1], predictions[0].shape[2])) # since predictions[0] is of size 4 x H x W
for (xx, yy, hh) in zip(x, y, h):
nms_out[yy, xx] = 1
box_out[yy, xx] = hh
assert(np.count_nonzero(nms_out) == len(x))
return nms_out, box_out
"""
A function to return dotmaps and box maps of either gt
or predictions. In case of predictions, it would be NMSed
output and in case of gt maps, it would be would be from each
individual scale.
Parameters
----------
pred: list of ndarray (currently MUST be of length 3
- each for one scale)
Returns
----------
nms_out: dot map of NMSed output of the given predictions.
h: box map of NMSed output
"""
def get_box_and_dot_maps(pred, thresh):
assert(len(pred) == 4)
all_dot_maps = []
all_box_maps = []
# NMS on the multi-scale outputs
nms_out, h = box_NMS(pred, thresh)
return nms_out, h
'''
Main training code for LSC-CNN.
Parameters
-----------
network : (torch model) network. In this case len(network) == 1
dataset: (class object) data_reader class object
network_function: (class) network_functions() class object to get test and train
functions.
log_path: (str) path to log losses and stats.
Returns
----------
This method does not return anything. It directly logs all the losses,
metrics and statistics of training/validation/testing stages.
'''
def train_networks(network, dataset, network_functions, log_path):