successful image generation on an laptop rtx5070

#257
by drmcchamburgers - opened

spent the morning battling image generation with flux and sd, finally got output with help from gemini with the following:

import os
import time
os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True"

import torch
from diffusers import StableDiffusionXLPipeline

# 1. Initialize the optimized pipeline
pipeline = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True
)

# 2. VRAM Optimizations for 8GB
pipeline.enable_model_cpu_offload()
pipeline.vae.enable_tiling()

# 3. Dynamic setup with descriptive style tagging
positive_prompt = (
    "A cinematic shot of a futuristic cyberpunk city street at night, "
    "glowing neon signs in violet and cyan, towering corporate skyscrapers, "
    "rain-slicked asphalt reflecting headlights, a lone traveler in high-tech armor "
    "standing in the foreground looking up, photorealistic, 8k resolution, ray tracing."
)

negative_prompt = (
    "blurry, low quality, distorted, extra limbs, ugly, bad anatomy, "
    "deformed, text, watermark, signature, oversaturated, worst quality"
)

# 4. Generate the image canvas
output = pipeline(
    prompt=positive_prompt,
    negative_prompt=negative_prompt,
    height=1024,
    width=1024,
    guidance_scale=7.5,        # 7.0 - 8.0 is the sweet spot for SDXL prompt adherence
    num_inference_steps=35,    # Slightly higher steps for deeper textures and crispness
)

# 5. Save the file with a unique timestamp to prevent overwriting
image = output.images[0]
filename = f"cyberpunk_{int(time.time())}.png"
image.save(filename)

print(f"Success! Image saved as {filename}")

hope that helps someone
cyberpunk_1780766943

Sign up or log in to comment