WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
image: {} isn't RGB mode.
Error message
image: {} isn't RGB mode. What it means
Identical guard to the efficientnetV2 dataset: MyDataSet.__getitem__ in the shufflenet script raises ValueError when a loaded PIL image's mode is not 'RGB'. Grayscale/palette/RGBA images are rejected because the model expects 3-channel inputs and the transform pipeline assumes RGB.
Source
Thrown at pytorch_classification/Test7_shufflenet/my_dataset.py:21
from torch.utils.data import Dataset
class MyDataSet(Dataset):
"""自定义数据集"""
def __init__(self, images_path: list, images_class: list, transform=None):
self.images_path = images_path
self.images_class = images_class
self.transform = transform
def __len__(self):
return len(self.images_path)
def __getitem__(self, item):
img = Image.open(self.images_path[item])
# RGB为彩色图片,L为灰度图片
if img.mode != 'RGB':
raise ValueError("image: {} isn't RGB mode.".format(self.images_path[item]))
label = self.images_class[item]
if self.transform is not None:
img = self.transform(img)
return img, label
@staticmethod
def collate_fn(batch):
# 官方实现的default_collate可以参考
# https://github.com/pytorch/pytorch/blob/67b7e751e6b5931a9f45274653f4f653a4e6cdf6/torch/utils/data/_utils/collate.py
images, labels = tuple(zip(*batch))
images = torch.stack(images, dim=0)
labels = torch.as_tensor(labels)
return images, labels
View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Convert offending files to RGB on disk with PIL and re-save them.
- Auto-normalize in code: replace the raise with img = img.convert('RGB') in __getitem__.
- Audit the dataset first: loop over self.images_path and report every file whose Image.open(p).mode != 'RGB'.
Example fix
// before
if img.mode != 'RGB':
raise ValueError("image: {} isn't RGB mode.".format(self.images_path[item]))
// after
if img.mode != 'RGB':
img = img.convert('RGB') Defensive patterns
Strategy: validation
Validate before calling
from PIL import Image
bad = [p for p in dataset.images_path if Image.open(p).mode != 'RGB']
if bad:
print("Non-RGB images:", bad)
raise SystemExit(1) Type guard
def is_rgb(path: str) -> bool:
return Image.open(path).mode == 'RGB' Try / catch
try:
img, label = next(iter(loader))
except ValueError as e:
if "isn't RGB mode" in str(e):
dataset.convert_all_to_rgb()
else:
raise Prevention
- Run a dataset pre-flight check for image modes before training.
- Convert all images to RGB at dataset preparation time.
- Exclude masks/labels from training image directories.
- Use img.convert('RGB') in the loader for robustness.
When it happens
Trigger: Iterating a DataLoader over MyDataSet where any file in the training/validation folder has PIL mode 'L', 'P', or 'RGBA' — e.g. grayscale PNGs, transparent PNGs, or palette GIFs inside the dataset directories.
Common situations: Mixed-format custom datasets (screenshots, downloaded web images with alpha); label/mask images accidentally placed in the training image folders; images converted by some editor to grayscale.
Related errors
- image: {} isn't RGB mode.
- 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/e974f2978e02965d.
Report an issue: GitHub.