How to use a keras custom trained classifier's .pb file inside OpenCV DNN module
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
afaik, saved_model from keras is not supported yet.
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:
Do you know why was that
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.