How to use a keras custom trained classifier's .pb file inside OpenCV DNN module

asked 2020-07-14 20:16:24 -0600

pardeep kumar gravatar image

I'm having trouble using the .pb file generated with keras, I think this is because I don't have a .pbtxt file but I can't generate one for that I would need a .config file and a relevant script (all scripts currently are for ssd and faster rcnn)

from tensorflow.keras.applications.vgg16 import VGG16
import cv2

model = VGG16(weights='imagenet')
model.save("vgg")

net =  cv2.dnn.readNetFromTensorflow("vgg/saved_model.pb")

Error:

OpenCV(4.1.2) /io/opencv/modules/dnn/src/tensorflow/tf_io.cpp:42: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: vgg/saved_model.pb in function 'ReadTFNetParamsFromBinaryFileOrDi

Tensorflow: 2.2.0

Opencv: 4.3.0

Note: Ofcourse I will use fine tuning to train this vgg before saving it, this is just the bare bones code to re produce the error.

Also I'm successfully able to use the classifier if I save it in .h5 and then convert to .onnx and then use it in opencv but I'm wondering how to use it in .pb

Thanks

edit retag flag offensive close merge delete

Comments

afaik, saved_model from keras is not supported yet.

berak gravatar imageberak ( 2020-07-15 11:08:04 -0600 )edit
1

Thanks for answering, are there any future plans for it.

Also even converting to onnx and then using it sometimes causes problems with some architectures even with popular architectures, the errors are hard to diagnose. For e.g. I just had an issue where the onnx model loaded in 4.0.0 but not in 4.2 and 4.3. I was getting this error:

params.has("paddings") in function 'PaddingLayerImpl'

Do you know why was that

pardeep kumar gravatar imagepardeep kumar ( 2020-07-15 20:17:07 -0600 )edit

are there any future plans for it.

i very much guess so, but they're uhhmm -- future plans

there were tons of recent changes in the tf api, tf2.x, tflite, saved models ... will take a while to catch up.

berak gravatar imageberak ( 2020-07-16 01:43:04 -0600 )edit