-
Notifications
You must be signed in to change notification settings - Fork 236
/
gen_pkl.py
47 lines (37 loc) · 1.28 KB
/
gen_pkl.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
import argparse
import pickle
from collections import OrderedDict
from ultralytics import YOLO
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-w',
'--weights',
type=str,
required=True,
help='YOLOv8 pytorch weights')
parser.add_argument('-o',
'--output',
type=str,
required=True,
help='Output file')
args = parser.parse_args()
return args
args = parse_args()
model = YOLO(args.weights)
model.model.fuse()
YOLOv8 = model.model.model
strides = YOLOv8[-1].stride.detach().cpu().numpy()
reg_max = YOLOv8[-1].dfl.conv.weight.shape[1]
state_dict = OrderedDict(GD=model.model.yaml['depth_multiple'],
GW=model.model.yaml['width_multiple'],
strides=strides,
reg_max=reg_max)
for name, value in YOLOv8.state_dict().items():
value = value.detach().cpu().numpy()
i = int(name.split('.')[0])
layer = YOLOv8[i]
module_name = layer.type.split('.')[-1]
stem = module_name + '.' + name
state_dict[stem] = value
with open(args.output, 'wb') as f:
pickle.dump(state_dict, f)