{"record":{"id":"24e5ee92367e75cf","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"image-format-not-jpeg-24e5ee","errorCode":null,"errorMessage":"Image '{}' format not JPEG","messagePattern":"Image '(.+?)' format not JPEG","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/ssd/my_dataset.py","lineNumber":52,"sourceCode":"        self.transforms = transforms\n\n    def __len__(self):\n        return len(self.xml_list)\n\n    def __getitem__(self, idx):\n        # read xml\n        xml_path = self.xml_list[idx]\n        with open(xml_path) as fid:\n            xml_str = fid.read()\n        xml = etree.fromstring(xml_str)\n        data = self.parse_xml_to_dict(xml)[\"annotation\"]\n        data_height = int(data[\"size\"][\"height\"])\n        data_width = int(data[\"size\"][\"width\"])\n        height_width = [data_height, data_width]\n        img_path = os.path.join(self.img_root, data[\"filename\"])\n        image = Image.open(img_path)\n        if image.format != \"JPEG\":\n            raise ValueError(\"Image '{}' format not JPEG\".format(img_path))\n\n        assert \"object\" in data, \"{} lack of object information.\".format(xml_path)\n        boxes = []\n        labels = []\n        iscrowd = []\n        for obj in data[\"object\"]:\n            # 将所有的gt box信息转换成相对值0-1之间\n            xmin = float(obj[\"bndbox\"][\"xmin\"]) / data_width\n            xmax = float(obj[\"bndbox\"][\"xmax\"]) / data_width\n            ymin = float(obj[\"bndbox\"][\"ymin\"]) / data_height\n            ymax = float(obj[\"bndbox\"][\"ymax\"]) / data_height\n\n            # 进一步检查数据，有的标注信息中可能有w或h为0的情况，这样的数据会导致计算回归loss为nan\n            if xmax <= xmin or ymax <= ymin:\n                print(\"Warning: in '{}' xml, there are some bbox w/h <=0\".format(xml_path))\n                continue\n                \n            boxes.append([xmin, ymin, xmax, ymax])","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/ssd/my_dataset.py#L34-L70","documentation":"SSD's VOCDataSet.__getitem__ opens each image with PIL and asserts the format is JPEG before parsing boxes. PNG, GIF, or other formats raise ValueError because the training pipeline (and its standard transformations) assume JPEG-encoded images typical of VOC datasets.","triggerScenarios":"An image referenced by an annotation XML in the dataset folder is not JPEG (e.g. a PNG or webp saved into the JPEGs folder); index.html or corrupt files mixed into the images directory.","commonSituations":"Downloading images from the web into the VOC JPEGImages folder; some VOC-style datasets (e.g. custom datasets converted from PNG) lacking JPEGs; manually adding screenshots or icons.","solutions":["Convert non-JPEG images to JPEG in place (PIL Image.open(...).convert('RGB').save(path, 'JPEG'))","Remove non-image or non-JPEG files from the images root (e.g. index.html artifacts)","Add a preprocessing script that scans Image.format and reports offending files before training"],"exampleFix":"// before\nimage = Image.open(img_path)\nif image.format != 'JPEG': raise ValueError(...)\n// after (preprocess)\nfor p in all_images:\n    im = Image.open(p)\n    if im.format != 'JPEG':\n        im.convert('RGB').save(p, 'JPEG')","handlingStrategy":"validation","validationCode":"from PIL import Image\nimport os\nbad = [f for f in os.listdir(img_root)\n       if Image.open(os.path.join(img_root, f)).format != 'JPEG']\nassert not bad, f'Non-JPEG images present: {bad[:5]}'","typeGuard":"def is_jpeg(path: str) -> bool:\n    with Image.open(path) as im:\n        return im.format == 'JPEG'","tryCatchPattern":"try:\n    image, target = dataset[idx]\nexcept ValueError as e:\n    print(f'Dataset contains non-JPEG image: {e}')","preventionTips":["Run a one-time dataset format audit before training","Only place VOC-style JPEGs in JPEGImages/","Convert web downloads to JPEG on ingest"],"tags":["image-format","dataset","pillow","preprocessing"],"backgroundTag":"non-jpeg-image-format","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}