Ask Your Question

Revision history [back]

Can't read Tensorflow pb & pbtxt using OpenCV 3.4.3

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

I can provide any files you ask, please help me. In tensorflow's github they told me to ask in Stackoverflow...

Can't read Tensorflow pb & pbtxt using OpenCV 3.4.3

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

I can provide any files you ask, please help me. In tensorflow's github they told me to ask in Stackoverflow...

click to hide/show revision 3
retagged

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

berak gravatar image

Can't read Tensorflow pb & pbtxt using OpenCV 3.4.3

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