- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I re-trained EfficientDet model (taken from tensorflow object detection API models and modified the pipeline.config) using my own dataset. Then, tested the trained model using TensorFlow for which the prediction results were good as in 1st image "tensorFlow_results".
Then I exported the model, converted the saved model to openvino using the command:
"mo --saved_model_dir <saved_model_path> --transformations_config <json_file> --tensorflow_object_detection_
This yielded the xml and bin files and after conversion to openvino, when i tested the results using openvino 2022.1 (both development and runtime are 2022.1) IEcore() the results were wrong and had lot of bounding boxes as in 2nd image "openvino_results". Code for which is:
import cv2
import numpy as np
from openvino.inference_engine import IECore
import os
import json
import time
#load model
model_xml = "path_to_xml"
model_bin = os.path.splitext(model_xml)[0] + ".bin"
# Initialize inference engine
ie = IECore()
# Read network
net = ie.read_network(model_xml, model_bin)
input_name = next(iter(net.input_info)) #retrieves the name of the first input layer of the network
input_data = net.input_info[input_name].input_data #retrieves the input data information from the network for the first input layer
input_shape = input_data.shape
input_layout = input_data.layout
input_size = (input_shape[2], input_shape[3])
current_time = time.time()
exec_network = ie.load_network(network=net, device_name="CPU", num_requests=1)
print("Time taken to load network is {} seconds".format((time.time() - current_time)))
# Load classes
classes_path = "/home/classes.json" #classes.json has {"0": "__background__", "1": "class0", "2": "class1", "3": "class2", "4": "class3", "5": """91": "hair brush"}
with open(classes_path) as f:
classes = f.read()
classes = json.loads(classes)
def draw_bounding_boxes(image, detections, classes, threshold=0.50, box_color=(255, 0, 0)):
image_copy = np.copy(image)
# Get image dimensions
image_height = image_copy.shape[0]
image_width = image_copy.shape[1]
# Iterate through detections
no_detections = detections.shape[2]
#print(detections[0, 0, 0])
#print(detections[0,0,0][2])
#print(classes[str(int(detections[0,0,0][1]))])
for i in range(no_detections):
detection = detections[0, 0, i]
# Skip detections with confidence below threshold
confidence = detection[2]
if confidence > threshold:
continue
# print(int(detections[0,0,i][1]))
class_ = classes[str(int(detections[0,0,i][1]))]
# print(class_)
if str(class_) == "class0":
# Draw bounding box
x_min = int(detection[3]*image_width)
y_min = int(detection[4]*image_height)
x_max = int(detection[5]*image_width)
y_max = int(detection[6]*image_height)
top_left = (x_min, y_min)
bottom_right = (x_max, y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, 2)
#Draw text background
text_size = cv2.getTextSize(class_, cv2.FONT_HERSHEY_PLAIN, 2, 1)
top_left = (x_min, y_max-text_size[0][1])
bottom_right = (x_min+text_size[0][0], y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, cv2.FILLED)
# Draw text
cv2.putText(image_copy,class_ , (x_min,y_max), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 1)
if str(class_) == "class1":
x_min = int(detection[3]*image_width)
y_min = int(detection[4]*image_height)
x_max = int(detection[5]*image_width)
y_max = int(detection[6]*image_height)
top_left = (x_min, y_min)
bottom_right = (x_max, y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, 2)
#Draw text background
text_size = cv2.getTextSize(class_, cv2.FONT_HERSHEY_PLAIN, 2, 1)
top_left = (x_min, y_max-text_size[0][1])
bottom_right = (x_min+text_size[0][0], y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, cv2.FILLED)
# Draw text
cv2.putText(image_copy,class_ , (x_min,y_max), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 1)
if str(class_) == "class2":
# Draw bounding box
x_min = int(detection[3]*image_width)
y_min = int(detection[4]*image_height)
x_max = int(detection[5]*image_width)
y_max = int(detection[6]*image_height)
top_left = (x_min, y_min)
bottom_right = (x_max, y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, 2)
#Draw text background
text_size = cv2.getTextSize(class_, cv2.FONT_HERSHEY_PLAIN, 2, 1)
top_left = (x_min, y_max-text_size[0][1])
bottom_right = (x_min+text_size[0][0], y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, cv2.FILLED)
# Draw text
cv2.putText(image_copy,class_ , (x_min,y_max), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 1)
if str(class_) == "class3":
# Draw bounding box
x_min = int(detection[3]*image_width)
y_min = int(detection[4]*image_height)
x_max = int(detection[5]*image_width)
y_max = int(detection[6]*image_height)
top_left = (x_min, y_min)
bottom_right = (x_max, y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, 2)
#Draw text background
text_size = cv2.getTextSize(class_, cv2.FONT_HERSHEY_PLAIN, 2, 1)
top_left = (x_min, y_max-text_size[0][1])
bottom_right = (x_min+text_size[0][0], y_max)
cv2.rectangle(image_copy, top_left, bottom_right, box_color, cv2.FILLED)
# Draw text
cv2.putText(image_copy,class_ , (x_min,y_max), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 1)
return image_copy
def prepare_image(image, target_size=(512,512), target_layout="NCHW"):
# Resize image, [H, W, C] -> [300, 300, C]
image_copy = cv2.resize(image, target_size) #output (512,512,3)
print("Resized", image_copy.shape)
# Swap axes, [H, W, C] -> [C, H, W]
if target_layout == "NCHW":
image_copy = np.swapaxes(image_copy, 0, 2)
image_copy = np.swapaxes(image_copy, 1, 2)
# Expand dimensions, [1, C, H, W]
# print("CHW", len(image_copy))
image_copy = np.expand_dims(image_copy,0)
print("NCHW", image_copy.shape) #o/p: (1,3,512,512)
return image_copy
# PROVIDE PATH TO IMAGE DIRECTORY
path=r''
for i in os.listdir(path):
IMAGE_PATHS = path+i
print(IMAGE_PATHS)
# Read and prepare data
image = cv2.imread(IMAGE_PATHS)
# print("im",im_cv)
# print(im_cv.shape)
# image = cv2.cvtColor(im_cv, cv2.COLOR_RGB2BGR)
image_prepared = prepare_image(image, target_size=input_size, target_layout=input_layout)
# Make inference
output = exec_network.infer({input_name: image_prepared})
# Get detections from output layer "DetectionOutput"
# print(output)
# detections = output["detections"]
detections = output["DetectionOutput"]
# Draw bounding boxes
image_boxed = draw_bounding_boxes(image, detections, classes)
current_path = ''
print('Done')
# DISPLAYS OUTPUT IMAGE
cv2.imshow("ssd_new",image_boxed)
result=cv2.imwrite(current_path+'output_ovino_trained/'+str(i)+'.jpg',image_boxed)
if result==True:
print("File saved successfully")
else:
print("Error in saving file\n-------------------")
#cv2.imshow("Detected image",image1)
cv2.waitKey(0)
# CLOSES WINDOW ONCE KEY IS PRESSED
cv2.imwrite('output.jpg',image_boxed)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page