Browse Source

EdgeTPU optimizations (#6808)

* 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 <glenn.jocher@ultralytics.com>
modifyDataloader
paradigm GitHub 2 years ago
parent
commit
c13d4ce7ef
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions
  1. +1
    -1
      export.py
  2. +6
    -4
      models/tf.py

+ 1
- 1
export.py View File

converter.target_spec.supported_types = [] converter.target_spec.supported_types = []
converter.inference_input_type = tf.uint8 # or tf.int8 converter.inference_input_type = tf.uint8 # or tf.int8
converter.inference_output_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') f = str(file).replace('.pt', '-int8.tflite')


tflite_model = converter.convert() tflite_model = converter.convert()

+ 6
- 4
models/tf.py View File

x.append(self.m[i](inputs[i])) x.append(self.m[i](inputs[i]))
# x(bs,20,20,255) to x(bs,3,20,20,85) # 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] 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 if not self.training: # inference
y = tf.sigmoid(x[i]) 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]) - 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 # Normalize xywh to 0-1 to reduce calibration error
xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
y = tf.concat([xy, wh, y[..., 4:]], -1) y = tf.concat([xy, wh, y[..., 4:]], -1)
z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no])) 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 @staticmethod
def _make_grid(nx=20, ny=20): def _make_grid(nx=20, ny=20):

Loading…
Cancel
Save