File size: 5,976 Bytes
dead143
 
5bf30ef
 
 
dead143
900d811
bbc3fe2
 
5bf30ef
 
 
dead143
5bf30ef
93f8c6b
5bf30ef
 
 
 
 
 
dead143
 
 
 
 
 
 
 
a5ec00a
dead143
5bf30ef
 
 
 
 
 
 
f8bc55b
 
 
 
 
 
 
 
 
dead143
 
 
5bf30ef
 
 
 
 
 
 
 
 
900d811
5bf30ef
 
 
 
 
 
bbc3fe2
5bf30ef
 
10412fe
 
 
 
5bf30ef
 
 
 
 
 
 
 
 
 
 
 
 
10412fe
5bf30ef
 
 
 
 
10412fe
 
dead143
 
 
 
 
 
 
b3857da
 
 
a5ec00a
b03f9b1
a5ec00a
b3857da
a5ec00a
b3857da
a5ec00a
b3857da
 
dead143
 
5bf30ef
dead143
 
 
 
b3857da
dead143
 
b3857da
 
dead143
5bf30ef
 
 
 
dead143
5bf30ef
 
dead143
 
5bf30ef
 
 
 
 
 
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
# 20251015 update for open data classification

from fastapi import FastAPI, UploadFile, File
from app.model import load_model, predict_image
from app.utils import save_uploaded_file, append_to_csv
from app.log import logger, configure_logger
from app.dashboard.router import router as dashboard_router
from app.auth import check_predict_key
from fastapi import Depends
import os
from app.utils import preprocess_image, is_valid_image
from contextlib import asynccontextmanager
import numpy as np

SIM_THRES = 0.35  # Similarity threshold (lowered from 0.55; cluster_centers.npy covers broader distribution)

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Set model loading path
    current_directory = os.path.dirname(os.path.abspath(__file__))
    parent_folder = os.path.dirname(current_directory)
    resource_dir = os.path.join(parent_folder, "resource")
    app.state.database_dir = os.path.join(parent_folder, "database")
    app.state.uploads_dir = os.path.join(parent_folder, "uploads")
    app.state.logs_dir = os.path.join(parent_folder, "logs")
    configure_logger(app.state.logs_dir)

    # MODEL_PATH = os.path.join(parent_folder, "resource", "model_2c_v1.onnx")
    # 改:使用 emb 版 ONNX,才能同時拿到 embedding + prob
    MODEL_PATH   = os.path.join(resource_dir, "model_b0_balanced_v2_emb.onnx")
    REF_EMB_PATH = os.path.join(resource_dir, "avg12_embedding.npy")

    # Load model
    logger.info("Starting application... Loading model.")
    model = load_model(MODEL_PATH)
    app.state.model = model  # Store the model in `app.state` for later access
    logger.info("Model loaded successfully!")

    # 優先載入 cluster_centers.npy(k-means 多中心點)
    # fallback: avg12_embedding.npy(舊版單中心點)
    cluster_path = os.path.join(resource_dir, "cluster_centers.npy")
    ref_emb = np.load(cluster_path if os.path.exists(cluster_path) else REF_EMB_PATH).astype(np.float32)
    # 舊版 avg12 shape=(1,1536) → squeeze 成 1-D;cluster_centers shape=(k,1536) 保持 2-D
    if ref_emb.ndim == 2 and ref_emb.shape[0] == 1:
        ref_emb = ref_emb.squeeze(0)
    mode = "cluster_centers" if (ref_emb.ndim == 2) else "avg_single"
    logger.info(f"Reference embedding loaded: {mode}, shape={ref_emb.shape}")
    app.state.ref_emb = ref_emb
    app.state.sim_thres = SIM_THRES

    yield  # Enter running state, FastAPI will start accepting requests at this point

    # Release resources
    logger.info("Shutting down application... Cleaning up resources.")
    del app.state.model
    logger.info("Model resources cleaned up.")

# Initialize app and bind lifespan
app = FastAPI(lifespan=lifespan)
app.include_router(dashboard_router)

@app.get("/")
async def read_root():
    return {"message": "Connection successful!"}

# Inference API
@app.post("/predict/", dependencies=[Depends(check_predict_key)])
async def predict(file: UploadFile = File(...)):
    # Save uploaded image and generate ID
    try:
        saved_path, image_id = save_uploaded_file(app.state.uploads_dir, file)
    except ValueError as e:
        return {"error": str(e)}
    logger.info(f"Image saved: {saved_path}, ID: {image_id}")

    # Check if the image is valid
    if not is_valid_image(saved_path):
        logger.error(f"Invalid image file - ID: {image_id}, Path: {saved_path}")
        os.remove(saved_path)  # Delete invalid file
        return {"error": "Invalid image file, please upload a valid image format"}
        
    # Preprocess image
    try:
        preprocessed_image = preprocess_image(saved_path)
    except Exception as e:
        logger.error(f"Image preprocessing failed - ID: {image_id}, Error: {str(e)}")
        os.remove(saved_path)
        return {"error": f"Image preprocessing failed: {str(e)}"}
    
    # Inference
    try:
        model = app.state.model
        ref_emb = app.state.ref_emb
        thres   = app.state.sim_thres

        # 新版 predict_image:回傳 (is_in_criteria: bool, prob: float, sim: float)
        is_in_criteria, probability, sim = predict_image(model, preprocessed_image, ref_emb, thres=thres)

        # 先把 positive 類別的原始機率四捨五入(保留原本行為)
        probability = round(float(probability), 2)

        # 所屬類別機率(bacteria prob 直接用;virus prob = 1 - bacteria prob)
        class_prob = probability if probability > 0.5 else round(1 - probability, 2)

        # 高信心 bypass:class_prob >= HIGH_CONF 且 sim >= MIN_SIM 時直接分類
        # 解決非典型拍攝角度(如臟器黑底標本)被 sim gate 誤擋的問題
        # MIN_SIM 防止完全無關圖片通過 bypass(B0 模型對非豬圖片也極端自信)
        HIGH_CONF = 0.85
        MIN_SIM_BYPASS = 0.24

        if class_prob >= HIGH_CONF and sim >= MIN_SIM_BYPASS:
            is_in_criteria = True   # bypass sim gate

        if not is_in_criteria:
            prediction = "None"
        else:
            if probability > 0.5:
                prediction = "bacteria"
            else:
                prediction = "virus"
                probability = round(1 - probability, 2)

        logger.info(
            f"Inference result - ID: {image_id}, Class: {prediction}, "
            f"Probability: {probability:.2f}, ClassProb: {class_prob:.2f}, Sim: {sim:.3f}"
        )
    except Exception as e:
        logger.error(f"Model inference failed - ID: {image_id}, Error: {str(e)}")
        return {"error": f"Model inference failed: {str(e)}"}
    
    
    # Save result to CSV
    try:
        csv_path = os.path.join(app.state.database_dir, "inference_results.csv")
        append_to_csv(csv_path, image_id, prediction, probability, sim)
    except Exception as e:
        logger.error(f"Failed to save result to CSV: {str(e)}")
        return {"error": f"Failed to save result to CSV: {str(e)}"}
    
    # Return inference result
    return {"id": image_id, "class": prediction, "probability": probability}