Ask Your Question

Can't read Tensorflow pb & pbtxt using OpenCV 3.4.3

asked 2019-03-14 09:20:57 -0500

updated 2019-03-14 16:54:32 -0500

berak gravatar image

I have followed this tutorial to retrain MobileNet SSD V1 using Tensorflow GPU as described and got 0.5 loss after training using GPU (below more info about config) and got model.ckpt.

This is the command I used for Training:

python ../models/research/object_detection/legacy/ --logtostderr --train_dir=./data/ --pipeline_config_path=./ssd_mobilenet_v1_pets.config

And this is the command for freezing (generate pb file):

python ../models/research/object_detection/ --input_type image_tensor --pipeline_config_path ./ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix ./data/model.ckpt-1407 --output_directory ./data/

This is the error I get when I use frozen pb and pbtxt:

Traceback (most recent call last):
File "", line 29, in <module>
    cvOut = cvNet.forward()
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:565: error: (-215:Assertion failed) inputs.size() == requiredOutputs in function 'cv::dnn::experimental_dnn_34_v7::DataLayer::getMemoryShapes'

This is the file I used:

import cv2 as cv
import os 
import time 
import logging

logger = logging.getLogger()
fh = logging.FileHandler('xyz.log')

cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'object_detection.pbtxt')
dir_x  = "C:\\Users\\Omen\\Desktop\\LP_dataset\\anno"
for filename in os.listdir(dir_x):
    if not (filename.endswith(".png") or filename.endswith(".jpg")):
    img = cv.imread(os.path.join(dir_x,filename))
    img = cv.resize(img, (300,300))
    img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    img = cv.cvtColor(img,cv.COLOR_GRAY2RGB)
    rows = img.shape[0]
    cols = img.shape[1]
    #cvNet.setInput(cv.dnn.blobFromImage(img, size=(cols,rows), swapRB=True, crop=False))
    cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), crop=False))
    t0  = time.time()
    cvOut = cvNet.forward()
    print(time.time() - t0)
    for detection in cvOut[0,0,:,:]:
        score = float(detection[2])
        if score > 0.80:
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
            cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)

    cv.imshow('img', img)

This is pbtxt file (also I tried the exported pbtxt and generated pbtxt from pb but not working):

item {
  id: 1
  name: 'licenseplate'


What is the top-level directory of the model you are using: object_detetion

Have I written custom code: no

OS Platform and Distribution: win10

TensorFlow installed from: binary

TensorFlow GPU version: 1.13.0

CUDA/cuDNN version: 10

GPU model: 1050 GTX

edit retag flag offensive close merge delete


i can't solve it, but a few thoughts:

  • if your pbtxt file really looks like this, it's not useable. (it does not reflect the network architecture) either try to generate a better one, or try using cv.dnn.readNet() without any pbtxt
  • all those bgr2gray / gray2rgb conversions look quite useless, and counter-productive. rahter decide, if or not it needs swapBR, and use that with the original (color !) image
berak gravatar imageberak ( 2019-03-15 07:17:52 -0500 )edit

I was able to fix the pbtxt which wasn't the right one before, now it works but doesn't detect, is it normal after getting 0.5-0.4 loss ? or should I train more?

soufiane sabiri gravatar imagesoufiane sabiri ( 2019-03-17 10:12:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-03-14 11:02:07 -0500

supra56 gravatar image

Change this:

cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), crop=False))


cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True))

This work for raspberry pi

edit flag offensive delete link more


Thanks for answering. I tried that, it didn't work for me... Please help!

soufiane sabiri gravatar imagesoufiane sabiri ( 2019-03-14 11:09:37 -0500 )edit

Can you coment out this img = cv.cvtColor(img,cv.COLOR_GRAY2RGB) and see what happened?

supra56 gravatar imagesupra56 ( 2019-03-14 11:14:29 -0500 )edit

Are you using raspberry pi or pc?

supra56 gravatar imagesupra56 ( 2019-03-14 11:15:52 -0500 )edit

PC Windows 10, I commented, it didn't work.. I am uploading the files

soufiane sabiri gravatar imagesoufiane sabiri ( 2019-03-14 11:17:45 -0500 )edit

Comment out this one img = cv.resize(img, (300,300))

supra56 gravatar imagesupra56 ( 2019-03-14 11:20:53 -0500 )edit

Thanks for link. Btw, it about lunch time.

supra56 gravatar imagesupra56 ( 2019-03-14 11:23:27 -0500 )edit

I tried that before, didn't work either... ok have a good lunch my friend.

soufiane sabiri gravatar imagesoufiane sabiri ( 2019-03-14 11:25:01 -0500 )edit

Can you coment out both cvtColor? You needed Colour but not gray.

                                    (300, 300),
                                    (127.5, 127.5, 127.5),
supra56 gravatar imagesupra56 ( 2019-03-14 21:17:34 -0500 )edit

I was able to solve it thanks to stackoverflow

soufiane sabiri gravatar imagesoufiane sabiri ( 2019-03-17 10:12:01 -0500 )edit

Question Tools

1 follower


Asked: 2019-03-14 09:20:57 -0500

Seen: 1,406 times

Last updated: Mar 14 '19