Spaces:
Sleeping
Sleeping
File size: 5,926 Bytes
619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 619672b b726d23 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import torch
import torch.nn as nn
import os
from typing import Tuple, Optional, Any
import warnings
def safe_load_model(model_path: str, device: torch.device, model_instance: nn.Module) -> Tuple[Optional[nn.Module], bool]:
"""
ุชุญู
ูู ุขู
ู ูููู
ูุฐุฌ ู
ุน ู
ุนุงูุฌุฉ ุงูุฃุฎุทุงุก
Args:
model_path: ู
ุณุงุฑ ู
ูู ุงููู
ูุฐุฌ
device: ุงูุฌูุงุฒ (CPU/GPU)
model_instance: instance ู
ู ุงููู
ูุฐุฌ
Returns:
tuple: (ุงููู
ูุฐุฌ ุงูู
ุญู
ูุ ูุฌุญ ุงูุชุญู
ูู ุฃู
ูุง)
"""
try:
# ุงูุชุญูู ู
ู ูุฌูุฏ ุงูู
ูู
if not os.path.exists(model_path):
print(f"โ ู
ูู ุงููู
ูุฐุฌ ุบูุฑ ู
ูุฌูุฏ: {model_path}")
print("๐ก ุชุฃูุฏ ู
ู ูุถุน ู
ูู best_model.pth ูู ููุณ ู
ุฌูุฏ ุงูุชุทุจูู")
return None, False
print(f"๐ ุฌุงุฑู ุชุญู
ูู ุงููู
ูุฐุฌ ู
ู: {model_path}")
# ุชุญู
ูู ุงููู
ูุฐุฌ
checkpoint = torch.load(model_path, map_location=device)
# ุงูุชุญูู ู
ู ููุน checkpoint
if isinstance(checkpoint, dict):
# ุฅุฐุง ูุงู ุงููู
ูุฐุฌ ู
ุญููุธ ูู state_dict
if 'model_state_dict' in checkpoint:
model_instance.load_state_dict(checkpoint['model_state_dict'])
print("โ
ุชู
ุชุญู
ูู state_dict ู
ู checkpoint")
elif 'state_dict' in checkpoint:
model_instance.load_state_dict(checkpoint['state_dict'])
print("โ
ุชู
ุชุญู
ูู state_dict")
else:
# ู
ุญุงููุฉ ุชุญู
ูู dict ู
ุจุงุดุฑุฉ
model_instance.load_state_dict(checkpoint)
print("โ
ุชู
ุชุญู
ูู ุงููู
ูุฐุฌ ูู state_dict")
else:
# ุฅุฐุง ูุงู ุงููู
ูุฐุฌ ู
ุญููุธ ูู full model
model_instance = checkpoint
print("โ
ุชู
ุชุญู
ูู ุงููู
ูุฐุฌ ุงููุงู
ู")
# ููู ุฅูู ุงูุฌูุงุฒ ุงูู
ูุงุณุจ
model_instance = model_instance.to(device)
model_instance.eval() # ูุถุน ุงูุชูููู
print(f"โ
ุชู
ุชุญู
ูู ุงููู
ูุฐุฌ ุจูุฌุงุญ ุนูู {device}")
return model_instance, True
except FileNotFoundError:
print(f"โ ู
ูู ุงููู
ูุฐุฌ ุบูุฑ ู
ูุฌูุฏ: {model_path}")
return None, False
except RuntimeError as e:
print(f"โ ุฎุทุฃ ูู ุชุญู
ูู ุงููู
ูุฐุฌ: {e}")
print("๐ก ุชุฃูุฏ ู
ู ุฃู ุจููุฉ ุงููู
ูุฐุฌ ู
ุชุทุงุจูุฉ ู
ุน ุงููู
ูุฐุฌ ุงูู
ุญููุธ")
return None, False
except Exception as e:
print(f"โ ุฎุทุฃ ุบูุฑ ู
ุชููุน ูู ุชุญู
ูู ุงููู
ูุฐุฌ: {e}")
return None, False
def validate_model_architecture(model: nn.Module, expected_input_size: int = 193) -> bool:
"""
ุงูุชุญูู ู
ู ุตุญุฉ ุจููุฉ ุงููู
ูุฐุฌ
Args:
model: ุงููู
ูุฐุฌ ุงูู
ุญู
ู
expected_input_size: ุญุฌู
ุงูุฅุฏุฎุงู ุงูู
ุชููุน
Returns:
bool: ุตุญูุญ ุฅุฐุง ูุงูุช ุงูุจููุฉ ุตุญูุญุฉ
"""
try:
# ุฅูุดุงุก tensor ุชุฌุฑูุจู
dummy_input = torch.randn(1, expected_input_size)
# ุชุฌุฑุจุฉ forward pass
with torch.no_grad():
output = model(dummy_input)
print(f"โ
ุจููุฉ ุงููู
ูุฐุฌ ุตุญูุญุฉ - ุงูุฅุฏุฎุงู: {expected_input_size}, ุงูุฅุฎุฑุงุฌ: {output.shape}")
return True
except Exception as e:
print(f"โ ุจููุฉ ุงููู
ูุฐุฌ ุบูุฑ ุตุญูุญุฉ: {e}")
return False
def create_dummy_model(num_classes: int = 8) -> nn.Module:
"""
ุฅูุดุงุก ูู
ูุฐุฌ ููู
ู ููุงุฎุชุจุงุฑ ุนูุฏ ุนุฏู
ูุฌูุฏ ุงููู
ูุฐุฌ ุงูุฃุตูู
Args:
num_classes: ุนุฏุฏ ุงููุฆุงุช
Returns:
ูู
ูุฐุฌ ููู
ู
"""
print("โ ๏ธ ุฅูุดุงุก ูู
ูุฐุฌ ููู
ู ููุงุฎุชุจุงุฑ...")
class DummyEmotionNet(nn.Module):
def __init__(self, num_classes=8):
super(DummyEmotionNet, self).__init__()
self.fc = nn.Linear(193, num_classes) # 193 ูู ุญุฌู
ุงูู
ูุฒุงุช ุงูู
ุณุชุฎุฑุฌุฉ
def forward(self, x):
return self.fc(x)
model = DummyEmotionNet(num_classes)
print("โ
ุชู
ุฅูุดุงุก ุงููู
ูุฐุฌ ุงูููู
ู")
return model
def check_model_file(model_path: str = 'best_model.pth'):
"""
ูุญุต ู
ูู ุงููู
ูุฐุฌ ูุฅุนุทุงุก ู
ุนููู
ุงุช ุนูู
Args:
model_path: ู
ุณุงุฑ ู
ูู ุงููู
ูุฐุฌ
"""
print(f"๐ ูุญุต ู
ูู ุงููู
ูุฐุฌ: {model_path}")
if not os.path.exists(model_path):
print("โ ู
ูู ุงููู
ูุฐุฌ ุบูุฑ ู
ูุฌูุฏ!")
print("๐ก ุชุฃูุฏ ู
ู:")
print(" 1. ูุถุน ู
ูู best_model.pth ูู ููุณ ู
ุฌูุฏ ุงูุชุทุจูู")
print(" 2. ุฃู ุงุณู
ุงูู
ูู ุตุญูุญ")
print(" 3. ุฃู ุงูู
ูู ุบูุฑ ุชุงูู")
return False
# ู
ุนููู
ุงุช ุงูู
ูู
file_size = os.path.getsize(model_path)
print(f"๐ ุญุฌู
ุงูู
ูู: {file_size / (1024*1024):.2f} MB")
# ู
ุญุงููุฉ ูุฑุงุกุฉ ุงูู
ูู
try:
checkpoint = torch.load(model_path, map_location='cpu')
print("โ
ูู
ูู ูุฑุงุกุฉ ุงูู
ูู")
if isinstance(checkpoint, dict):
print("๐ ู
ุญุชููุงุช ุงูู
ูู:")
for key in checkpoint.keys():
print(f" - {key}")
return True
except Exception as e:
print(f"โ ุฎุทุฃ ูู ูุฑุงุกุฉ ุงูู
ูู: {e}")
return False
if __name__ == "__main__":
# ุงุฎุชุจุงุฑ ุงููุธุงู
print("๐งช ุงุฎุชุจุงุฑ ูุธุงู
ุชุญู
ูู ุงููู
ูุฐุฌ...")
check_model_file() |