This project contains a simple convolutional neural network for image classification with PyTorch. The included example trains a binary classifier for cats and dogs and can also run inference on a single image after training.
- Builds a grayscale image dataset for training
- Downloads and extracts the Cats vs Dogs dataset automatically
- Splits the data into training and validation sets
- Trains a convolutional neural network with fully connected layers
- Saves model weights and optimizer state for reuse
- Runs prediction on a single image such as
cat.jpg
catvsdog_example.py: main example script for training and predictionimage_classifier/classifier.py: training loop, validation, saving, loading, and prediction logicimage_classifier/cnn_model.py: neural network architecture definitionimage_classifier/tools.py: dataset preparation, image loading, plotting, and helper functionscat.jpg: sample image for prediction
Install the required Python packages before running the example:
- torch
- torchvision
- tqdm
- opencv-python
- wget
- matplotlib
- numpy
Example install command:
pip install torch torchvision tqdm opencv-python wget matplotlib numpyWhen catvsdog_example.py runs with parameters['rebuild'] = True, the script:
- Downloads the dataset archive
- Extracts the image folders
- Resizes images to the configured resolution
- Converts images to grayscale
- Normalizes pixel values
- Saves the processed dataset as
dataset.npy - Trains the model and stores outputs in the configured save directory
If parameters['rebuild'] = False, the script loads a previously saved model instead of rebuilding the dataset and retraining.
Run the example with:
python catvsdog_example.pyThe current configuration in the example script:
- uses images from
PetImages/CatandPetImages/Dog - resizes images to
50 x 50 - trains for
6epochs - uses a validation split of
10% - saves outputs to
CNN_catsVsdogs - predicts the class of
cat.jpg
After training, the script stores generated artifacts in the save directory defined by parameters['savedir'].
With the current configuration, that directory is CNN_catsVsdogs and typically contains:
- the saved model file
- the processed dataset file
- the extracted
PetImagesfolder
The main configuration is stored in the parameters dictionary inside catvsdog_example.py.
Important values you may want to change include:
parameters['size']: image resolutionparameters['device']: execution device, currently set tocudaparameters['epochs']: number of training epochsparameters['learning rate']: optimizer learning rateparameters['batch_size_training']: training batch sizeparameters['batch_size_validation']: validation batch sizeparameters['savedir']: directory used for model outputsparameters['dataset']: processed dataset filename
The current example is configured with:
parameters['device'] = 'cuda'If your machine does not have CUDA available, change it to:
parameters['device'] = 'cpu'You can adapt this repository to another image-classification problem by changing the dataset paths and labels, then adjusting the output size and prediction logic as needed. For binary image classification, the current structure is already close to reusable.
- The example currently uses grayscale images
- The project is designed for binary classification in its current form
- Training downloads data from an external source, so internet access is required when rebuilding the dataset
Aditi Sah