Download app/model.py from CJR61/papi_cc: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/spaces/CJR61/papi_cc/resolve/a5ec00a6525a3ea20c112dfe4f265aa800b17ddf/app/model.py
- Command line
-
hf download hf://spaces/CJR61/papi_cc@a5ec00a6525a3ea20c112dfe4f265aa800b17ddf/app/model.py
-
curl -L -o model.py https://huggingface.co/spaces/CJR61/papi_cc/resolve/a5ec00a6525a3ea20c112dfe4f265aa800b17ddf/app/model.py
2.99 kB
| import onnxruntime as ort | |
| import numpy as np | |
| from numpy.linalg import norm | |
| def load_model(path): | |
| return ort.InferenceSession(path, providers=["CUDAExecutionProvider","CPUExecutionProvider"]) | |
| def cosine_similarity(a, b): | |
| a = a / (norm(a) + 1e-8) | |
| b = b / (norm(b) + 1e-8) | |
| return float(np.dot(a, b)) | |
| def predict_image(model, image, ref_emb, thres=0.55): | |
| """ | |
| ref_emb: 1-D (single reference) or 2-D (k cluster centers, shape [k, dim]). | |
| When 2-D, sim = max cosine similarity to any cluster center. | |
| Returns (is_in_criteria: bool, prob: float, sim: float). | |
| """ | |
| inp = model.get_inputs()[0].name | |
| out_map = {o.name: i for i, o in enumerate(model.get_outputs())} | |
| outs = model.run(None, {inp: image}) | |
| missing = [k for k in ("embedding", "probability") if k not in out_map] | |
| if missing: | |
| raise KeyError(f"Model output missing expected keys: {missing}. Found: {list(out_map.keys())}") | |
| emb = outs[out_map["embedding"]].squeeze().astype(np.float32) | |
| prob = float(outs[out_map["probability"]].squeeze()) | |
| # Multi-center: take max similarity to any cluster center | |
| if ref_emb.ndim == 2: | |
| sim = float(max(cosine_similarity(emb, c) for c in ref_emb)) | |
| else: | |
| sim = cosine_similarity(emb, ref_emb) | |
| return sim >= thres, prob, sim | |
| if __name__ == "__main__": | |
| from utils import preprocess_image | |
| MODEL_PATH = "/mnt/d/pig/api/resource/model_2c_v1_emb.onnx" | |
| REF_EMB_PATH = "/mnt/d/pig/api/resource/avg12_embedding.npy" | |
| IMAGE_PATH = "/mnt/d/pig/api/test/data/ec.jpg" | |
| THRES = 0.75 | |
| sess = load_model(MODEL_PATH) | |
| ref = np.load(REF_EMB_PATH).astype(np.float32) | |
| if ref.ndim > 1: | |
| ref = ref.mean(axis=0) | |
| x = preprocess_image(IMAGE_PATH) # [1,3,H,W] float32, ImageNet norm | |
| flag, prob, sim = predict_image(sess, x, ref, thres=THRES) | |
| print(f"🔹 Probability = {prob:.4f}") | |
| print(f"🔹 Similarity = {sim:.4f} (≥{THRES}? {flag})") | |
| ################################################################################# | |
| # import onnxruntime as ort | |
| # def load_model(MODEL_PATH): | |
| # return ort.InferenceSession(MODEL_PATH) | |
| # def predict_image(model, image): | |
| # input_name = model.get_inputs()[0].name | |
| # output_name = model.get_outputs()[0].name | |
| # outputs = model.run([output_name], {input_name: image}) | |
| # probability = outputs[0][0][0] | |
| # # prediction = "bacteria" if probability > 0.5 else "virus" | |
| # return probability | |
| # if __name__ == "__main__": | |
| # import os | |
| # from PIL import Image | |
| # import numpy as np | |
| # from utils import preprocess_image | |
| # MODEL_PATH = "/mnt/d/pig/api/resource/model_2c_v1_tp2.onnx" | |
| # IMAGE_DIR = "/mnt/d/pig/api/test/data/ec.jpg" | |
| # # 載入模型 | |
| # model = load_model(MODEL_PATH) | |
| # print("✅ Model loaded:", MODEL_PATH) | |
| # x = preprocess_image(IMAGE_DIR) | |
| # # 預測 | |
| # prob = predict_image(model, x) | |
| # print(f"🔹 Prediction probability = {prob:.4f}") | |