sushku commited on
Commit
241921e
Β·
verified Β·
1 Parent(s): 5a16109

Create new.py

Browse files
Files changed (1) hide show
  1. new.py +61 -0
new.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install required package
2
+ # pip install ultralytics
3
+
4
+ from ultralytics import YOLO
5
+
6
+ # Dataset structure expected:
7
+ # β”œβ”€β”€ dataset/
8
+ # β”‚ β”œβ”€β”€ train/
9
+ # β”‚ β”‚ β”œβ”€β”€ images/
10
+ # β”‚ β”‚ └── labels/
11
+ # β”‚ β”œβ”€β”€ valid/
12
+ # β”‚ β”‚ β”œβ”€β”€ images/
13
+ # β”‚ β”‚ └── labels/
14
+ # β”‚ └── test/
15
+ # β”‚ β”œβ”€β”€ images/
16
+ # β”‚ └── labels/
17
+
18
+ # data.yaml example:
19
+ # path: /path/to/dataset
20
+ # train: train/images
21
+ # val: valid/images
22
+ # test: test/images
23
+ # names:
24
+ # 0: class1
25
+ # 1: class2
26
+ # ...
27
+
28
+ def train_yolov8():
29
+ # Load the YOLOv8 Large model
30
+ model = YOLO('yolov8l.pt') # pretrained model
31
+
32
+ # Train the model
33
+ results = model.train(
34
+ data='data.yaml',
35
+ epochs=100,
36
+ batch=16,
37
+ imgsz=640,
38
+ device='0', # 'cpu' or '0' for GPU
39
+ name='yolov8l_custom',
40
+ optimizer='Adam',
41
+ lr0=0.001,
42
+ warmup_epochs=3,
43
+ augment=True,
44
+ patience=50,
45
+ pretrained=True
46
+ )
47
+
48
+ # Validate the model
49
+ metrics = model.val() # Validate on validation set
50
+ print(f"Validation [email protected]: {metrics.box.map}")
51
+
52
+ # Test the model (optional)
53
+ test_model = YOLO('runs/detect/yolov8l_custom/weights/best.pt')
54
+ test_metrics = test_model.val(data='data.yaml', split='test')
55
+ print(f"Test [email protected]: {test_metrics.box.map}")
56
+
57
+ # Export to ONNX format (optional)
58
+ model.export(format='onnx')
59
+
60
+ if __name__ == '__main__':
61
+ train_yolov8()