Reputation: 95
used to load model by calling tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_128/model.json')
, however, i needed to change the mobilinet version.
So, i took the version i needed from tensorflowhub, ran it on tensorflow_converter, and got the two files (.pb and the weight file). Then i loaded it using tf.loadGraphModel. hower, model.getLayer throws:
model.getLayer is not a function.
the loading looks like this:
const model = await tf.loadGraphModel(modelUrl); //url points to .pb
Then i saved the mobilinet model as a frozen model, ran it again on tensorflow_converter, and tried to load it as tf.loadFrozenModel. which returned the same thing.
im confused.
Is there a way to get layers from a non-keras model?
EDIT: for clarification, the model i took from tensoflowhub is : https://tfhub.dev/google/imagenet/mobilenet_v2_075_96/classification/2
Upvotes: 0
Views: 570
Reputation: 1699
TF.js supports two APIs and corresponding serialization formats: the Layers API (corresponding to Keras models) and the lower-level Core API (corresponding to arbitrary TensorFlow graphs).
Depending on where you obtain a model and how you convert it, your file can be loaded either via tf.loadLayersModel()
or tf.loadGraphModel()
, but not both. Please see the table of available conversions.
Even if a model was originally trained using Keras, it may have been saved as a low-level TensorFlow graph where the Keras layers structure is lost. I believe this is currently the case for all TF-Hub modules. Thus your current approach gives you a tf.GraphModel
, from which the layers cannot be reconstituted.
We provide MobileNet v1 already converted from the original Keras to the TF.js Layers format at the URL you listed, so you can use loadLayersModel()
(formerly loadModel()
) with that directly. We don't currently host a converted MobileNet v2. However you can get the original Keras .h5 model here, and then convert that to TF.js Layers format using tensorflowjs_converter
.
Upvotes: 1
Reputation: 18371
LoadFrozenModel
is deprecated since 0.15. LoadGraphModel
does the same thing with less parameter. It takes only as parameter the model topology file.
If there is no layers in the object loaded, it is either because the model is not well loaded or the frozen model does not contain any .
Upvotes: 1