-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_loader.py
More file actions
169 lines (135 loc) · 6.17 KB
/
Copy pathmodel_loader.py
File metadata and controls
169 lines (135 loc) · 6.17 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
"""
Robust Model Loader for PPE Detection
Handles model loading with fallbacks and error recovery
"""
import os
import torch
import logging
from pathlib import Path
from ultralytics import YOLO
import requests
import zipfile
logger = logging.getLogger(__name__)
class RobustModelLoader:
def __init__(self):
self.model_paths = [
# Improved models (highest priority)
'best.pt',
'model/PPE_DETECTION/runs/detect/*/weights/best.pt',
# Existing models
'weights/best.pt',
'model/PPE_DETECTION/Models/best_new.pt',
'model/PPE_DETECTION/Models/best.pt',
'best.pt',
# Fallback models
'yolov8n.pt',
'yolov8s.pt'
]
self.backup_urls = {
'yolov8n.pt': 'https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt',
'yolov8s.pt': 'https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s.pt'
}
def find_best_model(self):
"""Find the best available model"""
logger.info("🔍 Searching for best available model...")
for path_pattern in self.model_paths:
if '*' in path_pattern:
# Handle glob patterns
from glob import glob
matches = glob(path_pattern)
if matches:
# Use most recent model
best_match = max(matches, key=os.path.getmtime)
if self.validate_model(best_match):
logger.info(f"✅ Found improved model: {best_match}")
return best_match
else:
if os.path.exists(path_pattern):
if self.validate_model(path_pattern):
logger.info(f"✅ Found model: {path_pattern}")
return path_pattern
# No model found, try to download fallback
logger.warning("⚠️ No local model found, attempting to download fallback...")
return self.download_fallback_model()
def validate_model(self, model_path):
"""Validate that a model file is usable"""
try:
if not os.path.exists(model_path):
return False
# Check file size (should be > 1MB for a real model)
if os.path.getsize(model_path) < 1024 * 1024:
logger.warning(f"⚠️ Model file too small: {model_path}")
return False
# Try to load the model
test_model = YOLO(model_path)
# Quick test inference
import numpy as np
dummy_img = np.zeros((640, 640, 3), dtype=np.uint8)
results = test_model(dummy_img, verbose=False)
logger.info(f"✅ Model validation successful: {model_path}")
return True
except Exception as e:
logger.error(f"❌ Model validation failed for {model_path}: {e}")
return False
def download_fallback_model(self):
"""Download a fallback model if none available"""
for model_name, url in self.backup_urls.items():
try:
logger.info(f"📥 Downloading fallback model: {model_name}")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(model_name, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
if self.validate_model(model_name):
logger.info(f"✅ Successfully downloaded: {model_name}")
return model_name
except Exception as e:
logger.error(f"❌ Failed to download {model_name}: {e}")
continue
logger.error("❌ Could not find or download any usable model!")
return None
def load_model_with_fallback(self):
"""Load model with comprehensive error handling"""
model_path = self.find_best_model()
if not model_path:
raise RuntimeError("No usable model found!")
try:
logger.info(f"🤖 Loading model: {model_path}")
model = YOLO(model_path)
# Configure device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
logger.info(f"🔧 Using device: {device}")
try:
model.to(device)
if device == 'cuda' and hasattr(model.model, 'half'):
model.model.half() # Use FP16 for speed
except Exception as e:
logger.warning(f"⚠️ Could not optimize model: {e}")
# Test the model
import numpy as np
test_img = np.zeros((640, 640, 3), dtype=np.uint8)
results = model(test_img, verbose=False)
logger.info("✅ Model loaded and tested successfully!")
# Print model info
if 'advanced_training' in model_path:
logger.info("🚀 Using IMPROVED AI model!")
elif 'yolov8' in model_path:
logger.info("⚠️ Using basic YOLO model - consider training a custom PPE model")
else:
logger.info("📊 Using custom PPE model")
return model, model_path
except Exception as e:
logger.error(f"❌ Failed to load model {model_path}: {e}")
# Try fallback
if 'yolov8' not in model_path:
logger.info("🔄 Trying fallback model...")
fallback_path = self.download_fallback_model()
if fallback_path:
return self.load_model_with_fallback()
raise RuntimeError(f"Could not load any model: {e}")
def load_ppe_model():
"""Main function to load PPE detection model"""
loader = RobustModelLoader()
return loader.load_model_with_fallback()