-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (41 loc) · 1.67 KB
/
Copy pathutils.py
File metadata and controls
50 lines (41 loc) · 1.67 KB
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
49
50
"""
Some utility functions.
Copyright ETH Zurich, Manuel Kaufmann
"""
import glob
import os
import zipfile
def create_model_dir(experiment_main_dir, experiment_id, model_summary):
"""
Create a new model directory.
:param experiment_main_dir: Where all experiments are stored.
:param experiment_id: The ID of this experiment.
:param model_summary: A summary string of the model.
:return: A directory where we can store model logs. Raises an exception if the model directory already exists.
"""
model_name = "{}-{}".format(experiment_id, model_summary)
model_dir = os.path.join(experiment_main_dir, model_name)
if os.path.exists(model_dir):
raise ValueError("Model directory already exists {}".format(model_dir))
os.makedirs(model_dir)
return model_dir
def get_model_dir(experiment_dir, model_id):
"""Return the directory in `experiment_dir` that contains the given `model_id` string."""
model_dir = glob.glob(os.path.join(experiment_dir, str(model_id) + "-*"), recursive=False)
return None if len(model_dir) == 0 else model_dir[0]
def export_code(file_list, output_file):
"""Stores files in a zip."""
if not output_file.endswith('.zip'):
output_file += '.zip'
ofile = output_file
counter = 0
while os.path.exists(ofile):
counter += 1
ofile = output_file.replace('.zip', '_{}.zip'.format(counter))
zipf = zipfile.ZipFile(ofile, mode="w", compression=zipfile.ZIP_DEFLATED)
for f in file_list:
zipf.write(f)
zipf.close()
def count_parameters(net):
"""Count number of trainable parameters in `net`."""
return sum(p.numel() for p in net.parameters() if p.requires_grad)