WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
dataset have {} classes, but input {}
Error message
dataset have {} classes, but input {} What it means
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.
Source
Thrown at pytorch_classification/mini_imagenet/train_multi_gpu_using_launch.py:58
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),
"val": transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}
data_root = args.data_path
json_path = "./classes_name.json"
# 实例化训练数据集
train_dataset = MyDataSet(root_dir=data_root,
csv_name="new_train.csv",
json_path=json_path,
transform=data_transform["train"])
# check num_classes
if args.num_classes != len(train_dataset.labels):
raise ValueError("dataset have {} classes, but input {}".format(len(train_dataset.labels),
args.num_classes))
# 实例化验证数据集
val_dataset = MyDataSet(root_dir=data_root,
csv_name="new_val.csv",
json_path=json_path,
transform=data_transform["val"])
# 给每个rank对应的进程分配训练的样本索引
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
# 将样本索引每batch_size个元素组成一个list
train_batch_sampler = torch.utils.data.BatchSampler(
train_sampler, batch_size, drop_last=True)
nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers
if rank == 0:View on GitHub (pinned to 1ec3fe6f37)
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.
Example fix
// before python -m torch.distributed.launch --nproc_per_node=2 train_multi_gpu_using_launch.py --num_classes=1000 // after python -m torch.distributed.launch --nproc_per_node=2 train_multi_gpu_using_launch.py --num_classes=64
Defensive patterns
Strategy: validation
Validate before calling
import json
with open(json_path) as f:
class_indices = json.load(f)
if args.num_classes != len(class_indices):
raise ValueError(f"--num_classes={args.num_classes} but dataset has {len(class_indices)} classes") Type guard
def num_classes_matches(args, dataset_labels) -> bool:
return getattr(args, "num_classes", None) == len(dataset_labels) Try / catch
try:
train_loop(args)
except ValueError as e:
if "classes" in str(e):
print("Regenerate class_indices.json / new_train.csv, then pass the matching --num_classes")
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- dataset have {} classes, but input {}
- image: {} isn't RGB mode.
- image: {} isn't RGB mode.
- image: {} isn't RGB mode.
- image: {} isn't RGB mode.
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/3daafb723d97c6a4.
Report an issue: GitHub.