what raises StopIteration in mine Keras Model.fit_generator

前端 未结 3 1505
我在风中等你
我在风中等你 2021-01-12 14:20

I have next code:

from sklearn.model_selection import train_test_split
from scipy.misc import imresize

def _chunks(l, n):
    \"\"\"Yield successive n-sized         


        
相关标签:
3条回答
  • 2021-01-12 14:54

    I found problem source. Firstly - mine dataset fully readed before fit end, so it raises

    Exception in thread Thread-50:
    Traceback (most recent call last):
    File "C:\Anaconda3\Lib\threading.py", line 916, in _bootstrap_inner
    self.run()
    File "C:\Anaconda3\Lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
    File "C:\Users\user\venv\machinelearning\lib\site-packages\keras\utils\data_utils.py", line 560, in data_generator_task
    generator_output = next(self._generator)
    StopIteration
    

    Exception handlers set stop_event and reraise exception

    But :

    def get(self):
        """Creates a generator to extract data from the queue.
    
        Skip the data if it is `None`.
    
        # Returns
            A generator
        """
        while self.is_running():
            if not self.queue.empty():
                inputs = self.queue.get()
                if inputs is not None:
                    yield inputs
            else:
                time.sleep(self.wait_time)
    

    So when stop event setted - it's can load data from queue

    So I limited max_queue_size to 1.

    0 讨论(0)
  • 2021-01-12 14:59

    A note about this issue in case others come to this page chasing it. The StopIteration bug is a known issue in keras that can be fixed, some of the time, by making sure that you set your batch size to an integer multiple of your number of samples. If this does not fix the issue, one thing that I have found is that having funky file formats that can't be read by the data generator will also sometimes cause a stopIteration error. To fix this, I run a script on my training folder that converts all of the images to a standard file type (jpg or png) prior to training. It looks something like this.

    import glob
    from PIL import Image
    import os
    d=1
    for sample in glob.glob(r'C:\Users\Jeremiah\Pictures\training\classLabel_unformatted\*'):
        im = Image.open(sample)
        im.save(r'C:\Users\Jeremiah\Pictures\training\classLabel_formatted\%s.png' %d)
        d=d+1
    

    I've found that running this script or something like it drastically reduces my frequency of these sorts of errors, especially when my training data is coming off of somewhere like google image search.

    0 讨论(0)
  • 2021-01-12 15:04

    I also met this problem, and I find a method is that you can insert "while True" block in data generator func. But I cannot get source. You can refer to my code following:

    while True:
         assert len(inputs) == len(targets)
         indices = np.arange(len(inputs))
         if shuffle:
            np.random.shuffle(indices)
         if batchsize > len(indices):
            sys.stderr.write('BatchSize out of index size')
         batchsize = len(indices)
         for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
             if shuffle:
                excerpt = indices[start_idx:start_idx + batchsize]
             else:
                excerpt = slice(start_idx, start_idx + batchsize)
             yield inputs[excerpt], targets[excerpt]
    
    0 讨论(0)
提交回复
热议问题