How to load a keras model file to OpenCV code? - python

I've created a traffic sign classification model. I need to use it in OpenCV for Video traffic sign detection.
How can I do that using a .h5 file
Thanks in advance!

In openCV you can read weights with
cv2.dnn.readNetFromCaffe
cv2.dnn.readNetFromTensorFlow
cv2.dnn.readNetFromTorch
cv2.dnn.readhTorchBlob
Because you can not read with opencv without manipulations and you need:
You can read frames with OpenCV and put the frames to run in Keras
Or you can convert keras model to tensorflow model and run with Opencv cv2.dnn.readNetFromTensorFlow

Related

How is the MobileNet preprocess input in tensorflow

When we use some famous CNN deep neural networks such as MobileNet, it is recommended to preprocess an image before feeding it into the network. I found a sample code that uses MobileNet. In this code, the preprocess on the image is done by the following code in TensorFlow 2.7.0:
tf.keras.applications.mobilenet.preprocess_input(image)
I need to preprocess the input image only using PIL and OpenCV in python. Therefore, I need to know the procedure of MobileNet preprocesses in TensorFlow. I will be grateful to guide.
As already stated here:
[...] mobilenet.preprocess_input will scale input pixels between -1 and 1.
As already mentioned, you could also check out the source code itself. With opencv, you would just use cv2.resize(*) and cv2.normalize(*).

How to convert darknet image classification weights file to pytorch pt?

I created a model of darknet53.weights for image classification using my original data in darknet.
(This isn't a YOLO v3 model.)
Is there a way to convert a darknet53.weight to a pytorch pt model?
I tried quoting various codes on github etc., but all of them can convert only YOLOv3 weights file to pytorch's pt model.
I want to compare the accuracy of the darknet53 model created with darknet with other image classification models created with pytorch.
Initially, I tried to make a darknet53 model with pytorch, but that didn't work. Therefore, I created a darknet53 model with darknet.
If anyone knows a good way, please teach me.
Thanks.

Can I retrain OpenCV DNN face detector using my own face dataset and .pb .pbtxt files provided by OpenCV?

I want to fine tune existing OpenCV DNN face detector to a face images database that I own. I have opencv_face_detector.pbtxt and opencv_face_detector_uint8.pb tensorflow files provided by OpenCV. I wonder if based on this files is there any way to fit the model to my data? So far, I haven't also managed to find any tensorflow training script for this model in OpenCV git repository and I only know, that given model is and SSD with resnet-10 as a backbone. I am also not sure, reading the information on the internet, if I can resume training from .pb file. Are you aware of availability of any scripts defining the model, that could be used for training? Would pbtxt and pb files be enough to continue training on new data?
Also, I noticed that there is a git containing caffe version of this model https://github.com/weiliu89/caffe/tree/ssd. Although I never worked with caffe before, would it be possible/easier to use existing weight (caffe .pg and .pbtxt files are also available in OpenCV's github) and fit the model to my dataset?
I don't see a way to do this in opencv, but I think you'd be able to load the model into tensorflow and use model.fit() to retrain.
The usual advice about transfer learning applies. You'd probably want to freeze most of the early layers and only retrain the last one or two. A slow learning rate would be advised as well.

Bundle pre-processing steps to Tensorflow SavedModel

I have built an Image Classifier model using Inception V3 and I have saved the model in "SavedModel" format to deploy it to production. I am wondering how I can bundle the pre-processing steps to the final model so that the model ingest data in its natural form.
The pre-processing steps that I have are:
- resizing the image to target_size of 299, 299 using keras load_model
- change the image to numpy array
- expand dimensions
- pre_process input using inception_v3 import preprocess_input call
When a model is deployed, as per my understanding what actually is deployed is the python code for inference utilising the model. In this python code you can write code for all your preprocessing using openCV or any other python libraries and pass the image as an argument to this python code.
eg inferenceFile.py imageToInfer.png
An out of the box thought would be to write a different deep learning model to which as input your non-preprocessed image and output the preprocessed image you feed to the model, Not sure if this could be achievable.

Tensorflow Object Detection: training from scratch using a .h5 (hdf5) file

I need to train from scratch a CNN over a COCO dataset with a specific configuration: https://github.com/tensorflow/models/blob/master/research/object_detection/samples/configs/embedded_ssd_mobilenet_v1_coco.config
Thus, I installed TF Object Detection API and I downloaded the COCO dataset. However the dataset is in .h5 extension.
Is it possible to run the training with this kind of file or do I need to convert it in images in someway? If that is possible, what would the command be?
PS: I was not able to find a pre-trained model with that config, this is why I need to train a cnn from scratch.
My suggestion would be to convert the .hdf5 file to a .tfrecord file, you can find examples of how to do this here.

Categories

Resources