Ask Your Question
0

Can't read Tensorflow pb & pbtxt using OpenCV 3.4.3

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

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

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/train.py --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/export_inference_graph.py --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 "Object_detection_image.py", 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 Object_detection_image.py file I used:

import cv2 as cv
import os 
import time 
import logging

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

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):
    print(filename)
    if not (filename.endswith(".png") or filename.endswith(".jpg")):
        continue
    print('daz')
    img = cv.imread(os.path.join(dir_x,filename))
    img = cv.resize(img, (300,300))
    #cv.imshow('i',img)
    #cv.waitKey(0)
    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])
        #print(score)
        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)
    cv.waitKey(0)

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

item {
  id: 1
  name: 'licenseplate'
}

Config:

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

Comments

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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

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

supra56 gravatar image

Change this:

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

to

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

This work for raspberry pi

edit flag offensive delete link more

Comments

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 -0600 )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 -0600 )edit

Are you using raspberry pi or pc?

supra56 gravatar imagesupra56 ( 2019-03-14 11:15:52 -0600 )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 -0600 )edit

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

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

Thanks for link. Btw, it about lunch time.

supra56 gravatar imagesupra56 ( 2019-03-14 11:23:27 -0600 )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 -0600 )edit
1

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

cvNet.setInput(cv.dnn.blobFromImage(img,
                                    1.0/127.5,
                                    (300, 300),
                                    (127.5, 127.5, 127.5),
                                    swapRB=True,
                                    crop=False)
               )
supra56 gravatar imagesupra56 ( 2019-03-14 21:17:34 -0600 )edit

I was able to solve it thanks to stackoverflow https://stackoverflow.com/questions/5...

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

Question Tools

1 follower

Stats

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

Seen: 2,383 times

Last updated: Mar 14 '19