keras_to_tensorflow
keras_to_tensorflow copied to clipboard
opencv run keras to tensorflow file fail!
@amir-abdi help!!!
1、download https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5
2、convert only .pb file python3 keras_to_tensorflow.py --input_model="model/yolov3.h5" --output_model="model/yolov3.pb"
opencv exec:
import numpy as np
import tensorflow as tf
import cv2 as cv
# Read the graph
with tf.gfile.FastGFile('yolov3.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Session() as sess:
# Restore session
sess.graph.as_default()
tf.import_graph_def(graph_def, name='')
# Read and preprocess an image.
img = cv.imread('example.jpg')
rows = img.shape[0]
cols = img.shape[1]
inp = cv.resize(img, (300, 300))
inp = inp[:, :, [2, 1, 0]] # BGR2RGB
# Run the model
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
sess.graph.get_tensor_by_name('detection_scores:0'),
sess.graph.get_tensor_by_name('detection_boxes:0'),
sess.graph.get_tensor_by_name('detection_classes:0')],
feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
# Visualize detected bounding boxes.
num_detections = int(out[0][0])
for i in range(num_detections):
classId = int(out[3][0][i])
score = float(out[1][0][i])
bbox = [float(v) for v in out[2][0][i]]
if score > 0.3:
x = bbox[1] * cols
y = bbox[0] * rows
right = bbox[3] * cols
bottom = bbox[2] * rows
cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
cv.imshow('TensorFlow MobileNet-SSD', img)
cv.waitKey()
fail: KeyError: "The name 'num_detections:0' refers to a Tensor which does not exist. The operation, 'num_detections', does not exist in the graph."
3、convert yolov3.pb file and yolov3.pbtxt python3 keras_to_tensorflow.py --input_model="model/yolov3.h5" --output_model="model/yolov3.pb" --save_graph_def=True
opencv exec:
import cv2 as cv
cvNet = cv.dnn.readNetFromTensorflow('yolov3.pb', 'yolov3.pbtxt')
img = cv.imread('example.jpg')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
for detection in cvOut[0,0,:,:]:
score = float(detection[2])
if score > 0.3:
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()
fail:
Traceback (most recent call last):
File "test.py", line 3, in
os:
Ubuntu 16.04.6 LTS
Linux 4.15.0-55-generic #60~16.04.2-Ubuntu SMP Thu Jul 4 09:03:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
tensorflow-gpu version:
Name: tensorflow-gpu
Version: 1.13.1
opencv version:
Name: opencv-python
Version: 4.1.1.26
same issue!!! can anyone help?
same issue! is there any solution ?