Vehicles Counter
Real-time vehicle counter for a fixed street camera: a one-hidden-layer neural network written from scratch in Octave/MATLAB, backpropagation by hand, no ML framework.
Written in 2017 as my first neural network, right after Andrew Ng's Machine Learning course, and refreshed in 2026. Every part is visible: the sigmoid, the cost, the backward pass, the weight matrices. The model decides, frame by frame, whether a car is in a small crop of the road, and a few lines of glue turn those decisions into a running count.
- Model: huggingface.co/tomekceszke/vehicles-counter
- Dataset: huggingface.co/datasets/tomekceszke/vehicles-counter-frames
- Code: github.com/tomekceszke/vehicles-counter
What this is, and what it is not
It is a small binary image classifier (car / not car) and a counter built on it, where every line of the model can be read in a minute: 30,051 parameters in two matrices, a hand-derived gradient, and a forward pass of two matrix products.
It is not a general vehicle detector. It knows one camera, one crop and one viewing angle, and it learned from a few hundred frames. A pretrained CNN or YOLO would do much better. The point is the path from raw pixels to a working counter with the framework removed.
How it counts
camera stream βββΊ crop of one lane (VLC) βββΊ 120Γ40 frame, several per second
β
βΌ
grey + resize Γ0.5 βββΊ 20Γ60 = 1200 pixels βββΊ network βββΊ car / not car
β
counter += 1 when a "car" frame follows one or more "not car" frames ββββββββ
The network looks at one frame at a time. Counting happens in src/detect.m: a car
is counted on the transition from empty road to car, so a car that stays in the crop for five
frames is counted once.
Architecture
INPUT 1200 pixel intensities (20Γ60 grey, 0β255, column-major)
β Theta1 (25 Γ 1201), sigmoid
βΌ
HIDDEN 25 units
β Theta2 (1 Γ 26), sigmoid
βΌ
OUTPUT h β (0, 1) h β₯ 0.5 β not a car, h < 0.5 β car
| Component | Shape | Parameters |
|---|---|---|
Theta1 (incl. bias column) |
25 Γ 1201 | 30,025 |
Theta2 (incl. bias) |
1 Γ 26 | 26 |
| Total | 30,051 |
Training (2017, src/learn.m): cross-entropy cost with L2 regularisation
(Ξ» = 0.01), gradient by hand-written backpropagation (src/lib/nn.m),
optimised with fmincg (conjugate gradient, C. E. Rasmussen, as used in the course) for 50
iterations. Raw pixel values, no normalisation. Label convention: 1 = not car, 0 = car.
Results
Held-out test set (182 frames). Positive class = car.
| Split | Frames | Accuracy | Car precision | Car recall | Car F1 |
|---|---|---|---|---|---|
| test | 182 (55 car / 127 not car) | 0.9176 | 0.8030 | 0.9636 | 0.8760 |
| train (for reference only) | 234 (117 / 117) | 0.9615 | 0.9576 | 0.9658 | 0.9617 |
Confusion matrix on test: 53 cars found, 2 missed, 114 empty frames correct, 13 empty frames taken for a car. Almost all errors are false alarms on an empty road.
That fits the data: the frames come in passes, runs of consecutive frames of one vehicle or one stretch of empty road, so the effective number of examples is much smaller than the number of files. The training set holds 117 car frames from 20 vehicles, but its 117 empty-road frames come from only 4 stretches of time. The test set has 12 such stretches, with lighting the network never saw.
Progress log
Each row is a closed stage.
| Stage | Date | Model | Test accuracy | Test car F1 |
|---|---|---|---|---|
| 0 | 2017-07-09 | 1200-25-1 MLP, hand-written backprop, fmincg, raw pixels |
0.9176 | 0.8760 |
The 2026 refresh reproduces Stage 0 unchanged on GNU Octave 11.3. A retrained Stage 1 will be added
as a new row; see notes/refresh-2026.md.
Use it
Octave
git clone https://github.com/tomekceszke/vehicles-counter.git
cd vehicles-counter/src
octave-cli --eval 'pkg install -forge image' # once
octave-cli --eval 'pkg load image; check' # β Accuracy for test samples: 92%
src/detect.m runs the counter on a live crop written to data/live/scene.jpg
(the 2017 capture scripts for VLC are in tools/legacy/).
Python (no Octave)
data/processed/model_v7.mat holds the same weights in MATLAB 7
format:
import numpy as np, scipy.io as sio
from huggingface_hub import hf_hub_download
from PIL import Image
from scipy.special import expit
m = sio.loadmat(hf_hub_download("tomekceszke/vehicles-counter", "data/processed/model_v7.mat"))
T1, T2 = m["Theta1"], m["Theta2"]
def is_car(path): # path to a 120Γ40 crop of the lane
img = Image.open(path).convert("L").resize((60, 20), Image.BICUBIC)
x = np.asarray(img, dtype=float).flatten(order="F") # Octave is column-major
h = expit(T2 @ np.r_[1, expit(T1 @ np.r_[1, x])])[0]
return h < 0.5 # output 1 means "not a car"
On the test set this port agrees with Octave on 181 of 182 frames (the only difference is the resize implementation) and reaches 0.9231 accuracy.
Limitations
- One camera, one crop, one viewing angle, daylight, 2017. It will not transfer to another street without new training data.
- Empty road is represented by only 4 stretches of time in training, hence the false alarms.
- Two cars driving through back to back, with no empty frame between them, count as one.
- The 2017 stream the capture scripts point to may no longer exist.
Repository layout
src/ Octave code (run from here): learn.m, check.m, detect.m, lib/, config/
src/utils/ export of the weights for non-Octave tools
data/{train,test}/ the frames, by class (cars / notcars)
data/processed/ trained weights: Theta1.mat, Theta2.mat (Octave), model_v7.mat (MATLAB 7)
data/splits.csv every frame with its split, label and pass id
tools/ dataset build, Hugging Face sync, legacy capture scripts
notes/ refresh log and publishing ritual
License
Code: MIT, see LICENSE. Two exceptions are listed in NOTICE.md:
src/lib/fmincg.m (C. E. Rasmussen, own terms) and the camera frames in data/ and doc/
(third-party stream, research and education use only).
Dataset used to train tomekceszke/vehicles-counter
Evaluation results
- Test accuracy on Vehicles Counter Framestest set self-reported0.918
- Test F1 (car) on Vehicles Counter Framestest set self-reported0.876


