Py学习  »  Python

Python:Keras-执行“next(trainBatch)”,当我在train文件夹中有36张图片时出现除以零的错误

Hamza_HM • 4 年前 • 242 次点击  

更新2 问题终于解决了

ImageDataGenerstion().flow_from_directory() 期望您的数据在子目录中。例如在我的例子中:我的数据应该在 "data/train/cotton/" cotton目录和提供变量的路径应该如下所示 trainPath = "data/train" . 它希望您将每个类数据放在不同的目录中。 有关详细信息,请访问此问题的第一个答案 Question

更新 还没找到解决办法。以前我提供的数据路径是 data/train ,但实际上应该是 data/train/ ,所以我要按下面的问题更改它。

问题 我正在训练一个关于自定义数据的keras图像处理模型。我正在寻求帮助 youtube ,在第一个教程中,它只是加载图像,使用 ImageDataGenerator().flow_from_directory() 以及绘制指定标签的图像。 我的代码是

trainPath = "data/train/"
trainBatch = ImageDataGenerator().flow_from_director(directory=trainPath, target_size=(224,224), classes=["cotton"], batch_size=10)
imgs, lables = next(trainBatch)

当我执行最后一行时,它会给出错误

imgs, lables = next(trainBatch)
Traceback (most recent call last):
  File "C:\Users\Haier\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-4b8b727474cb>", line 1, in <module>
    imgs, lables = next(trainBatch)
  File "C:\Users\Haier\AppData\Local\Programs\Python\Python36\lib\site-packages\keras_preprocessing\image\iterator.py", line 100, in __next__
    return self.next(*args, **kwargs)
  File "C:\Users\Haier\AppData\Local\Programs\Python\Python36\lib\site-packages\keras_preprocessing\image\iterator.py", line 109, in next
    index_array = next(self.index_generator)
  File "C:\Users\Haier\AppData\Local\Programs\Python\Python36\lib\site-packages\keras_preprocessing\image\iterator.py", line 85, in _flow_index
    current_index = (self.batch_index * self.batch_size) % self.n
ZeroDivisionError: integer division or modulo by zero

我对堆栈溢出的错误有了一些了解 Question ,此处有人提问,指出当您清空数据文件夹时会出现此错误。但我的数据文件夹里有36张照片。

我所想的是,它并没有达到 数据/列车 文件夹。还应该怎么办?

你的帮助对我将是很大的帮助。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49720
 
242 次点击  
文章 [ 1 ]  |  最新文章 4 年前
vs97 Premendra Srivastava
Reply   •   1 楼
vs97 Premendra Srivastava    4 年前

尝试使用绝对路径,尝试使用 os 模块,并尝试打印trainBatch的形状。

按照上述步骤,您可以断定代码是否能够从文件夹中读取所有文件。