forked from airockchip/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
157 lines (123 loc) · 4.39 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# path alian
DATASET_PATH = '/usr/src/dataset/'
PROJECT_PATH = '/usr/src/app/project/'
IMAGES_PATH = '/usr/src/app/project/images/'
LABELS_PATH = '/usr/src/app/project/labels/'
LABELS_TXT_PATH = '/usr/src/app/project/labels/labels.txt'
ANCHORS_PATH = '/usr/src/app/RK_anchors.txt'
RUNS_PATH = '/usr/src/app/runs/'
RESULTS_CSV_PATH = '/usr/src/app/runs/train/exp/results.csv'
PT_PATH = '/usr/src/app/runs/train/exp/weights/best.pt'
ONNX_PATH = '/usr/src/app/runs/train/exp/weights/best.onnx'
RKNN_PATH = '/usr/src/app/runs/train/exp/weights/best.rknn'
YOLO_TAR_PATH = '/usr/src/app/project/yolov5.tar'
import singtownai
try:
import logging
# logging.basicConfig(filename='log.txt', encoding='utf-8', level=logging.INFO,
# format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
import os, sys, time, shutil, json
import requests
from datetime import datetime
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("tid", help="Train id on host")
args = parser.parse_args()
tid = args.tid
if tid == '-1':
event = json.loads(os.getenv('FC_CUSTOM_CONTAINER_EVENT'))
tid = event["tid"]
singtownai.start(tid)
singtownai.message("Prepare")
def ensure_empty(path):
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
dataset = singtownai.dataset()
train_info = dataset['train']
labels = dataset['train']['labels']
singtownai.download(dataset['annotations'], DATASET_PATH)
# prepare dataset format
ensure_empty(PROJECT_PATH)
os.mkdir(IMAGES_PATH)
os.mkdir(LABELS_PATH)
singtownai.convert_yolo(dataset, LABELS_PATH)
for anno in dataset['annotations']:
os.symlink(os.path.join(DATASET_PATH, anno['name']), os.path.join(IMAGES_PATH, anno['name']))
from utils.dataloaders import autosplit
autosplit(IMAGES_PATH)
import yaml
with open('data/dataset.yaml', 'w') as f:
f.write(yaml.dump({
'path': PROJECT_PATH,
'train': './autosplit_train.txt',
'val': './autosplit_val.txt',
'nc': len(labels),
'names': dict(enumerate(labels)),
}))
# train
from watchdog.observers import Observer
from watchdog.events import *
class FileEventHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path in [RESULTS_CSV_PATH]:
singtownai.log(event.src_path)
ensure_empty('runs')
observer = Observer()
event_handler = FileEventHandler()
observer.schedule(event_handler, RUNS_PATH, True)
observer.start()
singtownai.message("Training")
cmd = f"python3 train.py --data dataset.yaml \
--weights /usr/src/app/weights/{train_info['model']}_{train_info['weights']}.pt --img 640 \
--epochs {train_info['epochs']} --batch-size 32" #--patience 5
if train_info["freeze_backbone"]:
cmd += " --freeze 10"
ret = os.system(cmd)
if ret != 0:
singtownai.failed()
exit(-1)
observer.stop()
# export
singtownai.message("Exporting")
width = 640
if train_info['cpu'] == 'rv1103':
height = 480
elif train_info['cpu'] == 'rv1106':
height = 384
ret = os.system(f"python3 export.py --rknpu rv1103 --img {height} {width} --weights {PT_PATH} --include onnx")
if ret != 0:
singtownai.failed()
exit(-1)
# convert rknn
singtownai.message("Converting")
from rknn.api import RKNN
rknn = RKNN()
rknn.config(mean_values=[[0, 0, 0]], std_values=[[255, 255, 255]], target_platform="rv1106")
ret = rknn.load_onnx(ONNX_PATH)
if ret != 0:
singtownai.failed()
exit(-1)
ret = rknn.build(do_quantization=True, dataset=os.path.join(PROJECT_PATH, 'autosplit_val.txt'))
if ret != 0:
singtownai.failed()
exit(-1)
ret = rknn.export_rknn(RKNN_PATH)
if ret != 0:
singtownai.failed()
exit(-1)
rknn.release()
# package and upload
singtownai.message("Uploading")
import tarfile
with tarfile.open(YOLO_TAR_PATH, "w") as tar:
tar.add(RKNN_PATH, 'yolov5.rknn')
tar.add(ANCHORS_PATH, 'anchors.txt')
tar.add(LABELS_TXT_PATH, 'labels.txt')
singtownai.result(YOLO_TAR_PATH)
logging.info(f'p:{tid}, succeed')
singtownai.message("Finished")
singtownai.succeed()
except:
singtownai.failed()
raise