在使用keras的ImageDataGenerator時經常會出現圖片讀取錯誤的問題,對於這種已經封裝後的庫文件,報錯了,訓練時就終止了:
現在由兩種方法解決該問題:
1.自己檢查圖片,並刪除這些圖片
#遍歷圖片
try:
img = Image.open(imgpath)
img = img.convert('RGB')
except OSError:
os.remove(imgpath)
2.一個很滑稽的方法改庫文件
ImageDataGenerator讀取文件的函數是PIL庫裏的Image函數,可以在xx/python3.x/sitepackages/PIL/ImageFile.py文件中,將LOAD_TRUNCATED_IMAGES=False中的False改爲Ture,親測改了之後不會報錯,爲什麼要這樣做呢?在參加比賽時,用keras的predicet_generator預測的圖片中有部分圖片Image函數無法讀取,沒辦法,圖片又不能刪除,只能改這個庫文件了