-
Notifications
You must be signed in to change notification settings - Fork 30
/
util.py
65 lines (51 loc) · 1.83 KB
/
util.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import OrderedDict
import random
import string
import json
import os
import torch
def generate_id():
sw_id = "".join(random.choice("".join([random.choice(
string.ascii_letters + string.digits)
for ch in range(4)])) for _ in range(4))
return sw_id
def dump_json_config(conf_file_name, config):
with conf_file_name.open("w+") as conf:
json.dump(config, conf, indent=4, sort_keys=True,
default=lambda o: "<object>")
def check_support(models, supported):
supported = [sup.lower() for sup in supported]
for model in models:
print(f"Checking support of {model}")
if model.lower() not in supported:
print(f"Model {model} not supported!")
return False
return True
def setup_torch():
use_cuda = torch.cuda.is_available()
device = "cuda" if use_cuda else "cpu"
if use_cuda:
torch.backends.cudnn.benchmark = True
# Maximum determinism
torch.manual_seed(1)
print(f"Using {device} to train.")
return device
def check_dir(directory):
# create the folder if it does not exit
if not directory == "" and not os.path.exists(directory):
print(f"Folder {directory} does not exist! Creating...")
os.makedirs(directory)
def load_checkpoint(model, checkpoint_path, device="cpu"):
device = torch.device(device)
model_ckp = torch.load(checkpoint_path, map_location=device)
# handle both dataparallel and normal models
model_tmp_dict = OrderedDict()
for name, value in model_ckp["model_state_dict"].items():
if name.startswith("module."):
name = name[7:]
model_tmp_dict[name] = value
if isinstance(model, torch.nn.DataParallel):
model.module.load_state_dict(model_tmp_dict)
else:
model.load_state_dict(model_tmp_dict)
return model