paradigm glenn-jocher commited on
Commit
c13d4ce
1 Parent(s): 701e117

EdgeTPU optimizations (#6808)

Browse files

* removed transpose op for better edgetpu support

* fix for training case

* enabled experimental new quantizer flag

* precalculate add and mul ops at compile time

Co-authored-by: Glenn Jocher <[email protected]>

Files changed (2) hide show
  1. export.py +1 -1
  2. models/tf.py +6 -4
export.py CHANGED
@@ -331,7 +331,7 @@ def export_tflite(keras_model, im, file, int8, data, ncalib, prefix=colorstr('Te
331
  converter.target_spec.supported_types = []
332
  converter.inference_input_type = tf.uint8 # or tf.int8
333
  converter.inference_output_type = tf.uint8 # or tf.int8
334
- converter.experimental_new_quantizer = False
335
  f = str(file).replace('.pt', '-int8.tflite')
336
 
337
  tflite_model = converter.convert()
 
331
  converter.target_spec.supported_types = []
332
  converter.inference_input_type = tf.uint8 # or tf.int8
333
  converter.inference_output_type = tf.uint8 # or tf.int8
334
+ converter.experimental_new_quantizer = True
335
  f = str(file).replace('.pt', '-int8.tflite')
336
 
337
  tflite_model = converter.convert()
models/tf.py CHANGED
@@ -222,19 +222,21 @@ class TFDetect(keras.layers.Layer):
222
  x.append(self.m[i](inputs[i]))
223
  # x(bs,20,20,255) to x(bs,3,20,20,85)
224
  ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
225
- x[i] = tf.transpose(tf.reshape(x[i], [-1, ny * nx, self.na, self.no]), [0, 2, 1, 3])
226
 
227
  if not self.training: # inference
228
  y = tf.sigmoid(x[i])
229
- xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
230
- wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]
 
 
231
  # Normalize xywh to 0-1 to reduce calibration error
232
  xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
233
  wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
234
  y = tf.concat([xy, wh, y[..., 4:]], -1)
235
  z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no]))
236
 
237
- return x if self.training else (tf.concat(z, 1), x)
238
 
239
  @staticmethod
240
  def _make_grid(nx=20, ny=20):
 
222
  x.append(self.m[i](inputs[i]))
223
  # x(bs,20,20,255) to x(bs,3,20,20,85)
224
  ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
225
+ x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no])
226
 
227
  if not self.training: # inference
228
  y = tf.sigmoid(x[i])
229
+ grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5
230
+ anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4
231
+ xy = (y[..., 0:2] * 2 + grid) * self.stride[i] # xy
232
+ wh = y[..., 2:4] ** 2 * anchor_grid
233
  # Normalize xywh to 0-1 to reduce calibration error
234
  xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
235
  wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
236
  y = tf.concat([xy, wh, y[..., 4:]], -1)
237
  z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no]))
238
 
239
+ return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1), x)
240
 
241
  @staticmethod
242
  def _make_grid(nx=20, ny=20):