How to evaluate TensorFlow model given .pb file? - tensorflow

I have downloaded the Inception_v3 model of Tensorflow using following command:
curl http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz -o /tmp/inceptionv3.tgz
tar xzf /tmp/inceptionv3.tgz -C /tmp/
Now I have a classify_image_graph_def.pb file, which I believe is the model.
My question is, how to evaluate this model against the ImageNet 2012 data? Is there any scripts, or do I have to write some python code?

The classify_image.py script in the TensorFlow GitHub repository uses that pre-trained Inception model to perform classification on arbitrary JPEG images. You could adapt this script to evaluate it against the ImageNet 2012 data.

Related

OpenAI GPT-2 model use with TensorFlow JS

Is that possible to generate texts from OpenAI GPT-2 using TensorFlowJS?
If not what is the limitation, like model format or ...?
I don't see any reason as to why not, other than maybe some operation that is in gpt-2 that is not supported by tensorflowjs.
I don't know how to do it, but here's a nice starting point:
install.sh
python3 -m pip install -q git+https://github.com/huggingface/transformers.git
python3 -m pip install tensorflow
save.py
from transformers import TFGPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# add the EOS token as PAD token to avoid warnings
model = TFGPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)
model.save("./test_gpt2")
that will give you a SavedModel file. Now you can try figure out the input and output nodes, and use tensorflowjs_converter to try and convert it. Pointer: https://www.tensorflow.org/js/tutorials/conversion/import_saved_model.

freeze model for inference with output_node_name for ssd mobilenet v1 coco

I want to compile the TensorFlow Graph to Movidius Graph. I have used Model Zoo's ssd_mobilenet_v1_coco model to train it on my own dataset.
Then I ran
python object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=/home/redtwo/nsir/ssd_mobilenet_v1_coco.config \
--trained_checkpoint_prefix=/home/redtwo/nsir/train/model.ckpt-3362 \
--output_directory=/home/redtwo/nsir/output
which generates me frozen_interference_graph.pb & saved_model/saved_model.pb
Now to convert this saved model into Movidius graph. There are commands given
Export GraphDef file
python3 ../tensorflow/tensorflow/python/tools/freeze_graph.py \
--input_graph=inception_v3.pb \
--input_binary=true \
--input_checkpoint=inception_v3.ckpt \
--output_graph=inception_v3_frozen.pb \
--output_node_name=InceptionV3/Predictions/Reshape_1
Freeze model for inference
python3 ../tensorflow/tensorflow/python/tools/freeze_graph.py \
--input_graph=inception_v3.pb \
--input_binary=true \
--input_checkpoint=inception_v3.ckpt \
--output_graph=inception_v3_frozen.pb \
--output_node_name=InceptionV3/Predictions/Reshape_1
which can finally be feed to NCS Intel Movidius SDK
mvNCCompile -s 12 inception_v3_frozen.pb -in=input -on=InceptionV3/Predictions/Reshape_1
All of this is given at Intel Movidius Website here: https://movidius.github.io/ncsdk/tf_modelzoo.html
My model was already trained i.e. output/frozen_inference_graph. Why do I again freeze it using /slim/export_inference_graph.py or it's the output/saved_model/saved_model.py that will go as input to slim/export_inference_graph.py??
All I want is output_node_name=Inceptionv3/Predictions/Reshape_1. How to get this output_name_name directory structure & anything inside it? I don't know what all it contains
what output node should I use for model zoo's ssd_mobilenet_v1_coco model(trained on my own custom dataset)
python freeze_graph.py \
--input_graph=/path/to/graph.pbtxt \
--input_checkpoint=/path/to/model.ckpt-22480 \
--input_binary=false \
--output_graph=/path/to/frozen_graph.pb \
--output_node_names="the nodes that you want to output e.g. InceptionV3/Predictions/Reshape_1 for Inception V3 "
Things I understand & don't understand:
input_checkpoint: ✓ [check points that were created during training]
output_graph: ✓ [path to output frozen graph]
out_node_names: X
I don't understand out_node_names parameter & what should inside this considering its ssd_mobilnet not inception_v3
System information
What is the top-level directory of the model you are using:
Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
TensorFlow installed from (source or binary): TensorFlow installed with pip
TensorFlow version (use command below): 1.13.1
Bazel version (if compiling from source):
CUDA/cuDNN version: V10.1.168/7.*
GPU model and memory: 2080Ti 11Gb
Exact command to reproduce:
The graph in saved_model/saved_model.pb is the graph definition(graph architecture) of the pretrained inception_v3 model without the weights loaded to the graph. The frozen_interference_graph.pb is the graph frozen with the checkpoints you have provided and taking the default output nodes of the inception_v3 model.
To get output node names summarise_graph tool can be used
You can use the below commands to use summarise_graph tool if bazel is installed
bazel build tensorflow/tools/graph_transforms:summarize_graph
bazel-bin/tensorflow/tools/graph_transforms/summarize_graph \
--in_graph=/tmp/inception_v3_inf_graph.pb
In case if bazel is not installed Output nodes can be obtained using the tensorboard or any other graph visualising tools like Netron.
The additional freeze_graph.py can be used to freeze the graph specifying the output nodes(ie in a case where additional output nodes are added to the inceptionV3). The frozen_interference_graph.pb is also an equaly good fit for infrencing.

Serving a TensorFlow Custom Model

I am new in machine learning, basically i created own dataset of images and do training on them and recognize images on jupyter notebook, after this i tried to deploy this model by following this tutorial
I execute
bazel build -c opt //tensorflow_serving/example:mnist_saved_model
bazel-bin/tensorflow_serving/example/mnist_saved_model /tmp/mnist_model
it runs successfully.
How to export my own model and deploy? my model name is "GoogleTensorflow"
I created this model using
python3 export_inference_graph.py
--input_type image_tensor
--pipeline_config_path training/ssd_mobilenet_v1_pets.config
--trained_checkpoint_prefix training/model.ckpt-26456
--output_directory GoogleTensorflow
Move your custom training folder to tmp folder and that model should have version ex 1 folder inside it

Inception v3 flowers_train using the checkpoint model

I have used the flowers_train script found here: flowers_train.py.
To retrain the existing inception v3 model on 10 new classes. The flowers_train script generates some checkpoint files of the format:
checkpoint model.ckpt-1030000.index
events.out.tfevents.1501217995.tron model.ckpt-1030000.meta
model.ckpt-1020000.data-00000-of-00001 model.ckpt-1035000.data-00000-of-00001
model.ckpt-1020000.index model.ckpt-1035000.index
model.ckpt-1020000.meta model.ckpt-1035000.meta
model.ckpt-1025000.data-00000-of-00001 model.ckpt-1040000.data-00000-of-00001
model.ckpt-1025000.index model.ckpt-1040000.index
model.ckpt-1025000.meta model.ckpt-1040000.meta
model.ckpt-1030000.data-00000-of-00001
The classify_image.py script is found here.
It expects a .pb file, not a checkpoint file.
I've been pulling my hair over the past two weeks trying to figure out how to get from the checkpoint file to the .pb file so I can use the retrained model.
Any ideas would be appreciated.
Any ideas would be appreciated.
The code at https://www.tensorflow.org/hub/tutorials/image_retraining
cd ~
curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz
tar xzf flower_photos.tgz
mkdir ~/example_code
cd ~/example_code
curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py
python retrain.py --image_dir ~/flower_photos
produces the file ./output_graph.pb.

How do I get the 'checkpoint' of a retrained inception-v3 model?

I'm trying use a retrained inception-v3 model in tensorflow-serving. But it seems that I have to provide a 'checkpoint'. I was wondering how do I get those 'checkpoints'? The retrain.py returns me a retrained_graph.pb. I followed this tutorial (https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0)
Thank you!
You may want to look into the API changes for Tensorflow 1.0
https://www.tensorflow.org/install/migration
to make it work to create a checkpoint file/s instead of .pb file.
Also refer to:
https://www.tensorflow.org/tutorials/image_retraining
Here is an example of how to download the checkpoints for the latest edition of pre-trained Inception V3 model:
$ CHECKPOINT_DIR=/tmp/checkpoints
$ mkdir ${CHECKPOINT_DIR}
$ wget http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz
$ tar -xvf inception_v3_2016_08_28.tar.gz
$ mv inception_v3.ckpt ${CHECKPOINT_DIR}
$ rm inception_v3_2016_08_28.tar.gz