From 33854988bcd06ebd122cff25d0bec1a44f7af463 Mon Sep 17 00:00:00 2001 From: paradigm Date: Mon, 28 Feb 2022 15:26:58 +0100 Subject: [PATCH 1/4] removed transpose op for better edgetpu support --- models/tf.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/models/tf.py b/models/tf.py index 74681e403afd..b4da12258f9a 100644 --- a/models/tf.py +++ b/models/tf.py @@ -222,12 +222,14 @@ def call(self, inputs): x.append(self.m[i](inputs[i])) # x(bs,20,20,255) to x(bs,3,20,20,85) ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i] - x[i] = tf.transpose(tf.reshape(x[i], [-1, ny * nx, self.na, self.no]), [0, 2, 1, 3]) + x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no]) if not self.training: # inference y = tf.sigmoid(x[i]) - xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy - wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] + grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) + anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) + xy = (y[..., 0:2] * 2 - 0.5 + grid) * self.stride[i] # xy + wh = (y[..., 2:4] * 2) ** 2 * anchor_grid # Normalize xywh to 0-1 to reduce calibration error xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) From 48f9099b8048c37c60ce94252108ee3462c2ae92 Mon Sep 17 00:00:00 2001 From: paradigm Date: Mon, 28 Feb 2022 16:18:34 +0100 Subject: [PATCH 2/4] fix for training case --- models/tf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/tf.py b/models/tf.py index b4da12258f9a..dd6cf5df4171 100644 --- a/models/tf.py +++ b/models/tf.py @@ -236,7 +236,7 @@ def call(self, inputs): y = tf.concat([xy, wh, y[..., 4:]], -1) z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no])) - return x if self.training else (tf.concat(z, 1), x) + return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): From eb37fb976dab12c5f955b96da4ff454d82130bff Mon Sep 17 00:00:00 2001 From: paradigm Date: Mon, 7 Mar 2022 10:50:47 +0100 Subject: [PATCH 3/4] enabled experimental new quantizer flag --- export.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/export.py b/export.py index 7a5205d55ee6..956667b5843c 100644 --- a/export.py +++ b/export.py @@ -330,7 +330,7 @@ def export_tflite(keras_model, im, file, int8, data, ncalib, prefix=colorstr('Te converter.target_spec.supported_types = [] converter.inference_input_type = tf.uint8 # or tf.int8 converter.inference_output_type = tf.uint8 # or tf.int8 - converter.experimental_new_quantizer = False + converter.experimental_new_quantizer = True f = str(file).replace('.pt', '-int8.tflite') tflite_model = converter.convert() From 55f394b1d0932c3ab289c3baa99e0863791a3c7a Mon Sep 17 00:00:00 2001 From: paradigm Date: Mon, 7 Mar 2022 10:53:29 +0100 Subject: [PATCH 4/4] precalculate add and mul ops at compile time --- models/tf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/models/tf.py b/models/tf.py index dd6cf5df4171..728907f8fb47 100644 --- a/models/tf.py +++ b/models/tf.py @@ -226,10 +226,10 @@ def call(self, inputs): if not self.training: # inference y = tf.sigmoid(x[i]) - grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) - xy = (y[..., 0:2] * 2 - 0.5 + grid) * self.stride[i] # xy - wh = (y[..., 2:4] * 2) ** 2 * anchor_grid + grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5 + anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4 + xy = (y[..., 0:2] * 2 + grid) * self.stride[i] # xy + wh = y[..., 2:4] ** 2 * anchor_grid # Normalize xywh to 0-1 to reduce calibration error xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)