Skip to content

Instantly share code, notes, and snippets.

@nafeesb
Last active December 4, 2019 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafeesb/d79d5b57ee7ccb2214f7e69babf2c8e5 to your computer and use it in GitHub Desktop.
Save nafeesb/d79d5b57ee7ccb2214f7e69babf2c8e5 to your computer and use it in GitHub Desktop.
Keras Tensorflow TFLite common idioms

Evaluate TFLite model

From: https://stackoverflow.com/questions/50443411/how-to-load-a-tflite-model-in-script

Handles the case of multiple inputs and outputs.

interpreter = tf.lite.Interpreter(model_path=mfile)
interpreter.allocate_tensors()

# get input and output layer handles
input_details = sorted(interpreter.get_input_details(), key=lambda x:x['name'])
output_details = sorted(interpreter.get_output_details(), key=lambda x:x['name'])

phase = P[frame]
modelidx = int( phase * 50 ) % 50

Vin = X[np.newaxis,frame]
interpreter.set_tensor(input_details[modelidx]['index'], Vin)
interpreter.invoke()
Vout = interpreter.get_tensor(output_details[modelidx]['index'])

Constant inputs in Keras

The constant input arrays need to be dependent on an Input so that they will end up in the middle of the dependency tree.

L_Xmean = L.Lambda( lambda x : K.ones_like(x) * means.Xmean[np.newaxis], name=f'{prefix}_Xmean' )(inputX)

Visualize keras network

from tensorflow.keras.utils import plot_model
plot_model(model, to_file='network.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment