Upload docs/en/INFERENCE.md with huggingface_hub
Browse files- docs/en/INFERENCE.md +121 -0
docs/en/INFERENCE.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Inference Guide
|
| 2 |
+
|
| 3 |
+
## Specification
|
| 4 |
+
|
| 5 |
+
| Item | Value |
|
| 6 |
+
|---|---|
|
| 7 |
+
| Input name | `input` |
|
| 8 |
+
| Input shape | `[batch, 3, 64, 160]` |
|
| 9 |
+
| Input type | float32, range [0,1] (RGB) |
|
| 10 |
+
| Output name | `logits` |
|
| 11 |
+
| Output shape | `[batch, 4, 10]` |
|
| 12 |
+
| Decode | argmax per position → digit |
|
| 13 |
+
| ONNX opset | 18 |
|
| 14 |
+
|
| 15 |
+
Preprocessing is only three steps: RGB → resize to 160x64 (bilinear) → divide by 255.
|
| 16 |
+
No grayscale, no denoising, no mean/std normalization.
|
| 17 |
+
|
| 18 |
+
## Python
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
pip install onnxruntime pillow numpy
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
import numpy as np
|
| 26 |
+
import onnxruntime as ort
|
| 27 |
+
from PIL import Image
|
| 28 |
+
|
| 29 |
+
_SESSION = None
|
| 30 |
+
|
| 31 |
+
def get_session(model_path="model.onnx"):
|
| 32 |
+
global _SESSION
|
| 33 |
+
if _SESSION is None:
|
| 34 |
+
_SESSION = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
|
| 35 |
+
return _SESSION
|
| 36 |
+
|
| 37 |
+
def solve_captcha(img_bytes_or_path, model_path="model.onnx"):
|
| 38 |
+
sess = get_session(model_path)
|
| 39 |
+
im = Image.open(img_bytes_or_path).convert("RGB").resize((160, 64), Image.BILINEAR)
|
| 40 |
+
x = np.asarray(im, dtype=np.float32) / 255.0
|
| 41 |
+
x = x.transpose(2, 0, 1)[None] # (1,3,64,160)
|
| 42 |
+
logits = sess.run(None, {"input": x})[0] # (1,4,10)
|
| 43 |
+
return "".join(str(int(logits[0, p].argmax())) for p in range(4))
|
| 44 |
+
|
| 45 |
+
code = solve_captcha("captcha.png") # "4821"
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Go (onnxruntime_go)
|
| 49 |
+
|
| 50 |
+
Deps: `github.com/yalue/onnxruntime_go` + the onnxruntime shared library (.dylib/.so).
|
| 51 |
+
|
| 52 |
+
```go
|
| 53 |
+
package main
|
| 54 |
+
|
| 55 |
+
import (
|
| 56 |
+
"bytes"
|
| 57 |
+
"image"
|
| 58 |
+
_ "image/png"
|
| 59 |
+
|
| 60 |
+
"golang.org/x/image/draw"
|
| 61 |
+
ort "github.com/yalue/onnxruntime_go"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
const (
|
| 65 |
+
inW, inH = 160, 64
|
| 66 |
+
nDigits = 4
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
type Solver struct {
|
| 70 |
+
sess *ort.Session[float32]
|
| 71 |
+
in *ort.Tensor[float32]
|
| 72 |
+
out *ort.Tensor[float32]
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
func NewSolver(modelPath, libPath string) (*Solver, error) {
|
| 76 |
+
if libPath != "" { ort.SetSharedLibraryPath(libPath) }
|
| 77 |
+
if err := ort.InitializeEnvironment(); err != nil { return nil, err }
|
| 78 |
+
in, err := ort.NewEmptyTensor[float32](ort.NewShape(1, 3, inH, inW))
|
| 79 |
+
if err != nil { return nil, err }
|
| 80 |
+
out, err := ort.NewEmptyTensor[float32](ort.NewShape(1, nDigits, 10))
|
| 81 |
+
if err != nil { in.Destroy(); return nil, err }
|
| 82 |
+
sess, err := ort.NewSession[float32](modelPath,
|
| 83 |
+
[]string{"input"}, []string{"logits"},
|
| 84 |
+
[]*ort.Tensor[float32]{in}, []*ort.Tensor[float32]{out})
|
| 85 |
+
if err != nil { in.Destroy(); out.Destroy(); return nil, err }
|
| 86 |
+
return &Solver{sess, in, out}, nil
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
func (s *Solver) Solve(png []byte) (string, error) {
|
| 90 |
+
src, _, err := image.Decode(bytes.NewReader(png))
|
| 91 |
+
if err != nil { return "", err }
|
| 92 |
+
dst := image.NewRGBA(image.Rect(0, 0, inW, inH))
|
| 93 |
+
draw.ApproxBiLinear.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil)
|
| 94 |
+
|
| 95 |
+
data := s.in.GetData()
|
| 96 |
+
idx := 0
|
| 97 |
+
// NCHW layout: all R, then all G, then all B
|
| 98 |
+
for y := 0; y < inH; y++ { for x := 0; x < inW; x++ { r,_,_,_ := dst.At(x,y).RGBA(); data[idx]=float32(r>>8)/255; idx++ } }
|
| 99 |
+
for y := 0; y < inH; y++ { for x := 0; x < inW; x++ { _,g,_,_ := dst.At(x,y).RGBA(); data[idx]=float32(g>>8)/255; idx++ } }
|
| 100 |
+
for y := 0; y < inH; y++ { for x := 0; x < inW; x++ { _,_,b,_ := dst.At(x,y).RGBA(); data[idx]=float32(b>>8)/255; idx++ } }
|
| 101 |
+
|
| 102 |
+
if err := s.sess.Run(); err != nil { return "", err }
|
| 103 |
+
got := s.out.GetData()
|
| 104 |
+
code := make([]byte, nDigits)
|
| 105 |
+
for p := 0; p < nDigits; p++ {
|
| 106 |
+
best := 0
|
| 107 |
+
for c := 1; c < 10; c++ { if got[p*10+c] > got[p*10+best] { best = c } }
|
| 108 |
+
code[p] = '0' + byte(best)
|
| 109 |
+
}
|
| 110 |
+
return string(code), nil
|
| 111 |
+
}
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
> Note: `onnxruntime_go.NewSession` reads the model from a file. If you embed it with
|
| 115 |
+
> `go:embed`, use `NewSessionWithONNXData` with the bytes instead.
|
| 116 |
+
|
| 117 |
+
## Performance
|
| 118 |
+
|
| 119 |
+
- Single inference: ~5-15ms (CPU, M1/modern x86)
|
| 120 |
+
- Full flow (load + preprocess + infer): ~10-30ms
|
| 121 |
+
- No GPU needed, memory < 50MB
|