def eval_graph(sm, detections): time_start = time.time() unary_prob = detections.conf coordinates = detections.coord pairwise_regr = detections.pairwise cidx_list = range(0, sm.num_keypoints) #cidx_list = [0, 1, 2] unary_counts = [] for idx, cidx in enumerate(cidx_list): unary_counts.append(unary_prob[cidx].shape[0]) num_unary = sum(unary_counts) num_pairwise = 0 for idx1, cidx1 in enumerate(cidx_list): for cidx2 in cidx_list[idx1:]: if cidx1 == cidx2: num_pairwise += unary_prob[cidx1].shape[0] * ( unary_prob[cidx1].shape[0] - 1) // 2 else: num_pairwise += unary_prob[cidx1].shape[0] * unary_prob[ cidx2].shape[0] pos_array = np.zeros([num_unary, 2], dtype=np.float64) unary_array = np.zeros([num_unary, 1], dtype=np.float64) pw_array = np.zeros([num_pairwise, 1], dtype=np.float64) pwidx_array = np.zeros([num_pairwise, 2], dtype=np.uint16) firstidx = 0 firstidx_list = [] for idx, cidx in enumerate(cidx_list): lastidx = firstidx + unary_prob[cidx].shape[0] unary_array[firstidx:lastidx] = unary_prob[cidx] pos_array[firstidx:lastidx] = coordinates[cidx] firstidx_list.append(firstidx) firstidx = lastidx firstidx = 0 for idx1, cidx1 in enumerate(cidx_list): for idx2 in range(idx1, len(cidx_list)): if coordinates[cidx1].shape[0] > 0: cidx2 = cidx_list[idx2] if coordinates[cidx2].shape[0] > 0: if not sm.need_this_pairwise(cidx1, cidx2): continue cur_prob, ptidx = sm.eval(cidx1, cidx2, detections) lastidx = firstidx + cur_prob.shape[0] ptidx[:, 0] += firstidx_list[idx1] ptidx[:, 1] += firstidx_list[idx2] pw_array[firstidx:lastidx, 0] = cur_prob pwidx_array[firstidx:lastidx, :] = ptidx firstidx = lastidx is_sparse_graph = True solver_type = False do_suppression = True logit_in_solver = False if unary_array.shape[0] > 0: unary_array_solver = unary_array if logit_in_solver else logit_transform( unary_array) pw_array_solver = pw_array if logit_in_solver else logit_transform( pw_array) time_start = time.time() res = solve_nl_lmp(unary_array_solver, pwidx_array, pw_array_solver, is_sparse_graph, solver_type, do_suppression, logit_in_solver) unLab = np.array(res, dtype=np.uint64) firstidx = 0 for cidx in cidx_list: lastidx = firstidx + unary_prob[cidx].shape[0] unLab[firstidx:lastidx, 0] = cidx firstidx = lastidx else: unLab = np.array([]) return unLab, pos_array, unary_array, pwidx_array, pw_array
def eval_graph(sm, detections): time_start = time.time() unary_prob = detections.conf coordinates = detections.coord pairwise_regr = detections.pairwise cidx_list = range(0, sm.num_keypoints) #cidx_list = [0, 1, 2] unary_counts = [] for idx, cidx in enumerate(cidx_list): unary_counts.append(unary_prob[cidx].shape[0]) num_unary = sum(unary_counts) num_pairwise = 0 for idx1, cidx1 in enumerate(cidx_list): for cidx2 in cidx_list[idx1:]: if cidx1 == cidx2: num_pairwise += unary_prob[cidx1].shape[0]*(unary_prob[cidx1].shape[0] - 1) // 2 else: num_pairwise += unary_prob[cidx1].shape[0]*unary_prob[cidx2].shape[0] pos_array = np.zeros([num_unary, 2], dtype=np.float64) unary_array = np.zeros([num_unary, 1], dtype=np.float64) pw_array = np.zeros([num_pairwise, 1], dtype=np.float64) pwidx_array = np.zeros([num_pairwise, 2], dtype=np.uint16) firstidx = 0 firstidx_list = [] for idx, cidx in enumerate(cidx_list): lastidx = firstidx + unary_prob[cidx].shape[0] unary_array[firstidx:lastidx] = unary_prob[cidx] pos_array[firstidx:lastidx] = coordinates[cidx] firstidx_list.append(firstidx) firstidx = lastidx firstidx = 0 for idx1, cidx1 in enumerate(cidx_list): for idx2 in range(idx1, len(cidx_list)): if coordinates[cidx1].shape[0] > 0: cidx2 = cidx_list[idx2] if coordinates[cidx2].shape[0] > 0: if not sm.need_this_pairwise(cidx1, cidx2): continue cur_prob, ptidx = sm.eval(cidx1, cidx2, detections) lastidx = firstidx + cur_prob.shape[0] ptidx[:, 0] += firstidx_list[idx1] ptidx[:, 1] += firstidx_list[idx2] pw_array[firstidx:lastidx, 0] = cur_prob pwidx_array[firstidx:lastidx, :] = ptidx firstidx = lastidx is_sparse_graph = True solver_type = False do_suppression = True logit_in_solver = False if unary_array.shape[0] > 0: unary_array_solver = unary_array if logit_in_solver else logit_transform(unary_array) pw_array_solver = pw_array if logit_in_solver else logit_transform(pw_array) time_start = time.time() res = solve_nl_lmp(unary_array_solver, pwidx_array, pw_array_solver, is_sparse_graph, solver_type, do_suppression, logit_in_solver) unLab = np.array(res, dtype=np.uint64) firstidx = 0 for cidx in cidx_list: lastidx = firstidx + unary_prob[cidx].shape[0] unLab[firstidx:lastidx, 0] = cidx firstidx = lastidx else: unLab = np.array([]) return unLab, pos_array, unary_array, pwidx_array, pw_array