DifferentiableBinarization
DifferentiableBinarization copied to clipboard
incorrect results when using the model in opencv
Hi,
I tried to use the model in opencv-4.3 using the following steps:
from model import dbnet
import keras.backend as K
import tensorflow as tf
from openvino.inference_engine import IENetwork, IECore
import cv2
import numpy as np
import time
import math
from shapely.geometry import Polygon
import pyclipper
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ''
frozen_graph = tf.graph_util.convert_variables_to_constants(
session, input_graph_def, output_names, freeze_var_names)
return frozen_graph
_, model = dbnet()
K.set_learning_phase(0)
model.load_weights('path/to/2020-01-02/db_48_2.0216_2.5701.h5', by_name=True, skip_mismatch=True)
frozen_graph = freeze_session(K.get_session(),
output_names=[out.op.name for out in model.outputs])
tf.train.write_graph(frozen_graph, ".", "db_model.pb", as_text=False)
net = cv2.dnn.readNetFromTensorflow("db_model.pb")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
image = cv2.imread(path+"output_516.jpg")
h,w,c = image.shape
mean = np.array([103.939, 116.779, 123.68])
image = resize_image(image)
image = image.astype(np.float32)
image -= mean
blob = cv2.dnn.blobFromImage(image)
net.setInput(blob)
preds = net.forward()
bitmap = preds[0][0] > 0.3
boxes, scores = polygons_from_bitmap(preds[0][0], bitmap, w h, box_thresh=0.5)
But the output of boxes and scores is always an empty list. The same image results in the detection of 2 regions when it is run on the inference.py script.
Could you suggest what am i doing wrong here?
Same error with opencv-python==4.4.0.44.
Is it the issue of the opencv or other files.
Also while loading the weights it shows a warning
WARNING:tensorflow:Skipping loading of weights for layer conv2d_4 due to mismatch in number of weights (1 vs 2).
WARNING:tensorflow:Skipping loading of weights for layer conv2d_transpose_1 due to mismatch in number of weights (2 vs 1).