nathansut1 commited on
Commit
1b5db69
·
verified ·
1 Parent(s): dbbc096

Upload sample_workflow.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. sample_workflow.py +60 -0
sample_workflow.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Minimal example: binarize a document image using the SBB ONNX model.
3
+
4
+ pip install onnxruntime-gpu numpy Pillow
5
+ python3 sample_workflow.py input.jpg output.tif
6
+ """
7
+
8
+ import sys
9
+ import numpy as np
10
+ from PIL import Image
11
+ import onnxruntime as ort
12
+
13
+ MODEL = "model_convtranspose.onnx"
14
+ PATCH = 448
15
+
16
+ # Load model
17
+ sess = ort.InferenceSession(MODEL, providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
18
+
19
+ # Load image
20
+ img = np.array(Image.open(sys.argv[1]).convert("RGB"))
21
+ h, w = img.shape[:2]
22
+
23
+ # Extract 448x448 patches (the model requires fixed-size input)
24
+ patches, positions = [], []
25
+ for y in range(0, h, PATCH):
26
+ for x in range(0, w, PATCH):
27
+ patch = np.zeros((PATCH, PATCH, 3), dtype=np.uint8)
28
+ ph, pw = min(PATCH, h - y), min(PATCH, w - x)
29
+ patch[:ph, :pw] = img[y:y+ph, x:x+pw]
30
+ patches.append(patch)
31
+ positions.append((x, y))
32
+
33
+ # Normalize (matches original TF model's float64->float32 rounding)
34
+ lut = np.array([np.float32(np.float64(i) / 255.0) for i in range(256)], dtype=np.float32)
35
+ patches_float = lut[np.array(patches).astype(np.int32)]
36
+
37
+ # Run inference in batches
38
+ outputs = []
39
+ for i in range(0, len(patches), 64):
40
+ batch = patches_float[i:i+64]
41
+ out = sess.run(["activation_55"], {"input_1": batch})[0]
42
+ outputs.append(out)
43
+ output = np.concatenate(outputs)
44
+
45
+ # Threshold and reconstruct
46
+ result = np.zeros((h, w), dtype=np.float32)
47
+ weight = np.zeros((h, w), dtype=np.float32)
48
+ for i, (x, y) in enumerate(positions):
49
+ prob = output[i, :, :, 1]
50
+ binary = np.where((prob * 255).astype(np.uint8) <= 128, 255.0, 0.0)
51
+ ah, aw = min(PATCH, h - y), min(PATCH, w - x)
52
+ result[y:y+ah, x:x+aw] += binary[:ah, :aw]
53
+ weight[y:y+ah, x:x+aw] += 1.0
54
+ result = (result / np.maximum(weight, 1)).astype(np.uint8)
55
+
56
+ # Save
57
+ Image.fromarray(result, "L").convert("1").save(
58
+ sys.argv[2], format="TIFF", compression="group4", dpi=(300, 300)
59
+ )
60
+ print(f"Saved {sys.argv[2]}")