我认为在tensorflow研究文件夹中运行“单个图像推断”功能时,每次检测到(使用此代码)时,tensorflow都会创建一个会话。如何保持会话打开,并以此推断另一个图像?
我只尝试了这种“单一推断”功能,并将其循环到图像目录中。它的速度相当慢,比使用相同模型的“ cv2 =图像捕获”视频流检测要慢得多。
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
该代码将成功循环显示图像列表,但是速度很慢。我怀疑这是因为tensorflow一遍又一遍地重新加载相同的模型,因为它在每个循环中将新会话实例化到GPU中。那么如何保持会话在循环之间打开?
答案 0 :(得分:0)
所以我转移了:
with graph.as_default():
with tf.Session() as sess:
要实例化循环之外的内容,但老实说,性能并没有好得多,在gtx1080上运行,使用ssd_mobilenet_v1_coco_2017_11_17模型来处理352x288图像仍需要花费一秒钟的时间。
>有人对并行加载多张图像以进行批处理图像有任何建议吗?
奇怪的是,它使用了超过7.8GB的GPU空间来处理每个图像,我认为这可能太高了,它分配了所有可用空间,但并未实际使用它。
NVIDIA-SMI输出:
Thu Jan 31 11:33:53 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.79 Driver Version: 410.79 CUDA Version: 10.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 1080 Off | 00000000:01:00.0 On | N/A |
| 14% 54C P2 42W / 180W | 7879MiB / 8111MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
如图所示,该会话基本上使用了7.5GB(以PID显示),但是仅在42W功率下工作-因此,这似乎并不表示已满负荷。
有想法吗?
所有图像都是静态形状,所以也许我可以增加张量大小来分配多个图像吗?