Spaces:
Sleeping
Sleeping
feat: integrate ppnet inference backend
Browse files新增 PPNet baseline 推論流程並整合到 FastAPI 服務。
支援以設定切換 ppnet 與 resnet18 模型,並補上本地 inference 腳本與 README 說明。
Files changed (7)
- README.md+119-1
- app.py+5-2
- baseline_40_model.pt.tarAdded+3
- inference.pyAdded+46
- main.py+5-2
- model_service.py+103-18
- protopnet.pyAdded+315
9 unmodified lines101112139 unmodified linesshort_description: 成大資安計畫使用---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301319 unmodified linesshort_description: 成大資安計畫使用---# SecureMLAPI這個專案提供一個 FastAPI 服務,用來判斷圖片中是否有人。目前已整合兩種推論後端:- `ppnet_baseline`:使用 `people_detection_baseline/baseline_40_model.pt.tar`- `resnet18_presence`:使用 `best_global_model_presence.pt`預設模型是 `ppnet_baseline`。## 開發環境請使用 `uv` 安裝依賴與執行指令。```bashuv sync```## 啟動服務```bashuv run uvicorn app:app --host 0.0.0.0 --port 8000 --reload```啟動後可使用以下路徑:- `/docs`:Swagger UI- `/health`:健康檢查- `/predict`:上傳圖片並取得 JSON 推論結果- `/demo`:簡易網頁測試介面## 切換模型目前不需要從 HTML 介面切換模型,直接用程式設定即可。### 方式一:用環境變數切換```bashSECUREML_MODEL=ppnet_baseline uv run uvicorn app:app --host 0.0.0.0 --port 8000``````bashSECUREML_MODEL=resnet18_presence uv run uvicorn app:app --host 0.0.0.0 --port 8000```### 方式二:修改預設值可直接修改 `model_service.py` 裡的:```pythonDEFAULT_MODEL_NAME = os.getenv("SECUREML_MODEL", "ppnet_baseline")```以及 `MODEL_CONFIGS` 中對應模型的設定。## 本地推論專案提供 `inference.py`,可直接對單張圖片做推論:```bashuv run python inference.py --image person.jpg```指定模型:```bashuv run python inference.py --image person.jpg --model ppnet_baseline``````bashuv run python inference.py --image person.jpg --model resnet18_presence```## API 使用方式使用 `curl` 呼叫 `/predict`:```bashcurl -X POST \-F "file=@person.jpg" \http://127.0.0.1:8000/predict```回傳格式範例:```json{"label": "person","prediction_index": 1,"probabilities": {"no_person": 0.0,"person": 1.0},"model_name": "ppnet_baseline","model_backend": "ppnet","model_path": "baseline_40_model.pt.tar","filename": "person.jpg","content_type": "image/jpeg"}```## 目前模型設定位置模型切換與設定集中在 `model_service.py`:- `MODEL_CONFIGS`:定義可用模型- `DEFAULT_MODEL_NAME`:定義預設模型- `get_model_service()`:建立對應推論服務如果之後要新增模型,建議直接在 `MODEL_CONFIGS` 增加一筆設定,並在 `_load_model()` 補上對應後端載入方式。## 驗證可先做基本語法檢查:```bashuv run python -m py_compile app.py main.py inference.py model_service.py protopnet.py```
7 unmodified lines89101112131415161749 unmodified lines676869707172737 unmodified linesfrom fastapi.templating import Jinja2Templatesfrom PIL import Image, UnidentifiedImageErrorfrom model_service import MODEL_PATH, get_model_serviceBASE_DIR = Path(__file__).resolve().parenttemplates = Jinja2Templates(directory=str(BASE_DIR / "templates"))@asynccontextmanager49 unmodified linesreturn {"message": "Presence Detection API","docs": "/docs","model_path": str(MODEL_PATH.name),}7 unmodified lines8910111213141516171849 unmodified lines6869707172737475767 unmodified linesfrom fastapi.templating import Jinja2Templatesfrom PIL import Image, UnidentifiedImageErrorfrom model_service import get_model_config, get_model_serviceBASE_DIR = Path(__file__).resolve().parenttemplates = Jinja2Templates(directory=str(BASE_DIR / "templates"))ACTIVE_MODEL_CONFIG = get_model_config()@asynccontextmanager49 unmodified linesreturn {"message": "Presence Detection API","docs": "/docs","model_name": ACTIVE_MODEL_CONFIG.name,"model_backend": ACTIVE_MODEL_CONFIG.backend,"model_path": str(ACTIVE_MODEL_CONFIG.model_path.name),}
123version https://git-lfs.github.com/spec/v1oid sha256:555c304d21f6db8d41b53ff06b7c9bd9a7fe78a104b3ae69150cc0061532d94asize 8048503012345678910111213141516171819202122232425262728293031323334353637383940414243444546from __future__ import annotations
import argparsefrom pathlib import Path
from PIL import Image
from model_service import get_model_config, get_model_service
def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="Run local inference with the configured model.") parser.add_argument("--image", type=Path, default=Path("person.jpg"), help="Input image path.") parser.add_argument( "--model", type=str, default=None, help="Optional model name override. Defaults to SECUREML_MODEL or the project default.", ) return parser
def main() -> None: args = build_parser().parse_args() if not args.image.exists(): raise SystemExit(f"Image not found: {args.image}")
config = get_model_config(args.model) service = get_model_service(args.model) image = Image.open(args.image).convert("RGB") result = service.predict_image(image)
print(f"[INFO] device={service.device}") print(f"[INFO] model_name={config.name}") print(f"[INFO] model_backend={config.backend}") print(f"[INFO] model_path={config.model_path}") print(f"[INFO] image={args.image}") print("========== RESULT ==========") print(f"prediction: {result['prediction_index']} ({result['label']})") for label, prob in result["probabilities"].items(): print(f"P({label}) = {prob:.6f}") print("============================")
if __name__ == "__main__": main()12345675 unmodified lines1314151617181920from pathlib import Pathfrom PIL import Imagefrom model_service import MODEL_PATH, get_model_serviceIMAGE_PATH = Path("person.jpg")5 unmodified linesraise SystemExit(f"Image not found: {IMAGE_PATH}")service = get_model_service()print(f"[INFO] device={service.device}")print(f"[INFO] model={MODEL_PATH}")print(f"[INFO] image={IMAGE_PATH}")img = Image.open(IMAGE_PATH).convert("RGB")12345675 unmodified lines1314151617181920212223from pathlib import Pathfrom PIL import Imagefrom model_service import get_model_config, get_model_serviceIMAGE_PATH = Path("person.jpg")5 unmodified linesraise SystemExit(f"Image not found: {IMAGE_PATH}")service = get_model_service()config = get_model_config()print(f"[INFO] device={service.device}")print(f"[INFO] model_name={config.name}")print(f"[INFO] model_backend={config.backend}")print(f"[INFO] model_path={config.model_path}")print(f"[INFO] image={IMAGE_PATH}")img = Image.open(IMAGE_PATH).convert("RGB")
123451 unmodified line789101112131415161718192021222324252627282930313233343536373839404142434445464748495051524 unmodified lines575859606162636465from functools import lru_cachefrom pathlib import Pathimport torchimport torch.nn as nn1 unmodified linefrom PIL import Imagefrom torchvision import modelsBASE_DIR = Path(__file__).resolve().parentMODEL_PATH = BASE_DIR / "best_global_model_presence.pt"CLASS_NAMES = ["no_person", "person"]def build_resnet18(num_classes: int = 2) -> nn.Module:# We load task-specific weights from `best_global_model_presence.pt`, so no# pretrained backbone download is needed at runtime.model = models.resnet18(weights=None)in_features = model.fc.in_featuresmodel.fc = nn.Linear(in_features, num_classes)return modelclass PresenceModelService:def __init__(self, model_path: Path):if not model_path.exists():raise FileNotFoundError(f"Model not found: {model_path}")self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")self.model = build_resnet18(num_classes=2).to(self.device)state = torch.load(model_path, map_location="cpu")self.model.load_state_dict(state, strict=True)self.model.eval()self.transform = T.Compose([T.Resize((224, 224)),T.ToTensor(),T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])def predict_image(self, image: Image.Image) -> dict:x = self.transform(image).unsqueeze(0).to(self.device)with torch.no_grad():logits = self.model(x)probs = torch.softmax(logits, dim=-1)[0]pred_idx = int(torch.argmax(probs).item())4 unmodified lines"label": CLASS_NAMES[pred_idx],"prediction_index": pred_idx,"probabilities": probabilities,}@lru_cache(maxsize=1)def get_model_service() -> PresenceModelService:return PresenceModelService(MODEL_PATH)123456789101 unmodified line121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331344 unmodified lines139140141142143144145146147148149150from __future__ import annotationsimport osfrom dataclasses import dataclassfrom functools import lru_cachefrom pathlib import Pathfrom typing import Anyimport torchimport torch.nn as nn1 unmodified linefrom PIL import Imagefrom torchvision import modelsfrom protopnet import build_ppnetBASE_DIR = Path(__file__).resolve().parentCLASS_NAMES = ["no_person", "person"]@dataclass(frozen=True)class ModelConfig:name: strbackend: strmodel_path: Pathimage_size: intnormalize_mean: tuple[float, float, float]normalize_std: tuple[float, float, float]MODEL_CONFIGS: dict[str, ModelConfig] = {"resnet18_presence": ModelConfig(name="resnet18_presence",backend="resnet18",model_path=BASE_DIR / "best_global_model_presence.pt",image_size=224,normalize_mean=(0.485, 0.456, 0.406),normalize_std=(0.229, 0.224, 0.225),),"ppnet_baseline": ModelConfig(name="ppnet_baseline",backend="ppnet",model_path=BASE_DIR / "baseline_40_model.pt.tar",image_size=128,normalize_mean=(0.4914, 0.4822, 0.4465),normalize_std=(0.2023, 0.1994, 0.2010),),}DEFAULT_MODEL_NAME = os.getenv("SECUREML_MODEL", "ppnet_baseline")def build_resnet18(num_classes: int = 2) -> nn.Module:model = models.resnet18(weights=None)in_features = model.fc.in_featuresmodel.fc = nn.Linear(in_features, num_classes)return modeldef _normalize_prototype_shape(raw_value: Any) -> tuple[int, int, int, int]:if isinstance(raw_value, tuple):return raw_valueif isinstance(raw_value, list):return tuple(raw_value)raise ValueError(f"Unsupported prototype_shape value: {raw_value!r}")def get_model_config(name: str | None = None) -> ModelConfig:model_name = name or DEFAULT_MODEL_NAMEtry:return MODEL_CONFIGS[model_name]except KeyError as exc:available = ", ".join(sorted(MODEL_CONFIGS))raise ValueError(f"Unknown model '{model_name}'. Available: {available}") from excclass PresenceModelService:def __init__(self, config: ModelConfig):if not config.model_path.exists():raise FileNotFoundError(f"Model not found: {config.model_path}")self.config = configself.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")self.model = self._load_model().to(self.device)self.model.eval()self.transform = T.Compose([T.Resize((config.image_size, config.image_size)),T.ToTensor(),T.Normalize(config.normalize_mean, config.normalize_std),])def _load_model(self) -> nn.Module:if self.config.backend == "resnet18":model = build_resnet18(num_classes=len(CLASS_NAMES))state = torch.load(self.config.model_path, map_location="cpu")model.load_state_dict(state, strict=True)return modelif self.config.backend == "ppnet":checkpoint = torch.load(self.config.model_path, map_location="cpu")state_dict = checkpoint.get("state_dict")if not isinstance(state_dict, dict):raise ValueError("Invalid PPNet checkpoint: missing state_dict.")params = checkpoint.get("params_dict", {})model = build_ppnet(base_architecture=str(params.get("base_architecture", "vgg19")),img_size=int(params.get("img_size", self.config.image_size)),prototype_shape=_normalize_prototype_shape(params.get("prototype_shape", (40, 128, 1, 1))),num_classes=int(params.get("num_classes", len(CLASS_NAMES))),prototype_activation_function=str(params.get("prototype_activation_function", "log")),add_on_layers_type=str(params.get("add_on_layers_type", "regular")),)model.load_state_dict(state_dict, strict=True)return modelraise ValueError(f"Unsupported backend: {self.config.backend}")def predict_image(self, image: Image.Image) -> dict[str, Any]:x = self.transform(image).unsqueeze(0).to(self.device)with torch.no_grad():outputs = self.model(x)logits = outputs[0] if isinstance(outputs, (tuple, list)) else outputsprobs = torch.softmax(logits, dim=-1)[0]pred_idx = int(torch.argmax(probs).item())4 unmodified lines"label": CLASS_NAMES[pred_idx],"prediction_index": pred_idx,"probabilities": probabilities,"model_name": self.config.name,"model_backend": self.config.backend,"model_path": self.config.model_path.name,}@lru_cache(maxsize=None)def get_model_service(model_name: str | None = None) -> PresenceModelService:return PresenceModelService(get_model_config(model_name))
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315from __future__ import annotations
import math
import torchimport torch.nn as nnimport torch.nn.functional as F
_VGG_CFGS = { "vgg11": [64, "M", 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"], "vgg13": [64, 64, "M", 128, 128, "M", 256, 256, "M", 512, 512, "M", 512, 512, "M"], "vgg16": [64, 64, "M", 128, 128, "M", 256, 256, 256, "M", 512, 512, 512, "M", 512, 512, 512, "M"], "vgg19": [ 64, 64, "M", 128, 128, "M", 256, 256, 256, 256, "M", 512, 512, 512, 512, "M", 512, 512, 512, 512, "M", ],}
class VGGFeatures(nn.Module): def __init__(self, cfg: list[int | str], batch_norm: bool = False, init_weights: bool = True): super().__init__() self.batch_norm = batch_norm self.kernel_sizes: list[int] = [] self.strides: list[int] = [] self.paddings: list[int] = [] self.features = self._make_layers(cfg, batch_norm)
if init_weights: self._initialize_weights()
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.features(x)
def _make_layers(self, cfg: list[int | str], batch_norm: bool) -> nn.Sequential: layers: list[nn.Module] = [] in_channels = 3 self.n_layers = 0
for item in cfg: if item == "M": layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) self.kernel_sizes.append(2) self.strides.append(2) self.paddings.append(0) continue
conv2d = nn.Conv2d(in_channels, item, kernel_size=3, padding=1) if batch_norm: layers.extend([conv2d, nn.BatchNorm2d(item), nn.ReLU(inplace=True)]) else: layers.extend([conv2d, nn.ReLU(inplace=True)])
self.n_layers += 1 self.kernel_sizes.append(3) self.strides.append(1) self.paddings.append(1) in_channels = item
return nn.Sequential(*layers)
def _initialize_weights(self) -> None: for module in self.modules(): if isinstance(module, nn.Conv2d): nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu") if module.bias is not None: nn.init.constant_(module.bias, 0) elif isinstance(module, nn.BatchNorm2d): nn.init.constant_(module.weight, 1) nn.init.constant_(module.bias, 0) elif isinstance(module, nn.Linear): nn.init.normal_(module.weight, 0, 0.01) nn.init.constant_(module.bias, 0)
def conv_info(self) -> tuple[list[int], list[int], list[int]]: return self.kernel_sizes, self.strides, self.paddings
def __repr__(self) -> str: return f"VGG{self.n_layers + 3}, batch_norm={self.batch_norm}"
def build_vgg_features(name: str) -> VGGFeatures: if name not in _VGG_CFGS: raise ValueError(f"Unsupported VGG architecture: {name}") return VGGFeatures(_VGG_CFGS[name], batch_norm=name.endswith("_bn"))
def compute_layer_rf_info( layer_filter_size: int, layer_stride: int, layer_padding: int | str, previous_layer_rf_info: list[float],) -> list[float]: n_in, j_in, r_in, start_in = previous_layer_rf_info
if layer_padding == "SAME": n_out = math.ceil(float(n_in) / float(layer_stride)) if n_in % layer_stride == 0: pad = max(layer_filter_size - layer_stride, 0) else: pad = max(layer_filter_size - (n_in % layer_stride), 0) elif layer_padding == "VALID": n_out = math.ceil(float(n_in - layer_filter_size + 1) / float(layer_stride)) pad = 0 else: pad = layer_padding * 2 n_out = math.floor((n_in - layer_filter_size + pad) / layer_stride) + 1
pad_left = math.floor(pad / 2) j_out = j_in * layer_stride r_out = r_in + (layer_filter_size - 1) * j_in start_out = start_in + ((layer_filter_size - 1) / 2 - pad_left) * j_in return [n_out, j_out, r_out, start_out]
def compute_proto_layer_rf_info_v2( img_size: int, layer_filter_sizes: list[int], layer_strides: list[int], layer_paddings: list[int], prototype_kernel_size: int,) -> list[float]: if not ( len(layer_filter_sizes) == len(layer_strides) == len(layer_paddings) ): raise ValueError("Layer metadata length mismatch.")
rf_info: list[float] = [img_size, 1, 1, 0.5] for filter_size, stride_size, padding_size in zip( layer_filter_sizes, layer_strides, layer_paddings, strict=True ): rf_info = compute_layer_rf_info( layer_filter_size=int(filter_size), layer_stride=stride_size, layer_padding=padding_size, previous_layer_rf_info=rf_info, )
return compute_layer_rf_info( layer_filter_size=prototype_kernel_size, layer_stride=1, layer_padding="VALID", previous_layer_rf_info=rf_info, )
class PPNet(nn.Module): def __init__( self, features: nn.Module, img_size: int, prototype_shape: tuple[int, int, int, int], proto_layer_rf_info: list[float], num_classes: int, init_weights: bool = True, prototype_activation_function: str = "log", add_on_layers_type: str = "bottleneck", ): super().__init__() self.img_size = img_size self.prototype_shape = prototype_shape self.num_prototypes = prototype_shape[0] self.num_classes = num_classes self.epsilon = 1e-4 self.prototype_activation_function = prototype_activation_function self.proto_layer_rf_info = proto_layer_rf_info self.features = features
if self.num_prototypes % self.num_classes != 0: raise ValueError("Number of prototypes must be divisible by num_classes.")
self.prototype_class_identity = torch.zeros(self.num_prototypes, self.num_classes) num_prototypes_per_class = self.num_prototypes // self.num_classes for idx in range(self.num_prototypes): self.prototype_class_identity[idx, idx // num_prototypes_per_class] = 1
features_name = str(self.features).upper() if features_name.startswith("VGG") or features_name.startswith("RES"): in_channels = [m for m in features.modules() if isinstance(m, nn.Conv2d)][-1].out_channels elif features_name.startswith("DENSE"): in_channels = [m for m in features.modules() if isinstance(m, nn.BatchNorm2d)][-1].num_features else: raise ValueError("Unsupported base architecture.")
if add_on_layers_type == "bottleneck": add_on_layers: list[nn.Module] = [] current_in_channels = in_channels while current_in_channels > self.prototype_shape[1] or not add_on_layers: current_out_channels = max(self.prototype_shape[1], current_in_channels // 2) add_on_layers.append( nn.Conv2d(current_in_channels, current_out_channels, kernel_size=1) ) add_on_layers.append(nn.ReLU()) add_on_layers.append( nn.Conv2d(current_out_channels, current_out_channels, kernel_size=1) ) if current_out_channels > self.prototype_shape[1]: add_on_layers.append(nn.ReLU()) else: add_on_layers.append(nn.Sigmoid()) current_in_channels //= 2 self.add_on_layers = nn.Sequential(*add_on_layers) else: self.add_on_layers = nn.Sequential( nn.Conv2d(in_channels, self.prototype_shape[1], kernel_size=1), nn.ReLU(), nn.Conv2d(self.prototype_shape[1], self.prototype_shape[1], kernel_size=1), nn.Sigmoid(), )
self.prototype_vectors = nn.Parameter(torch.rand(self.prototype_shape), requires_grad=True) self.ones = nn.Parameter(torch.ones(self.prototype_shape), requires_grad=False) self.last_layer = nn.Linear(self.num_prototypes, self.num_classes, bias=False)
if init_weights: self._initialize_weights()
def conv_features(self, x: torch.Tensor) -> torch.Tensor: return self.add_on_layers(self.features(x))
def _l2_convolution(self, x: torch.Tensor) -> torch.Tensor: x2_patch_sum = F.conv2d(input=x**2, weight=self.ones) p2 = torch.sum(self.prototype_vectors**2, dim=(1, 2, 3)).view(-1, 1, 1) xp = F.conv2d(input=x, weight=self.prototype_vectors) distances = F.relu(x2_patch_sum - 2 * xp + p2) return distances
def prototype_distances(self, x: torch.Tensor) -> torch.Tensor: return self._l2_convolution(self.conv_features(x))
def distance_2_similarity(self, distances: torch.Tensor) -> torch.Tensor: if self.prototype_activation_function == "log": return torch.log((distances + 1) / (distances + self.epsilon)) if self.prototype_activation_function == "linear": return -distances raise ValueError( f"Unsupported prototype activation function: {self.prototype_activation_function}" )
def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: distances = self.prototype_distances(x) min_distances = -F.max_pool2d( -distances, kernel_size=(distances.size(2), distances.size(3)) ) min_distances = min_distances.view(-1, self.num_prototypes) prototype_activations = self.distance_2_similarity(min_distances) logits = self.last_layer(prototype_activations) return logits, min_distances
def set_last_layer_incorrect_connection(self, incorrect_strength: float) -> None: positive_locs = torch.t(self.prototype_class_identity) negative_locs = 1 - positive_locs self.last_layer.weight.data.copy_(positive_locs + incorrect_strength * negative_locs)
def _initialize_weights(self) -> None: for module in self.add_on_layers.modules(): if isinstance(module, nn.Conv2d): nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu") if module.bias is not None: nn.init.constant_(module.bias, 0) elif isinstance(module, nn.BatchNorm2d): nn.init.constant_(module.weight, 1) nn.init.constant_(module.bias, 0)
self.set_last_layer_incorrect_connection(incorrect_strength=-0.5)
def build_ppnet( *, base_architecture: str, img_size: int, prototype_shape: tuple[int, int, int, int], num_classes: int, prototype_activation_function: str, add_on_layers_type: str,) -> PPNet: features = build_vgg_features(base_architecture) layer_filter_sizes, layer_strides, layer_paddings = features.conv_info() proto_layer_rf_info = compute_proto_layer_rf_info_v2( img_size=img_size, layer_filter_sizes=layer_filter_sizes, layer_strides=layer_strides, layer_paddings=layer_paddings, prototype_kernel_size=prototype_shape[2], ) return PPNet( features=features, img_size=img_size, prototype_shape=prototype_shape, proto_layer_rf_info=proto_layer_rf_info, num_classes=num_classes, init_weights=True, prototype_activation_function=prototype_activation_function, add_on_layers_type=add_on_layers_type, )README.md+119-1
app.py+5-2
baseline_40_model.pt.tarAdded+3
inference.pyAdded+46
main.py+5-2
model_service.py+103-18
protopnet.pyAdded+315