Running PyTorch to use artificial intelligence to generate images with Nvidia GTX 1650Ti

I am using Ubuntu 22.04.4 LTS with 6.5.0-35-generic kernel.
I have nvidia-driver-555 with cuda-toolkit NVIDIA Container Runtime Hook version 1.17.1
installed. (Driver Version: 555.58.02 CUDA Version: 12.5)

this is the python code I’m running on conda environment

from diffusers import StableDiffusionPipeline
import torch

# Load the pre-trained Stable Diffusion model from Hugging Face
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)

# Move the model to GPU if available
pipe = pipe.to("cuda")

# Define your text prompt
prompt = "A scenic landscape with high mountains, a flowing river in the valley, and a colorful sunset sky with warm shades of orange and purple."

# Generate the image from the text prompt
image = pipe(prompt).images[0]

# Display the generated image
image.show()

# Optionally, save the image
image.save("generated_landscape.png")

I’m always getting black image with this error as output

from diffusers import StableDiffusionPipeline
import torch
import numpy as np
from PIL import Image

# Load the pre-trained Stable Diffusion model from Hugging Face
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)

# Disable the safety checker (optional)
pipe.safety_checker = None

# Move the model to CPU (instead of GPU)
pipe = pipe.to("cpu")

# Define a very simple prompt to test the image generation
prompt = "a blue square"  # Simple prompt to check if the image generation works

# Generate the image from the text prompt
output = pipe(prompt)

# Check the raw output (before image conversion)
print(f"Raw output: {output}")

# Check if any images were returned
if hasattr(output, 'images') and len(output.images) > 0:
    image = output.images[0]
    image_array = np.array(image)

    # Check raw image values
    print(f"Raw image array shape: {image_array.shape}")
    print(f"Min value in raw image: {np.min(image_array)}, Max value in raw image: {np.max(image_array)}")

    # If image is completely black
    if np.all(image_array == 0):
        print("The generated image is completely black.")

    # Normalize and convert to uint8
    image_array = np.clip(image_array * 255, 0, 255)
    image_array = image_array.astype(np.uint8)

    # Convert back to a PIL image
    image = Image.fromarray(image_array)

    # Display and save the image
    image.show()
    image.save("generated_blue_square.png")
else:
    print("Error: No images returned by the model.")

and I’m getting following error

/home/user/anaconda3/envs/myenv/lib/python3.9/site-packages/diffusers/image_processor.py:147: RuntimeWarning: invalid value encountered in cast
images = (images * 255).round().astype(“uint8”)

It looks like a warning from numpy during the cast, you aren’t using PyTorch here.

I am definetely using pytorch here based on code chatgpt gave, You could answer me with code that should be used then, instead of pointing out library I have no idea over what it is.

Sorry what I mean is that the part of the code that is erroring isn’t related to pytorch, so this forum isn’t the best place to ask this question.

It’s a numpy → PIL conversion in the diffusers library:

Well pytorch itself does not work, there is no cast in first code. Where is code of pytorch that works?

@soulitzer liked the right code from your error message, which is definitely executed as it fails.
PyTorch is a machine learning framework and it seems you are looking for applications without the interest to understand the underlying framework and technology.

It supposed to work before I look “under the hood”.

If we go further, why not start soldering graphics cards ourself to have “wider perspective”.

I don’t think this discussion is going anywhere.
Again, @soulitzer pointed out the failing numpy call and provided a link to the code reference.
Instead of trying to understand your code and checking why this code fails, you are deflecting from the issue.
We cannot fix things for you if you are unwilling to understand your code that you are running.

I DON’T CARE what he is pointing out, I asked where is proper pytorch using code with my graphics card. What is so hard to grasp?

I DID NOT ASK THE EXPLANATION FOR THE CODE. I NEED TUTORIALS ANSWERS AND STUFF. YOU ARE BEING RUDE buddy.

I don’t think I’m rude, but patient with you.

Is a good summary of your position, so good luck.

In case you have PyTorch related questions, please let us know.

I DID ASK PROPER QUESTION, AND YOU STARTED SOME NONESENSE ABOUT CODE I POSTED. NOBODY ASKED THAT are You a troll?

You clearly have problems with reading what I asked then.

where is proper pytorch using code with my graphics card???