How to get tensorflow keras to use my GPU? - python

I am trying to use keras in tensorflow to train a CNN network for some image classification. Obviously, the training running on my CPU is incredibly slow and so I need to use my GPU to do the training. I've found many similar questions on StackOverflow, none of which have helped me get the GPU to work, hence I am asking this question separately.
I've got an NVIDIA GeForce GTX 1060 3GB and the 466.47 NVIDIA driver installed. I've installed the CUDA toolkit from the NVIDIA website (installation is confirmed with nvcc -V command outputting my version 11.3), and downloaded the CUDNN library. I unzipped the CUDNN file and copied the files to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3, as stated on the NVIDIA website. Finally, I've checked that it's on PATH (C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\bin and C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\libnvvp are both in the environment variable 'Path').
I then set up an environment using conda, downloading some packages that I need, like scikit-learn, as well as tensorflow-gpu=2.3 After booting my environment into Jupyter Notebook, I run this code to check to see if it's picking up the GPU:
import tensorflow as tf
print(tf.__version__)
print(tf.config.list_physical_devices())
And get this:
2.3.0
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
I have tried literally everything I have come into contact with on this topic, but am not getting any success in getting it to work. Any help would be appreciated.

You, first, have to install all CUDA requirements. If you have Ubuntu 20.04, here is how you can install the requirements. Then it's the right time to install tensorflow. Asa you intended to utilize your GPU, you have install tensorflow-gpu library, not tensorflow alone.

I'm guessing you have installed TensorFlow correctly using pip install tensorflow.
NVIDIA GPU cards with CUDA architectures 3.5, 5.0, 6.0, 7.0, 7.5, 8.0 and higher than 8.0 are currently supported by TensorFlow. If you have the supported cards but TensorFlow cannot detect your GPU, you have to install the following software:
NVIDIA GPU drivers —CUDA 11.0 requires 450.x or higher.
CUDA Toolkit —TensorFlow supports CUDA 11 (TensorFlow >= 2.4.0)
cuDNN SDK 8.0.4
You can optionally install TensorRT 6.0 to improve latency and throughput for inference on some models.
For more info, please refer to the TensorFlow documentation: https://www.tensorflow.org/install/gpu

I recommend to use conda to install the CUDA Toolkit packages as well as CUDNN, which will avoid wasting time downloading the right packages (or making changes in the system folders)
conda install -c conda-forge cudatoolkit=11.0 cudnn=8.1
Then you can install keras and tensorflow-gpu by typing
conda install keras==2.7
pip install tensorflow-gpu==2.7
and it will work directly.
Based on this issue

Related

Tensorflow Nvidia GPU not detected

Hi I'm struggling to get Tensorflow V2.11 to find my eGPU (RTX 3060 Ti)
I am currently on Windows 11
CUDA version is 12
I am currently downloading CUDA 11 as well as CUDnn as I've heard it is recommended
I have tried the following code:
import tensorflow as tf
tf.config.list_physical_devices('GPU')
which outputs:
[]
any help would be great
Tensorflow 2.11 is not supporting GPU on Windows machine. TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. So you can try by installing Tensorflow 2.10 for the GPU setup.
Also you need to install the specific version of CUDA and cuDNN for GPU support in your system which is CUDA 11.2 and cuDNN 8.1 for Tensorflow 2.10(Tensorflow>=2.5).
Please check the Hardware/Software requirements as mentioned in the link and set the path to the bin directory after installing these software.
Now follow the step by step instructions mentioned in the same link and verify the GPU setup using below code.
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

How to use GPU in pytorch?

I tried following steps at: https://pytorch.org/get-started/locally/
First I created a conda environment as:
conda create -n facenet37_2 python=3.7
Then on above site I selected:
PyTorch Build: Stable (1.4)
OS: Linux (I am using Ubuntu 18.04)
Package: conda
Language: python
CUDA: 10.1
and it asked me to run following command:
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
But after that when I opened python and typed:
import torch
torch.cuda.is_available()
I get False
I have GeForce GT 630M (computeCapability: 2.1). But it is not getting detected. Why? Is it too old and no longer supported? How can I fix the issue?
Edit: Why did I get a negative vote?
The GeForce GT 630M has compute capability 2.1 and therefore only supports up to CUDA 8.
PyTorch binaries dropped support for compute capability <= 5.0 in PyTorch 0.3.1. It's not clear to me if compute capability 2.1 was ever included in the binaries.
The PyTorch codebase dropped CUDA 8 support in PyTorch 1.1.0.
Due to the second point there's no way short of changing the PyTorch codebase to make your GPU work with the latest version. Your options are:
Install PyTorch without GPU support.
Try compiling PyTorch < 1.1.0 from source (instructions). Make sure to checkout the v1.0.1 tag. This will produce a binary with support for your compute capability.
If acceptable you could try installing a really old version: PyTorch < 0.3.1 using conda or a wheel and see if that works. It may have compute capability 2.1 support though I can't verify this. See pytorch.org for information. Though it looks like the link to https://download.pytorch.org/whl/cu80/torch_stable.html is broken.
Your GPU architecture is Fermi. Last CUDA version with support for Fermi is 8.0 and you have installed 10.1
The possible solution might be to install older CUDA and PyTorch version.
conda install pytorch==1.0.0 torchvision==0.2.1 cuda80 -c pytorch

Tensorflow-gpu v1.14 is ignoring my GPU (Nvidia GTX 860M) with compute capability 3.0/5.0(**).

This may be a repeated question but I could not find a solution to it yet. Currently, I am running TF-gpu 1.6.0 with CUDA 9.0 and cuDNN 7.0.5 which is working fine and resulting in TRUE if I type
tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None).
But I need to install TF-gpu v1.14 to work on something that requires TF-gpu v1.14. But when I install TF-gpu v1.14 with compatible CUDA and cuDNN and type the above command again, it results in FALSE and these commands
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
only shows my CPU not the GPU. As I wrote in the title that Tensorflow-gpu v1.14 is ignoring my GPU (Nvidia GTX 860M) with compute capability 3.0/5.0 (**) and it requires minimum compute capability of 3.5.
Following is the link that I used to find the compute capability of my GPU.
https://developer.nvidia.com/cuda-gpus
I am running
Windows 10
Pycharm-2019.2
Can you please guide me on how can I solve this problem? I would be highly thankful.

TensorFlow GPU doesn't work, How to install it?

I started learning about the tensorflow recently and decided to switch to the GPU version, because it is much faster, but I can not import it, it always gives the same error.
I already tried:
Installing it by pip, in python 3.6.8, cuda 10 and the most recent cuDNN for cuda 10
I tried reinstalling python, CUDA and cuDNN
Tried installing Visual Studio and installed CUDA 9 and cuDnn
I tried installing the latest Anaconda, created a "default" env and another in python 3.6 (also tried in 3.5), pip install tensorflow-gpu in both cases
my last attempt was to follow a tutorial on youtube, I did exactly as demonstrated (https://www.youtube.com/watch?v=KZFn0dvPZUQ)
Everything i tried returned the same error.
Traceback: https://pastebin.com/KMEsZAmq
The complete code: https://pastebin.com/7tS0Rd5S (was working on CPU version)
.
My Specs:
i5-8400
8 GB Ram
GTX 1060 6GB
W10 home x64
just have a look here:
https://www.tensorflow.org/install/gpu
Tensorflow supports CUDA 9.0, you will need to downgrade your CUDA or use one of the tensorflow's docker images:
https://www.tensorflow.org/install/docker
via docker it won't use your CUDA drivers

Tensorflow GPU cuDNn: How do I load cuDDN libraries?

I am trying to use tensorflow with gpu and installed CUDA 8.0 toolkit and cuDNn v5.1 libraries as described in nvidia website. But when I try to import tensorflow as module in python3.5, it does not load cuDNn libraries (outputs nothing, just loads tensorflow module). And I do not observe speed in processing (same speed I obtained when I use CPU) with GPU.
Fresh install is the key but there are some important points:
1. Install CUDA 8.0 toolkit
2. Install cuDNn 5.1 version (not 6.0)
3. Install from source(bazel) and configure to use ensorflow with CUDA
Above steps worked for me! Hope it helps anyone.

Categories