High CPU usage even when running on GPU
System information
- What is the top-level directory of the model you are using: ssd_inception_v2_coco_11_06_2017
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): no
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): ('v1.2.0-5-g435cdfc', '1.2.1')
- CUDA/cuDNN version: 5.1
- GPU model and memory: GTX1070
- Exact command to reproduce:
You can collect some of this information using our environment capture script:
https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh
You can obtain the TensorFlow version with
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
Describe the problem
When running continuous image detection, the Tensorflow object detection models consume excessively high CPU usage even with GPU support enabled. On i5 with GTX1070 the inferences are running on GPU but TensorFlow also consumes 300% CPU.
One can verify that OpenCV is not consuming high CPU by commenting the sess.run() line and the box-printing for loop below it.
Source code / logs
#!/usr/bin/env python3
import numpy as np
import tensorflow as tf
import os
import sys
import time
import cv2
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
from matplotlib import pyplot as plt
MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
DEVICE = 0
WIDTH = 640
HEIGHT = 480
GPU_MEMORY = 0.1
# gpu settings
gpu_options = tf.GPUOptions(
per_process_gpu_memory_fraction = GPU_MEMORY,
allow_growth = True
)
config = tf.ConfigProto(
device_count = { 'GPU': 1 },
gpu_options = gpu_options
)
# open camera
cap = cv2.VideoCapture(DEVICE)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
# load graph
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# load labels
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
print(category_index)
i = 0
with detection_graph.as_default():
with tf.Session(config=config, graph=detection_graph) as sess:
while True:
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
ret, image_np = cap.read()
image_np_expanded = np.expand_dims(image_np, axis=0)
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
for box, score, _class in zip(np.squeeze(boxes), np.squeeze(scores), np.squeeze(classes)):
if score > 0.5:
label = category_index[_class]['name']
print(label, score, box)
print(i)
i += 1
Here's a sample timeline from one inference. There are an awful lot of NMS calculations on the CPU.

Hi @dheera, yes, our NMS calculations are all on CPU rather than on GPU, which can be a bit slow and the models that you are using have a score_threshold set to zero, meaning that we never prune any predictions prior to running non max suppression.
In the next few weeks we will release new frozen models that should be faster.
Awesome, thanks.
- Is there a workaround manually force a different score_threshold into the graph without going through the training process to build a new graph?
- Are there plans for a GPU implementation of NMS?
Thanks!
@dheera See this comment (and follow on comments) on how to update the NMS threshold on existing models: https://github.com/tensorflow/models/issues/1609#issuecomment-309502384
@parvizp Thanks for the comment! I tried re-exporting with score_threshold = 0.5 on SSD Inception v2 and the CPU usage is still high, at 170%. It's better than before, but still absurdly high for a system with GPU. A lot of other object detection networks (e.g. YOLO) have very low CPU usage. Any tips on this would be appreciated.
@jch1 Any thoughts an whether NMS can be implemented on the GPU in the near future?
We don't have concrete plans to implement NMS for GPU. At the very least, the NMS op we use would need GPU support in Tensorflow first.
@derekjchow I think the problem is more complicated, I did a run of ssd_mobilenet_v1 with a score threshold of 0.2 which made the NonMaximumSuppresion op calls to be very fast but the whole post processing is still taking 20% of the runtime - I guess implementing something like BatchMultiClassNonMaxSuppression as part of a graph instead of a bulitin op really makes it slow...
Hey guys, could someone of you give a short step by step guide/description/tutorial how to increase speed of NMS (re-exporting etc) started from the standard configuration that comes with cloning the repo :)
Is there any plan to build gpu-nms or batch-nms?
Hi There, We are checking to see if you still need help on this, as this seems to be considerably old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.