Add TorchScript-Lite export (#387)

* TorchScript-Lite export

* forgot the import
pull/396/head
Ian 2022-08-01 23:46:25 +02:00 committed by GitHub
parent 0a58f6d1cb
commit 0563c70705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -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