Interpretation script for Yolo's region based output to openvino - python

Hello I am new to OPENCV/CVAT, I use openvino to run auto annotation, I want to use YoloV3 for this mission.
I need to convert Yolo model to OpenVINO format for opencv/cvat/auto_annotation.
https://github.com/opencv/cvat/tree/develop/cvat/apps/auto_annotation.
To annotate a task with a custom model I need to prepare 4 files:
Model config (*.xml) - a text file with network configuration.
Model weights (*.bin) - a binary file with trained weights.
Label map (*.json) - a simple json file with label_map dictionary
like object with string values for label numbers.
Interpretation script (*.py) - a file used to convert net output
layer to a predefined structure which can be processed by CVAT. This
code will be run inside a restricted python's environment, but it's
possible to use some builtin functions like str, int, float, max,
min, range.
I converted Yolo model to OpenVINO format and created xml and bin files. I write the mapping lson file.
Now I need to write interpretation python script for Yolo's region based output. How can I do that?
Is there an interrupt file from tensorflow models to openvino?

Recommend you to start with Yolo V3 C++ or Python sample.
For C++:
https://github.com/opencv/open_model_zoo/tree/master/demos/object_detection_demo_yolov3_async
For python sample:
https://github.com/opencv/open_model_zoo/tree/master/demos/python_demos/object_detection_demo_yolov3_async
Similar discussion can be found in https://software.intel.com/en-us/forums/computer-vision/topic/816875

This is now bundled with CVAT. The interpret script and json file can be found in the repo here: https://github.com/opencv/cvat/tree/develop/utils/open_model_zoo/yolov3

Related

TFLite model maker custom object detector training using tfrecord

I am trying to train a custom object detector using tflite model maker (https://www.tensorflow.org/lite/tutorials/model_maker_object_detection). I want to deploy trained tflite model to coral edgeTPU. I want to use tensorflow tfrecord (multiple) as input for training a model like object detection API. I tried with
tflite_model_maker.object_detector.DataLoader(
tfrecord_file_patten, size, label_map, annotations_json_file=None
) but I am not able to work around it. I have following questions.
Is it possible to tfrecord for training like mentioned above?
Is it also possible to pass multiple CSV files for training?
For multiple CSV files, you could probably just append one file to the other. Then you'd just have to pass one csv file.
As for passing a tfrecord instead, this should be possible. I'm also attempting to do this, so if I get it working I'll update my post. Looking at the source, it seems from_cache is the function internally used. Following that structure, should be able to create a DataLoader object similarly:
train_data = DataLoader(tfrecord_file_patten, meta_data['size'],
meta_data['label_map'], ann_json_file)
In this case, tfrecord_file_patten should be a tfrecord of your training data. You can construct the validation and test data the same way. This will work provided you're constructing your TFRecords correctly. There appears to be some inconsistency to how it's done in different places, so make sure you follow the same structure in creating the TFRecords as found in the ModelMaker source. This worked for me. One specific thing to watch out for is to use an integer for the 'image/source_id' feature in your TFExamples. If you use a string it'll throw an error.

When to use the .ckpt vs .hdf5 vs. .pb file extensions in Tensorflow model saving?

Tensorflow explains that models can be saved in three file formats: .ckpt or .hdf5 or .pb. There's a lot of documentation so it would be nice to get a simpler comparison of when to use which file format.
Here's my current understanding:
ckpt
From https://www.tensorflow.org/guide/checkpoint:
Checkpoints capture the exact value of all parameters (tf.Variable
objects) used by a model. Checkpoints do not contain any description
of the computation defined by the model and thus are typically only
useful when source code that will use the saved parameter values is
available.
So it seems like you should use cpkt for checkpointing during training when you know that your source code will be the same. Why is it recommended though over .pb and .hdf5? Does it save space? Does it include data that the other file formats do not?
pb
Also from https://www.tensorflow.org/guide/checkpoint:
The SavedModel format on the other hand includes a serialized
description of the computation defined by the model in addition to the
parameter values (checkpoint). Models in this format are independent
of the source code that created the model. They are thus suitable for
deployment via TensorFlow Serving, TensorFlow Lite, TensorFlow.js, or
programs in other programming languages (the C, C++, Java, Go, Rust,
C# etc. TensorFlow APIs).
The SavedModel format is .pb plus some metadata. So you should save in .pb when you are deploying a model?
hdf5
Use when saving the model weights (matrix of numbers) only?
It seems you already know some of the differences, but just to add.
.ckpt
This is mainly used for resuming the training and also to allow users to customize savepoints and load to (ie. Highest Accuracy, Latest Trained Model, etc).
And also to create different models from different training checkpoints.
This only saves the weights of the variables or the graph therefore as you indicated you need to have full architectures and functions used.
.pb (Protobuffer)
This is the TensorFlow file format which saves everything about the Model including custom objects, this is the recommended file format to ensure maximum portability when using and exporting to different platforms (ie. Tensorflow Lite, Tensorflow Serving, etc.).
.h5 (HD5F)
This is the suggested saving format of Native Keras, which also saves everything about the model but when used in TensorFlow 2.1.0 (import tensorflow.keras) it will not save the custom objects automatically and will require additional steps to be performed.
You could read more about it in this link.

Convert TFLite to Lite

I have a working app with TFlite using tensorflow for poets. It works with a labels.txt and graph.lite pair files. I have downloaded another model in .tflite file format and wanted to use in my application. I wanted to ask what are the differences between .lite and .tflite files and are there any ways to convert tflite format to lite?
Thanks
There is no difference in ".lite" and ".tflite" format (as long as they can be correctly consumed by Tensonflow Lite). And there is no need to convert them.

Custom MLModel File

I am new with machine learning and want to do following implementation
Want to create a custom .mlmodel with input of "xls or csv or nsdata of this files" and output should be double or array.
Pythone file should be able to read input data because i am going to use train_data from this input.
Pythone will do some calculation on this input data and provide prediction on this (i will do this calculation using sklearn,LinearRegression)
Can any one please help me how i can do this ?
You can use python to train your model with SKLearn as you suggested. This is a good post on getting started with that (make sure you use Sklearn and not Statsmodels).
https://towardsdatascience.com/simple-and-multiple-linear-regression-in-python-c928425168f9
When you have trained your model, you can convert it using Apple's coremltools:
https://github.com/apple/coremltools
When you've converted it you can add your .mlmodel file to your xcode project. You'll then need to write some code to get all of the your model inputs collected from your app and pass them as inputs to the model.
Good luck!

How to Port a .ckpt to a .pb for use in Tensorflow for Mobile Poets

I am trying to convert a pretrained InceptionV3 model (.ckpt) from the Open Images Dataset to a .pb file for use in the Tensorflow for Mobile Poets example. I have searched the site as well as the GitHub Repository and have not found any conclusive answers.
(OpenImages Inception Model: https://github.com/openimages/dataset)
Thank you for your responses.
Below I've included some draft documentation I'm working on that might be helpful. One other thing to look out for is that if you're using Slim, you'll need to run export_inference_graph.py to get a .pb GraphDef file initially.
In most situations, training a model with TensorFlow will give you a folder containing a GraphDef file (usually ending with the .pb or .pbtxt extension) and a set of checkpoint files. What you need for mobile or embedded deployment is a single GraphDef file that’s been ‘frozen’, or had its variables converted into inline constants so everything’s in one file.
To handle the conversion, you’ll need the freeze_graph.py script, that’s held in tensorflow/pythons/tools/freeze_graph.py. You’ll run it like this:
bazel build tensorflow/tools:freeze_graph
bazel-bin/tensorflow/tools/freeze_graph \
--input_graph=/tmp/model/my_graph.pb \ --input_checkpoint=/tmp/model/model.ckpt-1000 \ --output_graph=/tmp/frozen_graph.pb \
--input_node_names=input_node \
--output_node_names=output_node \
The input_graph argument should point to the GraphDef file that holds your model architecture. It’s possible that your GraphDef has been stored in a text format on disk, in which case it’s likely to end in ‘.pbtxt’ instead of ‘.pb’, and you should add an extra --input_binary=false flag to the command.
The input_checkpoint should be the most recent saved checkpoint. As mentioned in the checkpoint section, you need to give the common prefix to the set of checkpoints here, rather than a full filename.
output_graph defines where the resulting frozen GraphDef will be saved. Because it’s likely to contain a lot of weight values that take up a large amount of space in text format, it’s always saved as a binary protobuf.
output_node_names is a list of the names of the nodes that you want to extract the results of your graph from. This is needed because the freezing process needs to understand which parts of the graph are actually needed, and which are artifacts of the training process, like summarization ops. Only ops that contribute to calculating the given output nodes will be kept. If you know how your graph is going to be used, these should just be the names of the nodes you pass into Session::Run() as your fetch targets. If you don’t have this information handy, you can get some suggestions on likely outputs by running the summarize_graph tool.
Because the output format for TensorFlow has changed over time, there are a variety of other less commonly used flags available too, like input_saver, but hopefully you shouldn’t need these on graphs trained with modern versions of the framework.

Categories

Resources