Update models/yolov8_model.pt
Browse files- models/yolov8_model.pt +47 -9
models/yolov8_model.pt
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
# scripts/download_yolov8_model.py
|
2 |
from ultralytics import YOLO
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
# Directory to save the model
|
6 |
MODEL_DIR = "models"
|
@@ -9,14 +12,49 @@ os.makedirs(MODEL_DIR, exist_ok=True)
|
|
9 |
# Path to save the model
|
10 |
MODEL_PATH = os.path.join(MODEL_DIR, "yolov8_model.pt")
|
11 |
|
12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
except Exception as e:
|
22 |
-
print(f"Failed to download
|
|
|
|
1 |
# scripts/download_yolov8_model.py
|
2 |
from ultralytics import YOLO
|
3 |
import os
|
4 |
+
import torch
|
5 |
+
import requests
|
6 |
+
import shutil
|
7 |
|
8 |
# Directory to save the model
|
9 |
MODEL_DIR = "models"
|
|
|
12 |
# Path to save the model
|
13 |
MODEL_PATH = os.path.join(MODEL_DIR, "yolov8_model.pt")
|
14 |
|
15 |
+
# URL for yolov8n.pt from Ultralytics GitHub releases
|
16 |
+
MODEL_URL = "https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8n.pt"
|
17 |
+
|
18 |
+
def download_model(url, output_path):
|
19 |
+
"""Download model file from URL and save to output_path."""
|
20 |
+
print(f"Downloading model from {url}...")
|
21 |
+
try:
|
22 |
+
response = requests.get(url, stream=True)
|
23 |
+
if response.status_code == 200:
|
24 |
+
with open(output_path, 'wb') as f:
|
25 |
+
shutil.copyfileobj(response.raw, f)
|
26 |
+
print(f"Model downloaded to {output_path}")
|
27 |
+
else:
|
28 |
+
raise Exception(f"Failed to download model: HTTP {response.status_code}")
|
29 |
+
except Exception as e:
|
30 |
+
raise Exception(f"Download error: {str(e)}")
|
31 |
+
|
32 |
+
def verify_model(path):
|
33 |
+
"""Verify that the model file is a valid PyTorch checkpoint."""
|
34 |
+
print(f"Verifying model at {path}...")
|
35 |
+
try:
|
36 |
+
checkpoint = torch.load(path, map_location='cpu', weights_only=False)
|
37 |
+
print(f"Model verified as valid PyTorch checkpoint")
|
38 |
+
return True
|
39 |
+
except Exception as e:
|
40 |
+
raise Exception(f"Verification failed: {str(e)}")
|
41 |
+
|
42 |
+
# Download and verify model
|
43 |
try:
|
44 |
+
# Remove existing file to avoid using corrupted version
|
45 |
+
if os.path.exists(MODEL_PATH):
|
46 |
+
os.remove(MODEL_PATH)
|
47 |
+
print(f"Removed existing file at {MODEL_PATH}")
|
48 |
+
|
49 |
+
# Download yolov8n.pt
|
50 |
+
download_model(MODEL_URL, MODEL_PATH)
|
51 |
+
|
52 |
+
# Verify the downloaded file
|
53 |
+
verify_model(MODEL_PATH)
|
54 |
+
|
55 |
+
# Load with YOLO to ensure compatibility
|
56 |
+
model = YOLO(MODEL_PATH)
|
57 |
+
print(f"Model successfully loaded with Ultralytics YOLO")
|
58 |
except Exception as e:
|
59 |
+
print(f"Failed to download or verify model: {str(e)}")
|
60 |
+
print("Please manually download yolov8n.pt from https://github.com/ultralytics/assets/releases and place it in backend/models/yolov8_model.pt")
|