tensorflow2.3 InvalidArgumentError: jpeg::Uncompress failed.报错解决办法

训练自己数据集的时候经常报错:
tensorflow2.3 InvalidArgumentError: jpeg::Uncompress failed
[[{{node decode_image/DecodeImage}}]] [Op:IteratorGetNext]

解决办法:
训练前检测一下图片是否损坏:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import os


num_skipped = 0
for folder_name in ("水果苹果", "水果香蕉","水果橘子"):
    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)

损坏图片删除,再次训练问题解决。
如果再次提示错误,使用:

# 从本地判断图片是否损坏
def is_valid_image(path):
    '''
    检查文件是否损坏
    '''
    try:
        bValid = True
        fileObj = open(path, 'rb')  # 以二进制形式打开
        buf = fileObj.read()
        if not buf.startswith(b'\xff\xd8'):  # 是否以\xff\xd8开头
            bValid = False
        elif buf[6:10] in (b'JFIF', b'Exif'):  # “JFIF”的ASCII码
            if not buf.rstrip(b'\0\r\n').endswith(b'\xff\xd9'):  # 是否以\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 ("水果苹果", "水果香蕉","水果橘子"):
    #os.path.join()连接两个或更多的路径名组件
    folder_path = os.path.join(".\data\image_data", folder_name)
    #os.listdir(path)列出该目录下的子目录
    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)#打印错误文件路径及名称
 

调整错误文件,再次训练问题解决。

你可能感兴趣的:(tensorflow,python,tensorflow,深度学习,人工智能)