### Setup Nerfstudio Environment Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Navigate to the nerfstudio examples directory and install the environment in editable mode. Ensure you are in the correct directory before running. ```bash cd examples/nerfstudio pip install -e . cd ../.. ``` -------------------------------- ### Quickstart: Difix Pipeline with Reference Image Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Load the Difix pipeline with reference image support and perform image restoration. This method uses a reference image to guide the denoising process. ```python from pipeline_difix import DifixPipeline from diffusers.utils import load_image pipe = DifixPipeline.from_pretrained("nvidia/difix_ref", trust_remote_code=True) pipe.to("cuda") input_image = load_image("assets/example_input.png") ref_image = load_image("assets/example_ref.png") prompt = "remove degradation" output_image = pipe(prompt, image=input_image, ref_image=ref_image, num_inference_steps=1, timesteps=[199], guidance_scale=0.0).images[0] output_image.save("example_output.png") ``` -------------------------------- ### Setup Nerfstudio Integration for Difix3D Source: https://context7.com/nv-tlabs/difix3d/llms.txt Installs the Nerfstudio integration for Difix3D by navigating to the example directory and installing the package in editable mode. This is a prerequisite for using Difix3D with Nerfstudio. ```bash # Setup nerfstudio integration cd examples/nerfstudio && pip install -e . && cd ../.. ``` -------------------------------- ### Quickstart: Difix Pipeline without Reference Image Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Load the Difix pipeline and perform image restoration using a prompt. Ensure the model is moved to the CUDA device for processing. ```python from pipeline_difix import DifixPipeline from diffusers.utils import load_image pipe = DifixPipeline.from_pretrained("nvidia/difix", trust_remote_code=True) pipe.to("cuda") input_image = load_image("assets/example_input.png") prompt = "remove degradation" output_image = pipe(prompt, image=input_image, num_inference_steps=1, timesteps=[199], guidance_scale=0.0).images[0] output_image.save("example_output.png") ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Clone the Difix3D repository and install all required dependencies using pip. ```bash git clone https://github.com/nv-tlabs/Difix3D.git cd Difix3D pip install -r requirements.txt ``` -------------------------------- ### Train Difix Model with Accelerate Source: https://context7.com/nv-tlabs/difix3d/llms.txt Shows how to launch the Difix training script using HuggingFace Accelerate for single-GPU and multi-GPU (8 GPUs) setups. Configures mixed precision, dataset paths, learning rates, and loss function weights. ```bash # Single-GPU training accelerate launch --mixed_precision=bf16 src/train_difix.py \ --output_dir=./outputs/difix/train \ --dataset_path="data/data.json" \ --max_train_steps 10000 \ --resolution=512 \ --learning_rate 2e-5 \ --train_batch_size=1 \ --dataloader_num_workers 8 \ --enable_xformers_memory_efficient_attention \ --checkpointing_steps=1000 \ --eval_freq 1000 \ --viz_freq 100 \ --lambda_lpips 1.0 \ --lambda_l2 1.0 \ --lambda_gram 1.0 \ --gram_loss_warmup_steps 2000 \ --report_to "wandb" \ --tracker_project_name "difix" \ --tracker_run_name "train" \ --timestep 199 # Multi-GPU (8 GPUs, single node) export NUM_NODES=1; export NUM_GPUS=8 accelerate launch --mixed_precision=bf16 \ --main_process_port 29501 \ --multi_gpu \ --num_machines $NUM_NODES \ --num_processes $NUM_GPUS \ src/train_difix.py \ --output_dir=./outputs/difix/train \ --dataset_path="data/data.json" \ --max_train_steps 10000 \ --resolution=512 \ --learning_rate 2e-5 \ --train_batch_size=1 \ --dataloader_num_workers 8 \ --enable_xformers_memory_efficient_attention \ --checkpointing_steps=1000 --eval_freq 1000 --viz_freq 100 \ --lambda_lpips 1.0 --lambda_l2 1.0 --lambda_gram 1.0 \ --gram_loss_warmup_steps 2000 \ --report_to "wandb" \ --tracker_project_name "difix" \ --tracker_run_name "train_multigpu" \ --timestep 199 ``` -------------------------------- ### Run Difix CLI for Batch Inference Source: https://context7.com/nv-tlabs/difix3d/llms.txt Provides command-line examples for processing single images, directories of images, or video sequences using the Difix inference script. Includes options for reference-guided processing and saving output as video. ```bash # Single image python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "assets/example_input.png" \ --prompt "remove degradation" \ --output_dir "outputs/difix" \ --timestep 199 # Directory of PNG images python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "path/to/rendered_frames/" \ --prompt "remove degradation" \ --output_dir "outputs/difix3d+" \ --height 576 --width 1024 # Reference-guided directory processing python src/inference_difix.py \ --model_path "checkpoints/model_ref.pkl" \ --input_image "path/to/rendered_frames/" \ --ref_image "path/to/reference_frames/" \ --prompt "remove degradation" \ --output_dir "outputs/ref_guided" # Save output as video (MP4 at 30fps) python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "path/to/rendered_frames/" \ --prompt "remove degradation" \ --output_dir "outputs/video" \ --video ``` -------------------------------- ### Initialize and Use DifixPipeline Source: https://context7.com/nv-tlabs/difix3d/llms.txt Demonstrates initializing DifixPipeline for standard single-image restoration and reference-guided denoising. Enables memory optimizations like VAE slicing and tiling for large images. ```python from pipeline_difix import DifixPipeline from diffusers.utils import load_image # --- Standard single-image mode --- pipe = DifixPipeline.from_pretrained("nvidia/difix", trust_remote_code=True) pipe.to("cuda") input_image = load_image("assets/example_input.png") result = pipe( prompt="remove degradation", image=input_image, num_inference_steps=1, # single diffusion step timesteps=[199], # fixed noise level guidance_scale=0.0, # unconditional (no CFG) ) result.images[0].save("output.png") # --- Reference-guided mode --- pipe_ref = DifixPipeline.from_pretrained("nvidia/difix_ref", trust_remote_code=True) pipe_ref.to("cuda") ref_image = load_image("assets/example_ref.png") result_ref = pipe_ref( prompt="remove degradation", image=input_image, ref_image=ref_image, # nearby clean training view num_inference_steps=1, timesteps=[199], guidance_scale=0.0, ) result_ref.images[0].save("output_ref.png") # --- Memory optimisation for large images --- pipe.enable_vae_slicing() # decode VAE in slices pipe.enable_vae_tiling() # tile VAE for very high-res images ``` -------------------------------- ### Resume Difix3D Training from Checkpoint Source: https://context7.com/nv-tlabs/difix3d/llms.txt Use the accelerate launch command to resume training from a specified checkpoint directory. Ensure the output directory and dataset path are correctly set. ```bash accelerate launch --mixed_precision=bf16 src/train_difix.py \ --output_dir=./outputs/difix/train \ --dataset_path="data/data.json" \ --resume ./outputs/difix/train/checkpoints \ --max_train_steps 10000 \ --tracker_run_name "resume" ``` -------------------------------- ### Run Difix3D Finetuning with Nerfstudio Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Execute Difix3D finetuning using a pretrained nerfstudio checkpoint. This command configures various training parameters, including data paths, checkpoint loading, and evaluation settings. ```bash SCENE_ID=032dee9fb0a8bc1b90871dc5fe950080d0bcd3caf166447f44e60ca50ac04ec7 DATA=DATA_DIR/${SCENE_ID} DATA_FACTOR=4 CKPT_PATH=CKPR_DIR/${SCENE_ID}/nerfstudio/nerfstudio_models/step-000029999.ckpt # Path to the pretrained checkpoint file OUTPUT_DIR=outputs/difix3d/nerfacto/${SCENE_ID} CUDA_VISIBLE_DEVICES=0 ns-train difix3d \ --data ${DATA} --pipeline.model.appearance-embed-dim 0 --pipeline.model.camera-optimizer.mode off --save_only_latest_checkpoint False --vis viewer \ --output_dir ${OUTPUT_DIR} --experiment_name ${SCENE_ID} --timestamp '' --load-checkpoint ${CKPT_PATH} \ --max_num_iterations 30000 --steps_per_eval_all_images 0 --steps_per_eval_batch 0 --steps_per_eval_image 0 --steps_per_save 2000 --viewer.quit-on-train-completion True \ nerfstudio-data --orientation-method none --center_method none --auto-scale-poses False --downscale_factor ${DATA_FACTOR} --eval_mode filename ``` -------------------------------- ### Single GPU Training Command Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Launch the training process on a single GPU using accelerate. Ensure mixed precision is enabled for efficiency. ```bash accelerate launch --mixed_precision=bf16 src/train_difix.py \ --output_dir=./outputs/difix/train \ --dataset_path="data/data.json" \ --max_train_steps 10000 \ --resolution=512 --learning_rate 2e-5 \ --train_batch_size=1 --dataloader_num_workers 8 \ --enable_xformers_memory_efficient_attention \ --checkpointing_steps=1000 --eval_freq 1000 --viz_freq 100 \ --lambda_lpips 1.0 --lambda_l2 1.0 --lambda_gram 1.0 --gram_loss_warmup_steps 2000 \ --report_to "wandb" --tracker_project_name "difix" --tracker_run_name "train" --timestep 199 ``` -------------------------------- ### Multi-GPU Training Command Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Launch the training process across multiple GPUs. Set NUM_NODES and NUM_GPUS environment variables accordingly. Ensure the main process port is specified. ```bash export NUM_NODES=1 export NUM_GPUS=8 accelerate launch --mixed_precision=bf16 --main_process_port 29501 --multi_gpu --num_machines $NUM_NODES --num_processes $NUM_GPUS src/train_difix.py \ --output_dir=./outputs/difix/train \ --dataset_path="data/data.json" \ --max_train_steps 10000 \ --resolution=512 --learning_rate 2e-5 \ --train_batch_size=1 --dataloader_num_workers 8 \ --enable_xformers_memory_efficient_attention \ --checkpointing_steps=1000 --eval_freq 1000 --viz_freq 100 \ --lambda_lpips 1.0 --lambda_l2 1.0 --lambda_gram 1.0 --gram_loss_warmup_steps 2000 \ --report_to "wandb" --tracker_project_name "difix" --tracker_run_name "train" --timestep 199 ``` -------------------------------- ### Run Difix3D Finetuning with Gsplat Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Finetune Difix3D using gsplat with a specified checkpoint. This command utilizes a Python script to manage the training process, including data directories, factors, and output paths. ```bash SCENE_ID=032dee9fb0a8bc1b90871dc5fe950080d0bcd3caf166447f44e60ca50ac04ec7 DATA=DATA_DIR/${SCENE_ID}/gaussian_splat DATA_FACTOR=4 CKPT_PATH=CKPT_DIR/${SCENE_ID}/ckpts/ckpt_29999_rank0.pt # Path to the pretrained checkpoint file OUTPUT_DIR=outputs/difix3d/gsplat/${SCENE_ID} CUDA_VISIBLE_DEVICES=0 python examples/gsplat/simple_trainer_difix3d.py default \ --data_dir ${DATA} --data_factor ${DATA_FACTOR} \ --result_dir ${OUTPUT_DIR} --no-normalize-world-space --test_every 1 --ckpt ${CKPT_PATH} ``` -------------------------------- ### Load and Run Difix for Reference-Guided Restoration Source: https://context7.com/nv-tlabs/difix3d/llms.txt Loads a Difix model with reference-guided mode enabled (mv_unet=True) for artifact removal using a clean reference view. Ensure the model is set to evaluation mode. The output is a PIL Image. ```python # Reference-guided mode: provide a clean nearby view model_ref = Difix( pretrained_path="checkpoints/model_ref.pkl", timestep=199, mv_unet=True, # enables mv_unet for cross-view attention ) model_ref.set_eval() ref_image = Image.open("assets/example_ref.png").convert("RGB") output_ref = model_ref.sample( image=input_image, height=576, width=1024, prompt="remove degradation", ref_image=ref_image, ) output_ref.save("output_ref_guided.png") ``` -------------------------------- ### Train Base Nerfstudio Model with Difix3D Source: https://context7.com/nv-tlabs/difix3d/llms.txt Use this command to train a base Nerfstudio model and then apply Difix3D fine-tuning. Ensure all environment variables are set correctly before execution. ```bash SCENE_ID=032dee9fb0a8bc1b90871dc5fe950080d0bcd3caf166447f44e60ca50ac04ec7 DATA=DATA_DIR/${SCENE_ID} CKPT_PATH=CKPT_DIR/${SCENE_ID}/nerfacto/nerfstudio_models/step-000029999.ckpt OUTPUT_DIR=outputs/difix3d/nerfacto/${SCENE_ID} CUDA_VISIBLE_DEVICES=0 ns-train difix3d \ --data ${DATA} \ --pipeline.model.appearance-embed-dim 0 \ --pipeline.model.camera-optimizer.mode off \ --save_only_latest_checkpoint False \ --vis viewer \ --output_dir ${OUTPUT_DIR} \ --experiment_name ${SCENE_ID} \ --timestamp '' \ --load-checkpoint ${CKPT_PATH} \ --max_num_iterations 30000 \ --steps_per_eval_all_images 0 \ --steps_per_eval_batch 0 \ --steps_per_eval_image 0 \ --steps_per_save 2000 \ --viewer.quit-on-train-completion True \ nerfstudio-data \ --orientation-method none \ --center_method none \ --auto-scale-poses False \ --downscale_factor 4 \ --eval_mode filename ``` -------------------------------- ### Data Preparation JSON Format Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Prepare your dataset in this JSON format for training. Each entry includes paths to the input image, target image, reference image, and a prompt. ```json { "train": { "{data_id}": { "image": "{PATH_TO_IMAGE}", "target_image": "{PATH_TO_TARGET_IMAGE}", "ref_image": "{PATH_TO_REF_IMAGE}", "prompt": "remove degradation" } }, "test": { "{data_id}": { "image": "{PATH_TO_IMAGE}", "target_image": "{PATH_TO_TARGET_IMAGE}", "ref_image": "{PATH_TO_REF_IMAGE}", "prompt": "remove degradation" } } } ``` -------------------------------- ### Load and Run Difix for Single-Image Restoration Source: https://context7.com/nv-tlabs/difix3d/llms.txt Loads a Difix model from a checkpoint for single-image artifact removal. Ensure the model is set to evaluation mode. The output is a PIL Image. ```python import torch from PIL import Image from src.model import Difix # --- Inference: load from a saved .pkl checkpoint --- model = Difix( pretrained_path="checkpoints/model.pkl", # .pkl with state_dict_unet, state_dict_vae timestep=199, # diffusion timestep (199 recommended) mv_unet=False, # set True to enable reference-guided mode ) model.set_eval() # disables gradients, sets eval mode # Single-image artifact removal input_image = Image.open("assets/example_input.png").convert("RGB") output_image = model.sample( image=input_image, height=576, width=1024, prompt="remove degradation", ref_image=None, # optional reference view ) output_image.save("output_fixed.png") # returns PIL.Image ``` -------------------------------- ### Difix3D Fine-tuning with Gaussian Splatting Source: https://context7.com/nv-tlabs/difix3d/llms.txt Integrates Difix3D into a full 3D Gaussian Splatting training loop. This script applies Difix3D at specified fix_steps to render novel views, refine them with DifixPipeline, and add them as training data. ```bash # Install gsplat first: https://github.com/nerfstudio-project/gsplat SCENE_ID=032dee9fb0a8bc1b90871dc5fe950080d0bcd3caf166447f44e60ca50ac04ec7 DATA=DATA_DIR/${SCENE_ID}/gaussian_splat DATA_FACTOR=4 CKPT_PATH=CKPT_DIR/${SCENE_ID}/ckpts/ckpt_29999_rank0.pt OUTPUT_DIR=outputs/difix3d/gsplat/${SCENE_ID} CUDA_VISIBLE_DEVICES=0 python examples/gsplat/simple_trainer_difix3d.py default \ --data_dir ${DATA} \ --data_factor ${DATA_FACTOR} \ --result_dir ${OUTPUT_DIR} \ --no-normalize-world-space \ --test_every 1 \ --ckpt ${CKPT_PATH} ``` -------------------------------- ### Inference Command Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Run inference using a trained model. Place the model checkpoint in the 'checkpoints' directory and specify the input image, prompt, and output directory. ```bash python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "assets/example_input.png" \ --prompt "remove degradation" \ --output_dir "outputs/difix" \ --timestep 199 ``` -------------------------------- ### Real-time Post-rendering Enhancement with Difix3D+ Source: https://context7.com/nv-tlabs/difix3d/llms.txt Applies Difix3D as a final per-frame post-processing step at render time to correct residual blur. This command processes a folder of rendered frames. ```bash # Post-process a folder of rendered frames with Difix3D+ python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "outputs/difix3d/nerfacto/${SCENE_ID}/renders/test/" \ --prompt "remove degradation" \ --output_dir "outputs/difix3d+" \ --timestep 199 ``` -------------------------------- ### Load and Process Paired Dataset with DataLoader Source: https://context7.com/nv-tlabs/difix3d/llms.txt Initializes the PairedDataset and DataLoader for training. The dataset normalizes images and stacks multi-view tensors. Ensure the tokenizer is loaded from the correct model. ```python from src.dataset import PairedDataset from torch.utils.data import DataLoader from transformers import AutoTokenizer # Dataset JSON format: # { # "train": { # "scene_001_frame_042": { # "image": "data/degraded/scene_001/042.png", # "target_image": "data/clean/scene_001/042.png", # "ref_image": "data/clean/scene_001/000.png", # optional # "prompt": "remove degradation" # } # }, # "test": { ... } # } tokenizer = AutoTokenizer.from_pretrained("stabilityai/sd-turbo", subfolder="tokenizer") dataset = PairedDataset( dataset_path="data/data.json", split="train", height=576, width=1024, tokenizer=tokenizer, ) loader = DataLoader(dataset, batch_size=2, shuffle=True, num_workers=4) for batch in loader: # batch["conditioning_pixel_values"]: # batch["output_pixel_values"]: # batch["input_ids"]: # batch["caption"]: print(batch["conditioning_pixel_values"].shape) # e.g. (2, 2, 3, 576, 1024) break ``` -------------------------------- ### Save and Load Difix3D Model Checkpoints Source: https://context7.com/nv-tlabs/difix3d/llms.txt Utilizes `save_ckpt` and `load_ckpt_from_state_dict` to manage model checkpoints efficiently. These functions serialize only the trained parameters (UNet, LoRA/skip-conv VAE decoder, optimizer state) to minimize file size. ```python import torch from src.model import Difix, save_ckpt, load_ckpt_from_state_dict # Save checkpoint model = Difix(timestep=199) optimizer = torch.optim.AdamW(model.unet.parameters(), lr=2e-5) # ... training loop ... save_ckpt(model, optimizer, "checkpoints/model_10000.pkl") # Saves: state_dict_unet, state_dict_vae (lora+skip only), optimizer, vae_lora_target_modules, rank_vae # Reload for continued training model2 = Difix(timestep=199) optim2 = torch.optim.AdamW(model2.unet.parameters(), lr=2e-5) model2, optim2 = load_ckpt_from_state_dict(model2, optim2, "checkpoints/model_10000.pkl") model2.set_eval() ``` -------------------------------- ### Difix - Core diffusion-based image restoration model Source: https://context7.com/nv-tlabs/difix3d/llms.txt The Difix class is a torch.nn.Module that wraps a modified SD-Turbo UNet and VAE for single-step artifact removal. It supports both single-image and reference-guided inference. ```APIDOC ## Difix ### Description The `Difix` class (`src/model.py`) is a `torch.nn.Module` that wraps a modified SD-Turbo UNet and VAE. The VAE encoder caches its intermediate down-block activations; the decoder applies learned skip-connection convolutions (`skip_conv_1–4`) that inject encoder features back into decoding, enabling coherent single-step artifact removal. LoRA adapters are applied to the VAE decoder for parameter-efficient fine-tuning. The model accepts batched multi-view tensors of shape `(B, V, C, H, W)`. ### Inference #### Single-image artifact removal ```python import torch from PIL import Image from src.model import Difix # --- Inference: load from a saved .pkl checkpoint --- model = Difix( pretrained_path="checkpoints/model.pkl", # .pkl with state_dict_unet, state_dict_vae timestep=199, # diffusion timestep (199 recommended) mv_unet=False, # set True to enable reference-guided mode ) model.set_eval() # disables gradients, sets eval mode # Single-image artifact removal input_image = Image.open("assets/example_input.png").convert("RGB") output_image = model.sample( image=input_image, height=576, width=1024, prompt="remove degradation", ref_image=None, # optional reference view ) output_image.save("output_fixed.png") # returns PIL.Image ``` #### Reference-guided mode ```python # Reference-guided mode: provide a clean nearby view model_ref = Difix( pretrained_path="checkpoints/model_ref.pkl", timestep=199, mv_unet=True, # enables mv_unet for cross-view attention ) model_ref.set_eval() ref_image = Image.open("assets/example_ref.png").convert("RGB") output_ref = model_ref.sample( image=input_image, height=576, width=1024, prompt="remove degradation", ref_image=ref_image, ) output_ref.save("output_ref_guided.png") ``` ``` -------------------------------- ### Difix.forward() — Batched multi-view forward pass Source: https://context7.com/nv-tlabs/difix3d/llms.txt The `forward` method of the Difix class performs a batched multi-view forward pass. It accepts a tensor of shape `(B, V, C, H, W)`, encodes, denoises, and decodes each view. ```APIDOC ## Difix.forward() ### Description The `forward` method accepts a `(B, V, C, H, W)` tensor (pixel values normalised to `[-1, 1]`), encodes each view through the VAE, denoises with the UNet at a fixed timestep, then decodes through the skip-connection VAE decoder. This is the interface used during training and for custom batched inference loops. ### Parameters #### Input Tensor - **img** (torch.Tensor) - Input image tensor of shape `(B, V, C, H, W)` with pixel values normalized to `[-1, 1]`. - **prompt** (str or torch.Tensor) - The text prompt for denoising, or pre-tokenized prompt tokens. ### Request Example ```python import torch from torchvision import transforms from src.model import Difix model = Difix(pretrained_path="checkpoints/model.pkl", timestep=199) model.set_eval() T = transforms.Compose([ transforms.Resize((576, 1024)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ]) img = T(Image.open("assets/example_input.png")).unsqueeze(0).unsqueeze(0).cuda() # shape: (1, 1, 3, 576, 1024) with torch.no_grad(): # pass either prompt string OR pre-tokenised prompt_tokens output = model.forward(img, prompt="remove degradation") # output shape: (1, 1, 3, 576, 1024), values in [-1, 1] # convert back to PIL out_pil = transforms.ToPILImage()(output[0, 0].cpu() * 0.5 + 0.5) out_pil.save("output_batch.png") ``` ### Response #### Success Response - **output** (torch.Tensor) - Denoised image tensor of shape `(B, V, C, H, W)` with pixel values in `[-1, 1]`. ``` -------------------------------- ### Difix3D+ Post-Rendering Enhancement Source: https://github.com/nv-tlabs/difix3d/blob/main/README.md Apply Difix3D as a post-processing step for enhancing novel views. This script takes a model path, input images, a prompt, and an output directory to perform the enhancement. ```bash python src/inference_difix.py \ --model_path "checkpoints/model.pkl" \ --input_image "PATH_TO_IMAGES" \ --prompt "remove degradation" \ --output_dir "outputs/difix3d+" \ --timestep 199 ``` -------------------------------- ### Batched Multi-View Forward Pass with Difix Source: https://context7.com/nv-tlabs/difix3d/llms.txt Performs a batched forward pass using the Difix model, accepting tensors normalized to [-1, 1]. This is suitable for training or custom batched inference. The output tensor is also in the range [-1, 1]. ```python import torch from torchvision import transforms from src.model import Difix model = Difix(pretrained_path="checkpoints/model.pkl", timestep=199) model.set_eval() T = transforms.Compose([ transforms.Resize((576, 1024)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ]) img = T(Image.open("assets/example_input.png")).unsqueeze(0).unsqueeze(0).cuda() # shape: (1, 1, 3, 576, 1024) with torch.no_grad(): # pass either prompt string OR pre-tokenised prompt_tokens output = model.forward(img, prompt="remove degradation") # output shape: (1, 1, 3, 576, 1024), values in [-1, 1] # convert back to PIL out_pil = transforms.ToPILImage()(output[0, 0].cpu() * 0.5 + 0.5) out_pil.save("output_batch.png") ``` -------------------------------- ### Compute Gram Matrix Loss using VGG-16 Source: https://context7.com/nv-tlabs/difix3d/llms.txt Calculates the Gram matrix loss between predicted and target images using a pre-trained VGG-16 model. Images must be normalized to ImageNet standards before being passed to the VGG network. ```python import torch import torchvision.models as models from torchvision import transforms from src.loss import gram_loss # Load pre-trained VGG-16 feature extractor (no gradients needed) vgg = models.vgg16(pretrained=True).features.cuda().eval() for p in vgg.parameters(): p.requires_grad_(False) # ImageNet normalisation (required before passing to VGG) normalize = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) predicted = torch.randn(2, 3, 400, 400).cuda() # model output, range [-1,1] target = torch.randn(2, 3, 400, 400).cuda() # ground truth, range [-1,1] # Convert from [-1,1] to [0,1] then apply ImageNet normalisation pred_norm = normalize(predicted * 0.5 + 0.5) target_norm = normalize(target * 0.5 + 0.5) loss = gram_loss(pred_norm, target_norm, vgg) # Returns scalar tensor; typical scale ~1e-3 to 1e-2 print(f"Gram loss: {loss.item():.6f}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.