🐶
Tensorflow

Force Keras to Use CPU or GPU: A How-to Guide

By Ondřej Dolanský on 12/12/2024

Learn how to seamlessly switch between CPU and GPU utilization in Keras with TensorFlow backend for optimal deep learning performance.

Force Keras to Use CPU or GPU: A How-to Guide

Table of Contents

Introduction

This guide provides a concise walkthrough on how to enable and verify GPU acceleration for your Keras models using TensorFlow as the backend. We'll cover installation, verification, and troubleshooting steps to ensure your deep learning projects leverage the power of your GPU. Additionally, we'll explore how to force CPU usage when needed and monitor resource utilization during model training.

Step-by-Step Guide

  1. Install TensorFlow with GPU support: Ensure you have a compatible NVIDIA GPU and drivers. Then, install the appropriate TensorFlow-GPU package:

    pip install tensorflow-gpu
  2. Verify GPU detection:

    import tensorflow as tf
    print(tf.config.list_physical_devices('GPU'))

    This should list your available GPUs. If it's empty, TensorFlow isn't detecting your GPU.

  3. Keras uses TensorFlow's configuration: Keras inherits TensorFlow's backend settings. If TensorFlow is set up to use the GPU, Keras will automatically use it.

  4. Force CPU usage (if needed):

    import os
    os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
    import tensorflow as tf
    # ... your Keras code ...

    This sets the CUDA_VISIBLE_DEVICES environment variable to an invalid value, effectively hiding the GPU from TensorFlow and forcing Keras to use the CPU.

  5. Check device usage during execution:

    import tensorflow as tf
    with tf.device('/CPU:0'):
        # Code to run on CPU
    with tf.device('/GPU:0'):
        # Code to run on GPU

    Use tf.device to specify CPU or GPU for specific parts of your code.

  6. Monitor resource utilization: Use tools like nvidia-smi (for NVIDIA GPUs) or TensorFlow Profiler to monitor GPU usage during training.

Code Example

This Python code demonstrates training a simple neural network on the MNIST dataset using Keras with GPU acceleration. It verifies GPU availability, loads and preprocesses the dataset, defines a sequential model, compiles it, and trains it on the training data using the first available GPU. Finally, it evaluates the trained model on the test data and prints the loss and accuracy.

This example demonstrates how to train a simple Keras model on the MNIST dataset, ensuring GPU utilization.

import tensorflow as tf
from tensorflow import keras
import os

# Optional: Force CPU usage for demonstration
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

# Verify GPU detection
print("Available GPUs:", tf.config.list_physical_devices('GPU'))

# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Preprocess data
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)

# Define the model
model = keras.Sequential(
    [
        keras.layers.Flatten(input_shape=(28, 28)),
        keras.layers.Dense(128, activation="relu"),
        keras.layers.Dense(10, activation="softmax"),
    ]
)

# Compile the model
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

# Train the model
with tf.device('/GPU:0'): # Specify GPU for training
    model.fit(x_train, y_train, epochs=5, batch_size=32)

# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", loss)
print("Test accuracy:", accuracy)

Explanation:

  1. Import necessary libraries: Import TensorFlow, Keras, and os for environment manipulation.
  2. Verify GPU detection: Print available GPUs to confirm TensorFlow recognizes your GPU.
  3. Load and preprocess data: Load the MNIST dataset and normalize pixel values.
  4. Define, compile, and train the model: Create a simple neural network, compile it with appropriate loss function and optimizer, and train it on the training data.
  5. Specify GPU usage: Use with tf.device('/GPU:0'): to explicitly run the training process on the first available GPU.
  6. Evaluate the model: Calculate loss and accuracy on the test data.

This example demonstrates how to leverage GPU acceleration with Keras for faster training. Remember to monitor GPU utilization using tools like nvidia-smi during execution.

Additional Notes

  • Compatibility is Key: Always cross-reference your NVIDIA GPU model, driver version, CUDA version, and cuDNN version with TensorFlow's official documentation for compatibility. Mismatches can lead to installation or runtime errors.
  • Virtual Environments Recommended: Use virtual environments (like venv or conda) to manage your TensorFlow installation, especially if you work with different projects requiring different TensorFlow versions.
  • TensorFlow-GPU Installation: The tensorflow-gpu package is quite large. Expect a significant download and installation time.
  • GPU Memory Management: GPUs have limited memory. Large models or datasets might require adjusting batch sizes or using techniques like mixed precision training to fit.
  • CUDA_VISIBLE_DEVICES: This environment variable offers fine-grained control. You can specify a single GPU ID or a comma-separated list for multi-GPU setups.
  • Beyond nvidia-smi: TensorFlow Profiler provides in-depth insights into model performance, including GPU utilization, kernel execution times, and memory usage.
  • Cloud TPUs: For very large-scale training, consider Google Cloud TPUs (Tensor Processing Units), which offer even greater performance than GPUs.
  • Keras Backend: While this guide focuses on TensorFlow, Keras can also use other backends like Theano and CNTK. The process of enabling GPU support might differ slightly for those backends.
  • Experimentation is Encouraged: Don't hesitate to experiment with different batch sizes, optimizers, and GPU settings to find the optimal configuration for your specific model and hardware.

Summary

This article provides a concise guide on enabling and verifying GPU usage with Keras, which leverages TensorFlow for its backend.

Key Takeaways:

  • Installation: Install tensorflow-gpu instead of tensorflow to enable GPU support.
  • Verification: Use tf.config.list_physical_devices('GPU') to confirm TensorFlow detects your GPU.
  • Automatic GPU Usage: Keras automatically utilizes the GPU if TensorFlow is configured to do so.
  • Forcing CPU: Set the environment variable CUDA_VISIBLE_DEVICES to "-1" to force Keras to use the CPU.
  • Specific Device Control: Use tf.device('/CPU:0') or tf.device('/GPU:0') to explicitly run code on the CPU or GPU, respectively.
  • Monitoring: Utilize tools like nvidia-smi or TensorFlow Profiler to monitor GPU usage during training.

Conclusion

By following these steps, you can ensure your Keras models are effectively utilizing your GPU for faster training and execution. Remember to consult the official TensorFlow documentation for compatibility information and explore advanced tools like TensorFlow Profiler for optimizing your deep learning workflows.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait