The current cnn.py implements multiple CNN classes that share similar initialisation logic, layers, and utility methods. Your task is to introduce a reusable BaseCNN class that captures shared functionality across the CNN variants, and refactor the remaining classes to enhance code maintainability and reduce redundancy.
Each existing CNN class should inherit from BaseCNN while preserving its current behaviour and public API.
This task assesses your ability to design for reusability, efficiency and clarity, as well as apply good software engineering practices: using pre-commit hooks and writing effective tests.
- Reusability: Move the shared CNN logic into a new
BaseCNNand remove duplicate code from subclasses. - Inheritance: Each CNN model should inherit from
BaseCNN, overriding only model-specific parts. - Compatibility: Existing APIs, inputs/outputs, and model behaviour must remain unchanged.
- Documentation: Add clear docstrings for
BaseCNNand all models. - Testing: Ensure all tests pass or update
test_cnn.pyto confirm identical behaviour.
- Review the CNN classes in
cnn.pyand identify shared logic. - Define and implement the
BaseCNNclass. - Refactor existing CNN models.
- Update and run tests to confirm correctness.
-
Create a Python environment (version 3.10-3.12) using any tool you prefer, such as:
- Conda:
conda create -n omaib python=3.12 && conda activate omaib - venv:
python3.12 -m venv .venv && source .venv/bin/activate
- Conda:
-
Install required packages:
-
PyTorch and Torchvision (adjust the
index-urlparameter if you wish to use a GPU build):pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
-
Pre-commit and pytest:
pip install pre-commit pytest
-
From the root of the repository, run the following commands in your terminal:
-
Install pre-commit hooks (only required once):
pre-commit install
-
Run pre-commit checks for code style and formatting on all files:
pre-commit run --all-files
-
Run tests cases to verify functionality:
pytest