Location>code7788 >text

Deep learning practice: Building image classification API from scratch (Flask/FastAPI version)

Popularity:773 ℃/2025-04-10 18:26:13

Introduction: Image classification requirements in the AI ​​era

In the intelligent era, image classification technology has penetrated into various fields such as medical image analysis, autonomous driving, and industrial quality inspection. As a developer, mastering how to encapsulate deep learning models into API services is a key step in realizing the implementation of technology. This article will teach you step by step to use the Flask/FastAPI framework in the Python ecosystem, and combine it with PyTorch/TensorFlow to deploy an end-to-end image classification API, and finally get an intelligent service that can be called through HTTP requests.

1. Technical stack selection guide

frame Features Applicable scenarios
Flask Lightweight, easy to learn, strong extensibility Small project, rapid prototyping
FastAPI High performance, automatic API documentation generation, support asynchronous Medium- and large-scale projects and production environment deployment
PyTorch Dynamic calculation charts, friendly research, strong flexibility Research-based projects, customized model development
TensorFlow Static computing graphs, industrial-grade deployment, ecological improvement Production environment, large-scale distributed training

Select a suggestion: Newbies can give priority to trying the Flask+PyTorch combination, and then explore the advanced usage of FastAPI+TensorFlow after getting familiar with it.

2. Practical tutorial: Building ResNet Image Classification API

(I) Stage 1: Environment construction

  1. Create a virtual environment
python -m venv image_api_env
source image_api_env/bin/activate  # Linux/Mac
image_api_env\Scripts\activate     # Windows
  1. Installation dependencies
pip install flask fastapi uvicorn torch torchvision pillow
 # or
 pip install flask fastapi uvicorn tensorflow pillow

(2) Stage 2: Model preparation

# models/ (PyTorch example)
 import torch
 from torchvision import models, transforms
 
 # Load pretrained ResNet
 model = models.resnet18(pretrained=True)
 () # Set as inference mode
 
 # Image preprocessing pipeline
 preprocess = ([
     (256),
     (224),
     (),
     (
         mean=[0.485, 0.456, 0.406],
         std=[0.229, 0.224, 0.225]
     )
 ])
 
 # Define inference functions
 def predict(image_tensor):
     with torch.no_grad():
         output = model(image_tensor.unsqueeze(0))
     probabilities = (output[0], dim=0)
     Return probabilities

(III) Stage 3: API Development (Flask Version)

# app_flask.py
 from flask import Flask, request, jsonify
 from PIL import Image
 import io
 import torch
 from import preprocess, predict
 
 app = Flask(__name__)
 
 @('/classify', methods=['POST'])
 def classify():
     # Get uploaded file
     file = ['image']
     img = ((()))
    
     # Image preprocessing
     img_tensor = preprocess(img)
    
     # Model reasoning
     probs = predict(img_tensor)
    
     # Get top5 prediction results
     top5_prob, top5_indices = (probs, 5)
    
     # Map ImageNet category tags
     with open('imagenet_classes.txt') as f:
         classes = [() for line in ()]
    
     results = [{
         'class': classes[idx],
         'probability': float(prob)
     } for idx, prob in zip(top5_indices, top5_prob)]
    
     return jsonify({'predictions': results})
 
 if __name__ == '__main__':
     (debug=True)

(IV) Stage 4: API Testing

bash copy code

 curl -X POST -F "image=@test_image.jpg" http://localhost:5000/classify

Or use Postman to send a POST request and select form-data format to upload the image.

(Five) Stage 5: Performance Optimization (FastAPI Version)

# app_fastapi.py
 from fastapi import FastAPI, File, UploadFile
 from import JSONResponse
 from PIL import Image
 import io
 import torch
 from import preprocess, predict
 
 app = FastAPI()
 
 @("/classify")
 async def classify(image: UploadFile = File(...)):
     # Image loading and preprocessing
     img = ((await()))
     img_tensor = preprocess(img)
    
     # Model reasoning
     probs = predict(img_tensor)
    
     # Get prediction results
     top5_prob, top5_indices = (probs, 5)
    
     # Read category tags
     with open('imagenet_classes.txt') as f:
         classes = [() for line in ()]
    
     results = [{
         'class': classes[idx],
         'probability': float(prob)
     } for idx, prob in zip(top5_indices, top5_prob)]
    
     return JSONResponse(content={'predictions': results})

Run the command:

bash copy code

 uvicorn app_fastapi:app --reload

3. Key optimization strategies

  1. Model quantization
# Quantitative Example (PyTorch)
  = .quantize_dynamic(
     model, {}, dtype=torch.qint8
 )

2.Asynchronous processing

# FastAPI asynchronous example
 from fastapi import BackgroundTasks
 
 @("/classify")
 async def classify_async(image: UploadFile = File(...), background_tasks: BackgroundTasks):
     # Put time-consuming operations into background tasks
     background_tasks.add_task(process_image, image)
     return {"status": "processing"}
 
 async def process_image(image):
     # Actual processing logic
     ...

3.Cache mechanism

From import Cache
 
 cache = Cache(ttl=3600) # 1 hour cache
 
 @("/recent")
 async def get_recent(id: str):
     result = (id)
     If not result:
         result = await fetch_data(id)
         (id, result)
     return result

4. Comparison of deployment plans

plan advantage shortcoming Applicable scenarios
Local deployment Easy to debug, low cost Limited concurrency capability Development and testing phase
Cloud Service High availability, automatic scaling Continuous operation and maintenance costs are required Production environment
Containerization Environmental isolation, easy migration Need knowledge of container orchestration Microservice architecture
Serverless Pay on demand, zero operation and maintenance Cold start delay Occasionally high concurrency scenarios

Recommended combination: Local deployment is used in the development stage, and the production environment can adopt the cloud service solution of Nginx+Gunicorn+Docker.

5. Troubleshooting of FAQs

  1. Image upload failed
  • Check whether the request header Content-Type is multipart/form-data;
  • Confirm the file size limit (Flask defaults to 16MB, which can be adjusted via MAX_CONTENT_LENGTH).

2.Model loads slowly

  • Use for model compilation;
  • Try model pruning and quantization.

3.Inaccurate prediction results

  • Check whether the image preprocessing process is consistent with the training time;
  • Verify the size and normalized parameters of the input image.

6. Learning expansion path

  1. Model optimization
  • Learn knowledge distillation technology
  • Explore AutoML automatic model compression

2.API Security

  • Add API key authentication
  • Implement request frequency limit

3.Advanced framework

  • Research on the API encapsulation of HuggingFace Transformers
  • Explore the cross-platform deployment of ONNX Runtime

7. Conclusion: Milestones for building end-to-end AI applications

Through the practice of this article, we not only master the development process of image classification API, but also establish a complete understanding from model training to production deployment. With the deepening of technology, we can try to encapsulate complex tasks such as face recognition and object detection into APIs and gradually build our own AI service ecosystem. Remember, the value of technology lies in application, maintain the enthusiasm for practice, and let AI truly empower the industry!