YUFENG GUO: TensorFlow.js is awesome.
Keras is awesome.
Wouldn't it be extra amazing if they could work together?
Well, I got great news for you.
They do!
Stay tuned to find out just how to import your trained Keras
model into TensorFlow.js.
Welcome to AI Adventures, where we explore the art, science,
and tools of machine learning.
My name is Yufeng Guo, and on this episode,
we're going to look at what it takes to import a Keras
model into TensorFlow.js.
TensorFlow.js has many functionalities,
but perhaps one of the most exciting ones
is its ability to operate in collaboration with other tools.
We're really seeing a future where
you can do machine learning across a range of platforms,
languages, and devices, all optimized
to perform at their peak performance
in the situation they're most well-suited for.
The tricky thing about loading a Keras model into TensorFlow.js
is that Keras models are typically saved as HDF5 files,
using something like model.save('my_model.h5').
TensorFlow.js provides a tool, the TensorFlow.js converter,
which is used to convert your Keras model into a format
that TensorFlow.js can consume, which
is a folder containing a file called model.json.
Let's see that in context with the Keras model
that we've been using in the last few episodes.
In the episode where we converted a Keras model
to a TensorFlow estimator, we also exported our Keras model
as a .h5 file, so we'll head on over to that Kernel
and download the file from the Output section.
So I've got my Keras model downloaded and named it
keras_model.h5 just to keep things clear which file is from
which library.
Now I need to run pip install tensorflowjs in my Python
environment to install the converter.
Notice that the pip library name is tensorflowjs,
even though the converter we'll use is called
tensorflowjs converter.
OK, so we've got the converter and we got the Keras model
all in one place.
One last step before we run the converter, though.
I'm going to create a new folder because we
need to store the outputs of the TensorFlow.js model files.
Let's just call it tfjs_files.
Now we can run the converter, passing in a flag
to indicate that it is a Keras model,
and we'll also need to pass the path to the Keras HDF5 file
in that folder we just created so it has a destination
to put the new files in.
Once our converter has run, let's
take a peek inside the folder.
We see that it's got not only the model.json file, but three
additional files.
There's a group1-shard1of1, group2-shard1of1, and
group3-shard1of1.
These shards are here to enable faster repeat loading
by the browser.
The files are below the typical cache size limit,
so they'll likely be cached for subsequent calls
when you're serving them.
So after you've successfully converted your Keras model
to model.json, you've just got one last step left--
got to load it into your JavaScript.
Thankfully, this is a one-line task.
Go ahead and use tf.modelload to point to the URL
where you have the files posted, and TensorFlow.js
takes care of the rest.
It's worth pointing out here that since the model is
operating on the client side, using JSON files,
it's not a great use case if you need to secure your model.
For that, you'll have to stick with client side models.
All right.
So now you're ready to import your own Keras
models into TensorFlow.js.
Go forth and make the next great machine
learning-inspired game on the web.
Thanks for watching this episode of "Cloud AI
Adventures," and if you enjoyed it, please like it
and subscribe to get all the latest episodes right
when they come out.
For now, try loading your Keras model into TensorFlow.js
and see just how easy it is.