-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_pruning.py
205 lines (174 loc) · 5.02 KB
/
run_pruning.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Prune the number of prototypes used.
import os
import shutil
import torch
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import argparse
from helpers import makedir
import model
import push
import prune
import train_and_test as tnt
import save
from log import create_logger
from preprocess import mean, std, preprocess_input_function
parser = argparse.ArgumentParser()
parser.add_argument("-gpuid", nargs=1, type=str, default="0")
parser.add_argument("-modeldir", nargs=1, type=str)
parser.add_argument("-model", nargs=1, type=str)
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpuid[0]
optimize_last_layer = True
# pruning parameters
k = 6
prune_threshold = 3
original_model_dir = args.modeldir[0] #'./saved_models/densenet161/003/'
original_model_name = args.model[0] #'10_16push0.8007.pth'
need_push = "nopush" in original_model_name
if need_push:
assert False # pruning must happen after push
else:
epoch = original_model_name.split("push")[0]
if "_" in epoch:
epoch = int(epoch.split("_")[0])
else:
epoch = int(epoch)
model_dir = os.path.join(
original_model_dir,
"pruned_prototypes_epoch{}_k{}_pt{}".format(epoch, k, prune_threshold),
)
makedir(model_dir)
shutil.copy(src=os.path.join(os.getcwd(), __file__), dst=model_dir)
log, logclose = create_logger(log_filename=os.path.join(model_dir, "prune.log"))
ppnet = torch.load(original_model_dir + original_model_name)
ppnet = ppnet.cuda()
ppnet_multi = torch.nn.DataParallel(ppnet)
class_specific = True
# load the data
from settings import train_dir, test_dir, train_push_dir
train_batch_size = 80
test_batch_size = 100
img_size = 224
train_push_batch_size = 80
normalize = transforms.Normalize(mean=mean, std=std)
# train set
train_dataset = datasets.ImageFolder(
train_dir,
transforms.Compose(
[
transforms.Resize(size=(img_size, img_size)),
transforms.ToTensor(),
normalize,
]
),
)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=train_batch_size,
shuffle=True,
num_workers=4,
pin_memory=False,
)
# test set
test_dataset = datasets.ImageFolder(
test_dir,
transforms.Compose(
[
transforms.Resize(size=(img_size, img_size)),
transforms.ToTensor(),
normalize,
]
),
)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=test_batch_size,
shuffle=False,
num_workers=4,
pin_memory=False,
)
log("training set size: {0}".format(len(train_loader.dataset)))
log("test set size: {0}".format(len(test_loader.dataset)))
log("batch size: {0}".format(train_batch_size))
# push set: needed for pruning because it is unnormalized
train_push_dataset = datasets.ImageFolder(
train_push_dir,
transforms.Compose(
[transforms.Resize(size=(img_size, img_size)), transforms.ToTensor(),]
),
)
train_push_loader = torch.utils.data.DataLoader(
train_push_dataset,
batch_size=train_push_batch_size,
shuffle=False,
num_workers=4,
pin_memory=False,
)
log("push set size: {0}".format(len(train_push_loader.dataset)))
tnt.test(
model=ppnet_multi, dataloader=test_loader, class_specific=class_specific, log=log
)
# prune prototypes
log("prune")
prune.prune_prototypes(
dataloader=train_push_loader,
prototype_network_parallel=ppnet_multi,
k=k,
prune_threshold=prune_threshold,
preprocess_input_function=preprocess_input_function, # normalize
original_model_dir=original_model_dir,
epoch_number=epoch,
# model_name=None,
log=log,
copy_prototype_imgs=True,
)
accu = tnt.test(
model=ppnet_multi, dataloader=test_loader, class_specific=class_specific, log=log
)
save.save_model_w_condition(
model=ppnet,
model_dir=model_dir,
model_name=original_model_name.split("push")[0] + "prune",
accu=accu,
target_accu=0.70,
log=log,
)
# last layer optimization
if optimize_last_layer:
last_layer_optimizer_specs = [{"params": ppnet.last_layer.parameters(), "lr": 1e-4}]
last_layer_optimizer = torch.optim.Adam(last_layer_optimizer_specs)
coefs = {
"crs_ent": 1,
"clst": 0.8,
"sep": -0.08,
"l1": 1e-4,
}
log("optimize last layer")
tnt.last_only(model=ppnet_multi, log=log)
for i in range(100):
log("iteration: \t{0}".format(i))
_ = tnt.train(
model=ppnet_multi,
dataloader=train_loader,
optimizer=last_layer_optimizer,
class_specific=class_specific,
coefs=coefs,
log=log,
)
accu = tnt.test(
model=ppnet_multi,
dataloader=test_loader,
class_specific=class_specific,
log=log,
)
save.save_model_w_condition(
model=ppnet,
model_dir=model_dir,
model_name=original_model_name.split("push")[0] + "_" + str(i) + "prune",
accu=accu,
target_accu=0.70,
log=log,
)
logclose()