-
Notifications
You must be signed in to change notification settings - Fork 15
/
decompose_model.py
299 lines (264 loc) · 9.22 KB
/
decompose_model.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
296
297
298
299
"""Tensor decomposition YOLO model.
- Author: Jongkuk Lim
- Contact: limjk@jmarple.ai
"""
import argparse
import os
import time
from copy import deepcopy
from pathlib import Path
from typing import Tuple
import numpy as np
import torch
import yaml
from kindle import YOLOModel
from torch import nn
from scripts.data_loader.data_loader import LoadImagesAndLabels
from scripts.tensor_decomposition.decomposition import decompose_model
from scripts.utils.logger import colorstr, get_logger
from scripts.utils.torch_utils import count_param, select_device
from scripts.utils.train_utils import YoloValidator
from scripts.utils.wandb_utils import get_ckpt_path
LOGGER = get_logger(__name__)
def get_parser() -> argparse.Namespace:
"""Get argument parser."""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("--weights", type=str, default="", help="Model weight path.")
parser.add_argument(
"--data-cfg",
type=str,
default="res/configs/data/coco.yaml",
help="Validation image root.",
)
parser.add_argument(
"--device",
type=str,
default="0",
help="GPU device id. '' will use all GPUs. EX) '0,2' or 'cpu'",
)
parser.add_argument(
"--dst",
type=str,
default=os.path.join("exp", "decompose"),
help="Export directory. Directory will be {dst}/decompose/{DATE}_runs1, ...",
)
parser.add_argument("--batch-size", type=int, default=8, help="Batch size to test")
parser.add_argument("-iw", "--img-width", type=int, default=640, help="Image width")
parser.add_argument(
"-ih",
"--img-height",
type=int,
default=-1,
help="Image height. (-1 will set image height to be identical to image width.)",
)
parser.add_argument(
"--prune-step",
default=0.01,
type=float,
help="Prunning trial max step. Maximum step while searching prunning ratio with binary search. Pruning will be applied priro to decomposition. If prune-step is equal or smaller than 0.0, prunning will not be applied.",
)
parser.add_argument(
"--loss-thr",
default=0.1,
type=float,
help="Loss value to compare original model output and decomposed model output to judge to switch to decomposed conv.",
)
parser.add_argument(
"-ct", "--conf-t", type=float, default=0.001, help="Confidence threshold."
)
parser.add_argument(
"-it", "--iou-t", type=float, default=0.65, help="IoU threshold."
)
parser.add_argument(
"--rect",
action="store_true",
dest="rect",
default=True,
help="Use rectangular image",
)
parser.add_argument(
"--no-rect", action="store_false", dest="rect", help="Use squared image.",
)
parser.add_argument(
"--single-cls",
action="store_true",
default=False,
help="Validate as single class only.",
)
parser.add_argument(
"--plot",
action="store_true",
default=False,
help="Save validation result plot.",
)
parser.add_argument("--seed", type=int, default=0, help="Random seed.")
return parser.parse_args()
def run_decompose(
args: argparse.Namespace,
model: nn.Module,
validator: YoloValidator,
device: torch.device,
) -> Tuple[nn.Module, Tuple[Tuple[list, ...], np.ndarray, tuple]]:
"""Run tensor decomposition on given model.
Args:
args: arguments for the tensor decomposition.
args.prune_step(float): prune step.
args.loss_thr(float): Loss threshold for decomposition.
model: Original model.
validator: validation runner.
device: device to run validation.
Return:
decomposed_model,
(
(mP, mR, mAP0.5, mAP0.5:0.95, 0, 0, 0),
mAP0.5 by classes,
time measured (pre-processing, inference, NMS)
)
"""
decomposed_model = deepcopy(model.cpu())
decompose_model(
decomposed_model, loss_thr=args.loss_thr, prune_step=args.prune_step
)
LOGGER.info(
f"Decomposed with prunning step: {args.prune_step}, decomposition loss threshold: {args.loss_thr}"
)
LOGGER.info(f" Original # of param: {count_param(model)}")
LOGGER.info(f"Decomposed # of param: {count_param(decomposed_model)}")
decomposed_model.to(device)
decomposed_model.eval()
validator.model = decomposed_model
t0 = time.monotonic()
val_result = validator.validation()
time_took = time.monotonic() - t0
LOGGER.info(f"Time took: {time_took:.5f}s")
return decomposed_model, val_result
if __name__ == "__main__":
args = get_parser()
# Fix random seed for reproducibility
torch.manual_seed(args.seed)
LOGGER.info(f"Random Seed: {args.seed}")
if args.img_height < 0:
args.img_height = args.img_width
if not args.weights:
LOGGER.error(
"Either "
+ colorstr("bold", "--weights")
+ " must be provided. (Current value: "
+ colorstr("bold", f"{args.weights}")
+ ")"
)
exit(1)
device = select_device(args.device, args.batch_size)
ckpt_path = get_ckpt_path(args.weights)
ckpt = torch.load(ckpt_path)
if isinstance(ckpt, YOLOModel):
model = ckpt.float()
elif "ema" in ckpt.keys() and ckpt["ema"] is not None:
model = ckpt["ema"].float()
else:
model = ckpt["model"].float()
with open(args.data_cfg, "r") as f:
data_cfg = yaml.safe_load(f)
# TODO(jeikeilim): config structure should be changed.
cfg = {
"train": {
"single_cls": args.single_cls,
"plot": args.plot,
"batch_size": args.batch_size,
"image_size": args.img_width,
},
"hyper_params": {"conf_t": args.conf_t, "iou_t": args.iou_t},
}
stride_size = int(max(model.stride)) # type: ignore
val_dataset = LoadImagesAndLabels(
data_cfg["val_path"],
img_size=args.img_width,
batch_size=args.batch_size,
rect=args.rect,
label_type="labels",
cache_images=None,
single_cls=False,
stride=stride_size,
pad=0.5,
n_skip=0,
prefix="[val]",
yolo_augmentation=None,
augmentation=None,
preprocess=None,
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
num_workers=min(os.cpu_count(), args.batch_size), # type: ignore
pin_memory=True,
collate_fn=LoadImagesAndLabels.collate_fn,
)
validator = YoloValidator(
model.to(device).eval(),
val_loader,
device,
cfg,
compute_loss=False,
half=False,
log_dir=args.dst,
incremental_log_dir=True,
export=True,
)
LOGGER.info("Validating original model ...")
t0 = time.monotonic()
original_result = validator.validation()
original_time = time.monotonic() - t0
decomposed_model, decomposed_result = run_decompose(args, model, validator, device)
LOGGER.info(
f"[ Original] # param: {count_param(model):,d}, mAP0.5: {original_result[0][2]}, Speed(pre-process, inference, NMS): {original_result[2][0]:.3f}, {original_result[2][1]:.3f}, {original_result[2][2]:.3f}"
)
LOGGER.info(
f"[Decomposed] # param: {count_param(decomposed_model):,d}, mAP0.5: {decomposed_result[0][2]}, Speed(pre-process, inference, NMS): {decomposed_result[2][0]:.3f}, {decomposed_result[2][1]:.3f}, {decomposed_result[2][2]:.3f}"
)
result = vars(args)
result["result"] = {
"original": {
"n_param": count_param(model),
"metric": {
"mR": original_result[0][0],
"mP": original_result[0][1],
"mAP50": original_result[0][2],
"mAP": original_result[0][3],
},
"mAP50_by_class": original_result[1],
"time": {
"preprocessing": original_result[2][0],
"inference": original_result[2][1],
"NMS": original_result[2][2],
},
},
"decomposed": {
"n_param": count_param(decomposed_model),
"metric": {
"mR": decomposed_result[0][0],
"mP": decomposed_result[0][1],
"mAP50": decomposed_result[0][2],
"mAP": decomposed_result[0][3],
},
"mAP50_by_class": decomposed_result[1],
"time": {
"preprocessing": decomposed_result[2][0],
"inference": decomposed_result[2][1],
"NMS": decomposed_result[2][2],
},
},
}
cfg_path = os.path.join(validator.log_dir, "args.yaml")
with open(cfg_path, "w") as f:
yaml.dump(result, f)
LOGGER.info("Decomposition config saved to " + colorstr("bold", cfg_path))
weight_path = (
str(Path(validator.log_dir) / Path(args.weights).stem)
+ f"_decomposed_seed_{args.seed}.pt"
)
torch.save(
{"model": decomposed_model.cpu().half(), "decomposed": True}, weight_path
)
LOGGER.info("Decomposed model saved to " + colorstr("bold", weight_path))