WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
dataset have {} classes, but input {}
Error message
dataset have {} classes, but input {} What it means
The single-GPU training script compares args.num_classes (used to build the model's classifier) with the number of distinct labels in the training dataset; if they differ it raises ValueError showing both numbers. This guard prevents training a model whose output dimensionality doesn't match the label space.
Source
Thrown at pytorch_classification/mini_imagenet/train_single_gpu.py:45
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"])
batch_size = args.batch_size
nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers
print('Using {} dataloader workers every process'.format(nw))
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
num_workers=nw,
collate_fn=train_dataset.collate_fn)
View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Pass the correct --num_classes matching the dataset (e.g. 64 for Mini-ImageNet train split).
- Regenerate class_indices.json and the CSVs after any dataset change.
- Print len(train_dataset.labels) before launching to confirm the expected count.
Example fix
// before python train_single_gpu.py --num_classes=5 // after python train_single_gpu.py --num_classes=64
Defensive patterns
Strategy: validation
Validate before calling
import json
with open(json_path) as f:
class_indices = json.load(f)
assert args.num_classes == len(class_indices), \
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(f"Fix: pass --num_classes={len(train_dataset.labels)}")
raise Prevention
- Read num_classes from class_indices.json programmatically
- Re-run the dataset parse/split script after data changes
- Log the dataset class count at startup
- Avoid hard-coded values like 1000 copied from ImageNet scripts
When it happens
Trigger: Running train_single_gpu.py with --num_classes N where N != number of classes in class_indices.json built from new_train.csv.
Common situations: Switching datasets without updating the CLI argument, stale class_indices.json from an older dataset, forgetting that Mini-ImageNet has 64 train classes instead of 1000.
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/613209bb358947b1.
Report an issue: GitHub.