Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
How do inference the pidinet onnx:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
"""
|
| 5 |
+
Run the PiDiNet ONNX model on one image and save the fused edge map.
|
| 6 |
+
|
| 7 |
+
Example:
|
| 8 |
+
python test_pidinet_onnx.py \
|
| 9 |
+
--onnx model_PIDINET/pidinet_table5.onnx \
|
| 10 |
+
--image Images/example.jpg \
|
| 11 |
+
--output Results/example_edges.png
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import argparse
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
import numpy as np
|
| 18 |
+
import onnxruntime as ort
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)[:, None, None]
|
| 23 |
+
STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)[:, None, None]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def preprocess(img_path: Path) -> np.ndarray:
|
| 27 |
+
img = Image.open(img_path).convert("RGB")
|
| 28 |
+
arr = np.asarray(img, dtype=np.float32) / 255.0 # HWC in [0,1]
|
| 29 |
+
arr = arr.transpose(2, 0, 1) # CHW
|
| 30 |
+
arr = (arr - MEAN) / STD
|
| 31 |
+
return arr[None, ...] # BCHW
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def postprocess(edge_map: np.ndarray, out_path: Path):
|
| 35 |
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
| 36 |
+
edge_map = np.clip(edge_map, 0.0, 1.0)
|
| 37 |
+
edge_img = (edge_map * 255.0).astype(np.uint8)
|
| 38 |
+
Image.fromarray(edge_img).save(out_path)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def parse_args():
|
| 42 |
+
parser = argparse.ArgumentParser(description="Test PiDiNet ONNX on a single image.")
|
| 43 |
+
parser.add_argument(
|
| 44 |
+
"--onnx",
|
| 45 |
+
type=Path,
|
| 46 |
+
default=Path("model_PIDINET/pidinet_table5.onnx"),
|
| 47 |
+
help="Path to the PiDiNet ONNX file.",
|
| 48 |
+
)
|
| 49 |
+
parser.add_argument(
|
| 50 |
+
"--image",
|
| 51 |
+
type=Path,
|
| 52 |
+
required=True,
|
| 53 |
+
help="Input image path.",
|
| 54 |
+
)
|
| 55 |
+
parser.add_argument(
|
| 56 |
+
"--output",
|
| 57 |
+
type=Path,
|
| 58 |
+
default=Path("Results/pidinet_edges.png"),
|
| 59 |
+
help="Where to save the fused edge map.",
|
| 60 |
+
)
|
| 61 |
+
parser.add_argument(
|
| 62 |
+
"--provider",
|
| 63 |
+
type=str,
|
| 64 |
+
default="CPUExecutionProvider",
|
| 65 |
+
help="ONNX Runtime provider (e.g., CPUExecutionProvider or CUDAExecutionProvider).",
|
| 66 |
+
)
|
| 67 |
+
return parser.parse_args()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
args = parse_args()
|
| 72 |
+
|
| 73 |
+
session = ort.InferenceSession(
|
| 74 |
+
str(args.onnx),
|
| 75 |
+
providers=[args.provider],
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
inp = preprocess(args.image)
|
| 79 |
+
outputs = session.run(None, {"image": inp})
|
| 80 |
+
|
| 81 |
+
fused = np.array(outputs[-1])[0, 0] # fused edge map
|
| 82 |
+
postprocess(fused, args.output)
|
| 83 |
+
|
| 84 |
+
print(f"Saved edge map to {args.output}")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
main()
|
| 89 |
+
|
| 90 |
+
```
|