---
# System prepended metadata

title: yolov8+cuda詳細教學
tags: [yolov8, cuda]

---

---
title: 'Cuda，Yolov8 詳細教學'
disqus: hackmd
---

Cuda，Yolov8 詳細教學
===

[TOC]

##  安裝Anaconda

https://www.anaconda.com/download

##  安裝Cuda+Cudnn+PyTorch
1. 查看顯示卡Cuda支援最高版本，在cmd中輸入
 ```gherkin=
 nvidia-smi 
 ```
  ![擷取](https://hackmd.io/_uploads/Skr-JV0_C.png)
 
2. 到 https://pytorch.org/ 下載對應版本，複製底下Command

![擷取](https://hackmd.io/_uploads/Synlg4C_C.png)

3. 打開Anaconda，建立新環境，點Open Terminal，將複製內容貼上並執行他

![擷取](https://hackmd.io/_uploads/BkGXmVRd0.png)

![擷取](https://hackmd.io/_uploads/SJDkSEAd0.png)


4. 在同樣地方輸入並執行以下內容，安裝yolov8

 ```
 pip install ultralytics
 ```

確認環境建立是否成功
---

在你的Jupyter Notebook中執行

```gherkin=
import torch
torch.cuda.is_available()
```
>確認cuda有正確安裝


```gherkin=
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
```
> 若成功執行，即成功指派GPU執行

```gherkin=
import ultralytics
ultralytics.checks()
```
> 若返回Ture，即安裝Yolov8

開始訓練Yolov8模型，使用自訂訓練集
---
https://github.com/ultralytics/ultralytics
```gherkin=
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.yaml")  # build a new model from scratch
model = YOLO("yolov8n.pt")  # load a pretrained model (recommended for training)

# Use the model
model.train(data="coco8.yaml", epochs=3)  # train the model
metrics = model.val()  # evaluate model performance on the validation set
path = model.export(format="onnx")  # export the model to ONNX format
```

> coco8.yaml改成你的yaml檔的檔案路徑

```
train: ../train/images  
val: ../valid/images
test: ../test/images

nc: 3
names: ['0', '1', '2']
```
> yaml內容分別為train、val、test的images資料夾位置
> nc為你有幾個label，names為你的label名稱

## 可調參數

https://docs.ultralytics.com/zh/modes/train/#train-settings
https://blog.csdn.net/weixin_45277161/article/details/131047101

## 測試你的模型

https://docs.ultralytics.com/modes/predict/#inference-sources

```gherkin=
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.yaml")  # build a new model from scratch
model = YOLO("yolov8n.pt")  # load a pretrained model (recommended for training)

# Use the model
import cv2
from PIL import Image
from ultralytics import YOLO

# 加載訓練好的模型
model = YOLO(r"C:\cookie-yolov8\cookie_datasets.v2i.yolov8\training_results\cookies2\weights\best.pt")

# 從攝像頭進行預測
#results = model.predict(source=0)  # 0 代表使用攝像頭

# 從文件夾中進行預測並顯示結果
results = model.predict(source=r"C:\cookie-yolov8\cookie_datasets.v2i.yolov8\test\images", 
                        show=False,
                        save=True,
                        save_dir=r"C:\cookie-yolov8\cookie_datasets.v2i.yolov8\test_result"
                       )  # 這裡 "folder" 需要替換為實際的文件夾路徑

# 從單個 PIL 圖像進行預測並保存結果
# im1 = Image.open("bus.jpg")
# results = model.predict(source=im1, save=True)  # save=True 表示保存繪製的圖片

# 從單個 ndarray 圖像進行預測並保存結果
# im2 = cv2.imread("bus.jpg")
# results = model.predict(source=im2, save=True, save_txt=True)  # save_txt=True 表示保存預測的標籤

# 從文件夾中的圖像列表進行預測
#results = model.predict(source=r"C:\cookie-yolov8\cookie_datasets.v2i.yolov8\test\images")  # 指定文件夾路徑

# 顯示結果
#result.save(r"C:\cookie-yolov8\cookie_datasets.v2i.yolov8\test_result")

```

## 恭喜成功!!

###### tags: `yolov8` `cuda`
