Road Accident Risk Prediction Challenge
Baseline source code for Road Accident Risk Prediction Challenge
Road Accident Risk Prediction Baseline Source Code
Challenge participants: Build a regression model to predict the likelihood of road accidents using features like road type, weather, lighting, and time of day.
Table of Contents
- Quick Start
- Introduction
- Requirements
- Project Structure
- Detailed Tutorial
- Submission Guidelines
- License
Quick Start
Clone the repository:
git clone [email protected]:AIOZAI/road_accident_risk_baseline.git
or, download directly: Road Accident Risk Baseline
Then, navigate into the project directory and follow the steps below:
# 1. Install dependencies (Add any other libraries your code imports to requirements.txt)
pip install -r requirements.txt
# 2. Start developing your solution. Follow the tutorial below to implement your AI model.
# 3. Verify Your Submission
python preflight.py
Note: You need to implement your Road Accident Risk Prediction solution following the tutorial guide.
Introduction
Road safety is a global concern, with thousands of accidents occurring every day. In this challenge, you will build a regression model to predict the likelihood of road accidents using features like road type, weather, lighting, and time of day.
This challenge aims to encourage innovative data-driven solutions for proactive accident prevention and safer road management.
In this challenge, participants will be provided with:
-
Model Access: Road Accident Risk Baseline
- Code baseline to develop solutions.
- Dependencies: Add every dependency you import in
requirements.txt.
-
Dataset Access: Road Accident Risk Dataset
- Training dataset (
train.csv) to train your machine learning models. - Testing dataset (
test.csv) to predict labels for generating the submission file.
- Training dataset (
Goal: Build a regression model that predicts the likelihood of accidents (accident_risk) for each road segment.
Requirements
System Requirements
- Python 3.10+
Dependencies
Install all required packages:
pip install -r requirements.txt
Project Structure
Your AI library should follow this structure:
repository/
├── my_ai_lib/ # Your AI library
│ ├── __init__.py # Required: Exposes run() — do not touch
│ ├── schemas.py # Required: Locked input/output schema for this challenge
│ ├── model.py # load_model() <- load your weights here
│ ├── pipeline.py # preprocess/predict/postprocess <- your logic
│ └── run.py # Required: Main workflow function — do not touch
├── models/ # Model weights directory
├── preflight.py # Demo script to verify output format
├── requirements.txt # Add every dependency you import
└── README.md # Documentation
What you may and may not change
| Part | Rule |
|---|---|
my_ai_lib package name | Locked — DO NOT EDIT. AIOZ AI imports my_ai_lib.run(...). Do not rename the folder or remove from .run import run. |
TaskInput / TaskOutput in schemas.py | Locked — DO NOT EDIT. |
run() in run.py | Locked — DO NOT EDIT. It is the entrypoint AIOZ AI calls; do not rename or remove it. |
load_model / preprocess / predict / postprocess | Yours. Fill in these stages with your logic in model.py and pipeline.py. |
requirements.txt | Yours. Add every dependency you import. |
Detailed Tutorial
Step 1: Initialize Your AI Library
1.1 Define my_ai_lib/__init__.py
from .run import run
This file exposes the run() function from run.py as an attribute of my_ai_lib, so you can call it as my_ai_lib.run().
1.2 Define Input/Output Objects in my_ai_lib/schemas.py
Note: In this template, schemas.py is pre-defined and locked. Do not edit it.
from aioz_ainode_adapter.schemas import FileObject, InputObject, OutputObject
class TaskInput(InputObject):
input_folder: str
class TaskOutput(OutputObject):
output_file: FileObject
Step 2: Understanding AIOZ Schema Objects
InputObject(base fields yourTaskInputinherits):device: one ofcpu,cuda,gpu(defaultcuda).model_storage_directory: where your weights live — read this path, do not hard-code one.
- This challenge adds one input field:
input_folder: str. OutputObject: this challenge returnsoutput_file: FileObject.FileObject:data(a localPath, an open binary file, or a URL) +name.
Step 3: Implement the Main Workflow
3.1 Implement Your AI Logic (my_ai_lib/model.py & my_ai_lib/pipeline.py)
Modify my_ai_lib/model.py and my_ai_lib/pipeline.py to add your custom logic.
- load_model: Load your weights/models from
model_storage_directory. - preprocess: Read and preprocess the test data (e.g.,
test.csv) frominput_folder. - predict: Run your loaded model on the features to predict
accident_risk. - postprocess: Save predictions to
result.csvand return its path.
3.2 Implement the Required run() Function (my_ai_lib/run.py)
Note: In this template, run() is pre-defined to link your pipeline stages. Do not edit it.
def run(input_obj: TaskInput) -> TaskOutput:
"""Mandatory entrypoint — DO NOT RENAME IT."""
task_input = TaskInput.model_validate(input_obj.model_dump()) # validate the input
model = load_model(task_input.model_storage_directory, task_input.device)
samples = preprocess(task_input.input_folder)
predictions = predict(model, samples)
output_path = postprocess(predictions)
output = open(output_path, "rb")
output_file = FileObject(data=output, name=os.path.basename(output.name))
return TaskOutput(output_file=output_file)
Critical: The
run()function name is mandatory and cannot be changed.
Step 4: Create Demo Script
The template includes a preflight script (preflight.py) to test your implementation end-to-end and verify the output schema.
Run this command to test your implementation:
python preflight.py
Expected console output:
✅ my_ai_lib.run found
✅ locked schema is intact
Input: type='InputObj' device='cuda' model_storage_directory='.../models' input_folder='sample_input/'
Output: type='OutputObj' output_file=FileObject(type='FileObj', data=..., name='result.csv')
✅ run() returned a valid TaskOutput with a file
Step 5: Add Model Weights
Place your trained model files in the models/ directory:
models/
├── model.pth # Your trained model
├── config.json # Model configuration
└── etc.
Step 6: Format Your Prediction Output (For Submission)
Your model must output predictions in a specific format for grading. Implement the logic to generate this file in the postprocess function of my_ai_lib/pipeline.py.
Requirements:
- Process test dataset (
test.csvin test data folder) - Generate accident risk predictions (a continuous value between 0 and 1) for each road segment
id - Save results as
result.csv
Implementation Example in pipeline.py:
import csv
from pathlib import Path
from typing import List, Tuple, Union
def postprocess(
predictions: List[Tuple[Union[str, int], float]], output_path: Path = Path("result.csv")
) -> Path:
"""Turn predictions into your output file and return its path.
Placeholder: write the predictions to result.csv.
"""
with output_path.open("w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["id", "accident_risk"])
writer.writerows(predictions)
return output_path
Important: The
result.csvmust match the challenge's submission format exactly.
Notes
- Use relative imports for your own modules (
from .lib import helper) so your code runs inside the sandbox. - Let exceptions propagate — AIOZ AI reports them; do not catch and hide them.
- Run
python preflight.pybefore every submission.
Submission Guidelines
Submission Format
Submit a CSV file with two columns:
id: The unique identifier for each road segment.accident_risk: The predicted likelihood of accidents (a continuous value between 0 and 1).
Example: result.csv
id,accident_risk
1001,0.352
1002,0.587
1003,0.102
License
This repository is licensed under the MIT License.