create_coco_tf_record.py from object detection API not creating tfrecord file - tensorflow

I am trying to create tfrecord (.record) files out of coco-format (.json). Therefore I am using this slightly modified version of the create_coco_tf_record.py script from the object detection api github repository.
I am running my notebook on colab. These are the few lines of code:
#Mount Google Drive.
from google.colab import drive
drive.mount('/content/gdrive') <br>
!pip install -U --pre tensorflow=="2.2.0"
#Download TensorFlow Model Garden.
import os
import pathlib
#cd into the TensorFlow directory in your Google Drive
%cd '/content/gdrive/My Drive/TensorFlow'
# Clone the tensorflow models repository if it doesn't already exist
if "models" in pathlib.Path.cwd().parts:
while "models" in pathlib.Path.cwd().parts:
os.chdir('..')
elif not pathlib.Path('models').exists():
!git clone --depth 1 https://github.com/tensorflow/models
# Install the Object Detection API
%%bash
cd '/content/gdrive/My Drive/TensorFlow/models/research/'
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .
#run model builder test
!python '/content/gdrive/My Drive/TensorFlow/models/research/object_detection/builders/model_builder_tf2_test.py'
!wget https://raw.githubusercontent.com/TannerGilbert/Tensorflow-Object-Detection-API-train-custom-Mask-R-CNN-model/master/create_coco_tf_record.py
# create tf record
!python create_coco_tf_record.py \
--logtostderr \
--train_image_dir='/content/gdrive/My Drive/TensorFlow/workspace/training_demo/images/combined/' \
--test_image_dir='/content/gdrive/My Drive/TensorFlow/workspace/training_demo/images/combined/' \
--train_annotations_file='/content/gdrive/My Drive/TensorFlow/workspace/training_demo/images/train_coco.json' \
--test_annotations_file='/content/gdrive/My Drive/TensorFlow/workspace/training_demo/images/test_coco.json' \
--output='/content/gdrive/My Drive/TensorFlow/workspace/training_demo/annotations/'
The create_coco_tf_record.py script runs through without any error. This is what it shows:
2020-10-28 08:58:44.931401: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
INFO:tensorflow:Found groundtruth annotations. Building annotations index.
I1028 08:58:46.678869 139783613331328 create_coco_tf_record.py:214] Found groundtruth annotations. Building annotations index.
INFO:tensorflow:0 images are missing annotations.
I1028 08:58:46.679595 139783613331328 create_coco_tf_record.py:227] 0 images are missing annotations.
INFO:tensorflow:On image 0 of 451
I1028 08:58:46.680609 139783613331328 create_coco_tf_record.py:232] On image 0 of 451
INFO:tensorflow:On image 100 of 451
I1028 08:58:51.800869 139783613331328 create_coco_tf_record.py:232] On image 100 of 451
INFO:tensorflow:On image 200 of 451
I1028 08:59:01.762672 139783613331328 create_coco_tf_record.py:232] On image 200 of 451
INFO:tensorflow:On image 300 of 451
I1028 08:59:22.197772 139783613331328 create_coco_tf_record.py:232] On image 300 of 451
INFO:tensorflow:On image 400 of 451
I1028 09:00:17.036898 139783613331328 create_coco_tf_record.py:232] On image 400 of 451
INFO:tensorflow:Finished writing, skipped 0 annotations.
I1028 09:00:32.919734 139783613331328 create_coco_tf_record.py:239] Finished writing, skipped 0 annotations.
INFO:tensorflow:Found groundtruth annotations. Building annotations index.
I1028 09:00:32.932144 139783613331328 create_coco_tf_record.py:214] Found groundtruth annotations. Building annotations index.
INFO:tensorflow:0 images are missing annotations.
I1028 09:00:32.932511 139783613331328 create_coco_tf_record.py:227] 0 images are missing annotations.
INFO:tensorflow:On image 0 of 152
I1028 09:00:32.932658 139783613331328 create_coco_tf_record.py:232] On image 0 of 152
INFO:tensorflow:On image 100 of 152
I1028 09:00:46.510094 139783613331328 create_coco_tf_record.py:232] On image 100 of 152
INFO:tensorflow:Finished writing, skipped 0 annotations.
I1028 09:01:08.650619 139783613331328 create_coco_tf_record.py:239] Finished writing, skipped 0 annotations.
Somehow it does not create any .record files.
Does anyone know what the problem could be here?
Thanks in advance!

You can run this command to generate your .record files in VS code terminal on a windows machine. "Imp. - Do not use 'commas' to include your path as I have mentioned just copy/paste path as such where I have suggested in the comment"-
python create_coco_tf_record.py
--train_image_dir 'Path where you have your training dataset'
--test_image_dir 'Path where you have your validation dataset'
--train_annotations_file 'Path to your training dataset .json file'
--test__annotations_file 'Path to your validation dataset .json file'
--logtostderr --output_dir 'Path where do you want to generate your .record file'
Also keep in mind and run the code with specific environment requirements needed to run this code.

Related

Tensorflow 2.0 model checkpoint files to .pb/onnx file

For model checkpoint files (usually consist of .meta, .data, .index) generated from TF-2.0, how can I convert it to onnx or pb file?
since I found most of the existing tools, such as tf2onnx only support TF-1.x.
tf2onnx supports tf2, saved models, and checkpoint files. I would recommend making a saved model:
model.save("path")
Then use tf2onnx command line:
tf2onnx.convert --saved-model path/to/model --output model.onnx --opset 12
https://github.com/onnx/tensorflow-onnx#--saved-model

kaggle api not showing everything

!kaggle competitions files -c planet-understanding-the-amazon-from-space
I ran the code in above, but it does not show me all the files. It only shows:
!kaggle competitions files -c planet-understanding-the-amazon-from-space
Warning: Looks like you're using an outdated API Version, please consider updating (server 1.5.6 /
client 1.5.4)
name size creationDate
------------------------------------------------- ----- -------------------
train_v2.csv/train_v2.csv 1MB 2019-12-15 22:14:13
sample_submission_v2.csv/sample_submission_v2.csv 3MB 2019-12-15 22:14:13
test_v2_file_mapping.csv/test_v2_file_mapping.csv 600KB 2019-12-15 22:14:13
Kaggle-planet-test-tif.torrent 2MB 2019-12-15 22:14:13
Kaggle-planet-train-tif.torrent 1MB 2019-12-15 22:14:13
It should show this, I can not download the train jpg tar file: all file image
[https://www.kaggle.com/c/planet-understanding-the-amazon-from-space/data][2]
My colab: https://colab.research.google.com/drive/19hLo3NN_NY6tbXjEpWOI5y_bEVujikTF

TensorFlow Serving Error - Could not find meta graph def matching supplied tags: { serve }

I am trying to restore a TensorFlow's Saver object (.ckpt.*) and convert it into SavedModel object(.pb) so that I can deploy it with TensorFlow Serving.
This is how I convert:
with tf.Session() as sess:
# Restore the graph from (.meta .data .index)
saver = tf.train.import_meta_graph(f"{checkpoint_path}/{meta_file_string}")
saver.restore(sess, tf.train.latest_checkpoint(str(checkpoint_path)))
# Convert into ".pb" using SavedModel API.
model_path = f'{savedmodel_path}/1'
builder = tf.saved_model.builder.SavedModelBuilder(model_path)
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.SERVING],
main_op=tf.tables_initializer(),
strip_default_attrs=True)
builder.save()
print("Saved")
Saving seems to work fine when I tree:
$ tree 1
1
├── saved_model.pb
└── variables
├── variables.data-00000-of-00001
└── variables.index
1 directory, 3 files
and when I use saved_model_cli:
$ saved_model_cli show --dir path/to/model/1
The given SavedModel contains the following tag-sets:
serve
However, when I run the TensorFlow serving docker container,
$ docker run \
-p 8500:8500 \
-v path/to/model:/models/aaa \
--env MODEL_NAME=aaa \
--name aaa \
tensorflow/serving
it complains that it cannot find the tag "serve" which I DID ADD:
2019-11-19 02:35:30.844163: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: /models/aaa/1
2019-11-19 02:35:30.916952: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
2019-11-19 02:35:30.927640: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:311] SavedModel load for tags { serve }; Status: fail. Took 83527 microseconds.
2019-11-19 02:35:30.927781: E tensorflow_serving/util/retrier.cc:37] Loading servable: {name: aaa version: 1} failed: Not found: Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`
What have I done wrong, how can I fix this?
otherwise, how can I dive into this issue deeper?
I am using tensorflow 1.14.0.
and using docker image tensorFlow-serving:1.14.0-devel.
you need to add prediction signature to your builder-
prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def({"input": inputs}, {"output":output})
builder = saved_model_builder.SavedModelBuilder('exported_moddel/')
builder.add_meta_graph_and_variables(session,
[tag_constants.SERVING],
signature_def_map={"classification":prediction_signature})
builder.save()
you can refer this notebook for more detail - https://github.com/CS-savvy/tf-graph-preprocessing-addition/blob/master/keras%20inject.ipynb
Replacing tensorflow/serving image with :latest version, which is :2.0.0 so far. And worked fine.
My local train environment still uses TensorFlow 1.14
No idea why this is so.

why tensorrt optimized pb can not be deployed by tf-serving?

I am using tensorrt to accelerate inference speed of Tacotron2 model. I used
tensorrt 5.0.2.6 version and tensorflow 1.13.0.rc0 version.
I convert savedmodel to tftrt savedmodel using the tensorrt api below:
trt.create_inference_graph(
input_graph_def=None,
outputs=None,
max_batch_size=32,
input_saved_model_dir=os.path.join(args.export_dir, args.version),
output_saved_model_dir=args.output_saved_model_dir,
precision_mode=args.precision_mode)
The outputed tensorrt_savedmodel.pb can not be imported into tensorboard for view but tensorrt_savedmodel.pb can be deployed with tf-serving.
However, when client request the tf-serving using grpc there is an error:
<_Rendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "The TF function for the TRT segment could not be empty
[[{{node model/inference/prenet/TRTEngineOp_33}}]]"
debug_error_string = "
{"created":"#1572417319.714936208","description":"Error received from peer ipv4:192.168.23.17:8500","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"The TF function for the TRT segment could not be empty\n\t [[{{node model/inference/prenet/TRTEngineOp_33}}]]","grpc_status":3}
any solutions about this issue?
TensorFlow saved model also provides a formal and consistent way to use tensorrt. You can try to convert it with the saved_model_cli and then deploy to tf-serving.
usage: saved_model_cli convert [-h] --dir DIR --output_dir OUTPUT_DIR
--tag_set TAG_SET
{tensorrt} ...
Usage example:
To convert the SavedModel to one that have TensorRT ops:
$saved_model_cli convert \
--dir /tmp/saved_model \
--tag_set serve \
--output_dir /tmp/saved_model_trt \
tensorrt
optional arguments:
-h, --help show this help message and exit
--dir DIR directory containing the SavedModel to convert
--output_dir OUTPUT_DIR
output directory for the converted SavedModel
--tag_set TAG_SET tag-set of graph in SavedModel to convert, separated by ','
conversion methods:
valid conversion methods
{tensorrt} the conversion to run with the SavedModel

Can not load pb file in tensorflow serving

I have used SavedModel (Inception_resnet_v2) to export the TensorFlow model files and use TensorFlow Serving to load the files.I have directly replaced offical minst saved_model.pb with my own Inception_resnet_v2 saved_model.pb file. But I got one error.
deep#ubuntu:~/serving$ bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/home/deep/serving/tmp/mnist_model
2017-06-18 10:39:41.963490: I tensorflow_serving/model_servers/main.cc:146] Building single TensorFlow model file config: model_name: mnist model_base_path: home/deep/serving/tmp/mnist_model model_version_policy: 0
2017-06-18 10:39:41.963752: I tensorflow_serving/model_servers/server_core.cc:375] Adding/updating models.
2017-06-18 10:39:41.963762: I tensorflow_serving/model_servers/server_core.cc:421] (Re-)adding model: mnist
2017-06-18 10:39:42.065556: I tensorflow_serving/core/basic_manager.cc:698] Successfully reserved resources to load servable {name: mnist version: 1}
2017-06-18 10:39:42.065610: I tensorflow_serving/core/loader_harness.cc:66] Approving load for servable version {name: mnist version: 1}
2017-06-18 10:39:42.065648: I tensorflow_serving/core/loader_harness.cc:74] Loading servable version {name: mnist version: 1}
2017-06-18 10:39:42.065896: I external/org_tensorflow/tensorflow/contrib/session_bundle/bundle_shim.cc:360] Attempting to load native SavedModelBundle in bundle-shim from: /home/deep/serving/tmp/mnist_model/1
2017-06-18 10:39:42.066130: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:226] Loading SavedModel from: /home/deep/serving/tmp/mnist_model/1
2017-06-18 10:39:42.080775: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:274] Loading SavedModel: fail. Took 14816 microseconds.
2017-06-18 10:39:42.080822: E tensorflow_serving/util/retrier.cc:38] Loading servable: {name: mnist version: 1} failed: Not found: Could not find meta graph def matching supplied tags.
What should I do? Thanks!
I chatted to the Serving engineers, and here are some of their thoughts on this:
Looks like they need to specify a tag either in the saved model, or on
the command line. (log line of note: failed: Not found: Could not find
meta graph def matching supplied tags. )
It looks like the SavedModel loader is unable to find a graph
corresponding to the tags they have supplied. Here is some
documentation:
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/saved_model#tags
Ah, to add: They could use the SavedModel CLI to inspect the model and
see what tag-sets are available. Here is the documentation for that:
https://www.tensorflow.org/versions/master/programmers_guide/saved_model_cli.
They can run
saved_model_cli show --dir <SavedModelDir>
to check what tag-sets are in the SavedModel if they have pip
installed tensorflow.