【发布时间】:2018-05-31 13:17:53
【问题描述】:
下面是我的预测代码:
start=time.time()
with tf.Session(graph=graph) as sess:
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
predict('/home/4_bikes/test_images/bikerider4.jpg',sess)
stop=time.time()
print('Time taken for prediction :: {}'.format(stop-start))
下面是我的predict函数:
def predict(file_name,sess):
t = read_tensor_from_image_file(
file_name,
input_height=input_height,
input_width=input_width,
input_mean=input_mean,
input_std=input_std)
results = sess.run(output_operation.outputs[0], {
input_operation.outputs[0]: t
})
results = np.squeeze(results)
index=results.argmax()
prediction=labels[index]
bike_predictor = bike_classifier()
if prediction == 'bikes':
bike_predictor.predict(t)
else:
print('Predicted as :: unknown')
我在 python-2 上安装了 tensorflow-gpu,在 python-3 上安装了 tensorflow-cpu。当我使用 tensorflow-gpu 运行它时,我得到:
Time taken for prediction :: 2.92091107368
当我使用 tensorflow-cpu 运行时,我得到:
Time taken for prediction :: 1.7942276000976562
我确定我使用的是 GPU,因为当使用 python-2 运行时,我会得到日志:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 10.28GiB
2018-05-31 18:23:26.762628: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-05-31 18:23:26.906629: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-05-31 18:23:26.906672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0
2018-05-31 18:23:26.906679: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N
2018-05-31 18:23:26.906856: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 9949 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
每次调用predict。
有人可以帮我解决这个问题吗?我哪里错了?使用 GPU 时所花费的时间应该更少。
安装我关注this链接。
我正在使用 Nvidia GeForce GTX 1080 Ti。
CPU 是 Intel(R) Core(TM) i7-7700K CPU
模型是 MobileNet_v1
【问题讨论】:
-
GPU 有一个 CPU 部分用于提供图像。这很可能是瓶颈。 GPU 在等待图像的大部分时间都处于理想状态。
-
@vijaym 无论如何要克服这个问题?
-
是的,使用数据集 API 构建
input data pipeline,您可以在其中并行化 CPU 上的数据加载部分和 GPU 上的数字运算。
标签: python tensorflow