You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inference speed on GPU is much slower than that on CPU. #17358
When I test my model using OpenCV DNN modules on CUDA backend and OPENCV backend, I find that the inference speed of my model on GPU is much slower than that on CPU.
And my net is just like a Siamese Network, It has two inputs and four outputs. @YashasSamaga finds that one of inputs may makes the model initialize more than once. There may be some bug in initialization ligic.
Steps to reproduce
Issue submission checklist
I report the issue, it's not a question
I checked the problem with documentation, FAQ, open issues,
answers.opencv.org, Stack Overflow, etc and have not found solution
I updated to latest OpenCV version and the issue is still there
There is reproducer code and related data files: videos, images, onnx, etc
The first number in each line is the actual time it took for net.forward() to finish executing. The second number in each line is what the layerwise sum of timings gives.
It appears like the CUDA backend is taking 42ms to compute the outputs. The remaining ~500ms is coming from initialization. The DNN module is reinitializing the backend even though the input shape is not changing.
The reinitialization is happening in OCV CPU backend too. The initialization cost is barely anything on CPU and hence it doesn't show up in the timings. So it's a bug in setInput.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
logits_concat, logits_color, logits_height = sess.run([concat_softmax, color_softmax, height_softmax], feed_dict={input_color: image, input_height: height})
print('color_input ans height_input: ',logits_concat)
print('color_input: ', logits_color)
print('height_input: ',logits_height)
print(np.argmax(logits_concat, axis=-1))
`` @alalek This is my testing code using tensorflow, and the output is as follows:
color_input ans height_input: [[1.318436e-05 1.318436e-05 9.999604e-01 1.318436e-05]]
color_input: [[5.8156675e-05 4.3866065e-05 9.9985409e-01 4.3866065e-05]]
height_input: [[4.5331984e-04 4.5331984e-04 9.9864000e-01 4.5331984e-04]]`
Activity
YashasSamaga commentedon May 24, 2020
I added getPerfProfile support to the CUDA backend here.
Code
The first number in each line is the actual time it took for
net.forward()to finish executing. The second number in each line is what the layerwise sum of timings gives.It appears like the CUDA backend is taking 42ms to compute the outputs. The remaining ~500ms is coming from initialization. The DNN module is reinitializing the backend even though the input shape is not changing.
YashasSamaga commentedon May 24, 2020
The reinitialization is happening in OCV CPU backend too. The initialization cost is barely anything on CPU and hence it doesn't show up in the timings. So it's a bug in
setInput.JTzhuang commentedon May 25, 2020
@YashasSamaga I found that inference time won't increase much if we increase the batch size of input.
JTzhuang commentedon Jun 7, 2020
@YashasSamaga Do you find any solutions about this issue?
alalek commentedon Jun 10, 2020
Looks like the input
color_inputis not connected to any layers in attached model (or there is another bug in DNN TF parser).DNN module doesn't handle this properly in TF parser.
Possible workaround:
net_ocv.setInput(colo_input_blob, "color_input");statements.JTzhuang commentedon Jun 11, 2020
@alalek So you think the error occured when I froze the model ? But when I test it using tensorflow, the model works well.
alalek commentedon Jun 11, 2020
Could you try the workaround above (drop setInput call for color_input) and check results (accuracy with TF)?
JTzhuang commentedon Jun 12, 2020
``
output_graph_def = tf.GraphDef()
with open('DP_concat.pb', "rb") as f:
output_graph_def.ParseFromString(f.read())
tensors = tf.import_graph_def(output_graph_def, name="")
graph = tf.get_default_graph()
input_color = graph.get_tensor_by_name("color_input:0")
input_height = graph.get_tensor_by_name("height_input:0")
concat_softmax = graph.get_tensor_by_name("feature_concat/Softmax:0")
color_softmax = graph.get_tensor_by_name("color/Softmax:0")
height_softmax = graph.get_tensor_by_name("height/Softmax:0")
image = cv2.imread('9-c.png')
height = cv2.imread('9-h.png')
image = cv2.resize(image, (224,224))
height = cv2.resize(height, (224, 224))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.astype(np.float32) / 255.
height = height.astype(np.float32) /255.
image = np.expand_dims(image, axis=0)
height = np.expand_dims(height, axis=0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
logits_concat, logits_color, logits_height = sess.run([concat_softmax, color_softmax, height_softmax], feed_dict={input_color: image, input_height: height})
print('color_input ans height_input: ',logits_concat)
print('color_input: ', logits_color)
print('height_input: ',logits_height)
print(np.argmax(logits_concat, axis=-1))
``
@alalek This is my testing code using tensorflow, and the output is as follows:
color_input ans height_input: [[1.318436e-05 1.318436e-05 9.999604e-01 1.318436e-05]]
color_input: [[5.8156675e-05 4.3866065e-05 9.9985409e-01 4.3866065e-05]]
height_input: [[4.5331984e-04 4.5331984e-04 9.9864000e-01 4.5331984e-04]]`
ocv output:
color_input and height_input: [1.318402e-05, 1.318402e-05, 0.99996042, 1.318402e-05]
color_input: [5.815634e-05, 4.3865861e-05, 0.99985409, 4.3865861e-05]
height_input: [0.00045329696, 0.00045329696, 0.99864012, 0.00045329696]
They are almost equal.