This notebook is designed to classify images into two categories: cats and dogs. It demonstrates how to preprocess image data, design a a convolutional neural network (CNN), and evaluate its perfromance using TensorFlow and Keras
The Dogs vs. Cats is a popular dataset for binary classification. I have provided a link to the dataset below fro those interested in downloading it. The file size is approximately 856.1 MB.
- Data Preparation: The notebook starts by unzipping the dataset sourced from Kaggle's "Dogs vs. Cats" competition. The dataset contains labeled images of cats and dogs which are used for training and testing the model.
- Data Exploration: Various techniques are used to visualize and understand the distribution and structure of the images.
- Data Preprocessing: The images are resized and normalized to be fed into the neural network.
- Model Building: A CNN is built using TensorFlow and Keras. The CNN contains multiple layers, including convolutional layers, pooling layers, and dense layers.
- Training: The model is trained on the dataset using appropriate optimizers and loss functions.
- Evaluation: After training, the model's performance is evaluated using various metrics such as accuracy, precision, recall, etc. Confusion matrices and other visualizations might also be used for a deeper understanding of the model's performance
Pillow==8.1.2
opencv-python==4.5.1.48
matplotlib==3.3.4
numpy==1.19.5
pandas==1.2.3
seaborn==0.11.1
scikit-learn==0.24.1
tensorflow==2.4.1
- Clone the repository
git clone https://github.com/sfajors/cats-vs-dogs.git
- Navigatte to the Project Directory
cd path_to_cloned_repo
- Install the Necessary Dependencies
pip install -r requirements.txt
jupyter notebook
Run the cells at the beginning of the notebook that contain import statements and any other initialization code. This will ensure all necessary libraries are loaded and any predefined functions or variables are set.
If you've saved the trained model to a file (e.g., model.h5
), there should be a cell in the notebook that loads this model. Run this cell to load the model into memory.
- Insert a New Cell in the Notebook: This will be where users input the path to their image and get the prediction
- Add Prediction Code: In the new cell, users should add code similar to the following:
def predict_image_using_model(model, image_path, img_size=(224, 224)): # Preprocess the image img = cv2.imread(image_path, cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert from BGR to RGB img = cv2.resize(img, img_size) img = np.array(img) img = np.expand_dims(img, axis=0) # Expand dimensions to match the model's input shape # Make a prediction predictions = model.predict(img) predicted_class = int(predictions > 0.5) # Convert probability to class label (0 or 1) # Interpret the prediction if predicted_class == 0: label = "cat" else: label = "dog" confidence = predictions[0][0] * 100 if label == "dog" else (1 - predictions[0][0]) * 100 print(f"The image is predicted to be a {label} with {confidence:.2f}% confidence.") image_path = "/path_to_image" predict_image_using_model(model, image_path)
- Run the cell: After adding the code, run the cell to see the prediction
The notebook will output the predicted class for the provided image.