ValueError: Image format not JPEG

I am trying to train a custom object detection model using TF Lite Model Maker by referring the Train a salad detector with TensorFlow Lite Model Maker notebook

The dataset can be loaded with

dataloader = object_detector.DataLoader.from_pascal_voc(‘cartoon-detection/images’, ‘cartoon-detection/annotations’, [‘doraemon’, ‘mcqueen’, ‘mickey’, ‘mrbean’, ‘scooby’])

However, as I run the cell, I see an error that says, ValueError: Image format not JPEG despite the images being of the said format. Here’s a snapshot of the same.

I have tried using both the extensions - .jpg and .jpeg but nothing seems to work. The error persists.

1 Like

Hi @Nitin_Tiwari

From the image you have posted I see also .xml files inside the folder. Maybe the DataLoader tries to use .jpg images and finds .xml files. Check again the folder or the link you have downloaded the dataset.

2 Likes

Hi @George_Soloupis,
I even tried using separate folders for images and annotations but the error persists. So, the suspicion of the DataLoader finding the .xml files instead of .jpg is ruled out too. With respect to the dataset, I manually scraped images from online sources and annotated them using LabelImg in PascalVOC format. Still wondering what’s going wrong.

2 Likes

Expecting an update on this.

Your error is coming from the autoML PIL.Image.open function call so strictly speaking it’s autoML PIL issue, but I guess since you’re trying to run it based on a tensorflow sample I guess it is worth raising here as well perhaps.
Have you tried writing a simple piece of code to loop through the images individually using PIL library within a try except and see if any particular image itself is causing an issue?

1 Like

@Keith_Hall, thanks for your response. I’d like to share that I have resolved the issue successfully. I explicitly and programmatically converted all the images in ‘RGB’ format using PIL library and it worked just fine afterwards. Thanks for the support.

@Nitin_Tiwari would you mind sharing your code to convert the files, please?

Sure, @fredy_rivas. Feel free to find the code in the repository linked below.

1 Like

If someone still has this problem, try to check the tag image_name.format the filename inside .xml file needs to match with the format, so convert the image only works if also your .xml has the filename tag with .jpg

I have the same issue but all my images are png. The extension in the xml is png too so I am screwed. I already labeled 2000 images :frowning:

Hi all. Sometimes images that have a .jpg extension are recognized by PIL as PNG format. To correct this, you need to isolate the images in your dataset that have this issue and convert them to RGB and then save them as JPEG. Here is the code for doing this:
import glob
from PIL import Image
files = glob.glob(“training_data_02/valid/*”)
len(files)
for file in files:
if “.jpg” in file:
image = PIL.Image.open(file)
if image.format not in [“JPG”, “JPEG”]:
print (file)
image.convert(‘RGB’).save(file, “JPEG”)

1 Like

Did you find any workaround for images in png format ?