-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
129 lines (114 loc) · 4.37 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import os
import json
import torch
import random
from torch.distributed import init_process_group
def set_seed(seed=0):
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def ddp_setup(rank: int, world_size: int):
"""
Args:
rank: Unique identifier of each process
world_size: Total number of processes
"""
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"
init_process_group(backend="nccl", rank=rank, world_size=world_size)
torch.cuda.set_device(rank)
def get_model(args):
model = None
if args.model == 'search_mixer':
from models import SearchController
model = SearchController(
device=args.device,
in_channels=3,
img_size=args.size,
hidden_size=args.hidden_size,
patch_size=args.patch_size,
hidden_s_candidates=args.hidden_s_candidates,
hidden_c_candidates=args.hidden_c_candidates,
n_cells=args.n_cells,
num_classes=args.num_classes,
drop_p=args.drop_p,
off_act=args.off_act,
is_cls_token=args.is_cls_token
)
elif args.model == 'fixed-mixer':
supernet_params = json.load(open(os.path.join(args.path_to_supernet, 'params.json')))
from models import SearchController, FixedMixer
search_model = SearchController(
device=args.device,
in_channels=3,
img_size=args.size,
hidden_size=supernet_params["hidden_size"],
patch_size=supernet_params["patch_size"],
hidden_s_candidates=supernet_params["hidden_s_candidates"],
hidden_c_candidates=supernet_params["hidden_c_candidates"],
n_cells=supernet_params["n_cells"],
num_classes=args.num_classes,
drop_p=supernet_params["drop_p"],
off_act=supernet_params["off_act"],
is_cls_token=supernet_params["is_cls_token"]
)
sd = torch.load(os.path.join(args.path_to_supernet, 'W.pt'))
sd_filtered = {k: v for k,v in sd.items() if 'clf' not in k}
search_model.load_state_dict(sd_filtered, strict=False)
alphas = search_model.get_detached_alphas(aslist=False, activated=True, th=args.th_arch, binarize=args.binarize_arch, top_k=args.top_k)
model = FixedMixer(
in_channels=3,
img_size=args.size,
hidden_size=supernet_params["hidden_size"],
patch_size=supernet_params["patch_size"],
hidden_s_candidates=supernet_params["hidden_s_candidates"],
hidden_c_candidates=supernet_params["hidden_c_candidates"],
n_cells=supernet_params["n_cells"],
num_classes=args.num_classes,
drop_p=supernet_params["drop_p"],
off_act=supernet_params["off_act"],
is_cls_token=supernet_params["is_cls_token"],
fixed_alphas=alphas)
elif args.model == 'mlp-mixer':
from models import MLPMixer
model = MLPMixer(
in_channels=3,
img_size=args.size,
patch_size=args.patch_size,
hidden_size=args.hidden_size,
hidden_s=args.hidden_s,
hidden_c=args.hidden_c,
num_layers=args.num_layers,
num_classes=args.num_classes,
drop_p=args.drop_p,
off_act=args.off_act,
is_cls_token=args.is_cls_token
)
else:
raise ValueError(f"No such model: {args.model}")
return model.to(args.device)
def save_config(args):
config = args.__dict__.copy()
config['device'] = config['device'].__str__()
path = os.path.join(args.output, args.experiment)
os.makedirs(path, exist_ok=True)
with open(path + '/params.json', 'w') as ff:
json.dump(config, ff, indent=4, sort_keys=True)
def rand_bbox(size, lam):
W = size[2]
H = size[3]
cut_rat = np.sqrt(1. - lam)
cut_w = np.int64(W * cut_rat)
cut_h = np.int64(H * cut_rat)
# uniform
cx = np.random.randint(W)
cy = np.random.randint(H)
bbx1 = np.clip(cx - cut_w // 2, 0, W)
bby1 = np.clip(cy - cut_h // 2, 0, H)
bbx2 = np.clip(cx + cut_w // 2, 0, W)
bby2 = np.clip(cy + cut_h // 2, 0, H)
return bbx1, bby1, bbx2, bby2