When training your own dataset, you often report errors:
tensorflow2.3 InvalidArgumentError: jpeg::Uncompress failed [[{{node decode_image/DecodeImage}}]] [Op:IteratorGetNext]
Solution:
check whether the picture is damaged before training:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import os
num_skipped = 0
for folder_name in ("Fruit apples", "Fruit bananas", "Fruit oranges"):
folder_path = os.path.join(".\data\image_data", folder_name)
for fname in os.listdir(folder_path):
fpath = os.path.join(folder_path, fname)
try:
fobj = open(fpath, mode="rb")
is_jfif = tf.compat.as_bytes("JFIF") in fobj.peek(10)
finally:
fobj.close()
if not is_jfif:
num_skipped += 1
# Delete corrupted image
os.remove(fpath)
print("Deleted %d images" % num_skipped)
Delete the damaged picture and train again to solve the problem
if an error is prompted again, use:
# Determine if an image is corrupt from local
def is_valid_image(path):
'''
Check if the file is corrupt
'''
try:
bValid = True
fileObj = open(path, 'rb') # Open in binary form
buf = fileObj.read()
if not buf.startswith(b'\xff\xd8'): # whether to start with \xff\xd8
bValid = False
elif buf[6:10] in (b'JFIF', b'Exif'): # ASCII code of "JFIF"
if not buf.rstrip(b'\0\r\n').endswith(b'\xff\xd9'): # whether it ends with \xff\xd9
bValid = False
else:
try:
Image.open(fileObj).verify()
except Exception as e:
bValid = False
print(e)
except Exception as e:
return False
return bValid
num_skipped = 0
for folder_name in ("fruit-apple", "fruit-banana", "fruit-orange"):
#os.path.join() joins two or more pathname components
folder_path = os.path.join(". \data\image_data", folder_name)
# os.listdir(path) lists the subdirectories under this directory
for fname in os.listdir(folder_path):
fpath = os.path.join(folder_path, fname)
flag1 = is_valid_image(fpath)
if not flag1:
print(flag1)
print(fpath)#Print the path and name of the error file
Adjust the error file and train again to solve the problem.