0. Ставим Python, если еще нет.
1. pip install keras_cv
2. KerasCV хочет TensorFlow at least 2.9.0. Соответственно обновляем:
pip install --upgrade tensorflow
3. Затем нужно установить датасеты для TensorFlow:
pip install tensorflow_datasets
4. Ставим matplotlib для отображения или сохранения созданных нейросетью изображений:
pip install matplotlib
Ну и собственно запускаем скрипт:
import time
import keras_cv
from tensorflow import keras
import matplotlib.pyplot as plt
import random
import string
import os
model = keras_cv.models.StableDiffusion(img_width=512, img_height=512)
#images = model.text_to_image("photograph of an astronaut riding a horse", batch_size=3)
images = model.text_to_image("photograph of a cat and duck", batch_size=3)
def Upper_Lower_string(length): # define the function and pass the length as argument
# Print the string in Lowercase
result = ''.join((random.choice(string.ascii_lowercase) for x in range(length))) # run loop until the define length
print(" Random string generated in Lowercase: ", result)
return result
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])
# save the image
image_name = Upper_Lower_string(8)
save_path = os.path.join('j:\\temp2\\img\\', '{}.png'.format(image_name))
plt.imsave(save_path, images[i])
plt.axis("off")
plot_images(images)
Астронавта на лошади уже все видели, поэтому попросим кота и утку. Получились следующие изображения:
Качество так себе конечно. Особенно морды котов.
Документация:
High-performance image generation using Stable Diffusion in KerasCV.
В процессе работы при первом запуске скрипт выкачивает какие-то датасеты с
https://huggingface.co/.
На Core i3 3250 работает больше 50 минут.
На Ryzen 5 2400G около 30 минут.
Видеокарту от AMD TensorFlow использовать не хочет.