Quantize a Keras neural network model

后端 未结 3 447
梦如初夏
梦如初夏 2021-02-04 03:49

Recently, I\'ve started creating neural networks with Tensorflow + Keras and I would like to try the quantization feature available in Tensorflow. So far, experimenting with exa

3条回答
  •  借酒劲吻你
    2021-02-04 04:12

    Tensorflow lite can be used to quantize keras model.

    The following code was written for tensorflow 1.14. It might not work for earlier versions.

    First, after training the model you should save your model to h5

    model.fit(train_images, train_labels, epochs=epochs)
    
    # evaluate the accuracy of model on test data
    test_loss, test_acc = model.evaluate(test_images, test_labels)
    print('Test accuracy:', test_acc)
    model.save("model.h5")
    

    To load keras model use tf.lite.TFLiteConverter.from_keras_model_file

    # load the previously saved model
    converter  = tf.lite.TFLiteConverter.from_keras_model_file("model.h5")
    tflite_model = converter.convert()
    # Save the model to file
    with open("tflite_model.tflite", "wb") as output_file:
        output_file.write(tflite_model)
    

    The saved model can be loaded to python script or to other platforms and languages. To use saved tflite model, tensorlfow.lite provides Interpreter. The following example from here shows how to load tflite model from local file using python scripts.

    import numpy as np
    import tensorflow as tf
    
    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path="tflite_model.tflite")
    interpreter.allocate_tensors()
    
    # Get input and output tensors.
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    
    # Test model on random input data.
    input_shape = input_details[0]['shape']
    input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)
    
    interpreter.invoke()
    
    # The function `get_tensor()` returns a copy of the tensor data.
    # Use `tensor()` in order to get a pointer to the tensor.
    output_data = interpreter.get_tensor(output_details[0]['index'])
    print(output_data)
    

提交回复
热议问题