louiecerv/testapp_cats_dogs_torch_nn
0
Cats vs Dogs Classifier
This is a simple web application that classifies images of cats and dogs using a pre-trained neural network model. The app is built using Streamlit and PyTorch, and the model is hosted on Hugging Face.
Features
- Upload an image of a cat or dog.
- The app preprocesses the image and displays the preprocessed version.
- The model predicts whether the image is of a cat or a dog and displays the prediction with confidence.
Installation
- Clone the repository:
git clone https://github.com/yourusername/cats_dogs_classifier.git
cd cats_dogs_classifier- Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`- Install the required packages:
pip install -r requirements.txt- Set your Hugging Face token as an environment variable:
export HF_TOKEN=your_huggingface_token # On Windows, use `set HF_TOKEN=your_huggingface_token`Usage
- Run the Streamlit app:
streamlit run app.py- Open your web browser and go to
http://localhost:8501.
- Upload an image of a cat or dog, and the app will display the preprocessed image and the prediction.
Code Overview
app.py: The main application file that contains the Streamlit UI and the model loading and prediction logic.requirements.txt: The list of required Python packages.
Model
The model is a simple neural network defined as follows:
class SimpleNN(nn.Module):
def __init__(self, input_size, n_classes):
super(SimpleNN, self).__init__()
self.model = nn.Sequential(
nn.Flatten(),
nn.Linear(input_size, 512), # Input
nn.ReLU(), # Activation for input
nn.Linear(512, 512), # Hidden
nn.ReLU(), # Activation for hidden
nn.Linear(512, n_classes) # Output
)
def forward(self, x):
return self.model(x)