使用TF ImageGenerator训练模型时出现UnidentifiedImageError [英] UnidentifiedImageError when training a model using TF ImageGenerator

查看:79
本文介绍了使用TF ImageGenerator训练模型时出现UnidentifiedImageError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行21250个图像的二进制分类器(这两个类总计).我的批量大小为425,步长为50.

I'm running a binary classifier of 21250 images (total for the 2 classes). My batch size is at 425 with steps at 50.

运行模型时,出现以下错误:

When I run the model I get the following error:

UnknownError: 2 root error(s) found.
  (0) Unknown:  UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000019FA183C8B0>
Traceback (most recent call last): 

据我了解,此处的图像可能已损坏或由于其他原因而无法读取该图像.有没有一种方法可以让培训师跳过无法识别的图像?

From what I understand, the image here could be corrupt or the image cannot be read for other reasons. Is there a way to get the trainer to skip images that are not identifiable?

请让我知道在我的代码/数据中要考虑的其他建议,以解决此问题.

Please do let me know other suggestions to consider in my code/ data to fix this problem.

推荐答案

由于图像为'NoneType',因此可能会发生此错误,尽管您可能会看到图像为 .jpeg或.png 在预处理图像期间,图像实际上由于某种原因被损坏.在大型数据集上,我已经多次遇到这个问题.

This error could be happening due to the images being of 'NoneType' where although you may see them being of .jpeg or .png the image was actually corrupted somehow during preprocessing the image. On large datasets I have faced this issue numerous times.

您可以做的是删除这些图像,因为我认为无法将它们转换为所需的格式.

What you can do is remove these images as I do not think it is possible to convert them to the desired format.

请记住在删除图像之前保留整个数据集的副本,以防万一代码中出现任何问题.

Bear in mind to keep a copy of the entire dataset before removing the images just in case anything goes wrong in the code.

我不知道您的图像文件夹的结构,所以我仅向您展示如何使用已找到图像的完整路径来进行操作.这是您必须要做的,但是可以轻松地通过 os.walk()完成,然后使用 image_path = os.path.join(root,文件),以获取图像的完整路径.这样做时,请遍历所有图像,以将其应用于所有图像.

I do not know the structure of your image folder so I will just show you how to do it with the full path to the image already found. This is something you would have to do but can be easily done with os.walk() and then combining the returned values of root and the files with image_path = os.path.join(root, files) to get the full path to an image. Loop over all the images as you do this to apply it to all the images.

import cv2
import imghdr
import os


image = cv2.imread(image_path)
img_type = imghdr.what(image_path)
if img_type != "jpeg":
    os.remove(image_path)

这篇关于使用TF ImageGenerator训练模型时出现UnidentifiedImageError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆