【问题标题】:Prediction with tensorflow-gpu is slower than tensorflow-cpu使用 tensorflow-gpu 进行预测比 tensorflow-cpu 慢
【发布时间】: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


【解决方案1】:

也许尝试start=time.time() 在创建会话(afterwith tf.Session(graph=graph) as sess:)之后输入此代码,因为我使用 gpu 创建会话需要更多时间,但可以快速做出预测。 您是否也曾尝试过使用知名模型,我的意思是您的 gpu 第一次表现不佳?
也许尝试使用 VGG Nets,您可以从 here 找到基准并与您的 gpu 进行比较。如果您的 gpu 似乎有问题,请关注它,但也许这与您的模型有关,有时模型会在 cpu 上提供更好的性能

【讨论】:

  • 结果相同,没有变化
  • @PratikKumar 你有没有试过用知名的模型我的意思是你的gpu第一次表现不好?也许尝试使用 VGG Nets,您可以从 here 找到基准并与您的 gpu 进行比较。如果您的 gpu 似乎有问题,请关注它,但也许这与您的模型有关,有时模型会在 cpu 上提供更好的性能。
  • 我正在运行 mobilenet_v1
  • 赞成在基准架构上进行测试的想法。因为,我使用的是移动网络,它比其他架构更小,这就是 CPU 可能更快的原因。
【解决方案2】:

您使用的是什么 GPU 之王。据我了解,Tensorflow 似乎针对 cuda -> Nvidia

进行了优化

【讨论】:

  • 我使用 Nvidia GeForce GTX 1080 Ti。
  • 这很奇怪......你的操作系统/驱动程序呢?另外,你的CPU是什么? (我的 threadripper 1950X 在许多高度并行化的工作中也比 GPU 更好)
  • 驱动程序是驱动程序版本:384.130。操作系统是 ubuntu-16。 CPU 是 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  • 所以您使用的是专有软件。检查您是否拥有 libcuda1-384。如果是这样,我没有更多线索
  • 你能指导我如何检查吗?
猜你喜欢
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2017-04-05
  • 2020-12-04
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
相关资源
最近更新 更多