Yolov4 weights conversion to tflite failure - python

I am using Google Colabs to convert Yolov4 darknet weights to tflite version.
I used this blog to train my own yolo detector and I got to acceptable accuracy for my detections.
Then I tried every repository on github 1,2,3 to covert my custom yolov4 weights to tflite version and I failed in each time. I faced the problem (can not reshape array) here and I solved it by modifying file.names and adjusting the change in the config file. In the end when I executed this code:
!python convert_tflite.py --weights ./data/yolov4.weights --output ./data/yolov4.tflite
I faced this error:
ValueError: Input 0 of node model_1/batch_normalization/AssignNewValue was passed float from model_1/batch_normalization/FusedBatchNormV3/ReadVariableOp/resource:0 incompatible with expected resource.
This problem was discussed here but I couldn't find any solution
I am using google colab which make it easier with using the preinstalled libraries but maybe there could be some incomparability in libraries used in conversion ?
Any help would be appreciated..

Related

tf.keras.models: load_model() ValueError: Unknown layer: Functional in OpenCV/DNN module. How to use pre-trained model with old versions of tf/keras?

I did transferlearning by using MaskRCNN for multiple-object detection in an environment with:
python=3.6.12
tensorflow==1.15.3
keras==2.2.4
mrcnn==2.1
And the model works.
Now I would like to implement mrcnn real-time with my laptop camera and OpenCV.
Firstly, I would apply face detection with res10_300x300_ssd_iter_140000.caffemodel because my mrcnn model works better if it is run on a face. I chose res10 because I have aleady used it in another project and it worked well!
Unfortunatly, I notice that MaskRCNN doesn't work with the latest version of tensorflow. Moreover, res10_300x300_ssd_iter_140000.caffemodel doesn't work with old versions of tensorflow and I get this error: "ValueError: Unknown layer: Functional".
I would like to know if it is possible to use res10_300x300_ssd_iter_140000.caffemodel
with previous versions of tensorflow, isn't it?
Is there a way to do a porting of MaskRCNN to a more recent version of tensorflow?
Or, is there a way to use res10 with old versions of tensorflow?
A different model for face detection in opencv with a good accuracy?
A different model rather than mrcnn tha is compatible with res10?
Any advice is welcome!
Thanks!
My Resources:
https://github.com/opencv/opencv/wiki/Deep-Learning-in-OpenCV
https://machinelearningmastery.com/how-to-train-an-object-detection-model-with-keras/
https://www.pyimagesearch.com/2020/05/04/covid-19-face-mask-detector-with-opencv-keras-tensorflow-and-deep-learning/

Tensorflow Object API TF2 not displaying visualizations in Tensorboard

Description
I had a setup for training using the object detection API that worked really well, however I have had to upgrade from TF1.15 to TF2 and so instead of using model_main.py I am now using model_main_tf2.py and using mobilenet ssd 320x320 pipeline to transfer train a new model.
When training my model in TF1.15 it would display a whole heap of scalars as well as detection box image samples. It was fantastic.
In TF2 training I get no such data, just loss scalars and 3 input images!! and yet the event files are huge gigabytes! where as they were in hundreds of megs using TF1.15
The thing is there is nowhere to specify what data is presented. I have not changed anything other than which model_main py file I use to run the training. I added num_visualizations: to the pipeline config file but no visualizations of detection boxes appear.
Can someone please explain to me what is going on? I need to be able to see whats happening throughout training!
Thank You
I am training on PC in virtual environment before performing TRT optimization in Linux but I think that is irrelevant here really.
Environment
GPU Type: P220
Operating System + Version: Win10 Pro
Python Version (if applicable): 3.6
TensorFlow Version (if applicable): 2
Relevant Files
TF1.15 vs TF2 screenshots:
TF1 (model_main.py) Tensorboard Results
TF2 (model_main_tf2.py) Tensorboard Results
Steps To Reproduce
The repo I am working with GitHub Object Detection API
Model
Pipeline Config File
UPDATE: I have investigated further and discovered that the tensorboard settings are being set in Object Detection Trainer 1 for TF1.15 and Object Detection Trainer 2 for TF2
So if someone who knows more than I do about this could work out what the difference is and what I need to do to get same result in tensorboard with v2 as I do with the first one that would be amazing and save me enormous headache. It would seem that this, even though it is documented as being for TF2, is not actually following TF2 syntax but I could be wrong.

Convert .pth Pytorch model to format readable by OpenCv

I'm trying to use this model: https://github.com/zllrunning/face-makeup.PyTorch/blob/master/cp/79999_iter.pth with opencv dnn module but because opencv doesn't read .pth file, I need to convert it to another format.
First I tried to convert to .onnx, I found code to do it but when I call the function "torch.onnx.export", I got "RuntimeError: ONNX symbolic expected a constant value in the trace".
I tried with differrent version of torch but I always got almost the same error message.
I tried to convert to caffe model, there is some available github to do it but none of them worked for me.
I didn't find how to convert directly to tensorflow, they always convert to onnx and then to tensorflow.
If someone can share me a code that works for this model and the version of all packages used. Or if you could share me the model converted to another format, it would be greatly appreciated.

Is there a way to use a compiled keras model on the RPI Zero?

I am working on a Letter Recognition Application for a robot. I used my home PC for training the model and wanted the recognition to be on the RPI Zero W with the already trained model.
I got an HDF model. When I try to install Tensorflow on the RPI zero, it's throwing a hash error, as far as I found it this is due to TF beeing for 64bit machines. When I try to install Tensorflow Lite, the installation stocks and crashes.
For saving the model I use:
classifier.save('test2.h5')
That are the Prediction lines:
test_image = ks.preprocessing.image.load_img('image.jpg')
test_image = ks.preprocessing.image.img_to_array(test_image)
result = classifier.predict(test_image)
I also tried to compile the python script via Nuitka, but as the RPI is ARM and nuitka is not offering cross-compile, this possibility felt out.
You can use already available TFLite to solve your issue.
If that does not help, you can also build TFLite from source.
Please refer to below links:
https://www.tensorflow.org/lite/guide/build_rpi
https://medium.com/#haraldfernengel/compiling-tensorflow-lite-for-a-raspberry-pi-786b1b98e646

Running MTCNN with OpenVino

I am trying to use OpenVino python API to run MTCNN face detection, however, the performance of the converted models degraded significantly from the original model. I am wondering how I could get similar results.
I converted the mtcnn caffe models into OpenVino *.xml and *.bin files using the following commands.
python3 mo.py --input_model path/to/PNet/det1.caffemodel --model_name det1 --output_dir path/to/output_dir
python3 mo.py --input_model path/to/RNet/det2.caffemodel --model_name det2 --output_dir path/to/output_dir
python3 mo.py --input_model path/to/ONet/det3.caffemodel --model_name det3 --output_dir path/to/output_dir
And used the step_by_step mtcnn jupyter notebook to check the performance of the converted models.
But detection results using OpenVino models degraded significantly. To regenerate the results you only need to load OpenVino models instead of pytorch model in the notebook.
To regenerate my results do the following steps.
Clone https://github.com/TropComplique/mtcnn-pytorch.git
And use this jupyter notebbok
As you will see the detected boxes in the first stage after P-Net are more than the detected boxes in the original model step_by_step mtcnn jupyter notebook.
Do you have any comment on this. It seems that there is no problem in model conversion the only difference is that pytorch has a variable tensor size (FloatTensor) but for OpenVino I have to reshape the input size for each scale. This might be the reason to get different results, however I have not been able to solve this problem.
I went through all the possible mistake I might had made and check parameters to convert mtcnn models from list_topologies.yaml. This file comes with OpenVino installation and list the parameters like scale mean values and etc.
Finally, I solved the problem by using MXNET pre-trained MTCNN networks.
I hope this would help other users who might encounter this problem.

Categories

Resources