Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. # Convert to mobile formats
  2. import coremltools
  3. import tensorflow as tf
  4. import tempfile
  5.  
  6. def convert_to_coreml(model):
  7. return coremltools.converters.keras.convert(
  8. model,
  9. input_names=['input'],
  10. output_names=['digit']
  11. )
  12.  
  13. def convert_to_tflite(model):
  14. # save the model to a temp file so we can
  15. # convert it.
  16. keras_file = tempfile.mktemp()
  17. model.save(keras_file, include_optimizer=False)
  18. converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
  19. return converter.convert()
  20.  
  21. mlmodel = convert_to_coreml(model)
  22. tflite = convert_to_tflite(model)
  23.  
  24. model_name = "mnist_cnn_lr001_batchsize128"
  25.  
  26. # Keras
  27. model.save(
  28. model_name + ".h5",
  29. include_optimizer="False"
  30. )
  31.  
  32. # Core ML
  33. mlmodel.save(model_name + ".mlmodel")
  34.  
  35.  
  36. # TensorFlow Lite
  37. with open(model_name + ".tflite", 'wb') as wfid:
  38. wfid.write(tflite)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement