|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import logging |
| 17 | +import os |
| 18 | + |
| 19 | +from dfm.src.automodel.datasets.multiresolutionDataloader import build_flux_multiresolution_dataloader |
| 20 | + |
| 21 | + |
| 22 | +def test_real_dataloader(cache_path: str): |
| 23 | + # Configure logging to see the initialization details |
| 24 | + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 25 | + |
| 26 | + if not os.path.exists(cache_path): |
| 27 | + print(f"ERROR: Cache directory not found at {cache_path}") |
| 28 | + return |
| 29 | + |
| 30 | + try: |
| 31 | + # 1. Initialize the real dataloader |
| 32 | + dataloader, sampler = build_flux_multiresolution_dataloader( |
| 33 | + cache_dir=cache_path, |
| 34 | + batch_size=2, # Small batch for printing |
| 35 | + num_workers=2, # Use a couple of workers to test multi-processing |
| 36 | + dynamic_batch_size=True, # Test the bucket logic |
| 37 | + shuffle=True, |
| 38 | + ) |
| 39 | + |
| 40 | + print("\n" + "=" * 50) |
| 41 | + print("DATALOADER LOADED SUCCESSFULLY") |
| 42 | + print(f"Total Batches: {len(dataloader)}") |
| 43 | + print("=" * 50 + "\n") |
| 44 | + |
| 45 | + # 2. Iterate through the first 2 batches |
| 46 | + pathes = [] |
| 47 | + for batch_idx, batch in enumerate(dataloader): |
| 48 | + # if batch_idx >= 2: # Stop after 2 batches to avoid flooding the console |
| 49 | + # break |
| 50 | + |
| 51 | + print(f"--- Batch {batch_idx} ---") |
| 52 | + print(f"Keys in batch: {list(batch.keys())}") |
| 53 | + |
| 54 | + # Print Tensor Shapes |
| 55 | + print(f"Image Latents Shape: {batch['image_latents'].shape} (B, C, H, W)") |
| 56 | + |
| 57 | + if "text_embeddings" in batch: |
| 58 | + print(f"Text Embeds Shape: {batch['text_embeddings']}") |
| 59 | + print(f"Pooled Embeds Shape: {batch['pooled_prompt_embeds']}") |
| 60 | + |
| 61 | + # Print Metadata for the first sample in the batch |
| 62 | + metadata = batch.get("metadata", {}) |
| 63 | + print("\nSample Metadata (First item in batch):") |
| 64 | + print(f" - Prompt: {metadata['prompts'][0][:100]}...") # Truncated |
| 65 | + print(f" - Path: {metadata['image_paths'][0]}") |
| 66 | + print(f" - Res: {metadata['original_resolution'][0]} -> {metadata['crop_resolution'][0]}") |
| 67 | + print(f" - Aspect: {metadata['aspect_ratios'][0]}") |
| 68 | + print("-" * 30 + "\n") |
| 69 | + pathes.append(metadata["image_paths"][0]) |
| 70 | + unique_paths = list(set(pathes)) |
| 71 | + print(f"Total paths: {len(pathes)}") |
| 72 | + print(f"Unique paths: {len(unique_paths)}") |
| 73 | + |
| 74 | + except Exception as e: |
| 75 | + logging.error(f"Failed to run dataloader: {e}", exc_info=True) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + # SET YOUR ACTUAL PATH HERE |
| 80 | + MY_CACHE_DIR = "/linnanw/Diffuser/FLUX/DATA" |
| 81 | + |
| 82 | + test_real_dataloader(MY_CACHE_DIR) |
0 commit comments