====================
Computer Vision project to classify vehicle makes (e.g., Toyota, Subaru, Ford) using a transfer‑learned ResNet‑101. The repository automates dataset download from Kaggle, dataset filtering/restructuring, pre‑cropping to bounding boxes, training, and validation with a saved confusion matrix.
- What this project does
- Repo structure
- Prerequisites
- Installation (Poetry or pip/venv)
- Configure Kaggle API (.env)
- Quickstart: download data, preprocess, train, and validate
- Outputs and artifacts
- Troubleshooting
- FAQ
- Contributing
- License
If you’re not comfortable with Python tools yet, follow these tips first:
-
You need Python 3.11–3.14 installed.
- Check your version (Windows PowerShell):
python --version - Check your version (macOS/Linux):
python3 --version
- Check your version (Windows PowerShell):
-
You need Git to clone this repository.
- Check Git:
git --versiongit --version
- Check Git:
-
How to open a terminal:
- Windows: Press Win key, type “PowerShell”, press Enter.
- macOS: Open “Terminal” app.
- Linux: Open your distribution’s terminal app.
-
Where to run commands: First navigate into the folder where you want the project. Example:
- Windows (PowerShell):
cd $HOME\Documents - macOS/Linux:
cd "$HOME/Documents"
- Windows (PowerShell):
-
Copy exactly what is inside each code box as a single line. Every command below is split so you can copy one line at a time.
At a high level:
- Downloads the “Stanford Car Dataset by classes folder” from Kaggle.
- Filters/reorganizes the dataset into
./data/filtered_cars/{train,test}based on supported makes incontrollers/CarMakeData.py. - Moves and filters Kaggle CSVs to generate annotation sheets (e.g.,
anno_train_filtered.csv,anno_test_filtered.csv). - Pre‑crops all images to their bounding boxes and resizes them to 224×224.
- Builds a ResNet‑101 model with a custom classifier head; freezes all base layers except
layer4and trains with a scheduled learning rate. - Saves the trained model to
./model/car_classifier.ptand evaluates on the test set. - Produces a normalized confusion matrix image saved to
./output/ConfusionMatrix.png.
Top‑level overview (non‑exhaustive):
main.py— Orchestrates dataset initialization, conditional training, and validation.controllers/— Dataset filtering, Kaggle API communication, metadata for makes/classes.modelConstruction/— Dataset init, normalization/augmentation, dataset class, model config, training loop.modelValidation/— Validation and confusion matrix builder.data/— Created at runtime; holds downloaded and filtered data and CSVs.model/— Created at runtime; Trained weights (car_classifier.pt). If you want to skip training, place a compatible weights file here.output/— Created at runtime; Confusion matrix and other outputs.pyproject.toml— Project metadata and dependencies (Poetry)..env— Your Kaggle API credentials.
Software
- Python 3.11–3.14 (project targets >=3.11, <3.15).
- Git (to clone the repository).
- One of:
- Poetry (recommended), or
- pip + venv.
- Kaggle account and API credentials (see “Configure Kaggle API”).
- Kaggle CLI available in your shell. Installing the
kagglePython package provides thekagglecommand but you may need to re‑open your shell so PATH updates take effect.
Hardware
- CPU‑only works (extremely slow); GPU (NVIDIA) recommended for performance. Model was trained on an Nvidia L40S -> ~1hrs on GPU.
- If using GPU, install an appropriate NVIDIA driver. Prebuilt PyTorch usually bundle CUDA runtime.
Disk and bandwidth
- Dataset download, repository files, extraction, and other files require ~4GB gigabytes free. Ensure you have 4GB of space free on your disk before cloning.
- Clone the repository.
- Windows (PowerShell):
git clone https://github.com/Admeen3581/AutomotiveClassifier.git cd AutomotiveClassifier
- macOS/Linux (Bash):
git clone https://github.com/Admeen3581/AutomotiveClassifier.git cd AutomotiveClassifier
- Windows (PowerShell):
- Install Poetry:
- Install/Upgrade pip first:
python -m pip install --upgrade pip
- Install Poetry:
pip install poetry
- Install/Upgrade pip first:
- Install project dependencies from pyproject using Poetry:
-
poetry install
-
- Build your .env file in the root directory:
-
KAGGLE_API_USER=<your_kaggle_username>
-
KAGGLE_API_TOKEN=<your_kaggle_api_key>
-
- Optional: Use Poetry to run the app:
-
poetry run python main.py
-
- Clone the repository.
- Windows (PowerShell):
git clone https://github.com/Admeen3581/AutomotiveClassifier.git cd AutomotiveClassifier
- macOS/Linux (Bash):
git clone https://github.com/Admeen3581/AutomotiveClassifier.git cd AutomotiveClassifier
- Windows (PowerShell):
- Install Poetry:
- Upgrade pip first:
python -m pip install --upgrade pip
- Install Poetry:
pip install requests kaggle python-dotenv opencv-python torch torchvision numpy pandas matplotlib scikit-learn tqdm
- Upgrade pip first:
- Build your .env file in the root directory:
-
KAGGLE_API_USER=<your_kaggle_username>
-
KAGGLE_API_TOKEN=<your_kaggle_api_key>
-
- Optional: Run the app:
-
python main.py
-
The dataset is downloaded via the Kaggle CLI and requires API credentials.
- Create a Kaggle account (https://www.kaggle.com/).
- Generate an API token: Account settings → Create New API Token. This downloads
kaggle.jsoncontainingusernameandkey. - In this project’s root directory, create a file named
.envwith these two lines (each line is separate):-
KAGGLE_API_USER=<your_kaggle_username>
-
KAGGLE_API_TOKEN=<your_kaggle_api_key>
-
- Ensure the
kagglecommand is available in your shell. If you just installed it, close and reopen your terminal. - You must accept the dataset’s terms on Kaggle to download it. Visit the dataset page and click “I Understand and Accept” if prompted:
Run the end‑to‑end pipeline. On first run, this will:
- Create
./data/if it does not exist. - Download and extract the Kaggle dataset into
./data/stanford_cars. - Reorganize and filter images into
./data/filtered_cars/{train,test}. - Move and filter CSVs, then pre‑crop training images to bounding boxes.
- Train the model if
./model/car_classifier.ptdoes not exist. - Validate on the test set and save
./output/ConfusionMatrix.png.
Using Poetry:
poetry run python main.pyUsing pip/venv:
python main.pyNotes
- If
./dataalready exists, dataset initialization will print a message and skip downloading. Delete./datato force a fresh re‑init. - Training uses the following defaults (see
modelConstruction/ModelTraining.py):- Batch size: 512
- Epochs per schedule chunk: 25
- Schedule chunks: 4 with learning rates 1e-2, 1e-3, 1e-4, 1e-5
- Device: CUDA if available, else CPU
./model/car_classifier.pt— Saved PyTorch weights after training../output/ConfusionMatrix.png— Normalized confusion matrix over the test set../data/filtered_cars/— Filtered and preprocessed dataset layout used by the model.
-
“kaggle: command not found” or Kaggle CLI not recognized
- Ensure the environment has Kaggle installed (it is declared in pyproject and installed by Poetry). Re‑open your terminal so the
kagglecommand is on PATH. - Confirm you accepted the dataset terms on Kaggle.
- Ensure
.envcontainsKAGGLE_API_USERandKAGGLE_API_TOKEN.
- Ensure the environment has Kaggle installed (it is declared in pyproject and installed by Poetry). Re‑open your terminal so the
-
Dataset init keeps skipping with a message that
./dataexists- Delete the
./datadirectory to force a fresh init. The initializer returns early if it detects an existing data directory.
- Delete the
-
FileNotFoundError: Could not find CSV file at ./data/anno_test_filtered.csv. Possible a dataset API call issue.-
- Delete the
./datadirectory to force a fresh init.
- Delete the
-
-
CUDA/CuDNN errors
- If you don’t need GPU acceleration, run on CPU (it will auto‑detect). If you do, ensure your NVIDIA driver is up to date and that your Torch install matches your CUDA runtime. Installing the default pip/Poetry wheel typically includes a compatible CUDA runtime.
-
OpenCV cannot read images (
Image not found)- Ensure the dataset successfully downloaded and reorganized and that your working directory is the project root when running
main.py.
- Ensure the dataset successfully downloaded and reorganized and that your working directory is the project root when running
-
TypeError: expected str, bytes or os.PathLike object, not NoneType- Ensure you .env file is setup correctly.
-
Which dataset do we use?
- Kaggle: “Stanford Car Dataset by classes folder” (jutrera/stanford-car-dataset-by-classes-folder).
-
What classes are supported?
- The list is defined in
controllers/CarMakeData.pyascar_brands. This drives the number of output classes and dataset filtering.
- The list is defined in
-
Can I skip training and just validate?
- Yes, place a compatible weights file at
./model/car_classifier.ptbefore runningmain.py. The script will detect the model and skip training.
- Yes, place a compatible weights file at
-
How do I re‑run preprocessing?
- Delete the
./datadirectory and runmain.pyagain.
- Delete the
Pull requests are welcome. Please keep to the existing code style and structure. If adding dependencies, update pyproject.toml and ensure installation works with both Poetry and pip/venv. Consider adding or updating tests under test/ as appropriate.
This project is licensed under the MIT License. See LICENSE.md for details.