A FastAPI-based web service for cat and dog image classification using PyTorch and ResNet18. This project provides a RESTful API endpoint that accepts image uploads and returns classification predictions with confidence scores.
- Image Classification: Classify images as either "Cat" or "Dog"
- RESTful API: Clean FastAPI-based web service
- Deep Learning Model: Uses ResNet18 backbone with custom classifier
- Confidence Scores: Returns probability distributions and best prediction
- File Upload Support: Accepts image files via multipart form data
- Logging: Comprehensive logging for HTTP requests and model predictions
- CORS Support: Cross-origin resource sharing enabled
- Modular Architecture: Well-organized code structure with separate modules
Image_Classfication_FastAPI/
βββ app.py # FastAPI application entry point
βββ server.py # Uvicorn server configuration
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
βββ config/ # Configuration files
β βββ catdog_cfg.py # Model and data configuration
β βββ logging_cfg.py # Logging configuration
βββ middleware/ # Custom middleware
β βββ cors.py # CORS configuration
β βββ http.py # HTTP logging middleware
βββ models/ # ML model files
β βββ catdog_model.py # ResNet18-based model definition
β βββ catdog_predictor.py # Model inference class
β βββ weights/ # Pre-trained model weights
β βββ catdog_weight.pt
βββ routes/ # API route definitions
β βββ base.py # Main router configuration
β βββ catdog_route.py # Classification endpoint
βββ schemas/ # Pydantic data models
β βββ catdog_schema.py # Response schema
βββ utils/ # Utility functions
β βββ logger.py # Logging utilities
βββ logs/ # Log files
βββ http.log # HTTP request logs
βββ predictor.log # Model prediction logs
- Python 3.8+
- pip or conda
-
Clone the repository (if applicable):
git clone <repository-url> cd Image_Classfication_FastAPI
-
Install dependencies:
pip install -r requirements.txt
-
Verify model weights: Ensure the pre-trained model weights are present at:
models/weights/catdog_weight.pt
Run the FastAPI server using:
python server.pyThe server will start on http://localhost:9000 with auto-reload enabled.
Once the server is running, you can access:
- Interactive API docs:
http://localhost:9000/docs - ReDoc documentation:
http://localhost:9000/redoc
POST /catdog_classification/predict
- Method: POST
- Content-Type: multipart/form-data
- Body: Image file upload
curl -X POST "http://localhost:9000/catdog_classification/predict" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@path/to/your/image.jpg"import requests
url = "http://localhost:9000/catdog_classification/predict"
files = {"file": open("path/to/your/image.jpg", "rb")}
response = requests.post(url, files=files)
result = response.json()
print(result){
"probs": [0.123456, 0.876544],
"best_prob": 0.876544,
"predicted_id": 1,
"predicted_class": "Dog",
"predicted_name": "resnet18"
}probs: List of probabilities for each class [Cat, Dog]best_prob: Highest confidence scorepredicted_id: Predicted class ID (0=Cat, 1=Dog)predicted_class: Predicted class name ("Cat" or "Dog")predicted_name: Model name used for prediction
- Classes: 2 (Cat, Dog)
- Image Size: 64x64 pixels
- Model: ResNet18 backbone
- Device: CPU (configurable)
- Normalization: ImageNet mean and std values
The application logs both HTTP requests and model predictions:
- HTTP logs:
logs/http.log - Prediction logs:
logs/predictor.log
- Backbone: ResNet18 (pre-trained on ImageNet)
- Classifier: Custom fully connected layer
- Transfer Learning: Frozen backbone, trainable classifier
- Input: RGB images resized to 64x64 pixels
- Framework: FastAPI
- Server: Uvicorn
- Middleware: Custom logging and CORS
- Validation: Pydantic schemas
- Async Support: Async/await for model inference
- FastAPI: Web framework for building APIs
- Uvicorn: ASGI server
- PyTorch: Deep learning framework
- Torchvision: Computer vision utilities
- Pillow: Image processing
- Python-multipart: File upload support
-
Model weights not found:
- Ensure
catdog_weight.ptexists inmodels/weights/ - Check file permissions
- Ensure
-
CUDA out of memory:
- The model is configured to use CPU by default
- Modify
DEVICEinconfig/catdog_cfg.pyif needed
-
Image format issues:
- The API accepts common image formats (JPEG, PNG, etc.)
- RGBA images are automatically converted to RGB
- Model inference runs on CPU by default
- Images are automatically resized to 64x64 pixels
- GPU memory is cleared after each prediction
This project is for educational and demonstration purposes.
Feel free to submit issues and enhancement requests!
For questions or issues, please check the logs in the logs/ directory for detailed error information.