File size: 7,208 Bytes
02b6b5d
 
 
 
b872f48
 
02b6b5d
 
82b164a
02b6b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b872f48
02b6b5d
 
 
 
 
 
 
 
 
 
 
 
 
b872f48
82b164a
8af372d
 
82b164a
 
 
8af372d
 
 
 
82b164a
8af372d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b872f48
02b6b5d
 
 
 
 
b872f48
 
02b6b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc41c89
 
 
 
 
 
 
 
 
 
 
 
02b6b5d
 
fc41c89
02b6b5d
 
 
 
 
 
 
 
 
 
b872f48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02b6b5d
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""FastAPI backend for AMSI-Net LULC Recognition System."""

import torch
import time
import base64
import io
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, ConfigDict
from typing import List, Dict, Any

from model_loader import get_model_loader
from preprocessing import get_preprocessor
from class_mappings import get_class_label
from explainers import generate_all_explanations

app = FastAPI(
    title="AMSI-Net LULC Recognition API",
    description="Multi-label Remote Sensing Image Classification with AMSI-Net",
    version="1.0.0"
)

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Request/Response models
class ClassPrediction(BaseModel):
    class_index: int
    class_label: str
    confidence: float

class PredictionResponse(BaseModel):
    predicted_labels: List[str]
    all_predictions: List[ClassPrediction]
    explainability_maps: Dict[str, str]
    uncertainty: float
    inference_time_ms: float
    image_info: Dict[str, Any]

class MobileRequest(BaseModel):
    image: str = Field(..., alias="image_b64")

    # Allow aliases in case the mobile app uses different names
    model_config = ConfigDict(
        populate_by_name=True,
        json_schema_extra={
            "example": {
                "image": "base64_string_here"
            }
        }
    )

from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi import Request

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    body = await request.body()
    print(f"--- 422 Validation Error ---")
    print(f"Method: {request.method} URL: {request.url}")
    print(f"Body: {body.decode()[:500]}...") # Print first 500 chars
    print(f"Errors: {exc.errors()}")
    print(f"---------------------------")
    return JSONResponse(
        status_code=422,
        content={"detail": exc.errors(), "body_received": body.decode()[:100]},
    )

# Constants
VALID_IMAGE_FORMATS = {"image/jpeg", "image/png", "image/jpg"}
MAX_IMAGE_SIZE = 10 * 1024 * 1024  # 10MB
CONFIDENCE_THRESHOLD = 0.5

def run_inference(image_bytes: bytes) -> PredictionResponse:
    """Core inference logic reused across endpoints."""
    try:
        preprocessor = get_preprocessor()
        loader = get_model_loader()
        
        # 1. Preprocess
        image_info = preprocessor.get_image_info(image_bytes)
        image_tensor = preprocessor.preprocess(image_bytes)
        image_tensor = image_tensor.to(loader.get_device())
        
        # 2. Inference
        start_time = time.time()
        model = loader.load_model()
        
        with torch.no_grad():
            outputs = model(image_tensor)
            logits = outputs['logits']
            uncertainty = outputs['uncertainty'].item()
            
            # Multi-label probability
            probabilities = torch.sigmoid(logits).squeeze()
            
        inference_time = (time.time() - start_time) * 1000
        
        # 3. Process results
        all_predictions = []
        predicted_labels = []
        
        for i, prob in enumerate(probabilities.tolist()):
            label = get_class_label(i)
            conf = float(prob)
            
            pred_obj = ClassPrediction(
                class_index=i,
                class_label=label,
                confidence=conf
            )
            all_predictions.append(pred_obj)
            
            if conf >= CONFIDENCE_THRESHOLD:
                predicted_labels.append(label)
        
        # If no label exceeds threshold, take the top-1
        if not predicted_labels:
            top_idx = torch.argmax(probabilities).item()
            predicted_labels.append(get_class_label(top_idx))
            
        # 4. Generate explanations
        explain_maps = generate_all_explanations(model, preprocessor, image_tensor, image_bytes)
        
        sorted_preds = sorted(all_predictions, key=lambda x: x.confidence, reverse=True)
        
        # LOGGING: Print detailed output to console/logs
        print(f"\n--- AMSI-Net Inference Results ---")
        print(f"Predicted Labels: {predicted_labels}")
        print(f"Uncertainty Score: {uncertainty:.6f}")
        print(f"Inference Latency: {inference_time:.2f}ms")
        print(f"Top 5 Predictions:")
        for p in sorted_preds[:5]:
            print(f"  - {p.class_label}: {p.confidence:.4f}")
        print(f"----------------------------------\n")
        
        return PredictionResponse(
            predicted_labels=predicted_labels,
            all_predictions=sorted_preds,
            explainability_maps=explain_maps,
            uncertainty=uncertainty,
            inference_time_ms=inference_time,
            image_info=image_info
        )
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))

def validate_image(file: UploadFile) -> bytes:
    """Validate and read image file."""
    if file.content_type not in VALID_IMAGE_FORMATS:
        raise HTTPException(
            status_code=400,
            detail=f"Invalid image format. Supported: JPEG, PNG. Got: {file.content_type}"
        )
    
    content = file.file.read()
    if len(content) > MAX_IMAGE_SIZE:
        raise HTTPException(
            status_code=413,
            detail=f"Image too large. Max size: {MAX_IMAGE_SIZE / 1024 / 1024}MB"
        )
    return content

@app.get("/")
async def root():
    return {"status": "ok", "message": "AMSI-Net API is running"}

@app.get("/health")
async def health():
    loader = get_model_loader()
    return {
        "status": "healthy",
        "device": str(loader.get_device()),
        "model_loaded": loader.model is not None
    }

@app.post("/predict", response_model=PredictionResponse)
async def predict(file: UploadFile = File(...)):
    """Standard multipart prediction (Web)."""
    image_bytes = validate_image(file)
    return run_inference(image_bytes)

@app.post("/predict_mobile", response_model=PredictionResponse)
async def predict_mobile(request: MobileRequest):
    """Base64 prediction (Mobile)."""
    try:
        # Strip potential base64 prefix (e.g. "data:image/jpeg;base64,")
        base64_str = request.image
        if "," in base64_str:
            base64_str = base64_str.split(",")[1]
            
        image_bytes = base64.b64decode(base64_str)
        return run_inference(image_bytes)
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Invalid base64 image data: {str(e)}")

@app.get("/info")
async def info():
    return {
        "model": "AMSI-Net",
        "task": "Multi-label LULC Recognition",
        "dataset": "MLRSNet (60 classes)",
        "input_size": "224x224",
        "features": ["GradCAM", "GradCAM++", "LIME", "Saliency", "Uncertainty Estimation"]
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)