nachiiiket commited on
Commit
c65784e
·
1 Parent(s): 259b938

Delete models/export.py

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