I'm trying to convert a stateful LSTM model from tf.keras saved model (.h5) to tfjs. Since, tfjs doesn't yet support stateful lstms, I need to handle states outside the model. Now, to achieve this i'm required to convert only a part of the existing (.h5) model to tfjs model.
I did the following slicing to the weights of Keras model and successfully converted the resultant sub model into tfjs layers model:
Snippet from python code:
weights = model.get_weights()
model_1.set_weights(weights[:8])
# convert sub model
tfjs.converters.save_keras_model(model_1, os.path.join(target_name,'model_js1'))
When I try to load this model into JS using tf.loadLayersModel
, following is the ERROR I get -
UnhandledPromiseRejectionWarning: Error: Unknown layer: TensorFlowOpLayer. This may be due to one of the following reasons:
The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
summary of the sub model:
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_3 (InputLayer) [(1, 2, 128, 2)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(1, 1, 257)] 0
__________________________________________________________________________________________________
tf_op_layer_strided_slice (Tens [(1, 128)] 0 input_3[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_1 (Te [(1, 128)] 0 input_3[0][0]
__________________________________________________________________________________________________
lstm_4 (LSTM) [(1, 1, 128), (1, 12 197632 input_2[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout) (1, 1, 128) 0 lstm_4[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_2 (Te [(1, 128)] 0 input_3[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_3 (Te [(1, 128)] 0 input_3[0][0]
__________________________________________________________________________________________________
lstm_5 (LSTM) [(1, 1, 128), (1, 12 131584 dropout_2[0][0]
__________________________________________________________________________________________________
tf_op_layer_stack (TensorFlowOp [(2, 1, 128)] 0 lstm_4[0][1]
lstm_5[0][1]
__________________________________________________________________________________________________
tf_op_layer_stack_1 (TensorFlow [(2, 1, 128)] 0 lstm_4[0][2]
lstm_5[0][2]
__________________________________________________________________________________________________
dense_2 (Dense) (1, 1, 257) 33153 lstm_5[0][0]
__________________________________________________________________________________________________
tf_op_layer_Reshape (TensorFlow [(1, 2, 128)] 0 tf_op_layer_stack[0][0]
__________________________________________________________________________________________________
tf_op_layer_Reshape_1 (TensorFl [(1, 2, 128)] 0 tf_op_layer_stack_1[0][0]
__________________________________________________________________________________________________
activation_2 (Activation) (1, 1, 257) 0 dense_2[0][0]
__________________________________________________________________________________________________
tf_op_layer_stack_2 (TensorFlow [(1, 2, 128, 2)] 0 tf_op_layer_Reshape[0][0]
tf_op_layer_Reshape_1[0][0]
==================================================================================================
Total params: 362,369
Trainable params: 362,369
Non-trainable params: 0
I understand the slicing, reshaping and stacking performed on tensors for my state handling requirements is creating tf_op_layers (i'm not sure what they are or how to manage them). Now, I'm not sure how to create custom layers in tfjs for loading this model. Can you help me create the custom layer for tf_op_layer_strided_slice
? And also how to handle - tf_op_layer_Reshape
, tf_op_layer_stack
as tfjs custom layers.