Skip to content

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

🧠 nano-explorer-ml-tools

License: MIT

Data collection and training scripts for the ML models used by nano-explorer, the CLI toolkit for the Waveshare JetBot / Jetson Nano.

This repo is the producer: it turns driving sessions into labelled datasets and trains the .pth model files that nano-explorer's nav/motion commands load at runtime. It doesn't depend on nano-explorer as a library except where a script needs to physically drive the robot to collect data (see below) — and nano-explorer doesn't depend on this repo at all.


Layout

Each ML capability gets its own directory with a collect_*.py (data capture, drives the robot) and train_*.py (model training, portable) pair:

nano-explorer-ml-tools/
  collision_avoidance/
    collect_collision_data.py
    train_collision_avoidance.py
  road_follower/
    collect_road_data.py
    train_road_follower.py
  models/        # trained .pth output — gitignored, copy what you want to keep
  datasets/      # collected data — gitignored, lives here or wherever --out points

Adding a new ML tool

Follow the same pattern for a new capability (e.g. gesture_classifier/):

  1. New directory named after the capability.
  2. collect_<name>_data.py if it needs data captured from the robot — only pull in lib.camera / lib.motor / lib.network from nano-explorer if it genuinely needs to drive the robot or read its camera live; keep everything else (model definition, training loop, CLI) self-contained in this repo.
  3. train_<name>.py — portable, no nano-explorer dependency, output defaults to models/<name>.pth (see the MODELS_DIR constant in the existing train_*.py scripts for the pattern).
  4. Document it in this README the same way as the sections below, and note where nano-explorer expects the trained file to end up.

Setup

Two different environments, matching the two kinds of script here:

Training (train_*.py) — portable, runs anywhere with PyTorch:

# Linux/Jetson (JetPack 4.6.1, Python 3.6, CUDA 10.2):
#   torch==1.10.0        — install manually, NVIDIA aarch64 wheel
#                          (see nano-explorer's doc/jetbot-setup.md)
#   torchvision==0.11.0  — build from source (see nano-explorer's doc/jetbot-setup.md)
#   OpenCV 4.1.1         — bundled with JetPack; do not reinstall via pip

# macOS / other dev machines (Python 3.8+):
pip install torch torchvision opencv-python
pip install -r requirements.txt

Collection (collect_*.py) — Nano-only, drives the physical robot, needs nano-explorer's hardware layer (lib.camera, lib.motor, lib.network) importable in the same environment:

pip install -e /path/to/nano-explorer

Clone both repos side by side on the Nano and activate the same virtualenv for both — that's it, no extra packaging glue needed.


Collision avoidance

Collect data

Drive the robot into position, then snapshot frames labelled free or blocked.

python collision_avoidance/collect_collision_data.py --out datasets/collision_001

# Options
#   --speed        Motor speed [0, 1]          (default: 0.25)
#   --turn-gain    Turn speed multiplier [0,1] (default: 0.60)
#   --camera       csi | usb                   (default: csi)
#   --stream       Enable MJPEG stream         (default: off)
#   --stream-port  MJPEG server port           (default: 8080)

# With live preview in browser
python collision_avoidance/collect_collision_data.py --out datasets/collision_001 --stream

Key bindings while running:

Key Action
↑ ↓ ← → Drive the robot
f Capture frame → free/
b Capture frame → blocked/
q / Ctrl-C Quit

Output layout:

datasets/collision_001/
  free/
    img_000001.jpg …
  blocked/
    img_000001.jpg …

Resuming is automatic — re-running the same --out path picks up where you left off.

Train

python collision_avoidance/train_collision_avoidance.py --dataset datasets/collision_001

# Options
#   --output     Output .pth path  (default: models/collision_avoidance.pth)
#   --epochs     Training epochs   (default: 15)
#   --lr         Learning rate     (default: 1e-4)
#   --batch-size Mini-batch size   (default: 32)
#   --val-split  Validation ratio  (default: 0.15)
#   --seed       Random seed       (default: 42)

Trains a ResNet18 binary classifier (free=0, blocked=1). Best checkpoint (highest val accuracy) is saved automatically.

Copy (or scp) the result to nano-explorer's assets/models/collision_avoidance.pth, or point --model / NANO_COLLISION_MODEL_PATH at it directly — that's what nano-explorer motion collision loads.


Road follower

Collect data

Drive the robot along the track while frames are captured at a fixed rate and labelled with the current steering angle.

python road_follower/collect_road_data.py --out datasets/road_001

# Options
#   --speed        Motor speed [0, 1]          (default: 0.25)
#   --turn-gain    Turn speed multiplier [0,1] (default: 0.60)
#   --fps          Frame capture rate (Hz)     (default: 10)
#   --camera       csi | usb                   (default: csi)
#   --stream       Enable MJPEG stream         (default: off)
#   --stream-port  MJPEG server port           (default: 8080)

# With live preview in browser
python road_follower/collect_road_data.py --out datasets/road_001 --stream

Key bindings while running:

Key Steering angle saved
↑ 0.0 (straight)
← -1.0 (full left)
→ +1.0 (full right)
q / Ctrl-C Quit

Frames are only written while a key is held (robot moving). Reverse (↓) is ignored.

Output layout:

datasets/road_001/
  labels.csv        # filename,angle
  img_000001.jpg …

Appends to an existing labels.csv so sessions can be resumed.

Train

python road_follower/train_road_follower.py --dataset datasets/road_001

# Options
#   --output     Output .pth path  (default: models/road_follower.pth)
#   --epochs     Training epochs   (default: 20)
#   --lr         Learning rate     (default: 1e-4)
#   --batch-size Mini-batch size   (default: 32)
#   --val-split  Validation ratio  (default: 0.15)
#   --seed       Random seed       (default: 42)

Trains a ResNet18 regression model (single output: steering angle ∈ [-1, 1]). Best checkpoint (lowest val MSE loss) is saved automatically.

Copy (or scp) the result to nano-explorer's assets/models/road_follower.pth, or point --model / NANO_ROAD_FOLLOW_MODEL_PATH at it directly — that's what nano-explorer nav road-follow loads.


License

MIT — see LICENSE.