Convert .pth Pytorch model to format readable by OpenCv - python

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.

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/

Yolov4 weights conversion to tflite failure

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..

Python Tensorflow 2.3.1 'utf-8' codec can't decode byte, invalid continuation byte. UnicodeDecodeError

I'm trying to train an object detection model, but I can't make it work. I'm trying based on this:
htps://gilberttanner.com/blog/tensorflow-object-detection-with-tensorflow-2-creating-a-custom-model
but in pycharm.
My issue is that I got the following error when running model_main_tf2.py:
utf-8 error
Python version is the latest 3.8 Pycharm 2020.2.3 and tensorflow 2.3.1. I had to add the object_detection folder to the tensorflow manually because pycharm couldn't manage to get it right.
Project
The main problem is here:
self._read
I tried creating the .record files with latin-1 encoding but no luck
Edit:
Used this to convert the labelled images to .csv files:xml_to_csv
This to create the tf files:tfpic1 tfpic2 tfpic3
Main_module I used: https://github.com/tensorflow/models/blob/master/research/object_detection/model_main.py
With 2 changes: import tensorflow as tf instead of the one in this.
And tf.compat.v1.app.run() instead of the one at the end of this. I had to change a lot of tf functions in the files for object detection because it only works when i call the functions as tf.compat.v1. rather than tf
I encountered the similar issue while following tensorflow object detection guide.
In my case the problem was in the incorrect pipeline.config PATHs (I thought PATHs were already specified for me when i copied them, but I was wrong).
Not sure if it will help you, but it would be nice if it works.

Unable to get keras flow_from_directory to work with .gif data

I am trying to build an Image classifier with keras and tensorflow. However currently flow_from_directory does not see my images due to them being in .gif format (I checked this with .jpg and it works here). How can I fix this?
This old github page claims that I should be able to put .gif on my white_list_formats in the keras/preprocessing/image.py file. But after opening it there seems to be no white_list_formats in my version of image.py. Did keras change anything here?
I'm on Windows using anaconda3 distribution in case that matters.
Thanks for your help!
The github post you are referring to was posted for keras version 1.1.0. In the latest version they have removed that variable.
In the latest version of Keras, below formats are only supported.
PNG, JPG, BMP, PPM, TIF
Please refer to the this for documentation.
If you still want to try and edit white_list_format, please install keras 1.1.0 and try it.

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