使用tensorflow-gpu进行预测比tensorflow-cpu慢

时间:2018-05-31 13:17:53

标签: python tensorflow

以下是我的预测代码:

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

2 个答案:

答案 0 :(得分:1)

也许尝试start=time.time()在创建会话之后(在with tf.Session(graph=graph) as sess:之后)放置此代码对我来说,使用gpu创建会话需要更多时间但是快速预测。 您是否曾尝试使用众所周知的模型,我的意思是您的gpu第一次表现不佳? 也许尝试使用VGG网络,您可以找到here的基准并与您的gpu进行比较。如果你的gpu关注它似乎有问题,但也许这与你的模型有关,有时模型在cpu上提供了更好的性能

答案 1 :(得分:0)

你使用的是什么GPU王。根据我的理解,Tensorflow似乎针对cuda进行了优化 - > Nvidia的