Search code examples
pythonmachine-learningkerasplaidml

Cannot Run Keras Model On GPU With Plaidml


I Want to Run this Keras Model on My GPU but it runs on my cpu I used Plaidml to use my AMD GPU, plaidml is properly set and it runs perfectly on other models I think Maybe Becaouse I'm importing tensoflow but I'm Not sure about that, I need the model to run on the GPU, I have other Models that doesn't import tensorflow that works perfectly with Plaidml

Source Code

import numpy as np

from os import environ
environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
from keras.layers import Dense


import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers


master_url_root = "https://raw.githubusercontent.com/numenta/NAB/master/data/"

df_small_noise_url_suffix = "artificialNoAnomaly/art_daily_small_noise.csv"
df_small_noise_url = master_url_root + df_small_noise_url_suffix
df_small_noise = pd.read_csv(
    df_small_noise_url, parse_dates=True, index_col="timestamp"
)

df_daily_jumpsup_url_suffix = "artificialWithAnomaly/art_daily_jumpsup.csv"
df_daily_jumpsup_url = master_url_root + df_daily_jumpsup_url_suffix
df_daily_jumpsup = pd.read_csv(
    df_daily_jumpsup_url, parse_dates=True, index_col="timestamp"
)



fig, ax = plt.subplots()
df_small_noise.plot(legend=False, ax=ax)
plt.show()



training_mean = df_small_noise.mean()
training_std = df_small_noise.std()
df_training_value = (df_small_noise - training_mean) / training_std
print("Number of training samples:", len(df_training_value))




TIME_STEPS = 288

# Generated training sequences for use in the model.
def create_sequences(values, time_steps=TIME_STEPS):
    output = []
    for i in range(len(values) - time_steps + 1):
        output.append(values[i : (i + time_steps)])
    return np.stack(output)


x_train = create_sequences(df_training_value.values)
print("Training input shape: ", x_train.shape)



model = keras.Sequential(
    [
        layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
        layers.Conv1D(
            filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
        ),
        layers.Dropout(rate=0.2),
        layers.Conv1D(
            filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
        ),
        layers.Conv1DTranspose(
            filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
        ),
        layers.Dropout(rate=0.2),
        layers.Conv1DTranspose(
            filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
        ),
        layers.Conv1DTranspose(filters=1, kernel_size=7, padding="same"),
    ]
)
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
model.summary()


history = model.fit(
    x_train,
    x_train,
    epochs=50,
    batch_size=128,
    validation_split=0.1,
    callbacks=[
        keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
    ],
)

plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
plt.show()

This is an Image of My Task Manager (Plaidml Is set to run on GPU 0 [it works on other source codes])

GPU is Not Used & model runs on cpu


Solution

  • Instead of using tensorflow keras, try importing keras from keras.

     import keras
     from keras import layers
    

    You should still have access to all of the same layers, models, etc. You may need to install keras without tensorflow using

     pip install keras
    

    Also look at your GPU memory usage. It should jump up, as data is stored on it. In plaidml, most computations are not actually run on GPU, but the weights and data are stored on it.

    Also, maybe use the following block of code instead of your current first lines.

     import numpy as np
     import plaidml.keras
     import os
     plaidml.keras.install_backend()