Clean up repo and sync all current files
Browse files- Add .gitignore to exclude __pycache__, logs/, database/, uploads/
- Remove tracked pyc, log, and CSV runtime files
- Add new model (model_2c_v1_emb.onnx) and reference embedding (avg12_embedding.npy)
- Add missing scripts, test data, and updated app source files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- .gitignore +7 -0
- Dockerfile +24 -2
- app/__pycache__/__init__.cpython-311.pyc +0 -0
- app/__pycache__/config.cpython-311.pyc +0 -0
- app/__pycache__/log.cpython-311.pyc +0 -0
- app/__pycache__/main.cpython-311.pyc +0 -0
- app/__pycache__/model.cpython-311.pyc +0 -0
- app/__pycache__/utils.cpython-311.pyc +0 -0
- app/log.py +39 -13
- app/main.py +49 -17
- app/model.py +83 -9
- app/utils.py +4 -6
- database/inference_results.csv +0 -3
- flow.txt +1 -0
- logs/app.log +0 -32
- resource/avg12_embedding.npy +3 -0
- resource/model_2c_v1_emb.onnx +3 -0
- scripts/deploy.sh +8 -1
- scripts/install_docker.sh +28 -0
- start_services.py +2 -1
- test/data/ec.JPG +2 -2
- test/data/nr1.png +3 -0
- test/data/nr2.jpg +3 -0
- test/data/nr3.jpg +3 -0
- test/data/nr4.jpg +3 -0
- test/data/ped.JPG +2 -2
- test/data/rw.jpg +3 -0
- test/test.py +10 -2
- tips.txt +8 -0
.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
|
| 5 |
+
logs/
|
| 6 |
+
database/
|
| 7 |
+
uploads/
|
Dockerfile
CHANGED
|
@@ -5,8 +5,13 @@ FROM python:3.11-slim
|
|
| 5 |
WORKDIR /api
|
| 6 |
COPY . .
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
RUN apt-get update && apt-get install -y \
|
| 9 |
-
libgl1-mesa-glx \
|
|
|
|
|
|
|
| 10 |
libglib2.0-0 \
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
|
@@ -14,9 +19,26 @@ RUN apt-get update && apt-get install -y \
|
|
| 14 |
# Install dependencies
|
| 15 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Expose the application's port
|
| 19 |
EXPOSE 8000
|
| 20 |
|
| 21 |
# Start the FastAPI application
|
| 22 |
-
ENTRYPOINT ["sh", "-c", "python start_services.py || exit 1"]
|
|
|
|
|
|
| 5 |
WORKDIR /api
|
| 6 |
COPY . .
|
| 7 |
|
| 8 |
+
# 系統套件多裝 execstack
|
| 9 |
+
# 結論:是 onnxruntime 的 .so 要求可執行 stack,導致 import 直接掛、容器秒退。
|
| 10 |
+
|
| 11 |
RUN apt-get update && apt-get install -y \
|
| 12 |
+
# libgl1-mesa-glx \
|
| 13 |
+
libgl1 \
|
| 14 |
+
libglx-mesa0 \
|
| 15 |
libglib2.0-0 \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
|
|
|
| 19 |
# Install dependencies
|
| 20 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
|
| 22 |
+
# 安裝 patchelf,用來清掉 onnxruntime .so 的 exec-stack 旗標
|
| 23 |
+
RUN apt-get update && apt-get install -y --no-install-recommends patchelf \
|
| 24 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
#########2025/0923 update#########
|
| 28 |
+
# 找到 onnxruntime 的 .so 並清除 exec-stack(沒有就略過)
|
| 29 |
+
RUN set -eux; \
|
| 30 |
+
so="$(find /usr/local/lib/python* -path '*/site-packages/onnxruntime/capi/onnxruntime_pybind11_state*.so' -print -quit 2>/dev/null)"; \
|
| 31 |
+
if [ -n "$so" ]; then \
|
| 32 |
+
patchelf --print-execstack "$so" || true; \
|
| 33 |
+
patchelf --clear-execstack "$so" || true; \
|
| 34 |
+
patchelf --print-execstack "$so" || true; \
|
| 35 |
+
fi
|
| 36 |
+
#########2025/0923 update#########
|
| 37 |
+
|
| 38 |
|
| 39 |
# Expose the application's port
|
| 40 |
EXPOSE 8000
|
| 41 |
|
| 42 |
# Start the FastAPI application
|
| 43 |
+
# ENTRYPOINT ["sh", "-c", "python start_services.py || exit 1"]
|
| 44 |
+
CMD ["python","-m","uvicorn","app.main:app","--host","0.0.0.0","--port","8000"]
|
app/__pycache__/__init__.cpython-311.pyc
DELETED
|
Binary file (156 Bytes)
|
|
|
app/__pycache__/config.cpython-311.pyc
DELETED
|
Binary file (382 Bytes)
|
|
|
app/__pycache__/log.cpython-311.pyc
DELETED
|
Binary file (766 Bytes)
|
|
|
app/__pycache__/main.cpython-311.pyc
DELETED
|
Binary file (4.16 kB)
|
|
|
app/__pycache__/model.cpython-311.pyc
DELETED
|
Binary file (923 Bytes)
|
|
|
app/__pycache__/utils.cpython-311.pyc
DELETED
|
Binary file (3.41 kB)
|
|
|
app/log.py
CHANGED
|
@@ -1,18 +1,44 @@
|
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
os.makedirs(log_dir, exist_ok=True)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 12 |
-
handlers=[
|
| 13 |
-
logging.FileHandler(os.path.join(log_dir, "app.log")), # Log output to file
|
| 14 |
-
logging.StreamHandler() # Log output to console
|
| 15 |
-
]
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/log.py
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
logger = logging.getLogger("API Logger")
|
| 6 |
+
logger.setLevel(logging.INFO)
|
|
|
|
| 7 |
|
| 8 |
+
def configure_logger(log_dir: str):
|
| 9 |
+
"""Configure handlers to write logs to the given directory."""
|
| 10 |
+
os.makedirs(log_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# 清掉舊 handler,避免重複寫入多次
|
| 13 |
+
logger.handlers.clear()
|
| 14 |
+
|
| 15 |
+
fmt = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
|
| 16 |
+
|
| 17 |
+
fh = logging.FileHandler(os.path.join(log_dir, "app.log"), encoding="utf-8")
|
| 18 |
+
fh.setFormatter(fmt)
|
| 19 |
+
|
| 20 |
+
sh = logging.StreamHandler()
|
| 21 |
+
sh.setFormatter(fmt)
|
| 22 |
+
|
| 23 |
+
logger.addHandler(fh)
|
| 24 |
+
logger.addHandler(sh)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# import logging
|
| 28 |
+
# import os
|
| 29 |
+
|
| 30 |
+
# # Ensure the log directory exists
|
| 31 |
+
# log_dir = "logs"
|
| 32 |
+
# os.makedirs(log_dir, exist_ok=True)
|
| 33 |
+
|
| 34 |
+
# # Set up logging configuration
|
| 35 |
+
# logging.basicConfig(
|
| 36 |
+
# level=logging.INFO,
|
| 37 |
+
# format="%(asctime)s [%(levelname)s] %(message)s",
|
| 38 |
+
# handlers=[
|
| 39 |
+
# logging.FileHandler(os.path.join(log_dir, "app.log")), # Log output to file
|
| 40 |
+
# logging.StreamHandler() # Log output to console
|
| 41 |
+
# ]
|
| 42 |
+
# )
|
| 43 |
+
|
| 44 |
+
# logger = logging.getLogger("API Logger")
|
app/main.py
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from app.model import load_model, predict_image
|
| 3 |
from app.utils import save_uploaded_file, append_to_csv
|
| 4 |
-
from app.log import logger
|
| 5 |
import os
|
| 6 |
from app.utils import preprocess_image, is_valid_image
|
| 7 |
from contextlib import asynccontextmanager
|
|
|
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
@asynccontextmanager
|
| 11 |
async def lifespan(app: FastAPI):
|
| 12 |
# Set model loading path
|
| 13 |
current_directory = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
parent_folder = os.path.dirname(current_directory)
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Load model
|
| 18 |
logger.info("Starting application... Loading model.")
|
|
@@ -20,6 +33,13 @@ async def lifespan(app: FastAPI):
|
|
| 20 |
app.state.model = model # Store the model in `app.state` for later access
|
| 21 |
logger.info("Model loaded successfully!")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
yield # Enter running state, FastAPI will start accepting requests at this point
|
| 24 |
|
| 25 |
# Release resources
|
|
@@ -38,7 +58,7 @@ async def read_root():
|
|
| 38 |
@app.post("/predict/")
|
| 39 |
async def predict(file: UploadFile = File(...)):
|
| 40 |
# Save uploaded image and generate ID
|
| 41 |
-
saved_path, image_id = save_uploaded_file(file)
|
| 42 |
logger.info(f"Image saved: {saved_path}, ID: {image_id}")
|
| 43 |
|
| 44 |
# Check if the image is valid
|
|
@@ -57,27 +77,39 @@ async def predict(file: UploadFile = File(...)):
|
|
| 57 |
# Inference
|
| 58 |
try:
|
| 59 |
model = app.state.model
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
else:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
logger.error(f"Model inference failed - ID: {image_id}, Error: {str(e)}")
|
| 75 |
return {"error": f"Model inference failed: {str(e)}"}
|
| 76 |
|
|
|
|
| 77 |
# Save result to CSV
|
| 78 |
try:
|
| 79 |
-
csv_path = os.path.join(
|
| 80 |
-
append_to_csv(csv_path, image_id, prediction, probability)
|
| 81 |
except Exception as e:
|
| 82 |
logger.error(f"Failed to save result to CSV: {str(e)}")
|
| 83 |
return {"error": f"Failed to save result to CSV: {str(e)}"}
|
|
|
|
| 1 |
+
# 20251015 update for open data classification
|
| 2 |
+
|
| 3 |
from fastapi import FastAPI, UploadFile, File
|
| 4 |
from app.model import load_model, predict_image
|
| 5 |
from app.utils import save_uploaded_file, append_to_csv
|
| 6 |
+
from app.log import logger, configure_logger
|
| 7 |
import os
|
| 8 |
from app.utils import preprocess_image, is_valid_image
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
+
import numpy as np
|
| 11 |
|
| 12 |
+
SIM_THRES = 0.55 # Similarity threshold
|
| 13 |
|
| 14 |
@asynccontextmanager
|
| 15 |
async def lifespan(app: FastAPI):
|
| 16 |
# Set model loading path
|
| 17 |
current_directory = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
parent_folder = os.path.dirname(current_directory)
|
| 19 |
+
resource_dir = os.path.join(parent_folder, "resource")
|
| 20 |
+
app.state.database_dir = os.path.join(parent_folder, "database")
|
| 21 |
+
app.state.uploads_dir = os.path.join(parent_folder, "uploads")
|
| 22 |
+
app.state.logs_dir = os.path.join(parent_folder, "logs")
|
| 23 |
+
configure_logger(app.state.logs_dir)
|
| 24 |
+
|
| 25 |
+
# MODEL_PATH = os.path.join(parent_folder, "resource", "model_2c_v1.onnx")
|
| 26 |
+
# 改:使用 emb 版 ONNX,才能同時拿到 embedding + prob
|
| 27 |
+
MODEL_PATH = os.path.join(resource_dir, "model_2c_v1_emb.onnx")
|
| 28 |
+
REF_EMB_PATH = os.path.join(resource_dir, "avg12_embedding.npy")
|
| 29 |
|
| 30 |
# Load model
|
| 31 |
logger.info("Starting application... Loading model.")
|
|
|
|
| 33 |
app.state.model = model # Store the model in `app.state` for later access
|
| 34 |
logger.info("Model loaded successfully!")
|
| 35 |
|
| 36 |
+
# 讀 ref emb(若是多向量則取平均)
|
| 37 |
+
ref_emb = np.load(REF_EMB_PATH).astype(np.float32)
|
| 38 |
+
if ref_emb.ndim > 1:
|
| 39 |
+
ref_emb = ref_emb.mean(axis=0).astype(np.float32)
|
| 40 |
+
app.state.ref_emb = ref_emb
|
| 41 |
+
app.state.sim_thres = SIM_THRES
|
| 42 |
+
|
| 43 |
yield # Enter running state, FastAPI will start accepting requests at this point
|
| 44 |
|
| 45 |
# Release resources
|
|
|
|
| 58 |
@app.post("/predict/")
|
| 59 |
async def predict(file: UploadFile = File(...)):
|
| 60 |
# Save uploaded image and generate ID
|
| 61 |
+
saved_path, image_id = save_uploaded_file(app.state.uploads_dir, file)
|
| 62 |
logger.info(f"Image saved: {saved_path}, ID: {image_id}")
|
| 63 |
|
| 64 |
# Check if the image is valid
|
|
|
|
| 77 |
# Inference
|
| 78 |
try:
|
| 79 |
model = app.state.model
|
| 80 |
+
ref_emb = app.state.ref_emb # <- lifespan 裡放好的參考 embedding
|
| 81 |
+
thres = app.state.sim_thres # <- lifespan 裡放好的門檻 (預設 0.75)
|
| 82 |
+
|
| 83 |
+
# 新版 predict_image:回傳 (is_in_criteria: bool, prob: float, sim: float)
|
| 84 |
+
is_in_criteria, probability, sim = predict_image(model, preprocessed_image, ref_emb, thres=thres)
|
| 85 |
+
|
| 86 |
+
# 先把 positive 類別的原始機率四捨五入(保留原本行為)
|
| 87 |
+
probability = round(float(probability), 2)
|
| 88 |
+
|
| 89 |
+
# 類別決策(維持你原本 bacteria/virus/反轉邏輯;但若未達門檻,class="None")
|
| 90 |
+
if not is_in_criteria:
|
| 91 |
+
prediction = "None"
|
| 92 |
+
# probability 照舊保留 positive 類別機率(如需改 0.0 可自行調整)
|
| 93 |
else:
|
| 94 |
+
if probability > 0.5:
|
| 95 |
+
prediction = "bacteria"
|
| 96 |
+
# probability 保持為 positive 類別機率
|
| 97 |
+
else:
|
| 98 |
+
prediction = "virus"
|
| 99 |
+
probability = round(1 - probability, 2) # 回傳所屬類別機率(和你原本一致)
|
| 100 |
+
|
| 101 |
+
logger.info(
|
| 102 |
+
f"Inference result - ID: {image_id}, Class: {prediction}, Probability: {probability:.2f}"
|
| 103 |
+
)
|
| 104 |
except Exception as e:
|
| 105 |
logger.error(f"Model inference failed - ID: {image_id}, Error: {str(e)}")
|
| 106 |
return {"error": f"Model inference failed: {str(e)}"}
|
| 107 |
|
| 108 |
+
|
| 109 |
# Save result to CSV
|
| 110 |
try:
|
| 111 |
+
csv_path = os.path.join(app.state.database_dir, "inference_results.csv")
|
| 112 |
+
append_to_csv(csv_path, image_id, prediction, probability, sim)
|
| 113 |
except Exception as e:
|
| 114 |
logger.error(f"Failed to save result to CSV: {str(e)}")
|
| 115 |
return {"error": f"Failed to save result to CSV: {str(e)}"}
|
app/model.py
CHANGED
|
@@ -1,12 +1,86 @@
|
|
| 1 |
import onnxruntime as ort
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def load_model(
|
| 4 |
-
return ort.InferenceSession(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def predict_image(model, image):
|
| 7 |
-
input_name = model.get_inputs()[0].name
|
| 8 |
-
output_name = model.get_outputs()[0].name
|
| 9 |
-
outputs = model.run([output_name], {input_name: image})
|
| 10 |
-
probability = outputs[0][0][0]
|
| 11 |
-
# prediction = "bacteria" if probability > 0.5 else "virus"
|
| 12 |
-
return probability
|
|
|
|
| 1 |
import onnxruntime as ort
|
| 2 |
+
import numpy as np
|
| 3 |
+
from numpy.linalg import norm
|
| 4 |
|
| 5 |
+
def load_model(path):
|
| 6 |
+
return ort.InferenceSession(path, providers=["CUDAExecutionProvider","CPUExecutionProvider"])
|
| 7 |
+
|
| 8 |
+
def cosine_similarity(a, b):
|
| 9 |
+
a = a / (norm(a) + 1e-8)
|
| 10 |
+
b = b / (norm(b) + 1e-8)
|
| 11 |
+
return float(np.dot(a, b))
|
| 12 |
+
|
| 13 |
+
def predict_image(model, image, ref_emb, thres=0.75):
|
| 14 |
+
# 取輸入/輸出名稱(用名稱避免順序出錯)
|
| 15 |
+
inp = model.get_inputs()[0].name
|
| 16 |
+
out_map = {o.name: i for i, o in enumerate(model.get_outputs())} # e.g. {'embedding':0,'probability':1}
|
| 17 |
+
|
| 18 |
+
outs = model.run(None, {inp: image})
|
| 19 |
+
|
| 20 |
+
# 盡量用名稱抓;若名稱不同則回落到索引 0/1
|
| 21 |
+
emb = outs[out_map.get("embedding", 0)].squeeze()
|
| 22 |
+
prob = float(outs[out_map.get("probability", 1)].squeeze())
|
| 23 |
+
|
| 24 |
+
# 相似度 + 門檻判斷
|
| 25 |
+
sim = cosine_similarity(emb, ref_emb)
|
| 26 |
+
is_true = sim >= thres
|
| 27 |
+
|
| 28 |
+
# 依你的需求:回傳 (bool, prob);同時也把 sim 一起回傳方便外面印
|
| 29 |
+
return is_true, prob, sim
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
from utils import preprocess_image
|
| 33 |
+
|
| 34 |
+
MODEL_PATH = "/mnt/d/pig/api/resource/model_2c_v1_emb.onnx"
|
| 35 |
+
REF_EMB_PATH = "/mnt/d/pig/api/resource/avg12_embedding.npy"
|
| 36 |
+
IMAGE_PATH = "/mnt/d/pig/api/test/data/ec.jpg"
|
| 37 |
+
THRES = 0.75
|
| 38 |
+
|
| 39 |
+
sess = load_model(MODEL_PATH)
|
| 40 |
+
ref = np.load(REF_EMB_PATH).astype(np.float32)
|
| 41 |
+
if ref.ndim > 1:
|
| 42 |
+
ref = ref.mean(axis=0)
|
| 43 |
+
|
| 44 |
+
x = preprocess_image(IMAGE_PATH) # [1,3,H,W] float32, ImageNet norm
|
| 45 |
+
|
| 46 |
+
flag, prob, sim = predict_image(sess, x, ref, thres=THRES)
|
| 47 |
+
print(f"🔹 Probability = {prob:.4f}")
|
| 48 |
+
print(f"🔹 Similarity = {sim:.4f} (≥{THRES}? {flag})")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
#################################################################################
|
| 52 |
+
# import onnxruntime as ort
|
| 53 |
+
|
| 54 |
+
# def load_model(MODEL_PATH):
|
| 55 |
+
# return ort.InferenceSession(MODEL_PATH)
|
| 56 |
+
|
| 57 |
+
# def predict_image(model, image):
|
| 58 |
+
# input_name = model.get_inputs()[0].name
|
| 59 |
+
# output_name = model.get_outputs()[0].name
|
| 60 |
+
# outputs = model.run([output_name], {input_name: image})
|
| 61 |
+
# probability = outputs[0][0][0]
|
| 62 |
+
# # prediction = "bacteria" if probability > 0.5 else "virus"
|
| 63 |
+
# return probability
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# if __name__ == "__main__":
|
| 68 |
+
# import os
|
| 69 |
+
# from PIL import Image
|
| 70 |
+
# import numpy as np
|
| 71 |
+
# from utils import preprocess_image
|
| 72 |
+
|
| 73 |
+
# MODEL_PATH = "/mnt/d/pig/api/resource/model_2c_v1_tp2.onnx"
|
| 74 |
+
# IMAGE_DIR = "/mnt/d/pig/api/test/data/ec.jpg"
|
| 75 |
+
|
| 76 |
+
# # 載入模型
|
| 77 |
+
# model = load_model(MODEL_PATH)
|
| 78 |
+
# print("✅ Model loaded:", MODEL_PATH)
|
| 79 |
+
|
| 80 |
+
# x = preprocess_image(IMAGE_DIR)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# # 預測
|
| 84 |
+
# prob = predict_image(model, x)
|
| 85 |
+
# print(f"🔹 Prediction probability = {prob:.4f}")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/utils.py
CHANGED
|
@@ -4,11 +4,9 @@ import os
|
|
| 4 |
import csv
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
-
def save_uploaded_file(file):
|
| 8 |
# Generate date-related ID
|
| 9 |
image_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 10 |
-
save_dir = "uploads"
|
| 11 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 12 |
saved_path = os.path.join(save_dir, f"{image_id}.jpg")
|
| 13 |
|
| 14 |
# Save file
|
|
@@ -39,12 +37,12 @@ def preprocess_image(image_path):
|
|
| 39 |
# print(f"Preprocessed output shape: {input_tensor.shape}") # Print shape
|
| 40 |
return input_tensor
|
| 41 |
|
| 42 |
-
def append_to_csv(csv_path, image_id, prediction, probability):
|
| 43 |
# Ensure CSV file exists
|
| 44 |
os.makedirs(os.path.dirname(csv_path), exist_ok=True)
|
| 45 |
file_exists = os.path.isfile(csv_path)
|
| 46 |
with open(csv_path, mode="a", newline="") as f:
|
| 47 |
writer = csv.writer(f)
|
| 48 |
if not file_exists:
|
| 49 |
-
writer.writerow(["ID", "Prediction", "Probability"])
|
| 50 |
-
writer.writerow([image_id, prediction, probability])
|
|
|
|
| 4 |
import csv
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
def save_uploaded_file(save_dir, file):
|
| 8 |
# Generate date-related ID
|
| 9 |
image_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
|
|
| 10 |
saved_path = os.path.join(save_dir, f"{image_id}.jpg")
|
| 11 |
|
| 12 |
# Save file
|
|
|
|
| 37 |
# print(f"Preprocessed output shape: {input_tensor.shape}") # Print shape
|
| 38 |
return input_tensor
|
| 39 |
|
| 40 |
+
def append_to_csv(csv_path, image_id, prediction, probability, similarity):
|
| 41 |
# Ensure CSV file exists
|
| 42 |
os.makedirs(os.path.dirname(csv_path), exist_ok=True)
|
| 43 |
file_exists = os.path.isfile(csv_path)
|
| 44 |
with open(csv_path, mode="a", newline="") as f:
|
| 45 |
writer = csv.writer(f)
|
| 46 |
if not file_exists:
|
| 47 |
+
writer.writerow(["ID", "Prediction", "Probability", "Similarity"])
|
| 48 |
+
writer.writerow([image_id, prediction, probability, round(float(similarity), 3)])
|
database/inference_results.csv
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
ID,Prediction,Probability
|
| 2 |
-
20250106_165619,virus,0.87
|
| 3 |
-
20250106_165637,bacteria,0.71
|
|
|
|
|
|
|
|
|
|
|
|
flow.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
api/scripts-->install docker sh
|
logs/app.log
DELETED
|
@@ -1,32 +0,0 @@
|
|
| 1 |
-
2025-01-06 16:34:05,957 [INFO] �Ϥ��w���\�W�ǨëO�s: uploads\20250106_163405.jpg�AID: 20250106_163405
|
| 2 |
-
2025-01-06 16:34:06,065 [INFO] �Ϥ��w�O�s: uploads\20250106_163406.jpg�AID: 20250106_163406
|
| 3 |
-
2025-01-06 16:35:28,323 [INFO] �Ϥ��w���\�W�ǨëO�s: uploads\20250106_163528.jpg�AID: 20250106_163528
|
| 4 |
-
2025-01-06 16:35:28,434 [INFO] �Ϥ��w�O�s: uploads\20250106_163528.jpg�AID: 20250106_163528
|
| 5 |
-
2025-01-06 16:38:23,977 [INFO] �Ϥ��w���\�W�ǨëO�s: uploads\20250106_163823.jpg�AID: 20250106_163823
|
| 6 |
-
2025-01-06 16:38:24,082 [INFO] �Ϥ��w�O�s: uploads\20250106_163824.jpg�AID: 20250106_163824
|
| 7 |
-
2025-01-06 16:38:24,547 [INFO] ���z���G - ID: 20250106_163824, ���O: virus, ���v: 0.13
|
| 8 |
-
2025-01-06 16:39:09,716 [INFO] �Ϥ��w���\�W�ǨëO�s: uploads\20250106_163909.jpg�AID: 20250106_163909
|
| 9 |
-
2025-01-06 16:39:09,816 [INFO] �Ϥ��w�O�s: uploads\20250106_163909.jpg�AID: 20250106_163909
|
| 10 |
-
2025-01-06 16:39:10,228 [INFO] ���z���G - ID: 20250106_163909, ���O: virus, ���v: 0.13
|
| 11 |
-
2025-01-06 16:40:12,940 [INFO] �Ϥ��w���\�W�ǨëO�s: uploads\20250106_164012.jpg�AID: 20250106_164012
|
| 12 |
-
2025-01-06 16:40:13,042 [INFO] �Ϥ��w�O�s: uploads\20250106_164013.jpg�AID: 20250106_164013
|
| 13 |
-
2025-01-06 16:40:13,454 [INFO] ���z���G - ID: 20250106_164013, ���O: virus, ���v: 0.13
|
| 14 |
-
2025-01-06 16:43:23,908 [INFO] �Ϥ��w�O�s: uploads\20250106_164323.jpg�AID: 20250106_164323
|
| 15 |
-
2025-01-06 16:43:24,328 [INFO] ���z���G - ID: 20250106_164323, ���O: virus, ���v: 0.13
|
| 16 |
-
2025-01-06 16:45:27,637 [INFO] �Ϥ��w�O�s: uploads\20250106_164527.jpg�AID: 20250106_164527
|
| 17 |
-
2025-01-06 16:45:28,047 [INFO] ���z���G - ID: 20250106_164527, ���O: virus, ���v: 0.13
|
| 18 |
-
2025-01-06 16:53:23,291 [INFO] Image saved: uploads\20250106_165323.jpg, ID: 20250106_165323
|
| 19 |
-
2025-01-06 16:53:23,703 [ERROR] Model inference failed - ID: 20250106_165323, Error: cannot unpack non-iterable numpy.float32 object
|
| 20 |
-
2025-01-06 16:54:13,253 [INFO] Image saved: uploads\20250106_165413.jpg, ID: 20250106_165413
|
| 21 |
-
2025-01-06 16:54:13,668 [ERROR] Model inference failed - ID: 20250106_165413, Error: cannot unpack non-iterable numpy.float32 object
|
| 22 |
-
2025-01-06 16:54:56,279 [INFO] Image saved: uploads\20250106_165456.jpg, ID: 20250106_165456
|
| 23 |
-
2025-01-06 16:54:56,697 [ERROR] Model inference failed - ID: 20250106_165456, Error: cannot unpack non-iterable numpy.float32 object
|
| 24 |
-
2025-01-06 16:56:19,885 [INFO] Image saved: uploads\20250106_165619.jpg, ID: 20250106_165619
|
| 25 |
-
2025-01-06 16:56:20,317 [INFO] Inference result - ID: 20250106_165619, Class: virus, Probability: 0.87
|
| 26 |
-
2025-01-06 16:56:37,681 [INFO] Image saved: uploads\20250106_165637.jpg, ID: 20250106_165637
|
| 27 |
-
2025-01-06 16:56:38,098 [INFO] Inference result - ID: 20250106_165637, Class: bacteria, Probability: 0.71
|
| 28 |
-
2025-01-06 16:58:13,784 [INFO] Image saved: uploads\20250106_165813.jpg, ID: 20250106_165813
|
| 29 |
-
2025-01-06 16:58:13,805 [ERROR] Image preprocessing failed - ID: 20250106_165813, Error: OpenCV(4.10.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:196: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
|
| 30 |
-
|
| 31 |
-
2025-01-06 17:01:39,323 [INFO] Image saved: uploads\20250106_170139.jpg, ID: 20250106_170139
|
| 32 |
-
2025-01-06 17:01:39,325 [ERROR] Invalid image file - ID: 20250106_170139, Path: uploads\20250106_170139.jpg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource/avg12_embedding.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bba524df541d37371643b08268ab2eba883588d0874e93be12d26f150d5051b7
|
| 3 |
+
size 6272
|
resource/model_2c_v1_emb.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6bbd2a72a4ab227c919b46b74c71b985bf4e6fcd527314f50885c3e44d9ac923
|
| 3 |
+
size 42713538
|
scripts/deploy.sh
CHANGED
|
@@ -62,7 +62,14 @@ if ! docker compose down; then
|
|
| 62 |
exit 1
|
| 63 |
fi
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
echo "Failed to start new services, please check the logs."
|
| 67 |
exit 1
|
| 68 |
fi
|
|
|
|
| 62 |
exit 1
|
| 63 |
fi
|
| 64 |
|
| 65 |
+
# Rebuild images without cache
|
| 66 |
+
if ! docker compose build --no-cache; then
|
| 67 |
+
echo "Failed to build images without cache, please check the logs."
|
| 68 |
+
exit 1
|
| 69 |
+
fi
|
| 70 |
+
|
| 71 |
+
# Start services
|
| 72 |
+
if ! docker compose up -d; then
|
| 73 |
echo "Failed to start new services, please check the logs."
|
| 74 |
exit 1
|
| 75 |
fi
|
scripts/install_docker.sh
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. 更新套件清單
|
| 2 |
+
sudo apt-get update
|
| 3 |
+
|
| 4 |
+
# 2. 安裝必要的工具
|
| 5 |
+
sudo apt-get install -y \
|
| 6 |
+
ca-certificates \
|
| 7 |
+
curl \
|
| 8 |
+
gnupg \
|
| 9 |
+
lsb-release
|
| 10 |
+
|
| 11 |
+
# 3. 加入 Docker 官方 GPG key
|
| 12 |
+
sudo mkdir -p /etc/apt/keyrings
|
| 13 |
+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
|
| 14 |
+
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
| 15 |
+
|
| 16 |
+
# 4. 設定 Docker repo
|
| 17 |
+
echo \
|
| 18 |
+
"deb [arch=$(dpkg --print-architecture) \
|
| 19 |
+
signed-by=/etc/apt/keyrings/docker.gpg] \
|
| 20 |
+
https://download.docker.com/linux/ubuntu \
|
| 21 |
+
$(lsb_release -cs) stable" | \
|
| 22 |
+
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
| 23 |
+
|
| 24 |
+
# 5. 再次更新套件清單
|
| 25 |
+
sudo apt-get update
|
| 26 |
+
|
| 27 |
+
# 6. 安裝 Docker engine
|
| 28 |
+
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
start_services.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import uvicorn
|
| 2 |
|
| 3 |
if __name__ == "__main__":
|
| 4 |
-
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
|
| 3 |
if __name__ == "__main__":
|
| 4 |
+
# uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
|
| 5 |
+
uvicorn.run("app.main:app", host="0.0.0.0", port=8000)
|
test/data/ec.JPG
CHANGED
|
|
Git LFS Details
|
|
|
Git LFS Details
|
test/data/nr1.png
ADDED
|
Git LFS Details
|
test/data/nr2.jpg
ADDED
|
Git LFS Details
|
test/data/nr3.jpg
ADDED
|
Git LFS Details
|
test/data/nr4.jpg
ADDED
|
Git LFS Details
|
test/data/ped.JPG
CHANGED
|
|
Git LFS Details
|
|
|
Git LFS Details
|
test/data/rw.jpg
ADDED
|
Git LFS Details
|
test/test.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
import requests
|
|
|
|
| 2 |
|
| 3 |
# define API URL
|
| 4 |
-
BASE_URL = "http://
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# inference
|
| 7 |
def test_predict(image_path):
|
| 8 |
url = f"{BASE_URL}/predict/"
|
|
|
|
| 9 |
with open(image_path, "rb") as file:
|
| 10 |
files = {"file": file}
|
| 11 |
response = requests.post(url, files=files)
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Print the response status code and content
|
| 14 |
print("Response status code:", response.status_code)
|
|
@@ -24,6 +31,7 @@ def test_predict(image_path):
|
|
| 24 |
|
| 25 |
# testing
|
| 26 |
if __name__ == "__main__":
|
| 27 |
-
IMAGE_PATH =
|
|
|
|
| 28 |
|
| 29 |
test_predict(IMAGE_PATH)
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
# define API URL
|
| 5 |
+
# BASE_URL = "http://104.199.172.213:8000"
|
| 6 |
+
BASE_URL = "http://35.185.152.182:8000"
|
| 7 |
+
|
| 8 |
+
# BASE_URL = "http://localhost:8000"
|
| 9 |
|
| 10 |
# inference
|
| 11 |
def test_predict(image_path):
|
| 12 |
url = f"{BASE_URL}/predict/"
|
| 13 |
+
start_time = time.time()
|
| 14 |
with open(image_path, "rb") as file:
|
| 15 |
files = {"file": file}
|
| 16 |
response = requests.post(url, files=files)
|
| 17 |
+
end_time = time.time()
|
| 18 |
+
print("Inference time:", end_time - start_time, "seconds")
|
| 19 |
|
| 20 |
# Print the response status code and content
|
| 21 |
print("Response status code:", response.status_code)
|
|
|
|
| 31 |
|
| 32 |
# testing
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
# IMAGE_PATH = "/mnt/d/pig/api/test/data/ec.jpg"
|
| 35 |
+
IMAGE_PATH = "/mnt/d/pig/api/test/data/nr4.jpg"
|
| 36 |
|
| 37 |
test_predict(IMAGE_PATH)
|
tips.txt
CHANGED
|
@@ -7,6 +7,10 @@ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o
|
|
| 7 |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
| 8 |
sudo apt update
|
| 9 |
sudo apt install -y docker-ce docker-ce-cli containerd.io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
docker --version
|
| 11 |
|
| 12 |
|
|
@@ -16,3 +20,7 @@ apt update
|
|
| 16 |
apt install -y net-tools
|
| 17 |
netstat -tuln | grep 8000
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
| 8 |
sudo apt update
|
| 9 |
sudo apt install -y docker-ce docker-ce-cli containerd.io
|
| 10 |
+
|
| 11 |
+
or
|
| 12 |
+
sudo apt install -y docker.io
|
| 13 |
+
|
| 14 |
docker --version
|
| 15 |
|
| 16 |
|
|
|
|
| 20 |
apt install -y net-tools
|
| 21 |
netstat -tuln | grep 8000
|
| 22 |
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
若 port沒通 可能要啟動ufw
|
| 26 |
+
要在跟目錄啟動 deploy.sh
|