Yiyao Wang
commited on
Commit
·
d30c3db
1
Parent(s):
46b1f95
revise the quantize-ort to quantize crnn into int8 with high accuracy (#94)
Browse files* revise the quantize-ort to quantize crnn into int8 with high accuracy
* change the comment to English
* reduce supported suffix
tools/quantize/quantize-ort.py
CHANGED
|
@@ -29,9 +29,10 @@ class DataReader(CalibrationDataReader):
|
|
| 29 |
|
| 30 |
def get_calibration_data(self, image_dir):
|
| 31 |
blobs = []
|
|
|
|
| 32 |
for image_name in os.listdir(image_dir):
|
| 33 |
image_name_suffix = image_name.split('.')[-1].lower()
|
| 34 |
-
if image_name_suffix
|
| 35 |
continue
|
| 36 |
img = cv.imread(os.path.join(image_dir, image_name))
|
| 37 |
img = self.transforms(img)
|
|
@@ -55,13 +56,13 @@ class Quantize:
|
|
| 55 |
|
| 56 |
def check_opset(self, convert=True):
|
| 57 |
model = onnx.load(self.model_path)
|
| 58 |
-
if model.opset_import[0].version !=
|
| 59 |
-
print('\tmodel opset version: {}. Converting to opset
|
| 60 |
-
# convert opset version to
|
| 61 |
-
|
| 62 |
# save converted model
|
| 63 |
-
output_name = '{}-
|
| 64 |
-
onnx.save_model(
|
| 65 |
# update model_path for quantization
|
| 66 |
self.model_path = output_name
|
| 67 |
|
|
@@ -98,7 +99,7 @@ models=dict(
|
|
| 98 |
# TBD: DB-EN & DB-CN
|
| 99 |
crnn_en=Quantize(model_path='../../models/text_recognition_crnn/text_recognition_CRNN_EN_2021sep.onnx',
|
| 100 |
calibration_image_dir='../../benchmark/data/text',
|
| 101 |
-
transforms=Compose([Resize(size=(100, 32)), ColorConvert(ctype=cv.COLOR_BGR2GRAY)])),
|
| 102 |
crnn_cn=Quantize(model_path='../../models/text_recognition_crnn/text_recognition_CRNN_CN_2021nov.onnx',
|
| 103 |
calibration_image_dir='../../benchmark/data/text',
|
| 104 |
transforms=Compose([Resize(size=(100, 32))]))
|
|
|
|
| 29 |
|
| 30 |
def get_calibration_data(self, image_dir):
|
| 31 |
blobs = []
|
| 32 |
+
supported = ["jpg", "png"] # supported file suffix
|
| 33 |
for image_name in os.listdir(image_dir):
|
| 34 |
image_name_suffix = image_name.split('.')[-1].lower()
|
| 35 |
+
if image_name_suffix not in supported:
|
| 36 |
continue
|
| 37 |
img = cv.imread(os.path.join(image_dir, image_name))
|
| 38 |
img = self.transforms(img)
|
|
|
|
| 56 |
|
| 57 |
def check_opset(self, convert=True):
|
| 58 |
model = onnx.load(self.model_path)
|
| 59 |
+
if model.opset_import[0].version != 13:
|
| 60 |
+
print('\tmodel opset version: {}. Converting to opset 13'.format(model.opset_import[0].version))
|
| 61 |
+
# convert opset version to 13
|
| 62 |
+
model_opset13 = version_converter.convert_version(model, 13)
|
| 63 |
# save converted model
|
| 64 |
+
output_name = '{}-opset.onnx'.format(self.model_path[:-5])
|
| 65 |
+
onnx.save_model(model_opset13, output_name)
|
| 66 |
# update model_path for quantization
|
| 67 |
self.model_path = output_name
|
| 68 |
|
|
|
|
| 99 |
# TBD: DB-EN & DB-CN
|
| 100 |
crnn_en=Quantize(model_path='../../models/text_recognition_crnn/text_recognition_CRNN_EN_2021sep.onnx',
|
| 101 |
calibration_image_dir='../../benchmark/data/text',
|
| 102 |
+
transforms=Compose([Resize(size=(100, 32)), Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5]), ColorConvert(ctype=cv.COLOR_BGR2GRAY)])),
|
| 103 |
crnn_cn=Quantize(model_path='../../models/text_recognition_crnn/text_recognition_CRNN_CN_2021nov.onnx',
|
| 104 |
calibration_image_dir='../../benchmark/data/text',
|
| 105 |
transforms=Compose([Resize(size=(100, 32))]))
|
tools/quantize/transform.py
CHANGED
|
@@ -41,6 +41,7 @@ class Normalize:
|
|
| 41 |
self.std = std
|
| 42 |
|
| 43 |
def __call__(self, img):
|
|
|
|
| 44 |
if self.mean is not None:
|
| 45 |
img[:, :, 0] = img[:, :, 0] - self.mean[0]
|
| 46 |
img[:, :, 1] = img[:, :, 1] - self.mean[1]
|
|
|
|
| 41 |
self.std = std
|
| 42 |
|
| 43 |
def __call__(self, img):
|
| 44 |
+
img = img.astype("float32")
|
| 45 |
if self.mean is not None:
|
| 46 |
img[:, :, 0] = img[:, :, 0] - self.mean[0]
|
| 47 |
img[:, :, 1] = img[:, :, 1] - self.mean[1]
|