在colab使用yolov5训练数据集时,出现以下错误,但不影响训练结果
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
解决方法:
在utils/dateset.py文件中加入以下代码:
if im.format.lower() in ('jpg', 'jpeg'):
with open(im_file, 'rb') as f:
f.seek(-2, 2)
assert f.read() == b'\xff\xd9', 'corrupted JPEG'
加入位置:在此代码段之后复制上述代码
def verify_image_label(args):
# Verify one image-label pair
im_file, lb_file, prefix = args
nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, corrupt
try:
# verify images
im = Image.open(im_file)
im.verify() # PIL verify
shape = exif_size(im) # image size
assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels'
assert im.format.lower() in img_formats, f'invalid image format {im.format}'
原理:添加数据检查以避免该错误的出现
错误原因:
yolov5原作者格伦乔赫解释其原因为:“这是由数据集中损坏或不完整的 jpeg 图像引起的低级 C 错误,在 python 中不容易检测到。”