Boundary drawing and site classification
Two machine-learning steps in this package come from Boundary Drawer, a separate inference server (Aerscape/boundary-drawer):
| Step | Server endpoint | What it does |
|---|---|---|
| Boundary drawing | POST /v1/segment | Takes a point and returns the outline of the oil and gas site there, as a polygon (or no_site). |
| Site classification | POST /v1/classify | Takes a point or polygon and returns one of six site types: well_site, small_complex_site, compressor_stations, no_equipment_visible, large_complex_site, processing_facilities. |
Both models run on the same server and share its imagery cache (Esri World Imagery), so classifying sites just after drawing their boundaries reuses the downloaded imagery.
The same server can be reached in two ways:
- The GPU server (production). Boundary Drawer runs on a desktop with an NVIDIA GPU and connects to the EC2 through a reverse SSH tunnel. Production jobs always use this.
- The CPU container (testing and local runs). The same server as a Docker image, running next to our containers. It's slower, about 2 s per boundary and 0.4 s per classification, but needs nothing else running. It is only used when you ask for it.
Quick start
# Normal run: inference goes to the GPU server through the SSH tunnel
scripts/run.sh config/examples/infrastructure/boundary_drawing.yaml --env local
# The same run with the local CPU container instead
scripts/run.sh config/examples/infrastructure/boundary_drawing.yaml --env local --local-inference
# Tests, including the ones that need a real server
scripts/run-tests.sh tests/inference --with-boundary-drawerThe container image is private, so log in to the GitHub container registry once per machine first (see Setting up the container).
How the switch between the two servers works
The code doesn't decide which server to use. Every request goes to the address in the environment variable BOUNDARY_DRAWER_URL, and Docker Compose decides what that address leads to.
In every service, BOUNDARY_DRAWER_URL is unix:/run/boundary-drawer/bd.sock, a Unix socket inside the container. What changes is what is mounted at /run/boundary-drawer:
scripts/run.sh <config> scripts/run.sh <config> --local-inference
(service: cli-tool) (service: cli-tool-local-inference)
/run/boundary-drawer ── bind mount of the host's named volume "bd-socket", shared with the
inside the container ~/.boundary-drawer (read-only) boundary-drawer container
│ │
▼ ▼
bd.sock, created by the SSH tunnel bd.sock, created by the boundary-drawer
when the desktop app is connected container (CPU), started by run.sh
│
▼
GPU desktop (Boundary Drawer app)cli-tool-local-inference is cli-tool with that one mount replaced. Compose matches mounts by their path in the container, so the volume takes the place of the tunnel's directory. A run with --local-inference cannot reach the tunnel at all, even if it's connected.
What happens in each situation
| Tunnel connected? | --local-inference? | What happens |
|---|---|---|
| Yes | No | Requests go to the GPU server. This is production. |
| No | No | The job fails straight away with ServerUnavailable: /run/boundary-drawer/bd.sock doesn't exist: Boundary Drawer isn't connected to this host. It isn't retried: a server that has never answered is most likely not running, and waiting won't help. |
| Yes | Yes | Requests go to the local container only. The tunnel is not mounted, so it receives nothing. |
| No | Yes | Requests go to the local container. |
If the tunnel drops during a run, after the server has answered at least once, the failed batch is retried 3 times, after 2, 4 and 8 s. A tunnel that reconnects within those ~14 s costs a pause, not the run; after that the job fails. See Retries.
There is no automatic fallback from the GPU server to the container. A production job that finds the tunnel down fails rather than quietly running on a CPU, which would be much slower and is not the production setup. Using the container is always an explicit choice.
The dependencies are created lazily. A program that has a boundary or classification client injected but never uses it (for example an infrastructure update with draw_boundaries: false) never connects, so it runs fine without either server.
Socket checks
Before using the socket, the client checks that the socket and its directory belong to the user running it, and that the directory can't be written by other users. Otherwise another account on the EC2 could put its own server there and receive our points.
The container's socket belongs to the container's user (uid 1000), not necessarily to you. The Compose services that use it set BOUNDARY_DRAWER_SOCKET_OWNER_UID: "1000", which also accepts a socket owned by that uid. Never set this variable for production runs. cli-tool doesn't set it.
Running jobs
With scripts/run.sh
Add --local-inference to any run.sh command:
scripts/run.sh config/examples/infrastructure/update_with_inference.yaml --env local --local-inferencerun.sh then:
- starts the
boundary-drawercontainer and waits until its models are loaded (about 10 s); - runs the job in
cli-tool-local-inferenceinstead ofcli-tool; - removes the container when the job ends, whether it finished, failed or was interrupted.
The flag works with any --env. It's meant for local testing and development: think twice before using it for a real import, since the results come from the CPU container rather than the production GPU setup (the models are the same, but the run is much slower).
Runnable examples of everything on this page, with what each one does and outputs, are in config/examples/infrastructure/ (see its README.md).
The inference programs
Two standalone programs read a CSV with a WKT geometry column (EPSG:4326):
| Task | Example config | Output |
|---|---|---|
infrastructure.inference.run_boundary_drawing_v2 | config/examples/infrastructure/boundary_drawing.yaml | drawn_boundaries.geojson: one feature per input row, with status, score, area_m2 and vertices. Polygons in the input are reduced to a point inside them first. |
infrastructure.inference.run_site_classification | config/examples/infrastructure/site_classification.yaml | site_classifications.csv: the input plus site_type_from_imagery, site_type_confidence, site_type_status and site_type_error. Every row is classified, points and polygons. |
In infrastructure updates
Infrastructure updates (infrastructure.process_update, infrastructure.apply_snapshot) use both steps through clean_data. The options are listed in the infrastructure user guide; in short:
| Option | Default | Effect |
|---|---|---|
draw_boundaries | false | Draws a boundary at each point site. |
draw_boundaries_for_new_sites_only | true | Only for sites that aren't in the platform yet. |
draw_boundaries_for_lowest_quality_polygons | false | Also redraws polygons of the lowest polygon quality. |
site_classification | off | Which polygon sites get a site_type_from_imagery: off, unclassified, unclassified_or_no_equipment or all. |
Boundary drawing in an update:
- Only new sites, by default. Sites already in the platform (they have an
aershed_id) are left alone, so an update doesn't redraw or add polygons to existing sites. A new point that matches an existing site takes on that site'saershed_idwhen they're combined, so it doesn't count as new either. Setdraw_boundaries_for_new_sites_only: falseto draw for existing point sites too. - What's added. The drawn polygon is added alongside the point with
polygon_source: AERSCAPE_AI_DRAWNandpolygon_quality: LOW, and the usual point handling then combines them. Points where nothing is found are left as they are.
Site classification in an update:
site_classification | Polygon sites classified |
|---|---|
off | None |
unclassified | Those with no site_type_from_imagery |
unclassified_or_no_equipment | As unclassified, plus those classified no_equipment_visible. Useful when the imagery may have changed, for example for sites that were under construction. |
all | Every polygon site, replacing existing classifications |
- Only polygons are classified. A polygon tells the classifier how big the site is; for a bare point it has to guess. On test sites polygons were classified with much higher confidence.
- Existing sites are included. Classification isn't limited to new sites, so
unclassified_or_no_equipmentandallcan change the site type of sites already in the platform; those changes go into the import as site updates. - Sites the server can't classify (for example when imagery fails) keep their current value, and a warning is logged.
- It runs after boundary drawing, so points that got a drawn boundary in the same run are classified too.
- The older
get_site_classifications: truestill works and meansunclassified; it logs a warning asking you to switch.
Setting up the container
The image is ghcr.io/aerscape/boundary-drawer, in the organisation's GitHub container registry. It's about 300 MB to download and has both models built in. It's private, so each machine needs a one-off login:
Create a classic personal access token with only the
read:packagesscope at https://github.com/settings/tokens/new?scopes=read:packages. The container registry doesn't accept fine-grained tokens. If the organisation uses SSO, click Configure SSO → Authorize next to the token.Log in, pasting the token when asked for the password:
bashdocker login ghcr.io -u <your-github-username>Docker stores the login (on Docker Desktop, in the system credential store), so you only do this once. Don't put the token in a file in the repository.
Check that it downloads:
bashdocker pull ghcr.io/aerscape/boundary-drawer:latest
Choosing the image version
:latest is the latest build of boundary-drawer's main branch. To pin a release, or use a local build, set BOUNDARY_DRAWER_IMAGE:
BOUNDARY_DRAWER_IMAGE=ghcr.io/aerscape/boundary-drawer:0.3.26 scripts/run-tests.sh tests/inference --with-boundary-drawer
# a local build of ../boundary-drawer (models pulled with git lfs pull first)
docker build -t boundary-drawer ../boundary-drawer
BOUNDARY_DRAWER_IMAGE=boundary-drawer scripts/run.sh <config> --local-inferenceTo see which version you have:
# The GPU server, through the tunnel: run on the machine the tunnel connects to (the EC2, or WSL)
curl --unix-socket ~/.boundary-drawer/bd.sock http://localhost/v1/info
# The container image: no need to start it
docker image inspect ghcr.io/aerscape/boundary-drawer:latest \
--format '{{index .Config.Labels "org.opencontainers.image.version"}}'The first returns the server's full settings as JSON, including version, the device the model runs on, and the app's current settings. The container's socket is inside a Docker volume rather than on your disk, so it can't be reached with curl from the host; the image label gives its version instead. From Python, in whichever setup a job runs in, BoundaryDrawingClient().info() returns the same JSON as the curl command.
Tests
| Tests | Server needed | How they run |
|---|---|---|
tests/inference/test_boundary_drawing.py, test_site_classification.py | No | Mock clients (MockBoundaryDrawingClient, MockSiteClassificationClient) that implement the client interfaces and answer locally. Everything above the client is the real code. They run in every test run and in CI. |
tests/inference/test_transport_retry.py | No | When requests are and aren't retried, and the option names sent to the server. |
tests/inference/test_boundary_drawer_server.py | Yes, the container | Marked boundary_drawer. They run the example sites in example_data/boundaries/ through the real programs and pipeline steps. |
The server tests are skipped unless BOUNDARY_DRAWER_TESTS=1, which only the datalab-tools-inference-tests Compose service sets. A plain pytest (or CI) therefore never sends test points to a production tunnel. With --with-boundary-drawer, an unreachable server fails these tests instead of skipping them.
scripts/run-tests.sh tests/inference --with-boundary-drawer # just the inference tests
scripts/run-tests.sh --with-boundary-drawer # everything, against the containerThe server fetches live imagery, so these tests allow for small changes: drawn outlines must overlap the hand-drawn examples with an IoU of at least 0.8 (they currently score 0.90 to 0.94), and classifications must be valid class names rather than specific ones.
How the code fits together
The two steps are built the same way, following the framework's injection pattern:
| Layer | Boundary drawing | Site classification |
|---|---|---|
| HTTP client (interface + implementation) | inference_tools/boundary_drawer_client.py: IBoundaryDrawingClient, BoundaryDrawingClient | inference_tools/site_classification_client.py: ISiteClassificationClient, SiteClassificationClient |
| Connection | inference_tools/transport.py: Transport, shared by both | |
| Pipeline step (interface + implementation) | infrastructure/inference/boundaries_v2.py: IBoundaryInference, BoundaryInference | infrastructure/inference/site_classification.py: ISiteClassificationInference, SiteClassificationInference |
| Standalone program | infrastructure/inference/run_boundary_drawing_v2.py | infrastructure/inference/run_site_classification.py |
All four interfaces are registered in core/inject.py, so a program gets them by declaring a parameter of that type:
@inject
def run(
config: BaseConfig,
boundary_drawer: IBoundaryInference,
site_classifier: ISiteClassificationInference,
): ...To test code that uses them, pass a mock: run(config, site_classifier=my_mock).
Both clients send points in batches of 64, with 3 batches in flight.
Retries
Both clients send every batch through Transport.request_with_retry, which decides whether a failure is worth retrying:
| Situation | What happens |
|---|---|
| The server has never answered this client (no request has succeeded yet) and is unavailable | Fails straight away. The server isn't running or the tunnel isn't connected, so a retry would only delay the error. |
| The server has answered before, then becomes unavailable (the tunnel dropped mid-run, or the server restarted) | The batch is retried up to 3 times, after 2, 4 and 8 s. If it's still unavailable, the job fails. |
| Any other error: a rejected option (HTTP 400), a missing token (401) | Fails straight away, never retried. |
"Unavailable" means the connection failed, or the server answered HTTP 500, 502 or 504. Each client tracks this separately, and a successful health() or info() call counts as an answer. Pass retries= to segment or classify to change the number of retries.
Note that "answered" means a whole batch came back. A drop while the very first batches are still being processed fails the job; on the CPU container a batch of 64 boundaries takes about two minutes.
segment takes the server's options: ground_res_m (imagery metres per pixel, default 0.4), simplify_m (how far the outline may stray from the traced edge, default 2.0 m), smooth_m (edge smoothing, default 1.0 m) and min_score (confidence below which a point is no_site, default 0.7). Leave them unset to use the server's settings. classify takes point_windows_m, the window sizes (default 200 m and 400 m) averaged when classifying a bare point.
Settings reference
| Variable | Set by | Meaning |
|---|---|---|
BOUNDARY_DRAWER_URL | docker-compose.yml | Where to send requests: unix:/path/to/bd.sock or http://host:port. Outside Docker it defaults to unix:~/.boundary-drawer/bd.sock. |
BOUNDARY_DRAWER_TOKEN | you, if needed | Only if the server is configured to require a token. |
BOUNDARY_DRAWER_SOCKET_OWNER_UID | the container services only | Also accept a socket owned by this uid. Never set it for production. |
BOUNDARY_DRAWER_IMAGE | you, optionally | The container image (default ghcr.io/aerscape/boundary-drawer:latest). |
BOUNDARY_DRAWER_TESTS | datalab-tools-inference-tests | 1 runs the tests that need the server. |
Troubleshooting
unauthorized when pulling the image. You're not logged in to ghcr.io, the token lacks read:packages, or it isn't authorised for the organisation's SSO. See Setting up the container.
bd.sock doesn't exist: Boundary Drawer isn't connected to this host. The tunnel is down. Check the desktop is awake and Boundary Drawer is connected to this host (Connect to host…), or use --local-inference for a local run.
refusing to use /run/boundary-drawer/bd.sock: its directory belongs to uid 0. ~/.boundary-drawer on the host belongs to root. Docker creates a missing mount directory as root, and older versions of run.sh didn't create it first. It only ever holds the tunnel's socket, so remove it and let run.sh recreate it (mode 700, owned by you):
sudo rm -rf ~/.boundary-drawerThe container never becomes healthy. Look at its log with docker compose --profile inference logs boundary-drawer. Loading the models takes about 10 seconds; under emulation (an arm64 image on an x86 machine, or the reverse) it takes much longer.
Everything is slow with --local-inference. That's expected: it runs on the CPU. Use it for small jobs and tests; large jobs belong on the GPU server.