简体   繁体   English

如何使用GPU运行Keras Model.Predict()

[英]How to use GPU to run Keras Model.Predict()

Using the Tensorflow CIFAR CNN demonstration , I verified that my TF was properly using my GPU. TF used the GPU to run model.fit(), and it saw about 50% usage in HWiNFO64.使用 Tensorflow CIFAR CNN 演示,我验证了我的 TF 正确使用了我的 GPU。TF 使用 GPU 运行 model.fit(),它在 HWiNFO64 中使用了大约 50%。 However, if I then add this cell to the notebook, which uses the model to predict the label of images in the test set:但是,如果我随后将此单元格添加到笔记本中,它使用 model 来预测测试集中的图像 label:

import numpy as np
for img in test_images:
    prediction = model.predict(np.expand_dims(img, axis=0)) # Here
    print(class_names[np.argmax(prediction)])

I see only 1% GPU usage (which is used by Chrome and other processes).我只看到 1% GPU 使用率(由 Chrome 和其他进程使用)。 Is there a way for me to run model.predict() on a GPU, or are there any alternatives where I can have a model output for a single input?有没有办法让我在 GPU 上运行 model.predict(),或者有什么替代方法可以让我使用 model output 作为单个输入?

Your code is running on the GPU, it is a misconception to think that GPU utilization can tell you if code is running in the GPU or not.您的代码在 GPU 上运行,认为 GPU 利用率可以告诉您代码是否在 GPU 上运行是一种误解。

The problem is that doing one predict call for each image is very inefficient, as almost no parallelism can be performed on the GPU, if you pass a whole array of images then it will increase GPU utilization as batches can be provided to the GPU and each image processed in parallel.问题是对每个图像进行一次predict调用效率非常低,因为几乎无法在 GPU 上执行并行性,如果您传递一整组图像,那么它将增加 GPU 的利用率,因为可以向 GPU 提供批次,并且每个图像并行处理。

GPUs only accelerate specific workloads, so your only choice is to use more images in your call to predict . GPU 只会加速特定的工作负载,因此您唯一的选择是在调用predict时使用更多图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM