chore: backup before migration to new repo
This commit is contained in:
@@ -162,9 +162,13 @@ class SceneClassifier:
|
||||
model_path: Core ML 模型路徑 (可選)
|
||||
"""
|
||||
self.model_path = model_path
|
||||
self.places365_model_path = (
|
||||
"/Users/accusys/momentry/models/resnet18_places365.pth.tar"
|
||||
)
|
||||
self.model = None
|
||||
self.coreml_model = None
|
||||
self.transform = None
|
||||
self.model_type = "unknown"
|
||||
|
||||
# 圖像預處理
|
||||
self.transform = transforms.Compose(
|
||||
@@ -189,23 +193,57 @@ class SceneClassifier:
|
||||
try:
|
||||
print(f"[SCENE] Loading Core ML model: {self.model_path}")
|
||||
self.coreml_model = ct.models.MLModel(self.model_path)
|
||||
self.model_type = "coreml"
|
||||
print("[SCENE] Core ML model loaded successfully")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"[SCENE] Warning: Failed to load Core ML model: {e}")
|
||||
|
||||
# 備案:使用 PyTorch + ResNet
|
||||
# 備案:使用 PyTorch + Places365
|
||||
if HAS_TORCH:
|
||||
try:
|
||||
print(f"[SCENE] Loading PyTorch model on {DEVICE}")
|
||||
# 使用預訓練的 ResNet18
|
||||
self.model = models.resnet18(pretrained=True)
|
||||
|
||||
# 檢查 Places365 模型是否存在
|
||||
if Path(self.places365_model_path).exists():
|
||||
print(
|
||||
f"[SCENE] Loading Places365 model: {self.places365_model_path}"
|
||||
)
|
||||
checkpoint = torch.load(
|
||||
self.places365_model_path, map_location=DEVICE
|
||||
)
|
||||
|
||||
# 建立 ResNet18 模型 (Places365 有 365 個類別)
|
||||
self.model = models.resnet18(num_classes=365)
|
||||
|
||||
# 移除 'module.' prefix (DataParallel training)
|
||||
state_dict = checkpoint["state_dict"]
|
||||
new_state_dict = {}
|
||||
for k, v in state_dict.items():
|
||||
if k.startswith("module."):
|
||||
new_state_dict[k[7:]] = v
|
||||
else:
|
||||
new_state_dict[k] = v
|
||||
|
||||
self.model.load_state_dict(new_state_dict)
|
||||
self.model_type = "places365"
|
||||
print("[SCENE] Places365 model loaded successfully (365 classes)")
|
||||
else:
|
||||
print(
|
||||
f"[SCENE] Places365 model not found, using ImageNet pretrained"
|
||||
)
|
||||
self.model = models.resnet18(pretrained=True)
|
||||
self.model_type = "imagenet"
|
||||
|
||||
self.model.to(DEVICE)
|
||||
self.model.eval()
|
||||
print("[SCENE] PyTorch model loaded successfully")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"[SCENE] Warning: Failed to load PyTorch model: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
|
||||
print("[SCENE] Error: No model available")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user