Skip to content

Commit

Permalink
Default optimize_for_mobile() on TorchScript models (ultralytics#2908)
Browse files Browse the repository at this point in the history
Per https://pytorch.org/tutorials/recipes/script_optimized.html this should improve performance on torchscript models (and maybe coreml models also since coremltools operates on a torchscript model input, though this still requires testing).
  • Loading branch information
glenn-jocher authored Apr 23, 2021
1 parent f662c18 commit 28db237
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import torch
import torch.nn as nn
from torch.utils.mobile_optimizer import optimize_for_mobile

import models
from models.experimental import attempt_load
Expand Down Expand Up @@ -65,6 +66,7 @@
print(f'\n{prefix} starting export with torch {torch.__version__}...')
f = opt.weights.replace('.pt', '.torchscript.pt') # filename
ts = torch.jit.trace(model, img, strict=False)
ts = optimize_for_mobile(ts) # https://pytorch.org/tutorials/recipes/script_optimized.html
ts.save(f)
print(f'{prefix} export success, saved as {f}')
except Exception as e:
Expand Down

0 comments on commit 28db237

Please sign in to comment.