0

UPDATE 2 Finally problem solved

ImageDataGenerstion().flow_from_directory() expect your data in a subdirectory. For example in my case: my data should be in "data/train/cotton/" cotton directory and my path providing variable should look like trainPath = "data/train". It expect you to put each class data in different respective directory. For more detail, visit first answer for this Question

UPDATE Didn't got solution yet. Previously I was providing data path as data/train, but actually it should be as data/train/, so I'm changing it as mentioned here in question below.

Question I'm training a keras image-processing model on custom data. I'm getting help from youtube , here in first tutorial it is just loading images, making batches using ImageDataGenerator().flow_from_directory() and plotting images with label specified. my code is

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

When I execute last line, it gives error

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

I got some understanding of error form stack overflow Question, here person asked question stated this error arise when you've empty data folder. But I've 36 images in my data folder.

What I thought is, it is not reaching or getting to my data/train folder. What else should have done?

Your assistance will be great help for me.

Hamza_HM
  • 651
  • 5
  • 5

1 Answers1

0

Try using absolute path, try printing content of folder using os module, and try printing the shape of trainBatch.

Following above steps you can conclude if code is able to read all files from folder.

vs97
  • 5,765
  • 3
  • 28
  • 41
  • I've executed code from first answer of this [Question](https://stackoverflow.com/questions/36774431/how-to-load-images-from-a-directory-on-the-computer-in-python), I wasn't able to see images until I changed my path from `"data/train"` to `"data/train/"`, after conversion code was showing all my images to me, but my primary code about which I've posted question is again giving me same error. I've also changed code as mentioned above. – Hamza_HM Jul 28 '19 at 09:08
  • Moreover, getting same results after providing even absolute path, And `trainBatch` is not providing any `shape` function. Instead `trainBatch.` is showing `image_shape` option which is actually a parameter of `ImageDataGenerator().flow_from_directory()`. – Hamza_HM Jul 28 '19 at 09:54