| |
| """ |
| Debug script to check dataset output types |
| Save this as: scripts/debug_dataset.py |
| """ |
| import sys |
| from pathlib import Path |
|
|
| |
| project_root = Path(__file__).parent.parent |
| sys.path.append(str(project_root)) |
|
|
| import torch |
| from src.data.data_utils import create_data_loaders |
|
|
| def debug_dataset(): |
| """Debug dataset to find the data type issue""" |
| |
| print("ποΈ Project structure debug") |
| print(f"π Script location: {Path(__file__).parent}") |
| print(f"π Project root: {project_root}") |
| print(f"π Data directory: {project_root / 'data'}") |
| |
| |
| data_dir = project_root / 'data' |
| train_split = data_dir / 'splits' / 'train.json' |
| val_split = data_dir / 'splits' / 'val.json' |
| |
| print(f"\nπ File existence check:") |
| print(f" Train split: {train_split.exists()} - {train_split}") |
| print(f" Val split: {val_split.exists()} - {val_split}") |
| |
| if not train_split.exists(): |
| print("β Train split file not found!") |
| return |
| |
| |
| config = { |
| 'data_dir': str(data_dir), |
| 'batch_size': 4, |
| 'num_workers': 0, |
| 'use_cached': False, |
| 'model_name': 'resnet50', |
| 'num_classes': 5 |
| } |
| |
| print(f"\nπ Creating data loaders with config:") |
| for key, value in config.items(): |
| print(f" {key}: {value}") |
| |
| try: |
| train_loader, val_loader, class_weights, class_names = create_data_loaders(config) |
| print(f"β
Data loaders created successfully") |
| print(f"π Class names: {class_names}") |
| print(f"π Train dataset size: {len(train_loader.dataset)}") |
| print(f"π Val dataset size: {len(val_loader.dataset)}") |
| |
| |
| print(f"\n㪠Checking first batch...") |
| for i, batch in enumerate(train_loader): |
| print(f"\nBatch {i}:") |
| print(f" Type of batch: {type(batch)}") |
| |
| if isinstance(batch, (list, tuple)): |
| print(f" Batch length: {len(batch)}") |
| for j, item in enumerate(batch): |
| print(f" Item {j}: type={type(item)}") |
| if hasattr(item, 'shape'): |
| print(f" shape={item.shape}") |
| if hasattr(item, 'dtype'): |
| print(f" dtype={item.dtype}") |
| |
| |
| if not isinstance(item, torch.Tensor): |
| try: |
| if hasattr(item, '__iter__') and not isinstance(item, str): |
| content = list(item)[:5] if len(item) > 5 else list(item) |
| print(f" content preview: {content}") |
| else: |
| print(f" content: {item}") |
| except Exception as e: |
| print(f" content preview error: {e}") |
| else: |
| print(f" Batch content: {batch}") |
| |
| |
| break |
| |
| except Exception as e: |
| print(f"β Error creating data loaders: {e}") |
| import traceback |
| traceback.print_exc() |
| |
| |
| print(f"\nπ Checking raw JSON data...") |
| try: |
| import json |
| with open(train_split, 'r') as f: |
| data = json.load(f) |
| print(f"π JSON loaded successfully: {len(data)} items") |
| if data: |
| print(f"π First item keys: {list(data[0].keys())}") |
| print(f"π First item: {data[0]}") |
| except Exception as json_e: |
| print(f"β Error reading JSON: {json_e}") |
|
|
| if __name__ == '__main__': |
| debug_dataset() |