From 0563c707058b31bee9b7f30d06a94cc6933c88f5 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 1 Aug 2022 23:46:25 +0200 Subject: [PATCH] Add TorchScript-Lite export (#387) * TorchScript-Lite export * forgot the import --- export.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/export.py b/export.py index c2d498d6..2b27b4ad 100644 --- a/export.py +++ b/export.py @@ -6,6 +6,7 @@ sys.path.append('./') # to run '$ python *.py' files in subdirectories import torch import torch.nn as nn +from torch.utils.mobile_optimizer import optimize_for_mobile import models from models.experimental import attempt_load, End2End @@ -75,6 +76,17 @@ if __name__ == '__main__': except Exception as e: print('TorchScript export failure: %s' % e) + # TorchScript-Lite export + try: + print('\nStarting TorchScript-Lite export with torch %s...' % torch.__version__) + f = opt.weights.replace('.pt', '.torchscript.ptl') # filename + ts = torch.jit.trace(model, img, strict=False) + ts = optimize_for_mobile(ts) + ts._save_for_lite_interpreter(f) + print('TorchScript-Lite export success, saved as %s' % f) + except Exception as e: + print('TorchScript-Lite export failure: %s' % e) + # ONNX export try: import onnx