Skip to content

Commit

Permalink
Add file_size() function (ultralytics#2911)
Browse files Browse the repository at this point in the history
* Add file_size() function

* Update export.py
  • Loading branch information
glenn-jocher committed Apr 23, 2021
1 parent dfb3b30 commit 5faef02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import models
from models.experimental import attempt_load
from utils.activations import Hardswish, SiLU
from utils.general import colorstr, check_img_size, check_requirements, set_logging
from utils.general import colorstr, check_img_size, check_requirements, file_size, set_logging
from utils.torch_utils import select_device

if __name__ == '__main__':
Expand Down Expand Up @@ -60,6 +60,7 @@
model.model[-1].export = not opt.grid # set Detect() layer grid export
for _ in range(2):
y = model(img) # dry runs
print(f"\n{colorstr('PyTorch:')} starting from {opt.weights} ({file_size(opt.weights):.1f} MB)")

# TorchScript export -----------------------------------------------------------------------------------------------
prefix = colorstr('TorchScript:')
Expand All @@ -69,7 +70,7 @@
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}')
print(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
except Exception as e:
print(f'{prefix} export failure: {e}')

Expand Down Expand Up @@ -103,7 +104,7 @@
onnx.save(model_onnx, f)
except Exception as e:
print(f'{prefix} simplifier failure: {e}')
print(f'{prefix} export success, saved as {f}')
print(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
except Exception as e:
print(f'{prefix} export failure: {e}')

Expand All @@ -117,7 +118,7 @@
model = ct.convert(ts, inputs=[ct.ImageType(name='image', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
f = opt.weights.replace('.pt', '.mlmodel') # filename
model.save(f)
print(f'{prefix} export success, saved as {f}')
print(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
except Exception as e:
print(f'{prefix} export failure: {e}')

Expand Down
5 changes: 5 additions & 0 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def emojis(str=''):
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str


def file_size(file):
# Return file size in MB
return Path(file).stat().st_size / 1e6


def check_online():
# Check internet connectivity
import socket
Expand Down

0 comments on commit 5faef02

Please sign in to comment.