SoFunction
Updated on 2024-11-17

keras .h5 transfer mobile .tflite file implementation

There used to be a bug in tensorflow that prevented it from running under winodws, but now there seems to be no problem, the code is as follows

Converted mobilenet_v2 under keras to tflite

from  import clear_session
import numpy as np
import tensorflow as tf
clear_session()
np.set_printoptions(suppress=True)
input_graph_name = "../models/weights.best_mobilenet224.h5"
output_graph_name = input_graph_name[:-3] + '.tflite'
converter = .from_keras_model_file(model_file=input_graph_name)
converter.post_training_quantize = True
# There is a problem with this function on windows platform, it doesn't work properly
tflite_model = ()
open(output_graph_name, "wb").write(tflite_model)
print ("generate:",output_graph_name)

Additional knowledge:How to convert a Tensorflow model to a TFLite model

Deep learning is growing rapidly and can now be ported to mobile use, TensorFlow launched TensorFlow Lite is a framework technology to apply deep learning to mobile.

Using TensorFlowLite requires a tflite file model which can be converted from a TensorFlow trained model. So first you need to know how to save the trained TensorFlow model.

There are these general forms of preservation:

1、Checkpoints

2、HDF5

3. SavedModel, etc.

Saving and Reading CheckPoint

When the model training is finished, the weights can be saved in checkpoint format using the following code

model.save_weights('./MyModel',True)

The checkpoints file only holds the trained weights, not the network structure, so it needs to be used in conjunction with a model when doing predictions.

As:

model = keras_segmentation..mobilenet_segnet(n_classes=2, input_height=224, input_width=224)
model.load_weights('./MyModel')

Save as H5

Saving the trained network as an h5 file is simple

('MyModel.h5')

H5 to TFLite

Here are the main points of the article

I'm used to using H5 files converted to tflite files

The official website code looks like this

converter = .from_keras_model_file('newModel.h5')
tflite_model = ()
open("converted_model.tflite", "wb").write(tflite_model)

But the keras 2.2.4 version I'm using reports the following error, it seems to say that the new version of keras has changed relu6, I can't find a way to do it

ValueError: Unknown activation function:relu6

So you need to define a relu6 yourself

import tensorflow as tf
from  import backend as K
from  import CustomObjectScope

def relu6(x):
 return (x, max_value=6)

with CustomObjectScope({'relu6': relu6}):
  converter = .from_keras_model_file('newModel.h5')
  tflite_model = ()
  open("", "wb").write(tflite_model)

Seeing the generated tflite file indicates that the save was successful

You can also see the inputs and outputs of the tflite network this way

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = (model_path="")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

print(input_details)
print(output_details)

The following information is output

[{'name': 'input_1', 'index': 115, 'shape': array([ 1, 224, 224, 3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]

[{'name': 'activation_1/truediv', 'index': 6, 'shape': array([ 1, 12544, 2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]

Each of the two shapes represents the structure of the numpy array of inputs and outputs, and dtype is the datatype

Above this keras .h5 transfer the dynamic end of the .tflite file to achieve the way) is all I have to share with you, I hope to be able to give you a reference, but also hope that you can support me more.