glenn-jocher commited on
Commit
4d7f222
1 Parent(s): 8666bc5

Update export.py with v3.0 Hardswish() support

Browse files
Files changed (2) hide show
  1. models/export.py +12 -7
  2. utils/activations.py +3 -3
models/export.py CHANGED
@@ -8,12 +8,14 @@ import argparse
8
 
9
  import torch
10
 
 
 
 
11
  from utils.general import set_logging
12
- from utils.google_utils import attempt_download
13
 
14
  if __name__ == '__main__':
15
  parser = argparse.ArgumentParser()
16
- parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path')
17
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')
18
  parser.add_argument('--batch-size', type=int, default=1, help='batch size')
19
  opt = parser.parse_args()
@@ -25,12 +27,15 @@ if __name__ == '__main__':
25
  img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
26
 
27
  # Load PyTorch model
28
- attempt_download(opt.weights)
29
- model = torch.load(opt.weights, map_location=torch.device('cpu'))['model'].float()
30
- model.eval()
31
- model.fuse()
32
 
33
  # Update model
 
 
 
 
 
 
34
  model.model[-1].export = True # set Detect() layer export=True
35
  y = model(img) # dry run
36
 
@@ -56,7 +61,7 @@ if __name__ == '__main__':
56
  # Checks
57
  onnx_model = onnx.load(f) # load onnx model
58
  onnx.checker.check_model(onnx_model) # check onnx model
59
- print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
60
  print('ONNX export success, saved as %s' % f)
61
  except Exception as e:
62
  print('ONNX export failure: %s' % e)
 
8
 
9
  import torch
10
 
11
+ from models.common import Conv
12
+ from models.experimental import attempt_load
13
+ from utils.activations import Hardswish
14
  from utils.general import set_logging
 
15
 
16
  if __name__ == '__main__':
17
  parser = argparse.ArgumentParser()
18
+ parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path') # from yolov5/models/
19
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')
20
  parser.add_argument('--batch-size', type=int, default=1, help='batch size')
21
  opt = parser.parse_args()
 
27
  img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
28
 
29
  # Load PyTorch model
30
+ model = attempt_load(opt.weights, map_location=torch.device('cpu')) # load FP32 model
 
 
 
31
 
32
  # Update model
33
+ for k, m in model.named_modules():
34
+ m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatability
35
+ if isinstance(m, Conv):
36
+ m.act = Hardswish() # assign activation
37
+ # if isinstance(m, Detect):
38
+ # m.forward = m.forward_export # assign forward (optional)
39
  model.model[-1].export = True # set Detect() layer export=True
40
  y = model(img) # dry run
41
 
 
61
  # Checks
62
  onnx_model = onnx.load(f) # load onnx model
63
  onnx.checker.check_model(onnx_model) # check onnx model
64
+ # print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model
65
  print('ONNX export success, saved as %s' % f)
66
  except Exception as e:
67
  print('ONNX export failure: %s' % e)
utils/activations.py CHANGED
@@ -10,11 +10,11 @@ class Swish(nn.Module): #
10
  return x * torch.sigmoid(x)
11
 
12
 
13
- class Hardswish(nn.Module): # alternative to nn.Hardswish() for export
14
  @staticmethod
15
  def forward(x):
16
- # return x * F.hardsigmoid(x)
17
- return x * F.hardtanh(x + 3, 0., 6.) / 6.
18
 
19
 
20
  class MemoryEfficientSwish(nn.Module):
 
10
  return x * torch.sigmoid(x)
11
 
12
 
13
+ class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
14
  @staticmethod
15
  def forward(x):
16
+ # return x * F.hardsigmoid(x) # for torchscript and CoreML
17
+ return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX
18
 
19
 
20
  class MemoryEfficientSwish(nn.Module):