MCPcopy
hub / github.com/ultralytics/yolov5 / export_tflite

Function export_tflite

export.py:373–400  ·  view source on GitHub ↗
(keras_model, im, file, int8, data, nms, agnostic_nms, prefix=colorstr('TensorFlow Lite:'))

Source from the content-addressed store, hash-verified

371
372@try_export
373def export_tflite(keras_model, im, file, int8, data, nms, agnostic_nms, prefix=colorstr('TensorFlow Lite:')):
374 # YOLOv5 TensorFlow Lite export
375 import tensorflow as tf
376
377 LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
378 batch_size, ch, *imgsz = list(im.shape) # BCHW
379 f = str(file).replace('.pt', '-fp16.tflite')
380
381 converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
382 converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
383 converter.target_spec.supported_types = [tf.float16]
384 converter.optimizations = [tf.lite.Optimize.DEFAULT]
385 if int8:
386 from models.tf import representative_dataset_gen
387 dataset = LoadImages(check_dataset(check_yaml(data))['train'], img_size=imgsz, auto=False)
388 converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib=100)
389 converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
390 converter.target_spec.supported_types = []
391 converter.inference_input_type = tf.uint8 # or tf.int8
392 converter.inference_output_type = tf.uint8 # or tf.int8
393 converter.experimental_new_quantizer = True
394 f = str(file).replace('.pt', '-int8.tflite')
395 if nms or agnostic_nms:
396 converter.target_spec.supported_ops.append(tf.lite.OpsSet.SELECT_TF_OPS)
397
398 tflite_model = converter.convert()
399 open(f, "wb").write(tflite_model)
400 return f, None
401
402
403@try_export

Callers 1

runFunction · 0.85

Calls 6

colorstrFunction · 0.90
LoadImagesClass · 0.90
check_datasetFunction · 0.90
check_yamlFunction · 0.90
infoMethod · 0.80

Tested by

no test coverage detected