zldrobit commited on
Commit
3beb871
1 Parent(s): 6b44ecd

Multiple TF export improvements (#4824)

Browse files

* Add fused conv support

* Set all saved_model values to non trainable

* Fix TFLite fp16 model export

* Fix int8 TFLite conversion

Files changed (2) hide show
  1. export.py +5 -2
  2. models/tf.py +3 -2
export.py CHANGED
@@ -145,6 +145,7 @@ def export_saved_model(model, im, file, dynamic,
145
  inputs = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size)
146
  outputs = tf_model.predict(inputs, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres)
147
  keras_model = keras.Model(inputs=inputs, outputs=outputs)
 
148
  keras_model.summary()
149
  keras_model.save(f, save_format='tf')
150
 
@@ -183,15 +184,17 @@ def export_tflite(keras_model, im, file, int8, data, ncalib, prefix=colorstr('Te
183
 
184
  print(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
185
  batch_size, ch, *imgsz = list(im.shape) # BCHW
186
- f = file.with_suffix('.tflite')
187
 
188
  converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
189
  converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
 
190
  converter.optimizations = [tf.lite.Optimize.DEFAULT]
191
  if int8:
192
  dataset = LoadImages(check_dataset(data)['train'], img_size=imgsz, auto=False) # representative data
193
  converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib)
194
  converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
 
195
  converter.inference_input_type = tf.uint8 # or tf.int8
196
  converter.inference_output_type = tf.uint8 # or tf.int8
197
  converter.experimental_new_quantizer = False
@@ -249,7 +252,7 @@ def run(data=ROOT / 'data/coco128.yaml', # 'dataset.yaml path'
249
  # Load PyTorch model
250
  device = select_device(device)
251
  assert not (device.type == 'cpu' and half), '--half only compatible with GPU export, i.e. use --device 0'
252
- model = attempt_load(weights, map_location=device, inplace=True, fuse=not any(tf_exports)) # load FP32 model
253
  nc, names = model.nc, model.names # number of classes, class names
254
 
255
  # Input
 
145
  inputs = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size)
146
  outputs = tf_model.predict(inputs, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres)
147
  keras_model = keras.Model(inputs=inputs, outputs=outputs)
148
+ keras_model.trainable = False
149
  keras_model.summary()
150
  keras_model.save(f, save_format='tf')
151
 
 
184
 
185
  print(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
186
  batch_size, ch, *imgsz = list(im.shape) # BCHW
187
+ f = str(file).replace('.pt', '-fp16.tflite')
188
 
189
  converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
190
  converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
191
+ converter.target_spec.supported_types = [tf.float16]
192
  converter.optimizations = [tf.lite.Optimize.DEFAULT]
193
  if int8:
194
  dataset = LoadImages(check_dataset(data)['train'], img_size=imgsz, auto=False) # representative data
195
  converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib)
196
  converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
197
+ converter.target_spec.supported_types = []
198
  converter.inference_input_type = tf.uint8 # or tf.int8
199
  converter.inference_output_type = tf.uint8 # or tf.int8
200
  converter.experimental_new_quantizer = False
 
252
  # Load PyTorch model
253
  device = select_device(device)
254
  assert not (device.type == 'cpu' and half), '--half only compatible with GPU export, i.e. use --device 0'
255
+ model = attempt_load(weights, map_location=device, inplace=True, fuse=True) # load FP32 model
256
  nc, names = model.nc, model.names # number of classes, class names
257
 
258
  # Input
models/tf.py CHANGED
@@ -70,8 +70,9 @@ class TFConv(keras.layers.Layer):
70
  # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
71
 
72
  conv = keras.layers.Conv2D(
73
- c2, k, s, 'SAME' if s == 1 else 'VALID', use_bias=False,
74
- kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()))
 
75
  self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
76
  self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
77
 
 
70
  # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
71
 
72
  conv = keras.layers.Conv2D(
73
+ c2, k, s, 'SAME' if s == 1 else 'VALID', use_bias=False if hasattr(w, 'bn') else True,
74
+ kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
75
+ bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
76
  self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
77
  self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
78