Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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