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

Ronalds_Mazitis: <p>I am using Ubuntu 22.04.4 LTS with 6.5.0-35-generic kernel.<br>
I have nvidia-driver-555 with cuda-toolkit NVIDIA Container Runtime Hook version 1.17.1<br>
installed. (Driver Version: 555.58.02 CUDA Version: 12.5)</p>
<p>this is the python code I’m running on conda environment</p>
<pre><code class="lang-auto">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")
</code></pre>
<p>I’m always getting black image with this error as output</p>
<pre><code class="lang-auto">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) &gt; 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.")
</code></pre>
<p>and I’m getting following error</p>
<p>/home/user/anaconda3/envs/myenv/lib/python3.9/site-packages/diffusers/image_processor.py:147: RuntimeWarning: invalid value encountered in cast<br>
images = (images * 255).round().astype(“uint8”)</p>

soulitzer: <p>It looks like a warning from numpy during the cast, you aren’t using PyTorch here.</p>

Ronalds_Mazitis: <p>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.</p>

soulitzer: <p>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.</p>
<p>It’s a numpy → PIL conversion in the diffusers library:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/huggingface/diffusers/blob/main/src/diffusers/image_processor.py#L133">
  <header class="source">

      <a href="https://github.com/huggingface/diffusers/blob/main/src/diffusers/image_processor.py#L133" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/huggingface/diffusers/blob/main/src/diffusers/image_processor.py#L133" target="_blank" rel="noopener nofollow ugc">huggingface/diffusers/blob/main/src/diffusers/image_processor.py#L133</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="123" style="counter-reset: li-counter 122 ;">
          <li>):</li>
          <li>    super().__init__()</li>
          <li>    if do_convert_rgb and do_convert_grayscale:</li>
          <li>        raise ValueError(</li>
          <li>            "`do_convert_rgb` and `do_convert_grayscale` can not both be set to `True`,"</li>
          <li>            " if you intended to convert the image into RGB format, please set `do_convert_grayscale = False`.",</li>
          <li>            " if you intended to convert the image into grayscale format, please set `do_convert_rgb = False`",</li>
          <li>        )</li>
          <li></li>
          <li>@staticmethod</li>
          <li class="selected">def numpy_to_pil(images: np.ndarray) -&gt; List[PIL.Image.Image]:</li>
          <li>    r"""</li>
          <li>    Convert a numpy image or a batch of images to a PIL image.</li>
          <li></li>
          <li>    Args:</li>
          <li>        images (`np.ndarray`):</li>
          <li>            The image array to convert to PIL format.</li>
          <li></li>
          <li>    Returns:</li>
          <li>        `List[PIL.Image.Image]`:</li>
          <li>            A list of PIL images.</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


Ronalds_Mazitis: <p>Well pytorch itself does not work, there is no cast in first code. Where is code of pytorch that works?</p>

ptrblck: <p><a class="mention" href="/u/soulitzer">@soulitzer</a> liked the right code from your error message, which is definitely executed as it fails.<br>
PyTorch is a machine learning <strong>framework</strong> and it seems you are looking for applications without the interest to understand the underlying framework and technology.</p>

Ronalds_Mazitis: <p>It supposed to work before I look “under the hood”.</p>

Ronalds_Mazitis: <p>If we go further, why not start soldering graphics cards ourself to have “wider perspective”.</p>

ptrblck: <p>I don’t think this discussion is going anywhere.<br>
Again, <a class="mention" href="/u/soulitzer">@soulitzer</a> pointed out the failing <code>numpy</code> call and provided a link to the code reference.<br>
Instead of trying to understand your code and checking why this code fails, you are deflecting from the issue.<br>
We cannot fix things for you if you are unwilling to understand your code that you are running.</p>

Ronalds_Mazitis: <p>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?</p>

Ronalds_Mazitis: <p>I DID NOT ASK THE EXPLANATION FOR THE CODE. I NEED TUTORIALS ANSWERS AND STUFF. YOU ARE BEING RUDE buddy.</p>

ptrblck: <p>I don’t think I’m rude, but patient with you.</p>
<aside class="quote no-group" data-username="Ronalds_Mazitis" data-post="10" data-topic="214280">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f08c70/48.png" class="avatar"> Ronalds_Mazitis:</div>
<blockquote>
<p>I DON’T CARE</p>
</blockquote>
</aside>
<p>Is a good summary of your position, so good luck.</p>
<p>In case you have PyTorch related questions, please let us know.</p>

Ronalds_Mazitis: <p>I DID ASK PROPER QUESTION, AND YOU STARTED SOME NONESENSE ABOUT CODE I POSTED. NOBODY ASKED THAT are You a troll?</p>

Ronalds_Mazitis: <p>You clearly have problems with reading what I asked then.</p>

Ronalds_Mazitis: <p>where is proper pytorch using code with my graphics card???</p>