I trained a model using yolov5, Then exported it to TensorFlow saved_model format, the result was a yolo5s.pt file. As far as I know yolov5 uses PyTorch, I prefer TensorFlow. Now I want to build a model in TensorFlow using the saved_model file, how can I do it?
It will be preferable if the solution is in google colab, I didn't included my code because I don't have any ideas how to start.
Related
Is it possible to convert the YOLOv5 PyTorch model to the Tensorflow.js model?
I am developing an object detection web app. so I have trained the data using Yolov5, but now I am looking for the correct method to convert that model to Tf.js.
I think this is what you are looking for https://github.com/zldrobit/tfjs-yolov5-example
Inside the YoloV5 repo, run the export.py command.
python export.py --weights yolov5s.pt --include tfjs
Then cd into the above linked repo and copy the weights folder to the public:
cp ./yolov5s_web_model public/web_model
Don't forget, you'll have to change the names array in src/index.js to match your custom model.
But unfortunately, it seems painfully slow at about 1-2 seconds. I don't think I was able to get WebGL working.
With few interim steps, but most of the times it works:
Export PyTorch to ONNX
Convert ONNX to TF Saved Model
Convert TF Saved Model to TFJS Graph Model
When converting from ONNX to TF, you might need to adjust target version if you run into unsupported ops.
Also, make sure to set input resolutions to a fixed values, any dynamic inputs get messed up in this multi-step conversion.
I followed the tutorial at
https://machinelearningmastery.com/how-to-train-an-object-detection-model-with-keras/
After successful training I got 5 .h5 files:
mask_rcnn_kangaroo_cfg_0001.h5
mask_rcnn_kangaroo_cfg_0002.h5
mask_rcnn_kangaroo_cfg_0003.h5
mask_rcnn_kangaroo_cfg_0004.h5
mask_rcnn_kangaroo_cfg_0005.h5
I am a newbie to this, so my understanding may be wrong:
How can I convert these .h5 files to .pb files or better to .tflite files, so I can use them in an Android Object Detection app?
You don't need to convert these .h5 to .pb, you can directly convert keras .h5 files to tflite. Here is the official documentation on how to.
Make sure to have the model with layers supported by TFLite, as mentioned here.
Once you have the .tflite model you can run an interpreter on Android.
I have downloaded the .weights and .cfg file for YOLOv3 from darknet (link: https://pjreddie.com/darknet/yolo/) I want to create a model and assign the weights from these files, and I want to save the model with the assigned weights to a .h5 file so that I can load the .h5 model into Keras by using keras.models.load_model().
Please help.
You should check the instructions given in this repository. This is basically the keras implementation of YOLOv3 (Tensorflow backend).
Download YOLOv3 weights from YOLO website.
Convert the Darknet YOLO model to a Keras model.
python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5
As you have already downloaded the weights and configuration file, you can skip the first step. Download the convert.py script from repository and simply run the above command.
Note: Above command assumes that yolov3.cfg, yolov3.weights and model_data(folder) are present at the same path as convert.py.
For people getting error from this try changing the layers part in 'convert.py'
Not sure if it was version problem but changing the way converter.py file was loading 'keras.layers' solved all errors for me
General question: is there tooling to convert from tflite format to any other format?
I'm trying to convert a keras model to a CoreML model, but I can't because the model uses a layer type unsupported by CoreML (Gaussian Noise). Converting the keras .h5 model to a .tflite is simple, removes the offending layer (which is only used in training anyway), and performs some other optimisations. But it doesn't seem possible to convert out of the resultant tflite to any other format. Coremltools doesn't support tflite. I thought I could probably load the model from tflite into a tensorflow session, save a .pb from there, and convert that to coreml using coremltools, but I can't see a way to load the tflite model into a tensorflow session. I saw the documentation linked to in this question, but that seems to use the tflite interpreter to read the tflite model, rather than a "true" Tensorflow session.
Went thru https://tensorflow.github.io/serving/serving_basic and was able to run inference MNIST example using Tensorflow Serving server.
Now, I would like to use a trained modified InceptionV3 model (which generates 2 files: .pb and .txt) to export and use it for inference.
Serving Basic tutorial use mnist_saved_model.py for training and exporting the model. Is this file to be modified for the trained modified InceptionV3 model? Also, what is the difference between mnist_saved_model.py and mnist_export.py?
Looked at How to serve the Tensorflow graph file (output_graph.pb) via Tensorflow serving? but the example mnist_saved_model.py creates a directory called 1 and subdirectories as shown below
$> ls /tmp/mnist_model/1
saved_model.pb variables
Thoughts??