|
| 1 | +# |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import numpy as np |
| 21 | +from PIL import Image |
| 22 | +from skimage.metrics import structural_similarity |
| 23 | +import glob |
| 24 | + |
| 25 | + |
| 26 | +def load_reference_image(image_path, verbose=False): |
| 27 | + """Load reference image from file path.""" |
| 28 | + if not os.path.exists(image_path): |
| 29 | + raise FileNotFoundError(f"Reference image not found: {image_path}") |
| 30 | + |
| 31 | + if verbose: |
| 32 | + print(f"[I] Loading reference image from {image_path}") |
| 33 | + return Image.open(image_path) |
| 34 | + |
| 35 | + |
| 36 | +def load_tripy_image(image_path, verbose=False): |
| 37 | + """Load tripy image from file path.""" |
| 38 | + if not os.path.exists(image_path): |
| 39 | + raise FileNotFoundError(f"Tripy image not found: {image_path}") |
| 40 | + |
| 41 | + if verbose: |
| 42 | + print(f"[I] Loading tripy image from {image_path}") |
| 43 | + return Image.open(image_path) |
| 44 | + |
| 45 | + |
| 46 | +def find_latest_image_in_output(output_dir="output", verbose=False): |
| 47 | + """Find the most recent image in the output directory.""" |
| 48 | + if not os.path.exists(output_dir): |
| 49 | + raise FileNotFoundError(f"Output directory not found: {output_dir}") |
| 50 | + |
| 51 | + # Look for PNG files in the output directory |
| 52 | + pattern = os.path.join(output_dir, "*.png") |
| 53 | + image_files = glob.glob(pattern) |
| 54 | + |
| 55 | + if not image_files: |
| 56 | + raise FileNotFoundError(f"No PNG images found in {output_dir}") |
| 57 | + |
| 58 | + image_files.sort(key=os.path.getmtime, reverse=True) |
| 59 | + |
| 60 | + if verbose: |
| 61 | + print(f"[I] Found {len(image_files)} images in {output_dir}") |
| 62 | + print(f"[I] Using most recent image: {image_files[0]}") |
| 63 | + |
| 64 | + return image_files[0] |
| 65 | + |
| 66 | + |
| 67 | +def compare_images(tripy_img, reference_img, threshold=0.80): |
| 68 | + """Compare two images using structural similarity index.""" |
| 69 | + # Convert both images to grayscale numpy arrays for comparison |
| 70 | + tripy_array = np.array(tripy_img.convert("L")) |
| 71 | + reference_array = np.array(reference_img.convert("L")) |
| 72 | + |
| 73 | + # Ensure both images have the same dimensions |
| 74 | + if tripy_array.shape != reference_array.shape: |
| 75 | + print(f"[W] Image shape mismatch: tripy {tripy_array.shape} vs reference {reference_array.shape}") |
| 76 | + # Resize reference to match tripy output |
| 77 | + reference_img_resized = reference_img.resize(tripy_img.size, Image.Resampling.LANCZOS) |
| 78 | + reference_array = np.array(reference_img_resized.convert("L")) |
| 79 | + |
| 80 | + # Calculate structural similarity |
| 81 | + ssim = structural_similarity(tripy_array, reference_array) |
| 82 | + |
| 83 | + if ssim >= threshold: |
| 84 | + print(f"[I] Passed: Images are similar (SSIM >= {threshold})") |
| 85 | + return True |
| 86 | + else: |
| 87 | + print(f"[I] Failed: Images are not similar enough (SSIM < {threshold})") |
| 88 | + return False |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + parser = argparse.ArgumentParser( |
| 93 | + description="Compare tripy diffusion output with a reference image", |
| 94 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 95 | + ) |
| 96 | + |
| 97 | + # Image loading options |
| 98 | + parser.add_argument( |
| 99 | + "--tripy-image", |
| 100 | + type=str, |
| 101 | + default=None, |
| 102 | + help="Path to tripy output image to compare. If not specified, will use the most recent image in output/ directory", |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + "--reference", |
| 106 | + type=str, |
| 107 | + default="assets/torch_ref_fp16_fuji_steps50_seed420.png", |
| 108 | + help="Path to reference image file to compare against", |
| 109 | + ) |
| 110 | + |
| 111 | + parser.add_argument("--threshold", type=float, default=0.80, help="SSIM threshold for considering images similar") |
| 112 | + parser.add_argument( |
| 113 | + "--verbose", action="store_true", default=False, help="Enable verbose output with timing and progress bars" |
| 114 | + ) |
| 115 | + |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + # Load reference image |
| 119 | + try: |
| 120 | + reference_img = load_reference_image(args.reference) |
| 121 | + except FileNotFoundError as e: |
| 122 | + print(f"[E] {e}") |
| 123 | + return 1 |
| 124 | + |
| 125 | + # Load tripy image |
| 126 | + try: |
| 127 | + if args.tripy_image: |
| 128 | + tripy_img = load_tripy_image(args.tripy_image, args.verbose) |
| 129 | + else: |
| 130 | + image_path = find_latest_image_in_output(verbose=args.verbose) |
| 131 | + tripy_img = load_tripy_image(image_path, args.verbose) |
| 132 | + except FileNotFoundError as e: |
| 133 | + print(f"[E] {e}") |
| 134 | + return 1 |
| 135 | + |
| 136 | + is_similar = compare_images(tripy_img, reference_img, args.threshold) |
| 137 | + |
| 138 | + return not is_similar |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + exit(main()) |
0 commit comments