Skip to content

Instantly share code, notes, and snippets.

@catichenor
Last active October 1, 2022 19:54
Show Gist options
  • Save catichenor/5a90f83ebd37bc30042cfdf1103e73db to your computer and use it in GitHub Desktop.
Save catichenor/5a90f83ebd37bc30042cfdf1103e73db to your computer and use it in GitHub Desktop.
Stable Diffusion Keras without Conda on Windows
# This was the only way I could get TensorFlow to recognize the CUDA DLLs without installing Conda.
# Adapted from https://keras.io/api/keras_cv/models/stable_diffusion/
# Set up paths as noted in https://www.youtube.com/watch?v=-Q6SM_usn84 (although ctypes is basically overriding this since the paths didn't seem get properly detected)
# ctypes workaround taken from https://stackoverflow.com/questions/57528027/importerror-could-not-find-cudart64-100-dll
# TensorFlow memory workaround taken from https://stackoverflow.com/questions/65493824/tensorflow-gpu-memory-allocation
# Needed to download zlib for Windows as posted on this page: http://www.winimage.com/zLibDll/zlib123dllx64.zip
import ctypes
cudart64dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cudart64_110.dll")
cublas6411dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cublas64_11.dll")
cublasLt6411dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cublasLt64_11.dll")
cufft6410dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cufft64_10.dll")
curand6410dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\curand64_10.dll")
cusolver6411dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cusolver64_11.dll")
cusparse6411dll = ctypes.WinDLL(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin\cusparse64_11.dll")
zlibwapidll = ctypes.WinDLL(r"C:\tools\cuda\bin\zlibwapi.dll") # Or point this to wherever you put zlibwapi.dll
cudnnadvinfer648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_adv_infer64_8.dll")
cudnnadvtrain648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_adv_train64_8.dll")
cudnnopsinfer648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_ops_infer64_8.dll")
cudnncnntrain648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_cnn_train64_8.dll")
cudnncnninfer648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_cnn_infer64_8.dll")
cudnnopstrain648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn_ops_train64_8.dll")
cudnn648dll = ctypes.WinDLL(r"C:\tools\cuda\bin\cudnn64_8.dll")
import time
import keras_cv
from tensorflow import keras
import matplotlib.pyplot as plt
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True) # Workaround for an issue with TensorFlow allocating more GPU memory than is available.
model = keras_cv.models.StableDiffusion(img_width=512, img_height=512)
images = model.text_to_image("photograph of an astronaut riding a unicorn", batch_size=3)
def plot_images(images):
plt.figure(figsize=(20, 20))
for i in range(len(images)):
ax = plt.subplot(1, len(images), i + 1)
plt.imshow(images[i])
plt.axis("off")
plot_images(images)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment