问题
Error in JS:
Uncaught (in promise) Error: Input 0 is incompatible with layer flatten: expected min_ndim=3, found ndim=2.
I have found threads for the same error from people trying to import pretrained models in keras/python (like VGG oder ResNets).
For them it was mostly because of them still including the top layers of the model, so those threads unfortunately have nothing to do with my problem of fully importing a self-trained model from python in TensorflowJS. My code:
Python code:
model = keras.models.Sequential([
keras.layers.GRU(128, return_sequences=True, input_shape=[ None, max_id+1]),
keras.layers.GRU(128, return_sequences=True),
keras.layers.GRU(128),
keras.layers.Flatten(),
keras.layers.Dense(output_size, activation="softmax")
])
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
history = model.fit(train_tokens_X, train_target, validation_data=(valid_tokens_X, valid_target), batch_size=32, epochs=15)
model.save(os.path.join(data_dir, "prototype.h5"))
Then I save it to .h5 and convert it with tensorflowjs_converter just as described here: https://www.tensorflow.org/js/tutorials/conversion/import_keras
Then I import it in JS:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Js test</title>
</head>
<body>
<h1>JavaScript TF test</h1>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
<script>
// init
let model = null;
(async () => {
alert('Hello World!');
tf.loadLayersModel('http://localhost:3000/prototype_web/model.json'); // Code crashes here with "Error: Input 0 is incompatible with layer flatten: expected min_ndim=3, found ndim=2."
alert('Hello World2!');
model.summary();
})();
</script>
</body>
</html>
What I've tried so far:
- Re-importing the .h5 model in python works perfectly fine.
- Converting a small MNIST-model without any RNN layers to Tensorflow JS works seamlessly, so it's not an issue of misconfiguration of TensorflowJS, so it seems like the GRU layers are the issue here?!
- Saving the model as a TF SavedModel instead of Keras' h5 fails to convert with
tensorflowjs_converter
- Fixing the
input_shape
toinput_shape=[ 61, max_id+1])
instead ofNone
makes the training impossible, because the training instances vary in length - Re-implementing the model in tfjs and just importing the weights does not work here, because tfjs lacks a function to load individual weights from file
I'm very grateful for any ideas.
Thank you in advance!
来源:https://stackoverflow.com/questions/59048354/importing-tf-model-into-tensorflowjs-trained-in-python-fails-input-0-is-inco