-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpytorch_yolov5_ptq.py
162 lines (142 loc) · 5.28 KB
/
pytorch_yolov5_ptq.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
from torchvision import models, datasets
import pytorch_quantization
from pytorch_quantization import nn as quant_nn
from pytorch_quantization import quant_modules
from pytorch_quantization import calib
from tqdm import tqdm
print(pytorch_quantization.__version__)
import os
import tensorrt as trt
import numpy as np
import time
import wget
import tarfile
import shutil
import cv2
import random
from models.yolo import Model
from models.experimental import End2End
def compute_amax(model, **kwargs):
# Load calib result
for name, module in model.named_modules():
if isinstance(module, quant_nn.TensorQuantizer):
if module._calibrator is not None:
if isinstance(module._calibrator, calib.MaxCalibrator):
module.load_calib_amax()
else:
module.load_calib_amax(**kwargs)
model.cuda()
def collect_stats(model, data_loader):
"""Feed data to the network and collect statistics"""
# Enable calibrators
for name, module in model.named_modules():
if isinstance(module, quant_nn.TensorQuantizer):
if module._calibrator is not None:
module.disable_quant()
module.enable_calib()
else:
module.disable()
# Feed data to the network for collecting stats
for i, image in tqdm(enumerate(data_loader)):
model(image.cuda())
# Disable calibrators
for name, module in model.named_modules():
if isinstance(module, quant_nn.TensorQuantizer):
if module._calibrator is not None:
module.enable_quant()
module.disable_calib()
else:
module.enable()
def get_crop_bbox(img, crop_size):
"""Randomly get a crop bounding box."""
margin_h = max(img.shape[0] - crop_size[0], 0)
margin_w = max(img.shape[1] - crop_size[1], 0)
offset_h = np.random.randint(0, margin_h + 1)
offset_w = np.random.randint(0, margin_w + 1)
crop_y1, crop_y2 = offset_h, offset_h + crop_size[0]
crop_x1, crop_x2 = offset_w, offset_w + crop_size[1]
return crop_x1, crop_y1, crop_x2, crop_y2
def crop(img, crop_bbox):
"""Crop from ``img``"""
crop_x1, crop_y1, crop_x2, crop_y2 = crop_bbox
img = img[crop_y1:crop_y2, crop_x1:crop_x2, ...]
return img
class CaliData(data.Dataset):
def __init__(self, path, num, inputsize=[384, 1280]):
self.img_files = [os.path.join(path, p) for p in os.listdir(path) if p.endswith('jpg')]
random.shuffle(self.img_files)
self.img_files = self.img_files[:num]
self.height = inputsize[0]
self.width = inputsize[1]
def __getitem__(self, index):
f = self.img_files[index]
img = cv2.imread(f) # BGR
crop_size = [self.height, self.width]
crop_bbox = get_crop_bbox(img, crop_size)
# crop the image
img = crop(img, crop_bbox)
img = img.transpose((2, 0, 1))[::-1, :, :] # BHWC to BCHW ,BGR to RGB
img = np.ascontiguousarray(img)
img = img.astype(np.float32) / 255.
return img
def __len__(self):
return len(self.img_files)
if __name__ == '__main__':
pt_file = 'runs/train/exp/weights/best.pt'
calib_path = 'XX/train'
num = 2000 # 用来校正的数目
batchsize = 4
# 准备数据
dataset = CaliData(calib_path, num)
dataloader = data.DataLoader(dataset, batch_size=batchsize)
# 模型加载
quant_modules.initialize() #保证原始模型层替换为量化层
device = torch.device('cuda:0')
ckpt = torch.load(pt_file, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
# QAT
q_model = ckpt['model']
yaml = ckpt['model'].yaml
q_model = Model(yaml, ch=yaml['ch'], nc=yaml['nc']).to(device) # creat
q_model.eval()
q_model = End2End(q_model).cuda()
ckpt = ckpt['model']
modified_state_dict = {}
for key, val in ckpt.state_dict().items():
# Remove 'module.' from the key names
if key.startswith('module'):
modified_state_dict[key[7:]] = val
else:
modified_state_dict[key] = val
q_model.model.load_state_dict(modified_state_dict)
# Calibrate the model using calibration technique.
with torch.no_grad():
collect_stats(q_model, dataloader)
compute_amax(q_model, method="entropy")
# Set static member of TensorQuantizer to use Pytorch’s own fake quantization functions
quant_nn.TensorQuantizer.use_fb_fake_quant = True
# Exporting to ONNX
dummy_input = torch.randn(26, 3, 384, 1280, device='cuda')
input_names = ["images"]
output_names = ["num_dets", 'det_boxes']
# output_names = ['outputs']
save_path = '/'.join(pt_file.split('/')[:-1])
onnx_file = os.path.join(save_path, 'best_ptq.onnx')
dynamic = dict()
dynamic['images'] = {0: 'batch'}
dynamic['num_dets'] = {0: 'batch'}
dynamic['det_boxes'] = {0: 'batch'}
torch.onnx.export(
q_model,
dummy_input,
onnx_file,
verbose=False,
opset_version=13,
do_constant_folding=False,
input_names=input_names,
output_names=output_names,
dynamic_axes=dynamic)