[PYTHON] [Windows] Memo to use Keras on GPU [Tensorflow-GPU]

Constitution

OS: Windows10 Home CPU: Intel Core i7-4790 GPU: GTX1660 Super (any NVIDIA GPU) Python: 3.6.10 Keras: 2.2.4 Tensorflow: 1.14.0 Cuda: 10.0 numpy: 1.16.4 sklearn: 0.22.2

1. Install Windows Visual Studio C ++

Installation page https://docs.microsoft.com/ja-jp/visualstudio/install/install-visual-studio?view=vs-2019 From here, install Visual Studio. This time, install the 2019 version. Be sure to select "Desktop development with C ++ workloads" during installation.

2. Install NVIDIA Driver

https://www.nvidia.co.jp/Download/index.aspx?lang=jp Select your GPU from this URL and download the NVIDIA driver. image.png Run the downloaded exe file to install it.

Postscript. Download and install CUDA toolkit

https://developer.nvidia.com/cuda-10.0-download-archive

3. Download cuDNN

https://developer.nvidia.com/rdp/cudnn-download Download "cuDNN v7.6.5 for CUDA 10.0" from this site image.png Then open "C: \ Program Files \ NVIDIA GPU Computing Toolkit \ CUDA \ v.10.0" in the folder. There are three folders, bin, include, and lib, in the unzipped folder. Save the files in bin in the bin folder, the files in include in the include folder, and the files in lib in the lib folder.

4. Install tensorflow-gpu

First, if you have regular tensorflow installed, Must be uninstalled and then installed.

pip uninstall tensorflow
pip install numpy==1.16.4
pip install tensorflow-gpu==1.14.0
pip install keras==2.2.4
pip install sklearn

Install the ones that are likely to be used for graph drawing and data processing. Below you don't have to install it.

pip install matplotlib
pip install pandas
pip install pillow
pip install opencv-python
pip install opencv-contrib-python

5. Check if the GPU version of Tensorflow recognizes it

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

I think a long sentence will be output, It is OK if there are characters such as device_type:" GPU "`` `and` `name:" / device: GPU: 0 " in one line.

6. After all, the test is MNIST.

MNIST is the test. Speed verification with MNIST.

import time
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop

(x_train, y_train), (x_test, y_test) = mnist.load_data()

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

start_time = time.time()#Start time measurement

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

history = model.fit(x_train, y_train,
                    batch_size=128,
                    epochs=10,
                    verbose=1,
                    validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)
print('loss:', score[0])
print('accuracy:', score[1])

end_time = time.time() - start_time#Measure the end time
print("Study time:", str(round(end_time, 3)), "It was seconds.")

Output result on GPU version: 48.13 seconds Output result on CPU version: 530.26 seconds

The speed was different by more than 10 times! !!

At the end

I personally wanted to do deep learning, so I rushed into an old PC and set up an AI machine. Also, if I have enough money, I would like to use a GPU with more memory or two stabs. see you.

Recommended Posts

[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
How to use Dataiku on Windows
How to use Google Assistant on Windows 10
Use Ansible on Windows
Use QuTiP on Windows
Use pip on Windows
A memorandum on how to use keras.preprocessing.image in Keras
A memo of installing Chainer 1.5 for GPU on Windows
How to operate Firefox with selenium on Windows Memo
Install Chainer 1.6 (GPU) on Windows 7.
Use Python on Windows (PyCharm)
Use Linux on Windows 10 (WSL2)
How to use VS Code in venv environment on windows
Use Xming to launch an Ubuntu GUI application on Windows.
How to use cron (personal memo)
Python environment construction memo on Windows 10
Python 3.6 on Windows ... and to Xamarin.
Notes on how to use pywinauto
Use jupyter on AWS GPU instance
How to deploy django-compressor on Windows
Notes on how to use featuretools
How to use homebrew on Debian
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
How to install music 21 on windows
Run Radeon GPU on Windows on QEMU / KVM
[Memo] How to use BeautifulSoup4 (1) Display html
[Kivy] How to install Kivy on Windows [Python]
If you want to use NumPy, Pandas, Matplotlib, IPython, SciPy on Windows
How to use Cmder with PyCharm (Windows)
I want to use Linux on mac
Memorandum on how to use gremlin python
Use without installing python 2.x on Windows
How to install richzhang / colorization on Windows 10
June 2017 version to build Tensorflow / Keras environment on GPU instance of AWS
Install python on xserver to use pip
How to run Cython on OSX Memo
I want to use Python in the environment of pyenv + pipenv on Windows 10
Put MicroPython on Windows to run ESP32 on Python
How to use Python Kivy ④ ~ Execution on Android ~
Use boto3-stubs to enable code completion on boto3
[Python] Memo to translate Matplotlib into Japanese [Windows]
How to quickly install h5py on Windows 10 [Unofficial]
How to live a decent life on 2017 Windows
I want to do pyenv + pipenv on Windows
[Python] How to install OpenCV on Anaconda [Windows]
How to install / verify graphviz on anaconda / windows10
Steps to build PyTorch 1.5 for CUDA 10.2 on Windows
tensorflow-gpu introduction memo
Python on Windows
[Keras] Personal memo to classify images by folder [Python]
Allow Keras 2.0 and OpenCV 3.2 to work in GPU environment
Preparing to use aws cli on Mac OS X
A memo of how to use AIST supercomputer ABCI
How to embed mod_wsgi into Apache on Python Windows
How to use Keras ~ From simple model generation to CNN ~
Install Debian 10 to use Remote Development on BeagleBone Black
[Environment construction] @anaconda that runs keras / tensorflow on GPU
How to use C216 Audio Controller on Arch Linux
Memo of how to use properly when combining pandas.DataFrame
How to use TensorFlow on GPUs less than Titan
Building a TensorFlow environment that uses GPU on Windows 10