-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_utils.py
154 lines (140 loc) · 8.03 KB
/
model_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
### code retrieved from https://github.com/princetonvisualai/RememberThePast-DatasetDistillation
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
from torchvision import datasets, transforms
from networks import ConvNet, AlexNet, ResNet12
def get_default_convnet_setting():
net_width, net_depth, net_act, net_norm, net_pooling = 128, 3, 'relu', 'instancenorm', 'avgpooling'
return net_width, net_depth, net_act, net_norm, net_pooling
def count_params(model, trainable=False):
if trainable:
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
else:
total_params = sum(p.numel() for p in model.parameters())
return total_params
def get_network(model, channel, num_classes, im_size):
torch.random.manual_seed(int(time.time() * 1000) % 100000)
net_width, net_depth, net_act, net_norm, net_pooling = get_default_convnet_setting()
if model == 'ConvNet':
if im_size[0] == 64 or im_size[0] == 84:
net_depth += 1
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetELU':
if im_size[0] == 64 or im_size[0] == 84:
net_depth += 1
net_act = 'elu'
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'AlexNet':
net = AlexNet(channel=channel, num_classes=num_classes)
elif model == 'ResNet12':
net = ResNet12(channel=channel, num_classes=num_classes)
elif model == 'ResNet34':
net = ResNet34(channel=channel, num_classes=num_classes)
elif model == 'ResNet18':
net = ResNet18(channel=channel, num_classes=num_classes)
elif model == 'ResNet18BN_AP':
net = ResNet18BN_AP(channel=channel, num_classes=num_classes)
elif model == 'ConvNetD1':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=1,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD2':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=2,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD3':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=3,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD4':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=4,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size,
pooling_stop=3)
elif model == 'ConvNetD5':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=5,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD6':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=6,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD7':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=7,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetD8':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=8,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size)
elif model == 'ConvNetW32':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=32, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetW64':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=64, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetW128':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=128, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetW256':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=256, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetAS':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act='sigmoid', net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetAR':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act='relu', net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetAL':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act='leakyrelu', net_norm=net_norm, net_pooling=net_pooling)
elif model == 'ConvNetNN':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm='none', net_pooling=net_pooling)
elif model == 'ConvNetBN':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm='batchnorm', net_pooling=net_pooling)
elif model == 'ConvNetLN':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm='layernorm', net_pooling=net_pooling)
elif model == 'ConvNetIN':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm='instancenorm', net_pooling=net_pooling)
elif model == 'ConvNetGN':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm='groupnorm', net_pooling=net_pooling)
elif model == 'ConvNetNP':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling='none')
elif model == 'ConvNetMP':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling='maxpooling')
elif model == 'ConvNetAP':
net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth,
net_act=net_act, net_norm=net_norm, net_pooling='avgpooling')
else:
net = None
exit('DC error: unknown model')
return net
def get_time():
return str(time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime()))
def get_loops(ipc):
# Get the two hyper-parameters of outer-loop and inner-loop.
# The following values are empirically good.
if ipc == 1:
outer_loop, inner_loop = 1, 1
elif ipc == 10:
outer_loop, inner_loop = 10, 50
elif ipc == 20:
outer_loop, inner_loop = 20, 25
elif ipc == 30:
outer_loop, inner_loop = 30, 20
elif ipc == 40:
outer_loop, inner_loop = 40, 15
elif ipc == 50:
outer_loop, inner_loop = 50, 10
elif ipc == 100:
outer_loop, inner_loop = 50, 10
elif ipc == 200:
outer_loop, inner_loop = 100, 10
else:
outer_loop, inner_loop = 0, 0
exit('DC error: loop hyper-parameters are not defined for %d ipc'%ipc)
return outer_loop, inner_loop