yolo官網
https://github.com/ultralytics/ultralytics?tab=readme-ov-file
下載python和解除限制
https://www.python.org/downloads/windows/
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1
vscode中配置python
https://code.visualstudio.com/docs/python/environments
?????
Tensorflow 理解訓練過程
y = 2x + 1 的學習過程 程序一開始是不知道這個函數的
一千次充分學習 分別求y值 得出的接近這個函數的結果
import tensorflow as tf
import numpy as np# 創建簡單的數據集(y = 2x + 1 加一些噪聲)
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
y = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)# 構建模型:單層神經網絡
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])
])# 編譯模型
model.compile(optimizer='sgd', loss='mean_squared_error')# 訓練模型
print("\n開始訓練模型...")
model.fit(x, y, epochs=1000)# 確保TensorFlow正常工作
print("TensorFlow版本:", tf.__version__)
# 使用模型進行預測
print("\n預測結果:")
print(f"當x=10時,預測y值為: {model.predict(np.array([10.0]))[0][0]}")
print(f"當x=11時,預測y值為: {model.predict(np.array([12.0]))[0][0]}")
print(f"當x=12時,預測y值為: {model.predict(np.array([13.0]))[0][0]}")
print(f"當x=13時,預測y值為: {model.predict(np.array([14.0]))[0][0]}")
結論
預測結果:
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step
當x=10時,預測y值為: 18.9998779296875
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step
當x=11時,預測y值為: 22.99984359741211
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 21ms/step
當x=12時,預測y值為: 24.999826431274414
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step
當x=13時,預測y值為: 26.999807357788086
yolo quick start
https://docs.ultralytics.com/zh/quickstart/
?????
訓練并導出模型
from ultralytics import YOLO# Create a new YOLO model from scratch
model = YOLO("yolo11n.yaml")# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo11n.pt")# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)# Evaluate the model's performance on the validation set
results = model.val()# Perform object detection on an image using the model
results = model("https://ultralytics.com/images/bus.jpg")# Export the model to ONNX format
success = model.export(format="onnx")
驗證
ownloading https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.pt to 'yolo11n.pt': 100%|██████
from ultralytics import YOLO
import cv2# 1. 加載.pt格式的預訓練模型(或自己訓練的模型)
# 可以是官方預訓練模型(如yolo11n.pt),也可以是自己訓練保存的模型(如runs/train/exp/weights/best.pt)
model = YOLO("yolo11n.pt") # 替換為你的.pt模型路徑# 2. 對單張圖片進行目標檢測
image_path = "bus.jpg" # 測試圖片路徑
results = model(image_path) # 執行推理# 3. 解析并可視化結果
for result in results:# 直接獲取處理后的圖像(已繪制檢測框和標簽)annotated_img = result.plot() # 生成帶檢測框的圖像# 保存結果cv2.imwrite("detection_result.jpg", annotated_img)# 顯示結果(可選)cv2.imshow("YOLO Detection", annotated_img)cv2.waitKey(0) # 按任意鍵關閉窗口cv2.destroyAllWindows()# 4. (擴展)對視頻進行檢測
video_path = "bus_test.mp4" # 測試視頻路徑
cap = cv2.VideoCapture(video_path)while cap.isOpened():ret, frame = cap.read()if not ret:break# 對每一幀進行檢測results = model(frame)annotated_frame = results[0].plot() # 繪制檢測框cv2.imshow("Video Detection", annotated_frame)if cv2.waitKey(1) & 0xFF == ord('q'): # 按q退出breakcap.release()
cv2.destroyAllWindows()
效果圖
yolo實例