from PIL import Image import os def check_image_targets(folder_path): """ Check if targets (labels) of images in a folder are between 0 and 1. Args: - folder_path (str): Path to the folder containing images. Returns: - list: List of filenames with targets outside the range [0, 1]. """ filenames = os.listdir(folder_path) invalid_files = [] for filename in filenames: file_path = os.path.join(folder_path, filename) # Load image using PIL try: img = Image.open(file_path) except Exception as e: print(f"Error loading {filename}: {str(e)}") continue # Assuming you have a way to extract the target value from the image or filename target = extract_target(filename) # Replace with your own function to extract target # Check if target is outside the range [0, 1] if not (0 <= target <= 1): invalid_files.append(filename) print(f"Invalid target {target} in {filename}") return invalid_files def extract_target(filename): """ Example function to extract target from filename. Modify this according to your actual filenames and target extraction method. """ # Example: Extract target assuming filename format "image_target.ext" parts = filename.split("_") target_str = parts[-1].split(".")[0] # Remove extension try: target = float(target_str) except ValueError: target = -1 # Invalid target value return target # Example usage: folder_path = r"data/LoveDA/Test/pv/masks_png_convert" invalid_files = check_image_targets(folder_path) if invalid_files: print(f"Found {len(invalid_files)} files with invalid targets:") for file in invalid_files: print(file) else: print("All targets are within the valid range [0, 1].")