Skip to content

Instantly share code, notes, and snippets.

@arnaldog12
Last active July 9, 2022 03:32
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arnaldog12/16efc663c869b35e2479bd607d56c1da to your computer and use it in GitHub Desktop.
Save arnaldog12/16efc663c869b35e2479bd607d56c1da to your computer and use it in GitHub Desktop.
BalancedDataGenerator
from keras.utils.data_utils import Sequence
from imblearn.over_sampling import RandomOverSampler
from imblearn.keras import balanced_batch_generator
class BalancedDataGenerator(Sequence):
"""ImageDataGenerator + RandomOversampling"""
def __init__(self, x, y, datagen, batch_size=32):
self.datagen = datagen
self.batch_size = min(batch_size, x.shape[0])
datagen.fit(x)
self.gen, self.steps_per_epoch = balanced_batch_generator(x.reshape(x.shape[0], -1), y, sampler=RandomOverSampler(), batch_size=self.batch_size, keep_sparse=True)
self._shape = (self.steps_per_epoch * batch_size, *x.shape[1:])
def __len__(self):
return self.steps_per_epoch
def __getitem__(self, idx):
x_batch, y_batch = self.gen.__next__()
x_batch = x_batch.reshape(-1, *self._shape[1:])
return self.datagen.flow(x_batch, y_batch, batch_size=self.batch_size).next()
@vaibhav-bisht-98
Copy link

I'm getting this error on running the script. Can someone help me?
BalancedDat

@WillArevalo
Copy link

@vaibhav-bisht-98 I have the same issue

@ishraq-dagamseh
Copy link

ishraq-dagamseh commented Dec 21, 2021

Hello, I try this code with medical images but its return error

@ishraq-dagamseh
Copy link

This error appears after fitting the data
Uploading Screenshot_٢٠٢١١٢٢١_١١٢٦٢١.jpg…

@arnaldog12
Copy link
Author

@ishraq-dagamseh , it's because there is another class called Sequence in keras.utils.data_utils. To avoid this error, you change the line

from keras.utils.data_utils import Sequence

to

from keras.utils.data_utils import Sequence as KerasSequence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment