Generator “TypeError: 'generator' object is not an iterator”

前端 未结 3 583
-上瘾入骨i
-上瘾入骨i 2020-12-18 11:03

Due to the limitation of RAM memory, I followed these instructions and built a generator that draw small batch and pass them in the fit_generator of Keras. But Keras can\'t

3条回答
  •  Happy的楠姐
    2020-12-18 11:54

    I was having the same problem, I managed to solve this by defining a __next__ method:

    class My_Generator(Sequence):
        def __init__(self, image_filenames, labels, batch_size):
            self.image_filenames, self.labels = image_filenames, labels
            self.batch_size = batch_size
            self.n = 0
            self.max = self.__len__()
    
    
        def __len__(self):
            return np.ceil(len(self.image_filenames) / float(self.batch_size))
    
        def __getitem__(self, idx):
            batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]
            batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]
    
            return np.array([
            resize(imread(file_name), (200, 200))
               for file_name in batch_x]), np.array(batch_y)
    
        def __next__(self):
            if self.n >= self.max:
               self.n = 0
            result = self.__getitem__(self.n)
            self.n += 1
            return result
    

    note that I have declared two new variables in __init__ function.

提交回复
热议问题