Diff of /YOLOv8.py [000000] .. [afa31e]

Switch to unified view

a b/YOLOv8.py
1
"""
2
data:
3
    CT         :     used
4
    mask       : not used
5
    labels(txt):     used
6
    labelsJson : not used
7
    dataset.yaml:    used
8
"""
9
10
from ultralytics import YOLO
11
import os
12
import shutil
13
import torch
14
import multiprocessing
15
16
17
if __name__ == "__main__":
18
    if torch.cuda.is_available():
19
        device = torch.device("cuda:0")
20
        print("GPU")
21
    else:
22
        device = torch.device("cpu")
23
        print("CPU")
24
25
    multiprocessing.freeze_support()
26
    torch.cuda.empty_cache()
27
28
    YOLODataset_path = "./YOLODataset"
29
    models_path = "./models/"
30
31
    save_result_path = "./runs"
32
    if os.path.exists(save_result_path):
33
        shutil.rmtree(save_result_path)
34
35
    # Train the model
36
    # Load a model
37
    yolov8_premodel_path = os.path.join(models_path, "yolov8n-seg.pt")
38
    yaml_dataset_path = os.path.join(YOLODataset_path, "dataset.yaml")
39
    model = YOLO(yolov8_premodel_path)  # load a pretrained model
40
41
    results = model.train(
42
        data=yaml_dataset_path,
43
        epochs=1,
44
        # imgsz=640,
45
        batch=2,
46
        flipud=0.5,
47
        # device="cpu",
48
        # device=0,
49
        seed=2024,
50
        amp=True,
51
    )
52
53
    model.val()
54
55
    model = YOLO("./runs/segment/train/weights/best.pt")
56
57
    source = "./test/testpng/"
58
    for f in os.listdir(source):
59
        file_path = os.path.join(source, f)
60
        model.predict(file_path, save=True, retina_masks=True)