-
Notifications
You must be signed in to change notification settings - Fork 17
/
prune_distill_step1.py
295 lines (206 loc) · 11.5 KB
/
prune_distill_step1.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
from torch.utils.data import DataLoader
from torch.optim import Adam
import torch
import torch.nn as nn
import random
from segment_anything_kd import SamPredictor, sam_model_registry
from segment_anything_kd.modeling.image_encoder import Attention
from load_sam_json import SamDataset
from torch.nn.functional import threshold, normalize
from segment_anything_kd.utils.transforms import ResizeLongestSide
from prune_funcs import calculate_iou, get_pos_init, del_pos_init, prune_sam_step1
import torch_pruning as tp
import copy
import json
from pycocotools import mask as mask_utils
import argparse
seed = 0
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
parser = argparse.ArgumentParser(description='SlimSAM')
parser.add_argument('--traindata_path', type=str,default = '')
parser.add_argument('--valdata_path', type=str,default = '')
parser.add_argument('--trainsize', type=int,default = 10000)
parser.add_argument('--gradsize', type=int,default = 1000)
parser.add_argument('--valsize', type=int,default = 50)
parser.add_argument('--epochs', type=int,default = 20)
parser.add_argument('--norm_type', type=str,default = 'mean')
parser.add_argument('--imptype', type=str,default = 'Disturb')
parser.add_argument('--global_way', type=bool,default = False)
parser.add_argument('--prune_ratio', type=float,default = 0.5)
args, unparsed = parser.parse_known_args()
def train_model():
# torch.backends.cudnn.deterministic = True
device = torch.device("cuda")
print("CUDA visible devices: " + str(torch.cuda.device_count()))
print("CUDA Device Name: " + str(torch.cuda.get_device_name(device)))
train_root_folder = args.traindata_path
val_root_folder = args.valdata_path
TRAIN_SIZE = args.trainsize
VAL_SIZE = args.valsize
GRAD_SIZE = args.gradsize
num_train_epochs = args.epochs
# Creating dataset loaders
batch_size = 1
grad_dataset = SamDataset(root_folder=train_root_folder, dataset_size=GRAD_SIZE, val=False)
grad_loader = DataLoader(dataset=grad_dataset, batch_size=1, shuffle=False, num_workers=4,
pin_memory=True, drop_last=True)
train_dataset = SamDataset(root_folder=train_root_folder, dataset_size=TRAIN_SIZE, val=False)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=4,
pin_memory=True, drop_last=True)
val_dataset = SamDataset(root_folder=val_root_folder, dataset_size=VAL_SIZE, val=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=1, shuffle=False, num_workers=2,
pin_memory=True, drop_last=False)
# student model
model_type = 'vit_b'
checkpoint = 'checkpoints/sam_vit_b_qkv.pth'
model = sam_model_registry[model_type](checkpoint=checkpoint)
# teacher model
teacher_model_type = 'vit_b'
checkpoint = 'checkpoints/sam_vit_b_qkv.pth'
teacher_model = sam_model_registry[teacher_model_type](checkpoint=checkpoint)
teacher_model.to(device)
teacher_model.eval()
MSE_loss = torch.nn.MSELoss()
lr = 1e-4
ratio = args.prune_ratio
loss_fn = torch.nn.MSELoss()
transform = ResizeLongestSide(1024)
norm_type = args.norm_type
imptype = args.imptype
global_way = args.global_way
a_weight = 0.5
round_to = model.image_encoder.num_heads
print("===========================Parameter Settings===========================")
print("Pruning Ratio:",ratio)
print("VIT num_heads:",round_to)
print("norm_type:",norm_type)
print("imptype:",imptype)
print("global:",global_way)
print("learning rate:",lr)
print("a_weight:",a_weight)
print("round_to",round_to)
print("TRAIN_SIZE",TRAIN_SIZE,"VAL_SIZE",VAL_SIZE, "GRAD_SIZE",GRAD_SIZE,"Epochs",num_train_epochs)
model_name = teacher_model_type
example_inputs = torch.randn(1, 3, 1024, 1024)
for k in range(1):
############################################get initial grad for importance estimation############################################
best_iou = 0
model.to(device)
model.image_encoder.train()
grad_iter = iter(grad_loader)
for i in range(len(grad_iter)):
batch = next(grad_iter)
input_image = batch["input_image"].to(device)
with torch.no_grad():
teacher_embedding, teacher_qkv_emb1, teacher_qkv_emb2, teacher_mid_emb,_ = teacher_model.image_encoder(input_image)
teacher_embedding += torch.normal(mean=0,std=0.01,size=(1, 256, 64, 64)).to(device) #Disturbed image embedding
student_embedding, student_qkv_emb1, student_qkv_emb2, student_mid_emb,_= model.image_encoder(input_image)
loss = loss_fn(teacher_embedding, student_embedding)
loss.backward()
#########################################################################################################
print("===========================Pruning Start===========================")
#Embedding Pruning
model.cpu().eval()
model = del_pos_init(model)
model.image_encoder = prune_sam_step1(model=model.image_encoder, example_inputs=example_inputs, model_name=model_name, round_to=round_to, ratio=ratio, imptype = imptype, norm_type=norm_type, global_way=global_way)
model = get_pos_init(model)
model.to(device)
model.image_encoder = torch.nn.DataParallel(model.image_encoder)
model.image_encoder.train()
optimizer = torch.optim.Adam(model.image_encoder.parameters(), lr=lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max',factor=0.5,patience=3,verbose=True)
model.zero_grad()
teacher_model.zero_grad()
#Bottleneck Aligning
for epoch in range(num_train_epochs):
torch.cuda.empty_cache()
train_iter = iter(train_loader)
for i in range(len(train_iter)):
batch = next(train_iter)
input_image = batch["input_image"].to(device)
with torch.no_grad():
teacher_embedding,teacher_qkv_emb1,teacher_qkv_emb2,teacher_mid_emb,teacher_block_emb = teacher_model.image_encoder(input_image)
student_embedding, student_qkv_emb1, student_qkv_emb2, student_mid_emb,student_block_emb = model.image_encoder(input_image)
if epoch<10:
loss = (1-a_weight)*loss_fn(student_embedding, teacher_embedding)+a_weight*loss_fn(student_qkv_emb1, teacher_qkv_emb1)+a_weight*loss_fn(student_qkv_emb2, student_qkv_emb2)+a_weight*loss_fn(student_mid_emb, teacher_mid_emb)
loss.backward()
else:
loss = loss_fn(student_embedding, teacher_embedding)
loss.backward()
#### batchsize×4 ####
if i%4==3:
optimizer.step()
optimizer.zero_grad()
#validation
if i == len(train_iter)-1:
iou = 0
model.image_encoder.eval()
with torch.no_grad():
val_iter = iter(val_loader)
for j in range(len(val_iter)):
batch = next(val_iter)
input_image = batch["input_image"].to(device)
input_size = batch["input_size"]
original_image_size = batch["original_image_size"]
original_image_size[0] = original_image_size[0].numpy()[0]
original_image_size[1] = original_image_size[1].numpy()[0]
original_image_size = ([original_image_size[0],original_image_size[1]])
input_size[0] = input_size[0].numpy()[0]
input_size[1] = input_size[1].numpy()[0]
input_size = ([input_size[0],input_size[1]])
id = batch["id"]
annot = batch["annot"][0]
path = id[0]
with open(annot, encoding="utf-8") as f:
dict_data = json.load(f)
dict_data = dict_data["annotations"]
sub_count = 0
sub_iou = 0
for example in dict_data:
sub_count += 1
input_point = np.array(example['point_coords'])
input_label = np.array([1])
mask = mask_utils.decode(example["segmentation"])
point_coords = transform.apply_coords(input_point, original_image_size)
coords_torch = torch.as_tensor(point_coords, dtype=torch.float, device=device)
labels_torch = torch.as_tensor(input_label, dtype=torch.int, device=device)
coords_torch, labels_torch = coords_torch[None, :, :], labels_torch[None, :]
points = (coords_torch, labels_torch)
# Model inference
image_embedding,_,_,_,_ = model.image_encoder(input_image)
sparse_embeddings, dense_embeddings = model.prompt_encoder(
points=points,
boxes=None,
masks=None,
)
low_res_masks, iou_predictions = model.mask_decoder(
image_embeddings=image_embedding,
image_pe=model.prompt_encoder.get_dense_pe(),
sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings,
multimask_output=False,
)
student_masks = teacher_model.postprocess_masks(low_res_masks, input_size, original_image_size)
student_masks = student_masks > teacher_model.mask_threshold
student_masks = student_masks[0].detach().cpu().numpy()[0]
sub_iou += calculate_iou(student_masks, mask)
sub_iou = sub_iou/sub_count
iou += sub_iou
iou = iou/len(val_iter)
model.image_encoder.train()
model.image_encoder.eval()
if iou>=best_iou:
best_iou = iou
filename = 'checkpoints/vit_b_slim_step1_'+'.pth'
torch.save(model, filename)
print("save checkpoint")
model.image_encoder.train()
scheduler.step(iou)
print("epoch:",epoch)
print("IOU: {} Best IOU {}".format(iou,best_iou))
if __name__ == '__main__':
train_model()