Your model training is complete with excellent results:
- Final Training Loss: 0.1614
- Final Validation Loss: 0.1579
- GPU Memory Usage: ~535 MB (efficient!)
- Training Time: ~28.5 minutes per epoch
checkpoints/
├── best_model.pth # Your best model (use this!)
├── checkpoint_epoch_5.pth
├── checkpoint_epoch_10.pth
├── checkpoint_epoch_15.pth
├── checkpoint_epoch_20.pth
└── training_history.json # Loss curves and metrics
from inference_deepfashion2 import DeepFashion2Predictor
# Load model
predictor = DeepFashion2Predictor(
checkpoint_path='checkpoints/best_model.pth',
device='cuda',
confidence_threshold=0.5
)
# Detect clothing in image
results = predictor.get_clothing_info('your_image.jpg')
# Print results
for item in results:
print(f"{item['category']}: {item['confidence']:.2f}")
# Visualize results
predictor.visualize('your_image.jpg', output_path='result.jpg')Use this when: Processing images in a Python script or Jupyter notebook
- Install Flask:
pip install flask flask-cors- Start the server:
python api_server.py- Use the API from any language:
Python Example:
import requests
# Upload and detect
with open('image.jpg', 'rb') as f:
response = requests.post(
'http://localhost:5000/detect',
files={'image': f},
data={'confidence': 0.5}
)
results = response.json()
print(results)JavaScript Example:
const formData = new FormData();
formData.append('image', fileInput.files[0]);
formData.append('confidence', 0.5);
fetch('http://localhost:5000/detect', {
method: 'POST',
body: formData
})
.then(r => r.json())
.then(data => console.log(data));cURL Example:
curl -X POST http://localhost:5000/detect \
-F "image=@your_image.jpg" \
-F "confidence=0.5"Use this when: Building web applications, mobile apps, or integrating with other services
- Start the API server:
python api_server.py- Open the web interface:
# Open web_interface.html in your browser- Drag and drop images to see instant results!
Use this when: Testing the model, showing demos, or quick visual inspection
Check if server is running
curl http://localhost:5000/healthDetect clothing items (returns JSON)
curl -X POST http://localhost:5000/detect \
-F "image=@image.jpg" \
-F "confidence=0.5"Response:
{
"success": true,
"num_items": 2,
"items": [
{
"category": "trousers",
"confidence": 0.95,
"bounding_box": {"x1": 100, "y1": 200, "x2": 300, "y2": 500},
"mask_area": 45000.0
}
]
}Returns image with detections drawn
curl -X POST http://localhost:5000/detect_visualize \
-F "image=@image.jpg" \
--output result.jpgProcess multiple images at once
curl -X POST http://localhost:5000/batch_detect \
-F "images[]=@image1.jpg" \
-F "images[]=@image2.jpg"Get list of all clothing categories
curl http://localhost:5000/categoriesYour model can detect these 13 clothing types:
- short_sleeve_top - T-shirts, short sleeve shirts
- long_sleeve_top - Long sleeve shirts, blouses
- short_sleeve_outwear - Short sleeve jackets
- long_sleeve_outwear - Coats, long jackets
- vest - Vests, waistcoats
- sling - Tank tops, sleeveless tops
- shorts - Short pants
- trousers - Long pants, jeans
- skirt - All types of skirts
- short_sleeve_dress - Short sleeve dresses
- long_sleeve_dress - Long sleeve dresses
- vest_dress - Sleeveless dresses
- sling_dress - Dress with thin straps
from inference_deepfashion2 import DeepFashion2Predictor
import os
import json
predictor = DeepFashion2Predictor('checkpoints/best_model.pth')
# Process all images in folder
image_folder = 'my_images/'
results_all = {}
for img_file in os.listdir(image_folder):
if img_file.endswith(('.jpg', '.png')):
img_path = os.path.join(image_folder, img_file)
results = predictor.get_clothing_info(img_path)
results_all[img_file] = results
# Save visualization
predictor.visualize(
img_path,
output_path=f'results/{img_file}',
show=False
)
# Save all results
with open('batch_results.json', 'w') as f:
json.dump(results_all, f, indent=2)# Lower threshold = more detections (may include false positives)
predictor.confidence_threshold = 0.3
# Higher threshold = fewer, more confident detections
predictor.confidence_threshold = 0.7# Modify in inference_deepfashion2.py
predictor.COLORS = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
# ... add more colors
]# views.py
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from inference_deepfashion2 import DeepFashion2Predictor
predictor = DeepFashion2Predictor('path/to/best_model.pth')
@csrf_exempt
def detect_clothing(request):
if request.method == 'POST':
image = request.FILES['image']
# Save temporarily
temp_path = f'/tmp/{image.name}'
with open(temp_path, 'wb') as f:
f.write(image.read())
# Detect
results = predictor.get_clothing_info(temp_path)
return JsonResponse({'items': results})from fastapi import FastAPI, File, UploadFile
from inference_deepfashion2 import DeepFashion2Predictor
app = FastAPI()
predictor = DeepFashion2Predictor('checkpoints/best_model.pth')
@app.post("/detect")
async def detect(file: UploadFile = File(...)):
contents = await file.read()
temp_path = f'/tmp/{file.filename}'
with open(temp_path, 'wb') as f:
f.write(contents)
results = predictor.get_clothing_info(temp_path)
return {"items": results}import streamlit as st
from inference_deepfashion2 import DeepFashion2Predictor
from PIL import Image
st.title("Clothing Detection App")
predictor = DeepFashion2Predictor('checkpoints/best_model.pth')
uploaded_file = st.file_uploader("Choose an image", type=['jpg', 'png'])
if uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image')
if st.button('Detect'):
# Save temp file
temp_path = 'temp.jpg'
image.save(temp_path)
# Detect
results = predictor.get_clothing_info(temp_path)
st.write(f"Found {len(results)} items:")
for item in results:
st.write(f"- {item['category']}: {item['confidence']:.2%}")- Automatic product categorization
- Visual search
- Style recommendations
- Outfit composition analysis
- Trend detection
- Wardrobe management apps
- Inventory management
- Virtual try-on systems
- Customer preference analysis
- Clothing verification
- Dress code compliance
- Image classification
# Use smaller confidence threshold for faster processing
predictor.confidence_threshold = 0.7
# Resize large images before processing
from PIL import Image
img = Image.open('large_image.jpg')
img = img.resize((1280, 960)) # Resize to reasonable size
img.save('resized.jpg')# Process one image at a time
# Clear GPU cache between images
import torch
for img_path in image_list:
results = predictor.predict(img_path)
# Process results...
torch.cuda.empty_cache() # Clear cache- High precision needed: confidence_threshold = 0.7-0.9
- High recall needed: confidence_threshold = 0.3-0.5
- Balanced: confidence_threshold = 0.5 (default)
Solution: Reduce image size or process on CPU
predictor = DeepFashion2Predictor(
checkpoint_path='best_model.pth',
device='cpu' # Use CPU instead of GPU
)Solutions:
- Lower confidence threshold
- Ensure good image quality
- Make sure clothing items are clearly visible
- Train longer or with more data
Check:
- Server is running:
python api_server.py - Correct port (5000)
- Firewall settings
- Check server logs for errors
- Train longer: Increase
num_epochsfrom 20 to 50 - Use more data: Increase
max_train_samples - Data augmentation: Add more transforms
- Fine-tune learning rate: Adjust based on loss curves
- Use Docker for easy deployment
- Add authentication to API
- Use HTTPS for security
- Monitor performance and errors
- Set up logging
- Use batch processing for multiple images
- Add queue system (Redis, Celery)
- Load balancing for multiple servers
- GPU optimization with TensorRT
If you have issues:
- Check this guide
- Review error messages
- Test with simple examples first
- Verify model file exists and is not corrupted
You now have:
- ✅ Trained Mask R-CNN model
- ✅ Inference scripts
- ✅ REST API server
- ✅ Web interface
- ✅ Integration examples
Start using your model right away with:
python quick_inference_example.pyGood luck with your project! 🚀