-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
49 lines (38 loc) · 1.35 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
##### HELPER FUNCTIONS #####
from torchprofile import profile_macs
import torch.nn as nn
Byte = 8
KiB = 1024 * Byte
MiB = 1024 * KiB
GiB = 1024 * MiB
def download_url(url, model_dir='.', overwrite=False):
import os, sys
from urllib.request import urlretrieve
target_dir = url.split('/')[-1]
model_dir = os.path.expanduser(model_dir)
try:
if not os.path.exists(model_dir):
os.makedirs(model_dir)
model_dir = os.path.join(model_dir, target_dir)
cached_file = model_dir
if not os.path.exists(cached_file) or overwrite:
sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file))
urlretrieve(url, cached_file)
return cached_file
except Exception as e:
# remove lock file so download can be executed next time.
os.remove(os.path.join(model_dir, 'download.lock'))
sys.stderr.write('Failed to download from url %s' % url + '\n' + str(e) + '\n')
return None
def get_model_flops(model, inputs):
num_macs = profile_macs(model, inputs)
return num_macs
def get_model_size(model: nn.Module, data_width=32):
"""
calculate the model size in bits
:param data_width: #bits per element
"""
num_elements = 0
for param in model.parameters():
num_elements += param.numel()
return num_elements * data_width