Python:Keras正在执行“next(trainBatch)”,当我在train fold中有36张图片时,会出现被零除的错误

2024-09-30 23:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

更新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张图片

我想的是,它并没有到达我的data/train文件夹。你还应该做什么

你的帮助将对我大有帮助


Tags: 数据inselfdataindexlinetrainflow