what is servable mean in tensorflow? - tensorflow-serving

I'm newest to tensorflow serving. Still confused with servable after read official tutorial. It seems like a servable is a tensorflow session, but has versions. It also looks like a model, but a servable may also correspond to a fraction of a model.
So what is servable exactly?

I'll try to answer it in a simple way.
A Servable, as per my understanding, is nothing but a Directory generated by Saving the Model, either using SavedModelBuilder or using export_saved_model. The contents of a Servable are shown below.
assets/
assets.extra/
variables/
variables.data-?????-of-?????
variables.index
saved_model.pb
If we use SavedModelBuilder to Save a Model, we need to explicitly mention the Version Numbers. Suppose if the Name of the Model is export, then we should mention the Export Directory as /usr/local/google/home/mothukuru/Jupyter_Notebooks/export/1 or
/usr/local/google/home/mothukuru/Jupyter_Notebooks/export/2, etc..
That is, we need to explicitly mention the version numbers.
If we use export_saved_model, versioning will be taken care automatically, mostly, based on time stamps. Example,
/usr/local/google/home/mothukuru/Jupyter_Notebooks/export/1554294699
/usr/local/google/home/mothukuru/Jupyter_Notebooks/export/1554736143
So, to answer your questions:
Q1. It seems like a servable is a tensorflow session, but has versions.
A1. Yes, it is a Session which has Versions. In that Session, we run the Model and we Save it
Q2. It also looks like a model
A2. Yes, it is a Model, which is Saved using SavedModelBuilder or using export_saved_model.
Q3. But a servable may also correspond to a fraction of a model.
A3. Yes. This Answer has 2 parts.
i. saved_model.pb comprises of Training Graph and Inference Graph. Inference Graph is the Key to TF Serving.
ii. There are 2 folders named assets. The folder assets contains auxiliary files such as vocabularies, etc. That also will be useful during Serving.

Related

How to convert a tensorflow hub pretrained model as to be consumable by tensorflow serving

I am trying to use this for my object detection task. The problems I am facing are:
On running the saved_model_cli command, I am getting the following output. There is no signature defined with tag-set "serve" also the method name is empty
The variable folder in the model directory only contains a few bytes of data which means the weights are not actually written to disk.
The model format seems to be HubModule V1 which seems to be the issue, any tips on making the above model servable are highly appreciated.
TF2 SavedModels should not have this problem, only Hub.Modules from TF1 since Hub.Modules use the signatures for other purposes. You can take a hub.Module and build a servable SavedModel, but it's quite complex and involves building the signatures yourself.
Instead, I recommend checking out the list of TF2 object detection models on TFHub.dev for a model you can use instead of the model you are using: https://tfhub.dev/s?module-type=image-object-detection&tf-version=tf2
These models should be servable with TF Serving

Distinguish types of on-disk models

Tensorflow has several types of model formats:
TensorFlow SavedModel 2. Frozen Model 3. Session Bundle 4. Tensorflow Hub module
How can you distinguish between them on-disk? (to later use with tensorflowjs-converter)
And how is each model created?
Yup, there are a LOT of different model types, and they all have good reasons. I'm not going to claim that I have perfect clarity of each, but here's what I know (I think I know).
The .pb file: PB stands for protobuff or Protocol Buffer. This is the model structure, generally without the trained weights and is stored in a binary format.
The .pbtxt file: Nonbinary of the pb file for human reading.
Protobuff files that aren't frozen will need a checkpoint .ckpt file, too. The Checkpoint file is the missing set of weights that the pb needs.
The .h5 file: The model + weights from a Keras save
The .tflite file would be a TensorflowLite model
Frozen Model: A frozen model combines the pb with the weights file, so you don't have to manage two of them. Usually, this means adding the word frozen to the filename. I'm sure this can be inferred when loading the file, but on disk they are a bit more on the honor system and no ckpt file. This strips out extraneous graph info; it's basically like the "Production Ready" version of the model.
Session Bundle: Are a directory. They are nolonger used, and rare.
Tensorflow Hub Module: These are pre-existing popular models that are very likely already exported to TFJS and don't require you to manually convert them. I assume they are supported for Google's benefit, more so than ours. But it's nice to know if you use hub, you can always convert it.
A multi-exported grouping of files looks like this image. From here, you can see quite a few that you could turn into TFJS.

Spacy v2 alpha - Can't find factory for 'TokenVectorEncoder'

I have trained a model, based off spacy.blank('en') and used nlp.to_disk to save it, however when I come to do spacy.load('path/to/model') I hit the error Can't find factory for 'TokenVectorEncoder'
Inside the model folder, there's a TokenVectorEncoder dir, and the meta.json file mentioned TokenVectorEncoder too.
Any ideas?
Are you loading the model with the same spaCy alpha version you used for training? It looks like this error might be related to a change in the very latest v2.0.0a17, which doesn't use the "tensorizer", i.e. TokenVectorEncoder in the model pipeline anymore. So the easiest solution would probably be to re-train your model with the latest version and then re-package and load it with the same version you used for training.

What are the assets in tensorflow?

I am reading tensorflow tutorial on saving and restoring model and came across the following statement:
If assets need to be saved and written or copied to disk,
they can be provided when the first MetaGraphDef is added. If multiple
MetaGraphDefs are associated with an asset of the same name,
only the first version is retained.
What does assets in this context mean?
Also another paragraphs says:
We provide a Python implementation of the SavedModel builder.
The SavedModelBuilder class provides functionality to save multiple MetaGraphDefs.
A MetaGraph is a dataflow graph, plus its associated variables, assets,
and signatures. A MetaGraphDef is the protocol buffer representation of
a MetaGraph. A signature is the set of inputs to and outputs from a graph.
What is dataflow graph and how is it different from graph?
Here is the tutorial
TL;DR
All you need to know when saving a Tensorflow model is that there are two files that are created (potentially more if you're using checkpoints):
file
file.meta
you save 'file' and restore 'file.meta'.
More info here: Tensorflow: how to save/restore a model?
#########
More to your question:
what you define in Tensorflow before you run your session is called a graph.
When you are saving your graph, a MetaGraph is created. This is the graph itself, and all the other metadata necessary for computations in this graph, as well as some user info that can be saved and version specification.
Assets are external files, such as vocabularies that were created with the graph.

What's the diff between tf.import_graph_def and tf.train.import_meta_graph

When training in model folder there are auto saved meta graph files.
So what's the diff between graph and meta graph.
If I want to load model and do inference witout building graph from scratch,
use tf.train.import_meta_grah is fine?
Well, I find the answer from
https://www.tensorflow.org/versions/r0.9/how_tos/meta_graph/index.html
and
How to load several identical models from save files into one session in Tensorflow
And you can also refer to this:
https://github.com/tensorflow/tensorflow/issues/4658