-
Notifications
You must be signed in to change notification settings - Fork 67
/
finetune.py
172 lines (137 loc) · 5.51 KB
/
finetune.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
# ------------------------------------------------------------------
# Author: Bowen Wu
# Email: [email protected]
# Affiliation: Sun Yat-sen University, Guangzhou
# Date: 13 JULY 2020
# ------------------------------------------------------------------
import os
import torch
import torch.optim as optim
from options.base_options import BaseOptions
from models.wrapper import ModelWrapper
from report import model_summary, Reporter
from data import custom_get_dataloaders
import torch.nn as nn
from tqdm import tqdm
import random
import numpy as np
import distiller
from thinning import thinning
def random_compression_scheduler(compression_scheduler, channel_configuration):
for i, item in enumerate(channel_configuration):
compression_scheduler.policies[1][i].pruner.desired_sparsity = item
return compression_scheduler
def get_channel_config(path, line_num):
# line_num starts from 0
with open(path) as data:
lines = data.readlines()
i = 0
for l in lines:
if i == line_num:
d = l.strip().split(" ")
channel_config = []
print("=" * 20, " read config")
for i in range(0, 2):
print("{} ".format(d[i]), end="")
for i in range(2, len(d)):
channel_config.append(float(d[i]))
break
i += 1
return channel_config
def train_epoch(model_wrapper, dataloader_train, optimizer):
optimizer.zero_grad()
model_wrapper._net.train()
loss_total = 0
total = 0
for iter_in_epoch, sample in enumerate(tqdm(dataloader_train, leave=False)):
loss = model_wrapper.get_loss(sample)
loss_total += loss.item()
total += 1
loss.backward()
optimizer.step()
optimizer.zero_grad()
return loss_total / total
def main(opt):
# basic settings
os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.gpu_ids)[1:-1]
if torch.cuda.is_available():
device = "cuda"
torch.backends.cudnn.benchmark = True
else:
device = "cpu"
##################### Get Dataloader ####################
dataloader_train, dataloader_val = custom_get_dataloaders(opt)
# dummy_input is sample input of dataloaders
if hasattr(dataloader_val, "dataset"):
dummy_input = dataloader_val.dataset.__getitem__(0)
dummy_input = dummy_input[0]
dummy_input = dummy_input.unsqueeze(0)
else:
# for imagenet dali loader
dummy_input = torch.rand(1, 3, 224, 224)
##################### Create Baseline Model ####################
net = ModelWrapper(opt)
net.load_checkpoint(opt.checkpoint)
flops_before, params_before = model_summary(net.get_compress_part(), dummy_input)
##################### Load Pruning Strategy ###############
compression_scheduler = distiller.file_config(
net.get_compress_part(), net.optimizer, opt.compress_schedule_path
)
channel_config = get_channel_config(
opt.search_result, opt.strategy_id
) # pruning strategy
compression_scheduler = random_compression_scheduler(
compression_scheduler, channel_config
)
###### Adaptive-BN-based Candidate Evaluation of Pruning Strategy ###
thinning(net, compression_scheduler, input_tensor=dummy_input)
flops_after, params_after = model_summary(net.get_compress_part(), dummy_input)
ratio = flops_after / flops_before
print("FLOPs ratio:", ratio)
net = net.to(device)
net.parallel(opt.gpu_ids)
net.get_compress_part().train()
with torch.no_grad():
for index, sample in enumerate(tqdm(dataloader_train, leave=False)):
_ = net.get_loss(sample)
if index > 100:
break
strategy_score = net.get_eval_scores(dataloader_val)["accuracy"]
print(
"Result file:{}, Strategy ID:{}, Evaluation score:{}".format(
opt.search_result, opt.strategy_id, strategy_score
)
)
##################### Fine-tuning #########################
lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(net.optimizer, opt.epoch)
reporter = Reporter(opt)
best_acc = 0
net._net.train()
for epoch in range(1, opt.epoch + 1):
reporter.log_metric("lr", net.optimizer.param_groups[0]["lr"], epoch)
train_loss = train_epoch(net, dataloader_train, net.optimizer,)
reporter.log_metric("train_loss", train_loss, epoch)
lr_scheduler.step()
scores = net.get_eval_scores(dataloader_val)
print("==> Evaluation: Epoch={} Acc={}".format(epoch, str(scores)))
reporter.log_metric("eval_acc", scores["accuracy"], epoch)
if scores["accuracy"] > best_acc:
best_acc = scores["accuracy"]
reporter.log_metric("best_acc", best_acc, epoch)
save_checkpoints(
scores["accuracy"], net._net, reporter, opt.exp_name, epoch,
)
print("==> Training epoch %d" % epoch)
def save_checkpoints(acc, model, reporter, exp_name, epoch):
if not hasattr(save_checkpoints, "best_acc"):
save_checkpoints.best_acc = 0
state_dict = model.state_dict()
reporter.save_checkpoint(state_dict, "{}_latest.pth".format(exp_name), epoch)
if acc > save_checkpoints.best_acc:
reporter.save_checkpoint(state_dict, "{}_best.pth".format(exp_name), epoch)
save_checkpoints.best_acc = acc
reporter.save_checkpoint(state_dict, "{}_{}.pth".format(exp_name, epoch), epoch)
if __name__ == "__main__":
# get options
opt = BaseOptions().parse()
main(opt)