{"record":{"id":"3daafb723d97c6a4","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"dataset-have-classes-but-input","errorCode":null,"errorMessage":"dataset have {} classes, but input {}","messagePattern":"dataset have (.+?) classes, but input (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/mini_imagenet/train_multi_gpu_using_launch.py","lineNumber":58,"sourceCode":"                                     transforms.RandomHorizontalFlip(),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),\n        \"val\": transforms.Compose([transforms.Resize(256),\n                                   transforms.CenterCrop(224),\n                                   transforms.ToTensor(),\n                                   transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}\n\n    data_root = args.data_path\n    json_path = \"./classes_name.json\"\n    # 实例化训练数据集\n    train_dataset = MyDataSet(root_dir=data_root,\n                              csv_name=\"new_train.csv\",\n                              json_path=json_path,\n                              transform=data_transform[\"train\"])\n\n    # check num_classes\n    if args.num_classes != len(train_dataset.labels):\n        raise ValueError(\"dataset have {} classes, but input {}\".format(len(train_dataset.labels),\n                                                                        args.num_classes))\n\n    # 实例化验证数据集\n    val_dataset = MyDataSet(root_dir=data_root,\n                            csv_name=\"new_val.csv\",\n                            json_path=json_path,\n                            transform=data_transform[\"val\"])\n\n    # 给每个rank对应的进程分配训练的样本索引\n    train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)\n    val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)\n\n    # 将样本索引每batch_size个元素组成一个list\n    train_batch_sampler = torch.utils.data.BatchSampler(\n        train_sampler, batch_size, drop_last=True)\n\n    nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8])  # number of workers\n    if rank == 0:","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/mini_imagenet/train_multi_gpu_using_launch.py#L40-L76","documentation":"The multi-GPU training script cross-checks args.num_classes (parsed from CLI/config, used to size the model head) against len(train_dataset.labels) (the class count in the dataset JSON). A mismatch means the model output layer would be sized wrongly for the data, so a ValueError reporting both counts is raised before training starts.","triggerScenarios":"Running train_multi_gpu_using_launch.py with --num_classes N where N differs from the number of classes recorded in class_indices.json generated from new_train.csv.","commonSituations":"Reusing a launch command from a previous dataset, forgetting to regenerate class_indices.json after adding/removing classes, copy-pasting num_classes=1000 (ImageNet) for Mini-ImageNet.","solutions":["Set --num_classes to the dataset's actual class count (len(train_dataset.labels)).","Regenerate class_indices.json / new_train.csv from the current dataset so the label map matches.","Re-run parse_data.py (or equivalent dataset-split script) after changing the training data."],"exampleFix":"// before\npython -m torch.distributed.launch --nproc_per_node=2 train_multi_gpu_using_launch.py --num_classes=1000\n// after\npython -m torch.distributed.launch --nproc_per_node=2 train_multi_gpu_using_launch.py --num_classes=64","handlingStrategy":"validation","validationCode":"import json\nwith open(json_path) as f:\n    class_indices = json.load(f)\nif args.num_classes != len(class_indices):\n    raise ValueError(f\"--num_classes={args.num_classes} but dataset has {len(class_indices)} classes\")","typeGuard":"def num_classes_matches(args, dataset_labels) -> bool:\n    return getattr(args, \"num_classes\", None) == len(dataset_labels)","tryCatchPattern":"try:\n    train_loop(args)\nexcept ValueError as e:\n    if \"classes\" in str(e):\n        print(\"Regenerate class_indices.json / new_train.csv, then pass the matching --num_classes\")\n    raise","preventionTips":["Derive num_classes from class_indices.json instead of typing it","Regenerate class_indices.json and CSVs after any dataset change","Print len(train_dataset.labels) when the training script starts","Don't reuse launch commands from previous datasets"],"tags":["python","pytorch","valueerror","dataset","cli-args"],"backgroundTag":"num-classes-mismatch","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}