### FluxPriorReduxPipeline Multi-Image Composition Setup (Python) Source: https://context7.com/posplexity/ponix-generator/llms.txt Initializes the FluxPriorReduxPipeline for multi-image composition by loading necessary text encoders and tokenizers for CLIP and T5 models. This setup is crucial for advanced image generation tasks that involve combining multiple images with masks and text prompts. Dependencies include `torch`, `diffusers`, `transformers`, `safetensors`, and `huggingface_hub`. ```python import torch from diffusers import FluxPipeline from src.flux_redux_pipeline import FluxPriorReduxPipeline from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast from safetensors.torch import load_file from huggingface_hub import hf_hub_download from diffusers.utils import load_image # Load text encoders and tokenizers clip_text_model = CLIPTextModel.from_pretrained( "black-forest-labs/FLUX.1-dev", subfolder="text_encoder", cache_dir="/workspace/ponix-generator/model", torch_dtype=torch.bfloat16 ) clip_tokenizer = CLIPTokenizer.from_pretrained( "black-forest-labs/FLUX.1-dev", subfolder="tokenizer", cache_dir="/workspace/ponix-generator/model" ) t5_text_model = T5EncoderModel.from_pretrained( "black-forest-labs/FLUX.1-dev", subfolder="text_encoder_2", cache_dir="/workspace/ponix-generator/model", torch_dtype=torch.bfloat16 ) t5_tokenizer = T5TokenizerFast.from_pretrained( "black-forest-labs/FLUX.1-dev", subfolder="tokenizer_2", cache_dir="/workspace/ponix-generator/model" ) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") clip_text_model = clip_text_model.to(device) t5_text_model = t5_text_model.to(device) ``` -------------------------------- ### DreamBooth LoRA Training Command Source: https://context7.com/posplexity/ponix-generator/llms.txt Command-line execution for training custom LoRA adapters using DreamBooth with the FLUX.1-dev model. This example specifies instance data, output directory, prompts, and various training parameters such as resolution, batch size, learning rate, and mixed precision. ```bash # Training command with DreamBooth accelerate launch src/train/train_dlora_flux.py \ --pretrained_model_name_or_path="black-forest-labs/FLUX.1-dev" \ --instance_data_dir="./data/instance-v0.2.0-front" \ --output_dir="./output/ponix-lora-v0.2.0" \ --instance_prompt="photo of plush bird" \ --resolution=512 \ --train_batch_size=1 \ --gradient_accumulation_steps=4 \ --learning_rate=1e-4 \ --text_encoder_lr=5e-6 \ --rank=4 \ --max_train_steps=1000 \ --checkpointing_steps=500 \ --validation_prompt="photo of plush bird playing soccer" \ --validation_epochs=50 \ --num_validation_images=4 \ --seed=42 \ --mixed_precision="bf16" \ --gradient_checkpointing \ --train_text_encoder \ --cache_latents \ --allow_tf32 \ --report_to="tensorboard" ``` -------------------------------- ### Downsample Image Embeddings (PyTorch) Source: https://context7.com/posplexity/ponix-generator/llms.txt Spatially downsamples image embeddings, treating them as a 2D grid. This function is useful for reducing the resolution of embeddings, for example, when matching different model scales. It supports 'area', 'bicubic', and 'nearest' interpolation modes. ```python import torch import torch.nn.functional as F def downsample_image_embeds(image_embeds: torch.FloatTensor, factor: int = 2, mode: str = "area"): """ Spatially downsample image embeddings interpreted as 2D grid. Args: image_embeds: Shape (batch, tokens, dim) where tokens = m*m factor: Downsampling factor (e.g., 2 means m -> m/2) mode: Interpolation mode ('area', 'bicubic', 'nearest') Returns: Downsampled embeddings with shape (batch, new_tokens, dim) """ b, t, d = image_embeds.shape side = int(t**0.5) if side * side != t: raise ValueError(f"Cannot interpret {t} tokens as a square (m*m).") image_embeds = image_embeds.view(b, side, side, d) image_embeds = image_embeds.permute(0, 3, 1, 2) new_side = side // factor if new_side < 1: raise ValueError(f"Downsample factor {factor} too large for side={side}.") image_embeds = F.interpolate(image_embeds, size=(new_side, new_side), mode=mode) image_embeds = image_embeds.permute(0, 2, 3, 1).reshape(b, new_side * new_side, d) return image_embeds ``` -------------------------------- ### DreamBooth LoRA Training with Prior Preservation (Python) Source: https://context7.com/posplexity/ponix-generator/llms.txt Python script for DreamBooth LoRA training that includes prior preservation for improved generalization. It utilizes the `main` and `parse_args` functions from `src.train.train_dlora_flux` and configures parameters for both instance and class data. ```python # Training with prior preservation for better generalization from src.train.train_dlora_flux import main, parse_args args = parse_args([ "--pretrained_model_name_or_path", "black-forest-labs/FLUX.1-dev", "--instance_data_dir", "./data/instance-v0.2.0-front", "--class_data_dir", "./data/class-plush-toys", "--output_dir", "./output/ponix-lora-prior", "--instance_prompt", "photo of plush bird", "--class_prompt", "photo of a plush toy", "--with_prior_preservation", "--prior_loss_weight", "1.0", "--num_class_images", "100", "--resolution", "512", "--train_batch_size", "2", "--learning_rate", "1e-4", "--rank", "4", "--max_train_steps", "1500", "--mixed_precision", "bf16", "--cache_dir", "/workspace/ponix-generator/model" ]) main(args) print("Training completed successfully") ``` -------------------------------- ### Load FluxPriorReduxPipeline and Generate Image Source: https://context7.com/posplexity/ponix-generator/llms.txt Loads the FluxPriorReduxPipeline with textual inversion embeddings, conditions it with masks and prompts, and then uses a FluxPipeline to generate the final image. It handles loading models, embeddings, LORA weights, and image conditioning steps. ```python from diffusers import FluxPipeline, FluxPriorReduxPipeline from huggingface_hub import hf_hub_download from safetensors.safetensors_rust import load_file from PIL import Image import torch # Assuming device, clip_text_model, clip_tokenizer, t5_text_model, t5_tokenizer are defined elsewhere # Load FluxPriorReduxPipeline with textual inversion pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained( pretrained_model_name_or_path="black-forest-labs/FLUX.1-Redux-dev", text_encoder=clip_text_model, tokenizer=clip_tokenizer, text_encoder_2=t5_text_model, tokenizer_2=t5_tokenizer, torch_dtype=torch.bfloat16, cache_dir="/workspace/ponix-generator/model", ).to(device) embedding_path = hf_hub_download( repo_id='cwhuh/ponix-generator-v0.2.0', filename='./ponix-generator-v0.2.0_emb.safetensors', repo_type="model" ) state_dict = load_file(embedding_path) pipe_prior_redux.load_textual_inversion( state_dict["clip_l"], token=["", "", ""], text_encoder=pipe_prior_redux.text_encoder, tokenizer=pipe_prior_redux.tokenizer ) # Load base generation pipeline pipe = FluxPipeline.from_pretrained( "black-forest-labs/FLUX.1-dev", text_encoder=None, text_encoder_2=None, torch_dtype=torch.bfloat16, cache_dir="/workspace/ponix-generator/model" ).to("cuda") pipe.load_lora_weights('cwhuh/ponix-generator-v0.2.0', weight_name='pytorch_lora_weights.safetensors') # First image conditioning with mask image1 = Image.open("./data/img/ponix.webp").convert("RGB") mask1 = Image.open("./data/img/ponix_mask.png").convert("RGB") pipe_prior_output = pipe_prior_redux( image=image1, mask=mask1, prompt="photo of plush bird wearing korean traditional clothes in front of a building" ) # Second image conditioning with mask and chaining image2 = Image.open("./data/img/hanbok.webp").convert("RGB") mask2 = Image.open("./data/img/hanbok_mask.png").convert("RGB") pipe_prior_output2 = pipe_prior_redux( image=image2, mask=mask2, downsample_factor=2, **pipe_prior_output ) # Third image conditioning without mask image3 = Image.open("./data/img/changeup.jpeg").convert("RGB") pipe_prior_output3 = pipe_prior_redux( image=image3, **pipe_prior_output2 ) # Convert embeddings to proper dtype pipe_prior_output3.prompt_embeds = pipe_prior_output3.prompt_embeds.to(torch.bfloat16) pipe_prior_output3.pooled_prompt_embeds = pipe_prior_output3.pooled_prompt_embeds.to(torch.bfloat16) # Generate final composition images = pipe( guidance_scale=2.5, num_inference_steps=50, generator=torch.Generator("cpu").manual_seed(0), **pipe_prior_output3, ).images images[0].save("flux-dev-redux.png") print("Multi-image composition saved successfully") ``` -------------------------------- ### Text-to-Image Generation with Ponix LoRA Weights (Python) Source: https://context7.com/posplexity/ponix-generator/llms.txt Generates images of the 'ponix' plush bird character using custom prompts and trained LoRA weights with the FLUX.1-dev pipeline. It loads LoRA adapters and textual inversion embeddings, supports multiple seeds for varied outputs, and saves generated images to a specified directory. Dependencies include `diffusers`, `torch`, `huggingface_hub`, `safetensors`, and `os`. ```python from diffusers import AutoPipelineForText2Image import torch from huggingface_hub import hf_hub_download from safetensors.torch import load_file import os from datetime import datetime # Load FLUX.1-dev pipeline with ponix LoRA weights pipeline = AutoPipelineForText2Image.from_pretrained( "black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16, cache_dir="/workspace/ponix-generator/model" ).to('cuda') # Load LoRA adapter pipeline.load_lora_weights('cwhuh/ponix-generator-v0.2.0', weight_name='pytorch_lora_weights.safetensors') # Load textual inversion embeddings for trigger tokens embedding_path = hf_hub_download( repo_id='cwhuh/ponix-generator-v0.2.0', filename='./ponix-generator-v0.2.0_emb.safetensors', repo_type="model" ) state_dict = load_file(embedding_path) pipeline.load_textual_inversion( state_dict["clip_l"], token=["", "", ""], text_encoder=pipeline.text_encoder, tokenizer=pipeline.tokenizer ) # Define prompt with trigger tokens prompt = """ photo of plush bird swimming in the ocean, gentle waves surrounding it, crystal clear blue water, sunlight reflecting off the water surface, hyper-realistic details, cinematic lighting, 8k resolution, ultra high quality photograph, serene, natural composition """ # Create output directory os.makedirs("./results", exist_ok=True) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") # Generate images with multiple seeds for seed in range(1, 6): generator = torch.Generator("cuda").manual_seed(seed) image = pipeline( prompt, num_inference_steps=50, generator=generator ).images[0] image.save(f"./results/ponix_generated_{timestamp}_seed{seed}.png") print(f"Generated image with seed {seed}") ``` -------------------------------- ### Batch Background Removal with RMBG-2.0 Source: https://context7.com/posplexity/ponix-generator/llms.txt Processes a batch of training images to remove their backgrounds using the RMBG-2.0 model. It saves the resulting images with transparent backgrounds (implied by the white background replacement) to a specified output directory. Handles image loading, transformation, prediction, and saving. ```python from transformers import AutoModelForImageSegmentation import os import cv2 import numpy as np from PIL import Image from tqdm import tqdm import torch from pathlib import Path from torchvision import transforms # Load background removal model model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-2.0", trust_remote_code=True) input_dir = "./data/raw-v0.2.0-front" output_dir = "./data/instance-v0.2.0-front" os.makedirs(output_dir, exist_ok=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) torch.set_float32_matmul_precision('high') model.eval() # Image transformation pipeline image_size = (1024, 1024) transform_image = transforms.Compose([ transforms.Resize(image_size), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) def remove_background(image_path, output_path): img = Image.open(image_path).convert("RGB") img_np = np.array(img) input_image = transform_image(img).unsqueeze(0).to(device) with torch.no_grad(): pred = model(input_image)[-1].sigmoid().cpu() mask = pred[0].squeeze(0) mask_pil = transforms.ToPILImage()(mask) mask_resized = mask_pil.resize(img.size, resample=Image.BILINEAR) mask_np = np.array(mask_resized, dtype=np.float32) / 255.0 white_bg = np.ones_like(img_np, dtype=np.uint8) * 255 mask_np_3ch = np.stack([mask_np, mask_np, mask_np], axis=2) img_float = img_np.astype(np.float32) white_bg_float = white_bg.astype(np.float32) result_float = img_float * mask_np_3ch + white_bg_float * (1.0 - mask_np_3ch) result = np.clip(result_float, 0, 255).astype(np.uint8) result_img = Image.fromarray(result) # Note: The original code rotates by -90 degrees. This might be specific to the dataset or desired output format. rotated_img = result_img.rotate(-90, expand=True) rotated_img.save(output_path) # Batch process images image_files = list(Path(input_dir).glob("*.jpg")) + \ list(Path(input_dir).glob("*.png")) + \ list(Path(input_dir).glob("*.jpeg")) print(f"Processing {len(image_files)} images...") for img_path in tqdm(image_files): output_path = os.path.join(output_dir, img_path.name) try: remove_background(img_path, output_path) except Exception as e: print(f"Error processing {img_path}: {e}") print(f"All images processed. Results saved to {output_dir}") ``` -------------------------------- ### Interactive Mask Generation with Gradio Source: https://context7.com/posplexity/ponix-generator/llms.txt Creates custom masks for selective image conditioning via an interactive web interface. It uses Gradio for the UI and PIL for image manipulation. The function `save_mask` extracts an alpha channel from an RGBA image and saves it as a grayscale mask. ```python import gradio as gr from PIL import Image def save_mask(input_image_editor): mask_path = input_image_editor["layers"][0] if not mask_path: return "No image provided." # Load edited image as RGBA edited_img = Image.open(mask_path).convert("RGBA") # Extract alpha channel as mask alpha = edited_img.split()[-1] mask = alpha.point(lambda p: 255 if p > 0 else 0).convert("L") # Save mask mask.save("my_mask.png") return "Mask saved as 'my_mask.png'!" with gr.Blocks() as demo: gr.Markdown("## Draw a mask on the image, then click 'Save Mask'") image_editor = gr.ImageEditor( label="Image Editor (draw your mask in white)", type="filepath", image_mode='RGBA', sources=["upload", "webcam"], brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"), layers=False ) btn_save = gr.Button("Save Mask") info_msg = gr.Textbox(label="Info") btn_save.click(fn=save_mask, inputs=image_editor, outputs=info_msg) demo.launch() print("Gradio mask generator launched at http://localhost:7860") ``` -------------------------------- ### Patchify Mask with Ponix Generator Source: https://context7.com/posplexity/ponix-generator/llms.txt This snippet demonstrates how to use the `patchify_mask` function to convert a 2D mask into a patchified format. It takes a PyTorch mask tensor and a patch size as input, outputting the patched mask. This is often used in conjunction with image generation models that operate on patches. ```python import torch from ponix_generator.utils.mask_utils import patchify_mask mask = torch.rand(2, 224, 224) patched_mask = patchify_mask(mask, patch_size=14) print(f"Original mask: {mask.shape}, Patched mask: {patched_mask.shape}") ``` -------------------------------- ### Patchify Mask (PyTorch) Source: https://context7.com/posplexity/ponix-generator/llms.txt Converts a high-resolution mask into a patch-level mask that corresponds to vision embeddings, typically used with models like CLIP. It uses `MaxPool2d` to aggregate mask information within each patch. The function handles masks with different input dimensions. ```python import torch def patchify_mask(mask: torch.Tensor, patch_size: int = 14) -> torch.Tensor: """ Convert high-resolution mask to patch-level mask matching vision embeddings. Args: mask: Shape (B, H, W) or (B, 1, H, W) patch_size: Size of each patch (default 14 for CLIP vision) Returns: Patchified mask with shape (B, m, m) where m = H//patch_size """ if mask.dim() == 3: mask = mask.unsqueeze(1) b, c, h, w = mask.shape pool = torch.nn.MaxPool2d(kernel_size=patch_size, stride=patch_size) mask_patched = pool(mask) mask_patched = (mask_patched > 0).float().squeeze(1) return mask_patched ``` -------------------------------- ### Downsample Image Embeddings with Ponix Generator Source: https://context7.com/posplexity/ponix-generator/llms.txt This snippet shows how to downsample image embeddings using the `downsample_image_embeds` function. It takes PyTorch tensor embeddings and a downsampling factor as input, returning the downsampled embeddings. This is useful for reducing the computational load or preparing embeddings for subsequent processing stages. ```python import torch from ponix_generator.utils.image_utils import downsample_image_embeds embeddings = torch.randn(2, 256, 768) # 2 images, 16x16 tokens, 768 dims downsampled = downsample_image_embeds(embeddings, factor=2) print(f"Original: {embeddings.shape}, Downsampled: {downsampled.shape}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.