See the README for prerequisites and instructions to build and run the service.
If you use mise, the repository pins Go and exposes the common development tasks:
mise install
mise run ci # formatting, vet, race-enabled tests, and build
mise run model # download and verify the ONNX modelsGitHub Actions runs mise run ci and validates the Docker image for both
linux/amd64 and linux/arm64 on pull requests and pushes to main.
Run tests with:
make testThe default suite uses checked-in photographs in JPEG, PNG, GIF, and lossy,
lossless, and transparent WebP formats. It checks decoding, all eight EXIF
orientations, normalization, letterbox padding, raw and multipart uploads,
corrupt images, and recovery after analysis failures. Fixtures and their
source licenses live in the fixture gallery.
Additional natural photographs cover a dog low in a portrait and two puppies in
grass. A licensed panda eating bamboo is also included as a regression for a
previously reported failure with a similar image. FocalNet letterbox and
centroid math is covered without ONNX Runtime; a contract-compatible dummy
ONNX is exercised in the integration suite and is never packaged as production
weights. Candidate generation, letterbox
content, and the retention-gated ranking path are checked against FocalNet
Python goldens.
make model-focalnet downloads FocalNet weights for local runs. They are not
required for the default tests.
To also run the real YuNet and U²-Net models:
export ONNXRUNTIME_LIB=/absolute/path/to/libonnxruntime.dylib
make test-integrationThis downloads and verifies the models, then runs race-enabled tests including
face and subject-location checks, mirrored-image consistency, and equivalent
raw and multipart results. The integration suite requires a working runtime and
models; it fails rather than silently skipping when they are missing. GitHub
Actions installs the pinned runtime and runs this suite on every pull request
and push to main. Default tests need neither the runtime nor network access.
Integration tests follow MODEL_PRECISION (default int8) and honor explicit
MODEL_PATH overrides. make test-integration-fp32 tests the FP32 environment
switch; CI runs both modes. The INT8 binary is intentionally versioned in
models/ and checksum-verified, so release builds do not need the experimental
SSH host or the calibration dataset. See models/README.md for provenance.
The gallery also includes three difficult natural scenes with manually annotated
subject regions. Run make evaluate to check a person in a room, a pedestrian
with a dog, and a bird above tree foliage. Full U²-Net passes the person-in-room
case, but the other two still fail. This quality evaluation is separate from the
CI regression gate; its expected regions are not adjusted to accept incorrect
model predictions.
To measure preprocessing time and Go allocations without loading ONNX Runtime:
go test ./internal/imageutil -run '^$' -bench BenchmarkPrepare -benchmem -count=3BenchmarkPrepareInto measures the reusable input-buffer path used by the HTTP
handler. BenchmarkPrepare includes input-buffer allocation. The already-sized
case isolates normalization from resizing. These are preprocessing measurements,
not end-to-end inference throughput.
The benchmark uses the included landscape JPEG and portrait PNG. It measures image decoding, orientation handling, resize and normalization, ONNX inference, and focal-point calculation. Model startup is excluded.
export ONNXRUNTIME_LIB=/absolute/path/to/libonnxruntime.dylib
mise run benchUse .so instead of .dylib on Linux. Regenerate the synthetic example
images with go generate ./internal/benchmark.
To reproduce the README benchmark results, verify the model and collect five samples of at least three seconds per image:
make model
go test -run '^$' -bench '^BenchmarkAnalyze$' -benchmem -benchtime=3s -count=5 ./internal/benchmarkSet ONNXRUNTIME_LIB before running either benchmark command; without it, the
benchmark is skipped. Report the median ns/op across the five samples,
converted to milliseconds, together with the hardware, OS, Go version, and
ONNX Runtime version. The fixtures are synthetic, and the benchmark runs
analyses sequentially. File reads, HTTP handling, uploads, and model startup
are excluded.
To tune production throughput, benchmark MAX_CONCURRENT_ANALYSES values such
as 1, 2, 4, and 8 under an HTTP workload while recording requests/second,
latency percentiles, and peak memory. Test ONNX_INTRA_OP_THREADS alongside it:
the default of 1 is intended for concurrent requests, while a higher value may
reduce single-request latency when more CPU cores are available.
cmd/autogravity/ HTTP server and lifecycle
internal/facedetection/ YuNet preprocessing, inference, and face selection
internal/focalnet/ FocalNet human-ranking inference and crop selection
internal/imageutil/ decoding, EXIF orientation, resize, normalization
internal/ortenv/ shared ONNX Runtime lifecycle
internal/saliency/ ONNX Runtime model session and inference
internal/gravity/ saliency-weighted focal-point calculation
models/ checked-in and downloaded model artifacts and licenses