### Basic Text-to-Image Generation with RealVisXL V4.0 Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Generate photorealistic images using text prompts with recommended parameters for optimal quality. This example uses a fixed seed for reproducible results. ```python from diffusers import StableDiffusionXLPipeline import torch pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") # Recommended generation parameters image = pipe( prompt="cinematic photo of a businessman in a modern office, soft lighting, 8k uhd, high quality", negative_prompt="(face asymmetry, eyes asymmetry, deformed eyes, open mouth)", num_inference_steps=25, # Recommended: 25+ guidance_scale=7.5, width=1024, height=1024, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("businessman_portrait.png") ``` -------------------------------- ### Load RealVisXL V4.0 Pipeline Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Load the complete RealVisXL V4.0 pipeline for text-to-image generation. Ensure you have PyTorch and Diffusers installed. The model is moved to CUDA for GPU acceleration. ```python from diffusers import StableDiffusionXLPipeline import torch # Load the full pipeline from Hugging Face pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) pipe = pipe.to("cuda") # Generate an image prompt = "professional portrait photo of a woman, natural lighting, sharp focus, highly detailed" negative_prompt = "(face asymmetry, eyes asymmetry, deformed eyes, open mouth)" image = pipe( prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=25, guidance_scale=7.5, width=1024, height=1024 ).images[0] image.save("output.png") ``` -------------------------------- ### Loading Individual Model Components Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Load and access individual model components (VAE, UNet, text encoders, tokenizers) for custom pipelines or inspection. This example shows loading VAE, UNet, dual text encoders, and tokenizers. ```python from diffusers import AutoencoderKL, UNet2DConditionModel from transformers import CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer import torch # Load VAE separately (1024x1024 sample size, 4 latent channels) vae = AutoencoderKL.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="vae", torch_dtype=torch.float16 ).to("cuda") # Load UNet (2048 cross attention dim, 128 sample size) unet = UNet2DConditionModel.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="unet", torch_dtype=torch.float16 ).to("cuda") # Load dual text encoders text_encoder = CLIPTextModel.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="text_encoder", torch_dtype=torch.float16 ).to("cuda") text_encoder_2 = CLIPTextModelWithProjection.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="text_encoder_2", torch_dtype=torch.float16 ).to("cuda") # Load tokenizers (max 77 tokens) tokenizer = CLIPTokenizer.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="tokenizer" ) tokenizer_2 = CLIPTokenizer.from_pretrained( "SG161222/RealVisXL_V4.0", subfolder="tokenizer_2" ) print(f"VAE latent channels: {vae.config.latent_channels}") # Output: 4 print(f"UNet cross attention dim: {unet.config.cross_attention_dim}") # Output: 2048 print(f"Max token length: {tokenizer.model_max_length}") # Output: 77 ``` -------------------------------- ### Batch Image Generation Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Generate multiple images efficiently in a single batch for variations or comparison purposes. This example demonstrates generating 4 images with different seeds. ```python from diffusers import StableDiffusionXLPipeline import torch pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") prompt = "photorealistic portrait of a young woman with freckles, natural makeup, soft lighting" negative_prompt = "(face asymmetry, eyes asymmetry, deformed eyes, open mouth)" # Generate batch of 4 images with different seeds images = pipe( prompt=[prompt] * 4, negative_prompt=[negative_prompt] * 4, num_inference_steps=25, guidance_scale=7.5, generator=[torch.Generator(device="cuda").manual_seed(i) for i in range(4)] ).images for idx, img in enumerate(images): img.save(f"batch_output_{idx}.png") ``` -------------------------------- ### Configure EulerDiscreteScheduler Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Configure the EulerDiscreteScheduler with custom parameters for fine-tuned control over the diffusion sampling process. This example shows how to set specific beta values, schedule, number of timesteps, prediction type, timestep spacing, and steps offset. ```python from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler import torch pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") # Configure scheduler with model's default parameters pipe.scheduler = EulerDiscreteScheduler.from_config( pipe.scheduler.config, beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000, prediction_type="epsilon", timestep_spacing="leading", steps_offset=1 ) image = pipe( prompt="realistic photo of autumn forest, fallen leaves, misty atmosphere", negative_prompt="oversaturated, cartoon, illustration", num_inference_steps=30, guidance_scale=7.0 ).images[0] image.save("autumn_forest.png") ``` -------------------------------- ### Configure DPM++ 2M Karras Scheduler Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Configure the recommended DPM++ 2M Karras scheduler for improved image quality and faster convergence. This involves replacing the default scheduler with a new instance configured for Karras sigmas. ```python from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler import torch pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") # Configure recommended DPM++ 2M Karras scheduler pipe.scheduler = DPMSolverMultistepScheduler.from_config( pipe.scheduler.config, algorithm_type="dpmsolver++", use_karras_sigmas=True ) image = pipe( prompt="photorealistic landscape, mountain lake at sunset, golden hour, reflection", negative_prompt="cartoon, illustration, painting, drawing", num_inference_steps=25, guidance_scale=7.0 ).images[0] image.save("landscape.png") ``` -------------------------------- ### Using the Safetensors Single-File Checkpoint Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Load the model from the single-file safetensors checkpoint for alternative loading methods or integration with other tools. This is an alternative to loading from a directory. ```python from diffusers import StableDiffusionXLPipeline import torch # Load from single-file safetensors checkpoint pipe = StableDiffusionXLPipeline.from_single_file( "SG161222/RealVisXL_V4.0/RealVisXL_V4.0.safetensors", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") image = pipe( prompt="hyperrealistic photo of a coffee cup on a wooden table, morning light", negative_prompt="cartoon, painting, drawing", num_inference_steps=25, guidance_scale=7.5 ).images[0] image.save("coffee_scene.png") ``` -------------------------------- ### Image-to-Image Transformation Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Transform existing images with text prompts while preserving composition using the img2img pipeline. The 'strength' parameter controls how much the original image is preserved. ```python from diffusers import StableDiffusionXLImg2ImgPipeline from PIL import Image import torch pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") # Load input image init_image = Image.open("input_photo.png").convert("RGB") init_image = init_image.resize((1024, 1024)) # Transform with prompt guidance image = pipe( prompt="professional studio portrait, dramatic lighting, sharp focus", negative_prompt="blurry, low quality, distorted", image=init_image, strength=0.4, # Lower = more original preservation num_inference_steps=25, guidance_scale=7.5 ).images[0] image.save("transformed_portrait.png") ``` -------------------------------- ### Memory-Efficient Generation with CPU Offloading Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Enable sequential CPU offloading for running on GPUs with limited VRAM while maintaining full model capabilities. This method also enables VAE slicing and tiling for further memory optimization. ```python from diffusers import StableDiffusionXLPipeline import torch pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ) # Enable memory-efficient features pipe.enable_sequential_cpu_offload() pipe.enable_vae_slicing() pipe.enable_vae_tiling() # Generate with reduced memory footprint image = pipe( prompt="ultra realistic photo of a cat sitting on a windowsill, natural daylight", negative_prompt="cartoon, drawing, painting, blurry", num_inference_steps=25, guidance_scale=7.5 ).images[0] image.save("cat_portrait.png") ``` -------------------------------- ### High-Resolution Generation with Hires Fix Source: https://context7.com/sg161222/realvisxl_v4.0/llms.txt Apply upscaling and refinement for enhanced detail using the img2img pipeline. This involves generating a base image in latent space and then refining it with a separate pipeline, using a denoising strength between 0.1 and 0.5. ```python from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline import torch # Load both pipelines base_pipe = StableDiffusionXLPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") refiner_pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained( "SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, use_safetensors=True ).to("cuda") prompt = "professional headshot, studio lighting, detailed skin texture" negative_prompt = "(face asymmetry, eyes asymmetry, deformed eyes, open mouth)" # Generate base image at 1024x1024 base_image = base_pipe( prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=25, guidance_scale=7.5, output_type="latent" ).images # Apply hires fix with recommended parameters # Denoising strength: 0.1-0.5 recommended hires_image = refiner_pipe( prompt=prompt, negative_prompt=negative_prompt, image=base_image, num_inference_steps=10, # Hires steps: 10+ strength=0.3, # Denoising strength guidance_scale=7.5 ).images[0] hires_image.save("hires_portrait.png") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.