PinHsuan commited on
Commit
f59fb67
·
verified ·
1 Parent(s): ed6b96b

Update tongue_model.py

Browse files
Files changed (1) hide show
  1. tongue_model.py +8 -3
tongue_model.py CHANGED
@@ -6,7 +6,6 @@ import numpy as np
6
  from PIL import Image
7
  from torchvision import models, transforms
8
 
9
- # --- 模型架構定義 ---
10
  class CBAM(nn.Module):
11
  def __init__(self, channels, reduction=16):
12
  super(CBAM, self).__init__()
@@ -51,13 +50,19 @@ class TongueArcResNet(nn.Module):
51
  features = self.avgpool(x).flatten(1)
52
  return self.arcface(features)
53
 
54
- # --- 2. 定義預處理與推論類別 ---
55
  class TongueModelWrapper:
56
  def __init__(self, model_path, num_classes=3):
57
  self.device = torch.device("cpu")
58
  self.model = TongueArcResNet(num_classes=num_classes)
59
  torch.serialization.add_safe_globals([np._core.multiarray.scalar])
60
- self.model.load_state_dict(torch.load(model_path, map_location=self.device, weights_only=False))
 
 
 
 
 
 
 
61
  self.model.eval()
62
  self.transform = transforms.Compose([
63
  transforms.ToTensor(),
 
6
  from PIL import Image
7
  from torchvision import models, transforms
8
 
 
9
  class CBAM(nn.Module):
10
  def __init__(self, channels, reduction=16):
11
  super(CBAM, self).__init__()
 
50
  features = self.avgpool(x).flatten(1)
51
  return self.arcface(features)
52
 
 
53
  class TongueModelWrapper:
54
  def __init__(self, model_path, num_classes=3):
55
  self.device = torch.device("cpu")
56
  self.model = TongueArcResNet(num_classes=num_classes)
57
  torch.serialization.add_safe_globals([np._core.multiarray.scalar])
58
+ checkpoint = torch.load(model_path, map_location=self.device, weights_only=False)
59
+
60
+ if isinstance(checkpoint, dict) and 'model_state' in checkpoint:
61
+ self.model.load_state_dict(checkpoint['model_state'])
62
+ print(f"成功載入權重!模型訓練指標:AUC={checkpoint.get('auc', 0):.4f}")
63
+ else:
64
+ self.model.load_state_dict(checkpoint)
65
+
66
  self.model.eval()
67
  self.transform = transforms.Compose([
68
  transforms.ToTensor(),