ceyxprime commited on
Commit
1f1eb3d
·
verified ·
1 Parent(s): a12c7cd

Upload inference_demo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. inference_demo.py +65 -0
inference_demo.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Minimal demo: batched inference with the fixed det_500m model.
4
+
5
+ Stacks N input images into a single batch and runs one forward pass.
6
+ Prints output shapes and a per-frame summary (count of cls anchors above a
7
+ score threshold per scale). Intended as a sanity check, not a full detector —
8
+ plug your own NMS / anchor decoding for actual face boxes.
9
+ """
10
+ import argparse
11
+ import cv2
12
+ import numpy as np
13
+ import onnxruntime as ort
14
+
15
+ DET_SIZE = (640, 640)
16
+ SCORE_THRESHOLD = 0.5
17
+
18
+ parser = argparse.ArgumentParser()
19
+ parser.add_argument('--model', required=True, help='Path to det_500m_fixed.onnx')
20
+ parser.add_argument('--images', nargs='+', required=True,
21
+ help='One or more image paths (any number — they form the batch)')
22
+ args = parser.parse_args()
23
+
24
+
25
+ def preprocess(path):
26
+ img = cv2.imread(path)
27
+ if img is None:
28
+ raise FileNotFoundError(path)
29
+ blob = cv2.dnn.blobFromImage(
30
+ cv2.resize(img, DET_SIZE),
31
+ 1.0 / 128.0, DET_SIZE,
32
+ (127.5, 127.5, 127.5), swapRB=True,
33
+ )
34
+ return blob[0]
35
+
36
+
37
+ # Build batch
38
+ batch = np.stack([preprocess(p) for p in args.images], axis=0)
39
+ print(f'Input batch: {batch.shape} ({len(args.images)} image(s))')
40
+
41
+ # One forward pass
42
+ sess = ort.InferenceSession(args.model, providers=['CPUExecutionProvider'])
43
+ inp_name = sess.get_inputs()[0].name
44
+ out_names = [o.name for o in sess.get_outputs()]
45
+ outputs = sess.run(None, {inp_name: batch})
46
+
47
+ # Output shapes
48
+ print('\nOutputs:')
49
+ for n, o in zip(out_names, outputs):
50
+ print(f' {n}: {list(o.shape)}')
51
+
52
+ # Per-frame summary using the 3 cls heads (post-Sigmoid, indices 0/1/2)
53
+ strides = [8, 16, 32]
54
+ cls_outputs = outputs[:3]
55
+
56
+ print(f'\nPer-frame anchor counts above score {SCORE_THRESHOLD}:')
57
+ print(f' {"image":<40} {"stride 8":>10} {"stride 16":>10} {"stride 32":>10} {"total":>8}')
58
+ for n in range(batch.shape[0]):
59
+ counts = [int((cls[n] > SCORE_THRESHOLD).sum()) for cls in cls_outputs]
60
+ name = args.images[n]
61
+ if len(name) > 38:
62
+ name = '...' + name[-35:]
63
+ print(f' {name:<40} {counts[0]:>10} {counts[1]:>10} {counts[2]:>10} {sum(counts):>8}')
64
+
65
+ print('\nIf all images are the same, the per-frame counts must match exactly.')