Upload 22 files
Browse files- .gitattributes +20 -0
- examples/left/000051_11.png +3 -0
- examples/left/000058_11.png +3 -0
- examples/left/000059_10.png +3 -0
- examples/left/000121_10.png +3 -0
- examples/left/000164_10.png +3 -0
- examples/left/000167_11.png +3 -0
- examples/left/000172_11.png +3 -0
- examples/left/000179_10.png +3 -0
- examples/left/000193_10.png +3 -0
- examples/left/000195_10.png +3 -0
- examples/right/000051_11.png +3 -0
- examples/right/000058_11.png +3 -0
- examples/right/000059_10.png +3 -0
- examples/right/000121_10.png +3 -0
- examples/right/000164_10.png +3 -0
- examples/right/000167_11.png +3 -0
- examples/right/000172_11.png +3 -0
- examples/right/000179_10.png +3 -0
- examples/right/000193_10.png +3 -0
- examples/right/000195_10.png +3 -0
- infer.py +125 -0
- infer_onnx.py +100 -0
.gitattributes
CHANGED
|
@@ -38,3 +38,23 @@ ax630c/raft_steoro256x640_r1_npu2.axmodel filter=lfs diff=lfs merge=lfs -text
|
|
| 38 |
ax630c/raft_steoro256x640_r4_npu2.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 39 |
ax650/raft_steoro256x640_r1.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 40 |
ax650/raft_steoro384x1280_r4.axmodel filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
ax630c/raft_steoro256x640_r4_npu2.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 39 |
ax650/raft_steoro256x640_r1.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 40 |
ax650/raft_steoro384x1280_r4.axmodel filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/left/000051_11.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
examples/left/000058_11.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
examples/left/000059_10.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
examples/left/000121_10.png filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
examples/left/000164_10.png filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
examples/left/000167_11.png filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
examples/left/000172_11.png filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
examples/left/000179_10.png filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
examples/left/000193_10.png filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
examples/left/000195_10.png filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
examples/right/000051_11.png filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
examples/right/000058_11.png filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
examples/right/000059_10.png filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
examples/right/000121_10.png filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
examples/right/000164_10.png filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
examples/right/000167_11.png filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
examples/right/000172_11.png filter=lfs diff=lfs merge=lfs -text
|
| 58 |
+
examples/right/000179_10.png filter=lfs diff=lfs merge=lfs -text
|
| 59 |
+
examples/right/000193_10.png filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
examples/right/000195_10.png filter=lfs diff=lfs merge=lfs -text
|
examples/left/000051_11.png
ADDED
|
Git LFS Details
|
examples/left/000058_11.png
ADDED
|
Git LFS Details
|
examples/left/000059_10.png
ADDED
|
Git LFS Details
|
examples/left/000121_10.png
ADDED
|
Git LFS Details
|
examples/left/000164_10.png
ADDED
|
Git LFS Details
|
examples/left/000167_11.png
ADDED
|
Git LFS Details
|
examples/left/000172_11.png
ADDED
|
Git LFS Details
|
examples/left/000179_10.png
ADDED
|
Git LFS Details
|
examples/left/000193_10.png
ADDED
|
Git LFS Details
|
examples/left/000195_10.png
ADDED
|
Git LFS Details
|
examples/right/000051_11.png
ADDED
|
Git LFS Details
|
examples/right/000058_11.png
ADDED
|
Git LFS Details
|
examples/right/000059_10.png
ADDED
|
Git LFS Details
|
examples/right/000121_10.png
ADDED
|
Git LFS Details
|
examples/right/000164_10.png
ADDED
|
Git LFS Details
|
examples/right/000167_11.png
ADDED
|
Git LFS Details
|
examples/right/000172_11.png
ADDED
|
Git LFS Details
|
examples/right/000179_10.png
ADDED
|
Git LFS Details
|
examples/right/000193_10.png
ADDED
|
Git LFS Details
|
examples/right/000195_10.png
ADDED
|
Git LFS Details
|
infer.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import cv2
|
| 3 |
+
# from PIL import Image
|
| 4 |
+
enable_cv2 = True
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from axengine import InferenceSession
|
| 9 |
+
|
| 10 |
+
def bilinear_resize_numpy(array, new_h, new_w):
|
| 11 |
+
h, w = array.shape
|
| 12 |
+
x_ratio = w / new_w
|
| 13 |
+
y_ratio = h / new_h
|
| 14 |
+
|
| 15 |
+
resized = np.zeros((new_h, new_w), dtype=array.dtype)
|
| 16 |
+
|
| 17 |
+
for i in range(new_h):
|
| 18 |
+
for j in range(new_w):
|
| 19 |
+
x = j * x_ratio
|
| 20 |
+
y = i * y_ratio
|
| 21 |
+
|
| 22 |
+
x_floor = int(x)
|
| 23 |
+
y_floor = int(y)
|
| 24 |
+
x_ceil = min(x_floor + 1, w - 1)
|
| 25 |
+
y_ceil = min(y_floor + 1, h - 1)
|
| 26 |
+
|
| 27 |
+
dx = x - x_floor
|
| 28 |
+
dy = y - y_floor
|
| 29 |
+
|
| 30 |
+
a = array[y_floor, x_floor]
|
| 31 |
+
b = array[y_floor, x_ceil]
|
| 32 |
+
c = array[y_ceil, x_floor]
|
| 33 |
+
d = array[y_ceil, x_ceil]
|
| 34 |
+
|
| 35 |
+
resized[i, j] = a * (1 - dx) * (1 - dy) + b * dx * (1 - dy) + c * (1 - dx) * dy + d * dx * dy
|
| 36 |
+
|
| 37 |
+
return resized
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def resize_disp(disp, target_width, target_height, use_cv2=True):
|
| 41 |
+
if use_cv2:
|
| 42 |
+
disp = cv2.resize(disp, (target_width, target_height))
|
| 43 |
+
else:
|
| 44 |
+
# This implementation is slower than cv2.resize
|
| 45 |
+
disp = bilinear_resize_numpy(disp, target_height, target_width)
|
| 46 |
+
|
| 47 |
+
return disp
|
| 48 |
+
|
| 49 |
+
def load_and_preprocess_image(image_path, target_width, target_height, use_cv2=True):
|
| 50 |
+
|
| 51 |
+
if use_cv2:
|
| 52 |
+
img = cv2.imread(image_path)
|
| 53 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 54 |
+
orig_height, orig_width = img.shape[:2]
|
| 55 |
+
img_resized = cv2.resize(img, (target_width,target_height) )
|
| 56 |
+
img_batch = img_resized[None]
|
| 57 |
+
else:
|
| 58 |
+
img = Image.open(image_path).convert('RGB')
|
| 59 |
+
orig_width, orig_height = img.size
|
| 60 |
+
img_resized = img.resize((target_width, target_height))
|
| 61 |
+
img_array = np.array(img_resized) #
|
| 62 |
+
img_batch = img_array[None] #
|
| 63 |
+
|
| 64 |
+
return img_batch, (orig_height, orig_width)
|
| 65 |
+
|
| 66 |
+
def parse_args() -> argparse.Namespace:
|
| 67 |
+
parser = argparse.ArgumentParser()
|
| 68 |
+
parser.add_argument(
|
| 69 |
+
"--left",
|
| 70 |
+
type=str,
|
| 71 |
+
required=True,
|
| 72 |
+
help="Path to left image.",
|
| 73 |
+
)
|
| 74 |
+
parser.add_argument(
|
| 75 |
+
"--right",
|
| 76 |
+
type=str,
|
| 77 |
+
required=True,
|
| 78 |
+
help="Path to right image.",
|
| 79 |
+
)
|
| 80 |
+
parser.add_argument(
|
| 81 |
+
"--model",
|
| 82 |
+
type=str,
|
| 83 |
+
required=True,
|
| 84 |
+
help="Path to ONNX model.",
|
| 85 |
+
)
|
| 86 |
+
parser.add_argument(
|
| 87 |
+
"--width",
|
| 88 |
+
type=int,
|
| 89 |
+
required=True,
|
| 90 |
+
help="Width of input image.",
|
| 91 |
+
)
|
| 92 |
+
parser.add_argument(
|
| 93 |
+
"--height",
|
| 94 |
+
type=int,
|
| 95 |
+
required=True,
|
| 96 |
+
help="Height of input image.",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
return parser.parse_args()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def infer(left: str, right: str, model: str, width:int, height:int):
|
| 103 |
+
|
| 104 |
+
image_left, (orig_h_left, orig_w_left) = load_and_preprocess_image(left, width, height, use_cv2=enable_cv2)
|
| 105 |
+
image_right, (orig_h_right, orig_w_right) = load_and_preprocess_image(right, width, height, use_cv2=enable_cv2)
|
| 106 |
+
|
| 107 |
+
assert orig_h_left == orig_h_right and orig_w_left == orig_w_right
|
| 108 |
+
|
| 109 |
+
session = InferenceSession.load_from_model(model)
|
| 110 |
+
flow_up = session.run(input_feed={"x1":image_left, "x2":image_right})["output"]
|
| 111 |
+
|
| 112 |
+
# Please use opencv whenever possible
|
| 113 |
+
flow_up = resize_disp(flow_up[0,0], orig_w_left, orig_h_left, use_cv2=enable_cv2)
|
| 114 |
+
|
| 115 |
+
flow_up *= orig_w_left/width
|
| 116 |
+
output = np.abs(flow_up)
|
| 117 |
+
|
| 118 |
+
plt.imsave(f"output-ax.png", output, cmap='jet')
|
| 119 |
+
|
| 120 |
+
return output
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
args = parse_args()
|
| 125 |
+
infer(**vars(args))
|
infer_onnx.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
def parse_args() -> argparse.Namespace:
|
| 8 |
+
parser = argparse.ArgumentParser()
|
| 9 |
+
parser.add_argument(
|
| 10 |
+
"--left",
|
| 11 |
+
type=str,
|
| 12 |
+
required=True,
|
| 13 |
+
help="Path to left image.",
|
| 14 |
+
)
|
| 15 |
+
parser.add_argument(
|
| 16 |
+
"--right",
|
| 17 |
+
type=str,
|
| 18 |
+
required=True,
|
| 19 |
+
help="Path to right image.",
|
| 20 |
+
)
|
| 21 |
+
parser.add_argument(
|
| 22 |
+
"--model",
|
| 23 |
+
type=str,
|
| 24 |
+
required=True,
|
| 25 |
+
help="Path to ONNX model.",
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
return parser.parse_args()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def infer(left: str, right: str, model: str):
|
| 32 |
+
# 自动检测可用的执行提供者,优先使用 CUDA,否则使用 CPU
|
| 33 |
+
available_providers = ort.get_available_providers()
|
| 34 |
+
if "CUDAExecutionProvider" in available_providers:
|
| 35 |
+
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
|
| 36 |
+
print("使用 CUDA 执行提供者")
|
| 37 |
+
else:
|
| 38 |
+
providers = ["CPUExecutionProvider"]
|
| 39 |
+
print("使用 CPU 执行提供者 (CUDA 不可用)")
|
| 40 |
+
|
| 41 |
+
print(f"正在加载模型: {model}")
|
| 42 |
+
session = ort.InferenceSession(
|
| 43 |
+
model, providers=providers
|
| 44 |
+
)
|
| 45 |
+
print("模型加载完成")
|
| 46 |
+
|
| 47 |
+
input_info = []
|
| 48 |
+
input_tensors = session.get_inputs()
|
| 49 |
+
for tensor in input_tensors:
|
| 50 |
+
info = dict(name=tensor.name , type=tensor.type, shape=tensor.shape)
|
| 51 |
+
input_info.append(info)
|
| 52 |
+
|
| 53 |
+
H,W = input_info[0]['shape'][2:4]
|
| 54 |
+
print(f"输入尺寸: {H}x{W}")
|
| 55 |
+
|
| 56 |
+
print(f"正在读取左图: {left}")
|
| 57 |
+
left_raw = cv2.imread(left)
|
| 58 |
+
if left_raw is None:
|
| 59 |
+
raise ValueError(f"无法读取左图: {left}")
|
| 60 |
+
image_left = cv2.cvtColor(left_raw, cv2.COLOR_BGR2RGB)
|
| 61 |
+
orig_h_left, orig_w_left = image_left.shape[:2]
|
| 62 |
+
print(f"左图原始尺寸: {orig_h_left}x{orig_w_left}")
|
| 63 |
+
image_left = cv2.resize(image_left, (W,H) )
|
| 64 |
+
|
| 65 |
+
image_left = image_left.transpose(2,0,1)
|
| 66 |
+
image_left = image_left[None].astype(np.float32)
|
| 67 |
+
|
| 68 |
+
print(f"正在读取右图: {right}")
|
| 69 |
+
right_raw = cv2.imread(right)
|
| 70 |
+
if right_raw is None:
|
| 71 |
+
raise ValueError(f"无法读取右图: {right}")
|
| 72 |
+
image_right = cv2.cvtColor(right_raw, cv2.COLOR_BGR2RGB)
|
| 73 |
+
orig_h_right, orig_w_right = image_right.shape[:2]
|
| 74 |
+
print(f"右图原始尺寸: {orig_h_right}x{orig_w_right}")
|
| 75 |
+
image_right = cv2.resize(image_right, (W,H) )
|
| 76 |
+
|
| 77 |
+
image_right = image_right.transpose(2,0,1)
|
| 78 |
+
image_right = image_right[None].astype(np.float32)
|
| 79 |
+
|
| 80 |
+
assert orig_h_left == orig_h_right and orig_w_left == orig_w_right
|
| 81 |
+
|
| 82 |
+
print("正在进行推理...")
|
| 83 |
+
flow_up = session.run(None, {input_info[0]['name']: image_left, input_info[1]['name']:image_right})[0]
|
| 84 |
+
print("推理完成")
|
| 85 |
+
|
| 86 |
+
flow_up = cv2.resize(flow_up[0,0], (orig_w_left, orig_h_left))
|
| 87 |
+
flow_up *= orig_w_left/W
|
| 88 |
+
|
| 89 |
+
output = np.abs(flow_up)
|
| 90 |
+
|
| 91 |
+
print(f"正在保存结果到 output-onnx.png")
|
| 92 |
+
plt.imsave(f"output-onnx.png", output, cmap='jet')
|
| 93 |
+
print("推理完成,结果已保存到 output-onnx.png")
|
| 94 |
+
|
| 95 |
+
return output
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
args = parse_args()
|
| 100 |
+
infer(**vars(args))
|