-
Notifications
You must be signed in to change notification settings - Fork 138
Description
I am currently trying to convert a simple Keras model with a Scattering1D Layer to TFJS.
The conversion seems to work, but when i try to execute the model i get an error:
Error: Failed to cast to unknown dtype null
Has anyone successfully converted a model which is working in Javascript/Typescript? Any idea what i am doing wrong or what i could try to get it running?
Steps to reproduce (im working in windows 10):
create new conda env:
- conda create -n tfjs_model python=3.8 tensorflow
- activate tfjs_model
- pip install kymatio tensorflowjs
create a simple model with:
import tensorflow as tf
from kymatio.keras import Scattering1D
T = 2**10
J = 5
Q = 8
scattering = Scattering1D(J, Q=Q, max_order=2)
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(T)))
model.add(scattering)
model.compile(metrics='accuracy', loss=None)
model.save('scatter_transform')
The resulting model can be used in python as expected:
import tensorflow as tf
model = tf.keras.models.load_model('scatter_transform')
model.summary()
data_tensor = tf.zeros([1, 1024], tf.float32)
res = model.predict(data_tensor)
print(res.shape)
Convert the model to a tfjs graph model by running:
tensorflowjs_converter scatter_transform scatter_transform_js
I am able to open the resulting model (model.json with one group-shard and a total size of ~2mb), but when i try to execute the model i get the error message:
C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:12600
throw new Error("Failed to cast to unknown dtype ".concat(dtype));
Error: Failed to cast to unknown dtype null
at cast_ (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:12600:15)
at Object.cast__op [as cast] (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:11981:29)
at executeOp$1 (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:29382:25)
at C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:29492:50
at C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:4478:22
at Engine.scopedRun (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:4488:23)
at Engine.tidy (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:4477:21)
at tidy (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:9102:19)
at C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:29492:24
at executeOp (C:\Scripts\typescript test\tensorflow_model\0_bugreport\node_modules\@tensorflow\tfjs-converter\dist\tf-converter.node.js:29508:7)
JS-Code:
import * as tf from '@tensorflow/tfjs-node'
console.log('testing keras model...')
async function runModel() {
const model_ = await tf.loadGraphModel('file://scatter_transform_js/model.json');
const data = tf.zeros([1, 1024], 'float32');
console.log(data.shape)
//console.log(model_)
const res = model_.execute(data)
console.log((res).arraySync())
console.log('finished')
}
runModel()
with package.json:
{
"name": "TFJS Scattering1D-Layer",
"type": "module",
"dependencies": {
"@tensorflow/tfjs-node": "^4.1.0"
}
}