Last active
July 9, 2022 03:32
-
-
Save arnaldog12/16efc663c869b35e2479bd607d56c1da to your computer and use it in GitHub Desktop.
BalancedDataGenerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ishraq-dagamseh , it's because there is another class called
Sequence
inkeras.utils.data_utils
. To avoid this error, you change the lineto