hank1996 commited on
Commit
a7a1ae5
·
1 Parent(s): 78e8355

Delete models/export.py

Browse files
Files changed (1) hide show
  1. models/export.py +0 -100
models/export.py DELETED
@@ -1,100 +0,0 @@
1
-
2
- import argparse
3
- import sys
4
- import time
5
-
6
- sys.path.append('./') # to run '$ python *.py' files in subdirectories
7
-
8
- import torch
9
- import torch.nn as nn
10
-
11
- import models
12
- from models.experimental import attempt_load
13
- from utils.activations import Hardswish, SiLU
14
- from utils.general import set_logging, check_img_size
15
- from utils.torch_utils import select_device
16
-
17
- if __name__ == '__main__':
18
- parser = argparse.ArgumentParser()
19
- parser.add_argument('--weights', type=str, default='./yolor-csp-c.pt', help='weights path')
20
- parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') # height, width
21
- parser.add_argument('--batch-size', type=int, default=1, help='batch size')
22
- parser.add_argument('--dynamic', action='store_true', help='dynamic ONNX axes')
23
- parser.add_argument('--grid', action='store_true', help='export Detect() layer grid')
24
- parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
25
- opt = parser.parse_args()
26
- opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
27
- print(opt)
28
- set_logging()
29
- t = time.time()
30
-
31
- # Load PyTorch model
32
- device = select_device(opt.device)
33
- model = attempt_load(opt.weights, map_location=device) # load FP32 model
34
- labels = model.names
35
-
36
- # Checks
37
- gs = int(max(model.stride)) # grid size (max stride)
38
- opt.img_size = [check_img_size(x, gs) for x in opt.img_size] # verify img_size are gs-multiples
39
-
40
- # Input
41
- img = torch.zeros(opt.batch_size, 3, *opt.img_size).to(device) # image size(1,3,320,192) iDetection
42
-
43
- # Update model
44
- for k, m in model.named_modules():
45
- m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
46
- if isinstance(m, models.common.Conv): # assign export-friendly activations
47
- if isinstance(m.act, nn.Hardswish):
48
- m.act = Hardswish()
49
- elif isinstance(m.act, nn.SiLU):
50
- m.act = SiLU()
51
- # elif isinstance(m, models.yolo.Detect):
52
- # m.forward = m.forward_export # assign forward (optional)
53
- model.model[-1].export = not opt.grid # set Detect() layer grid export
54
- y = model(img) # dry run
55
-
56
- # TorchScript export
57
- try:
58
- print('\nStarting TorchScript export with torch %s...' % torch.__version__)
59
- f = opt.weights.replace('.pt', '.torchscript.pt') # filename
60
- ts = torch.jit.trace(model, img, strict=False)
61
- ts.save(f)
62
- print('TorchScript export success, saved as %s' % f)
63
- except Exception as e:
64
- print('TorchScript export failure: %s' % e)
65
-
66
- # ONNX export
67
- try:
68
- import onnx
69
-
70
- print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
71
- f = opt.weights.replace('.pt', '.onnx') # filename
72
- torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
73
- output_names=['classes', 'boxes'] if y is None else ['output'],
74
- dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'}, # size(1,3,640,640)
75
- 'output': {0: 'batch', 2: 'y', 3: 'x'}} if opt.dynamic else None)
76
-
77
- # Checks
78
- onnx_model = onnx.load(f) # load onnx model
79
- onnx.checker.check_model(onnx_model) # check onnx model
80
- # print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
81
- print('ONNX export success, saved as %s' % f)
82
- except Exception as e:
83
- print('ONNX export failure: %s' % e)
84
-
85
- # CoreML export
86
- try:
87
- import coremltools as ct
88
-
89
- print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
90
- # convert model from torchscript and apply pixel scaling as per detect.py
91
- model = ct.convert(ts, inputs=[ct.ImageType(name='image', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
92
- f = opt.weights.replace('.pt', '.mlmodel') # filename
93
- model.save(f)
94
- print('CoreML export success, saved as %s' % f)
95
- except Exception as e:
96
- print('CoreML export failure: %s' % e)
97
-
98
- # Finish
99
- print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t))
100
-