-
Notifications
You must be signed in to change notification settings - Fork 2
/
pruning.py
173 lines (152 loc) · 7 KB
/
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
import os
import shutil
import torch
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import numpy as np
import argparse
from dataHelper import DatasetFolder
from helpers import makedir
import model
import last_layer
import push
import prune
import find_nearest
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('-modeldir', nargs=1, type=str)
parser.add_argument('-model', nargs=1, type=str)
parser.add_argument('-train_dir', nargs=1, type=str)
parser.add_argument('-test_dir', nargs=1, type=str)
parser.add_argument('-push_dir', nargs=1, type=str)
args = parser.parse_args()
optimize_last_layer = True
# proto_to_keep = [0,1,2,3,5,6,9,10,11,12,13] #for model /usr/xtmp/mammo/alina_saved_models/vgg16/0129_pushonall_topkk=9_fa=0.001_random=4/50_4push0.9546.pth
# proto_to_keep = [0,1,9,10,12] #for model /usr/xtmp/mammo/alina_saved_models/vgg16/0125_topkk=9_fa=0.001_random=12/100_3push0.9496.pth
# proto_to_keep = [0,1,7,9,10,11,14] #for model /usr/xtmp/mammo/alina_saved_models/vgg16/0125_topkk=9_fa=0.001_random=7/100_9push0.9514.pth
proto_to_keep = [0,1,5,9,10,11] #for model /usr/xtmp/mammo/alina_saved_models/vgg16/0125_topkk=9_fa=0.001_random=4/50_9push0.9645.pth
# proto_to_keep = [0,1,5,8,9,10,11,12] #for model /usr/xtmp/mammo/alina_saved_models/vgg16/finer_annotations_rerun/
# proto_to_keep = [0,1,2,5,6,8,9,10,11] #for model 1202_top05_fa1e-3_withfinerfa
# pruning parameters
k = 5
prune_threshold = 3
original_model_dir = args.modeldir[0] #'./saved_models/densenet161/003/'
original_model_name = args.model[0] #'10_16push0.8007.pth'
train_dir, test_dir, train_push_dir = args.train_dir[0], args.test_dir[0], args.push_dir[0]
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
train_batch_size = 80
test_batch_size = 100
img_size = 224
train_push_batch_size = 80
# all datasets
# train set
train_dataset = DatasetFolder(
train_dir,
augmentation=False,
loader=np.load,
extensions=("npy",),
transform = transforms.Compose([
torch.from_numpy,
]))
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=train_batch_size, shuffle=True,
num_workers=4, pin_memory=False)
# push set
train_push_dataset = DatasetFolder(
root = train_push_dir,
loader = np.load,
extensions=("npy",),
transform = transforms.Compose([
torch.from_numpy,
]))
train_push_loader = torch.utils.data.DataLoader(
train_push_dataset, batch_size=train_push_batch_size, shuffle=False,
num_workers=4, pin_memory=False)
# test set
test_dataset =DatasetFolder(
test_dir,
loader=np.load,
extensions=("npy",),
transform = transforms.Compose([
torch.from_numpy,
]))
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=test_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)
print(find_nearest.find_k_nearest_patches_to_prototypes(dataloader=train_push_loader,
prototype_network_parallel=ppnet_multi,
k=5,
preprocess_input_function=preprocess_input_function,
full_save=False,
log=log))
print("last layer trasnpose: \n", last_layer.show_last_layer_connections_T(ppnet))
# 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,
prototypes_to_keep=proto_to_keep)
accu = tnt.test(model=ppnet_multi, dataloader=test_loader,
class_specific=class_specific, log=log)
print(find_nearest.find_k_nearest_patches_to_prototypes(dataloader=train_push_loader,
prototype_network_parallel=ppnet_multi,
k=5,
preprocess_input_function=preprocess_input_function,
full_save=False,
log=log))
print("last layer trasnpose: \n", last_layer.show_last_layer_connections_T(ppnet))
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)
from settings import coefs
log('optimize last layer')
tnt.last_only(model=ppnet_multi, log=log)
for i in range(25):
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)
print("last layer trasnpose: \n", last_layer.show_last_layer_connections_T(ppnet))
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()