Last active
May 11, 2022 00:02
-
-
Save MiaoDX/ecd445ad631136602739451e44250073 to your computer and use it in GitHub Desktop.
PMSNet stereo disparity 3 pixel error at test phase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
KITTI stereo disparity 3 pixel error | |
REFERENCES: | |
* [PMSNet code](https://github.com/JiaRenChang/PSMNet/blob/b6520ff9b77168e3dfe3485f8bf6a2a798361d85/finetune.py) | |
* [type converter](https://github.com/traveller59/torchplus/blob/master/tools.py) | |
MATLAB code: | |
``` | |
% disp_error.m | |
function d_err = disp_error (D_gt,D_est,tau) | |
E = abs(D_gt-D_est); | |
n_err = length(find(D_gt>0 & E>tau(1) & E./abs(D_gt)>tau(2))); | |
n_total = length(find(D_gt>0)); | |
d_err = n_err/n_total; | |
%main.m | |
% error threshold | |
tau = [3 0.05]; | |
% stereo demo | |
disp('Load and show disparity map ... '); | |
D_est = disp_read('data/disp_est.png'); | |
D_gt = disp_read('data/disp_gt.png'); | |
d_err = disp_error(D_gt,D_est,tau); | |
``` | |
So, we should also include the `equal` of both comparison | |
FrameWork: PyTorch | |
""" | |
import torch | |
import numpy as np | |
# [type converter](https://github.com/traveller59/torchplus/blob/master/tools.py) | |
def np_dtype_to_torch(dtype): | |
type_map = { | |
np.dtype(np.float16): torch.HalfTensor, | |
np.dtype(np.float32): torch.FloatTensor, | |
np.dtype(np.float64): torch.DoubleTensor, | |
np.dtype(np.int32): torch.IntTensor, | |
np.dtype(np.int64): torch.LongTensor, | |
np.dtype(np.uint8): torch.ByteTensor, | |
} | |
return type_map[dtype] | |
def to_tensor(arg): | |
if isinstance(arg, np.ndarray): | |
return torch.from_numpy(arg).type(np_dtype_to_torch(arg.dtype)) | |
elif isinstance(arg, (list, tuple)): | |
arg = np.array(arg) | |
return torch.from_numpy(arg).type(np_dtype_to_torch(arg.dtype)) | |
else: | |
raise ValueError("unsupported arg type.") | |
def calc_3pe(disp_pred, disp_true, original=False): | |
assert disp_pred.shape == disp_true.shape | |
assert disp_pred.dim() == 3 | |
disp_pred = disp_pred.clone().type(torch.FloatTensor) | |
disp_true = disp_true.clone().type(torch.FloatTensor) | |
if original: | |
true_disp = disp_true | |
else: | |
true_disp = disp_true.clone() | |
index = np.argwhere(true_disp > 0) | |
disp_true[index[0][:], index[1][:], index[2][:]] = np.abs( | |
true_disp[index[0][:], index[1][:], index[2][:]] - | |
disp_pred[index[0][:], index[1][:], index[2][:]]) | |
correct = (disp_true[index[0][:], index[1][:], index[2][:]] <= | |
3) | (disp_true[index[0][:], index[1][:], index[2][:]] <= | |
true_disp[index[0][:], index[1][:], index[2][:]] * 0.05) | |
return 1 - (float(torch.sum(correct)) / float(len(index[0]))) | |
def calc_3pe_standalone(disp_src, disp_dst): | |
assert disp_src.shape == disp_dst.shape, "{}, {}".format( | |
disp_src.shape, disp_dst.shape) | |
assert len(disp_src.shape) == 2 # (N*M) | |
not_empty = (disp_src > 0) & (~np.isnan(disp_src)) & (disp_dst > 0) & ( | |
~np.isnan(disp_dst)) | |
disp_src_flatten = disp_src[not_empty].flatten().astype(np.float32) | |
disp_dst_flatten = disp_dst[not_empty].flatten().astype(np.float32) | |
disp_diff_l = abs(disp_src_flatten - disp_dst_flatten) | |
accept_3p = (disp_diff_l <= 3) | (disp_diff_l <= disp_dst_flatten * 0.05) | |
err_3p = 1 - np.count_nonzero(accept_3p) / len(disp_diff_l) | |
return err_3p | |
if __name__ == '__main__': | |
USE_ORIGINAL = False | |
np.random.seed(42) | |
# bz, h, w = (1, 2, 5) | |
bz, h, w = (20, 20, 50) | |
# est_disp = np.random.randint(low=10, high=200, size= (bz, h, w), dtype=np.uint8) | |
# gt_disp = np.random.randint(low=10, high=200, size= (bz, h, w), dtype=np.uint8) | |
est_disp = np.random.randint(low=100, high=2000, size=(bz, h, w)) / 10.0 | |
gt_disp = np.random.randint(low=100, high=2000, size=(bz, h, w)) / 10.0 | |
def err_two_ndarr(arr1, arr2): | |
tensor1 = to_tensor(arr1) | |
tensor2 = to_tensor(arr2) | |
# print(tensor1.shape) | |
e = calc_3pe(tensor1, tensor2, original=USE_ORIGINAL) | |
return e | |
def split_arr(arr, step): | |
l = [] | |
for i in range(0, arr.shape[0], step): | |
l.append(arr[i:i + step]) | |
return l | |
def clc_split_step(step=1): | |
est_l = split_arr(est_disp, step=step) | |
gt_l = split_arr(gt_disp, step=step) | |
err = 0 | |
for est, gt in zip(est_l, gt_l): | |
err_tmp = err_two_ndarr(est, gt) | |
# print('{:.4f}'.format(err_tmp)) | |
err += err_tmp | |
return err / len(est_l) | |
step_l = [1, 2, 3, 4, 6] | |
print("ORIGINAL:{}".format(USE_ORIGINAL)) | |
for step in step_l: | |
e = clc_split_step(step=step) | |
print("step:{}, err:{:.6f}".format(step, e)) | |
err = 0 | |
for i in range(len(est_disp)): | |
est = est_disp[i] | |
gt = gt_disp[i] | |
err_3p = calc_3pe_standalone(disp_src=est, disp_dst=gt) # gt as dst | |
# print('{:.4f}'.format(err_3p)) | |
err += err_3p | |
print("STANDALONE: {:.6f}".format(err / len(est_disp))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, the analysis is helpful, but not that correct, please refer the correct one.