This project consists of two parts:
optimalcolloidpython.ipynb— trains theOptimalColloidNetmodel on synthetically generated microscopy images and saves a checkpoint.analyze_video.py— loads the trained checkpoint and runs inference on a real microscopy video, producing an annotated output video.
- Go to kaggle.com and sign in.
- Click + New Notebook.
- In the top-right, click the ⚙️ settings panel and set Accelerator to GPU T4 x2 (recommended) or GPU P100.
- Make sure Internet is toggled on (required for any pip installs).
In the notebook sidebar, click Add data → Upload and add:
optimalcolloidpython.ipynbanalyze_video.py- Your microscopy video file (e.g.
sample_microscopy.mp4)
Uploaded files land at /kaggle/input/<dataset-name>/.
All major libraries are pre-installed on Kaggle (use Requirements.txt).
Open optimalcolloidpython.ipynb and run all cells. Training will:
- Synthetically generate 2000 microscopy images on the fly.
- Train for up to 60 epochs with early stopping (patience = 8).
- Save the best checkpoint to
colloid_output/best_checkpoint.pt.
Expected training time on a single T4 GPU: ~20–40 minutes.
Once training is done, run analyze_video.py. Update the three path variables at the bottom of the script before executing:
INPUT_VIDEO = "/kaggle/input/<your-dataset>/sample_microscopy.mp4"
MODEL_WEIGHTS = "colloid_output/best_checkpoint.pt"
OUTPUT_VIDEO = "/kaggle/working/annotated_output.mp4"Then run from a notebook cell:
%run analyze_video.pyOr from a terminal cell:
!python analyze_video.pyThe script will:
- Auto-detect particle scale from the first frame.
- Run 8-fold test-time augmentation (TTA) per frame.
- Write an annotated video with detected particle centres overlaid in green.
Output is saved to /kaggle/working/annotated_output.mp4, which you can download directly from the Kaggle file browser on the right-hand panel.
| File | Location | Description |
|---|---|---|
best_checkpoint.pt |
colloid_output/ |
Trained model weights |
loss_curve.png |
colloid_output/ |
Training/validation loss plot |
annotated_output.mp4 |
/kaggle/working/ |
Video with particle detections |
- GPU is required. The model will fall back to CPU but inference will be very slow.
- Out of memory? Reduce
BATCH_SIZEin the notebook (default is 8; try 4). - No particles detected? Lower
DETECT_THRESHOLDinanalyze_video.py(default0.15; try0.10). - Wrong particle size? The
estimate_optimal_scalefunction auto-scales based on Hough circle detection on the first frame. If it fails, setscale_factormanually inprocess_video().