You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于MJSynth数据集空白图像致文本识别网络训练中断的求助

Handling Blank Images in MJSynth Dataset for Text Recognition Training

Hey there, I’ve been in your exact shoes—dealing with random blank images in MJSynth crashing text recognition model training is super annoying! Unfortunately, there’s no official public list of these blank images floating around, but I’ve got two solid solutions to fix this: either generate your own list of blank/corrupted images, or build a check directly into your data loader to skip them on the fly.

Option 1: Generate a Blank Image List with a Simple Script

You can write a quick Python script to scan your entire MJSynth dataset and flag images that are completely white, black, or unreadable. Here’s a robust version using PIL:

import os
from PIL import Image

def detect_blank_images(dataset_root, output_file="mjsynth_blank_images.txt"):
    blank_image_paths = []
    total_scanned = 0

    # Walk through all subdirectories in MJSynth
    for root, _, files in os.walk(dataset_root):
        for filename in files:
            if not filename.lower().endswith(('.png', '.jpg', '.jpeg')):
                continue
            total_scanned += 1
            img_path = os.path.join(root, filename)
            
            try:
                with Image.open(img_path) as img:
                    # Convert to grayscale to simplify pixel value checks
                    gray_img = img.convert("L")
                    pixel_histogram = gray_img.histogram()
                    total_pixels = img.width * img.height

                    # Check if image is all black (all pixels 0) or all white (all pixels 255)
                    if pixel_histogram[0] == total_pixels or pixel_histogram[-1] == total_pixels:
                        blank_image_paths.append(img_path)
            except Exception as e:
                # Catch any unreadable images (corrupted files) and add them to the list too
                print(f"Failed to read {img_path}: {str(e)}")
                blank_image_paths.append(img_path)
    
    # Save the list to a text file
    with open(output_file, "w") as f:
        for path in blank_image_paths:
            f.write(f"{path}\n")
    
    print(f"Scanned {total_scanned} images. Found {len(blank_image_paths)} blank/corrupted images. List saved to {output_file}")

# Replace with your actual MJSynth dataset directory
detect_blank_images("/path/to/your/mjsynth/dataset")

Once you run this, you’ll get a text file with all the problematic paths—you can then manually delete these files or use the list to filter them out in your training code.

Option 2: Skip Blank Images Directly in Your Data Loader

If you don’t want to pre-scan the dataset, you can add a check right in your PyTorch (or TensorFlow) Dataset class to skip bad images when loading data:

import os
from torch.utils.data import Dataset
from PIL import Image

class FilteredMJSynthDataset(Dataset):
    def __init__(self, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform = transform
        # Collect all image paths first
        self.image_paths = []
        for root, _, files in os.walk(root_dir):
            for filename in files:
                if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
                    self.image_paths.append(os.path.join(root, filename))

    def __len__(self):
        return len(self.image_paths)

    def __getitem__(self, idx):
        while True:
            img_path = self.image_paths[idx]
            try:
                with Image.open(img_path) as img:
                    gray_img = img.convert("L")
                    total_pixels = img.width * img.height
                    hist = gray_img.histogram()
                    # Check if image is blank
                    if hist[0] == total_pixels or hist[-1] == total_pixels:
                        # Skip to next image
                        idx = (idx + 1) % len(self.image_paths)
                        continue
                    # Apply transforms if any
                    if self.transform:
                        img = self.transform(img)
                    # Return valid image and its label (adjust label logic to match your setup)
                    label = os.path.basename(img_path).split("_")[0]  # MJSynth filename format: [text]_[font]_[size].png
                    return img, label
            except Exception as e:
                print(f"Skipping corrupted image: {img_path}")
                idx = (idx + 1) % len(self.image_paths)
                continue

This way, your training loop will automatically skip any blank or unreadable images without crashing.

A quick note: MJSynth’s blank images are rare (usually less than 0.1% of the dataset), so either method will get you back to training quickly. I prefer pre-scanning first because it lets me clean up the dataset once, but the on-the-fly check works great if you’re short on time.

内容的提问来源于stack exchange,提问作者jd95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:52:50