我正在将Python3与2个Nvidia 1070 Ti图形以及ubuntu 16.04上的Tensorflow 1.13一起使用,以进行实时对象检测,但是它仅使用单个GPU。因此,我尝试遍历GPU,但看起来它一次只使用一个GPU,而不是同时使用两个GPU来预测对象
##########################################################
#use multi GPU
##########################################################
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.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='')
for i, id in enumerate(['/device:GPU:0', '/device:GPU:1']):
with tf.device(id):
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.7
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while True:
ret, image_np = cap.read()
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
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')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
看起来像我使用时 对于我,id枚举(['/ device:GPU:0','/ device:GPU:1']): 使用tf.device(id):它只运行两次我的代码,而没有同时使用两个GPU
有解决方案吗?我对这种multigpu东西还是陌生的