Skip to content

Instantly share code, notes, and snippets.

@maskaravivek
Created June 22, 2020 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maskaravivek/7173fbc93554b3de58067faa249e5020 to your computer and use it in GitHub Desktop.
Save maskaravivek/7173fbc93554b3de58067faa249e5020 to your computer and use it in GitHub Desktop.
from tensorflow import keras
class My_Custom_Generator(keras.utils.Sequence):
def __init__(self, images, labels, batch_size):
self.images = images
self.labels = labels
self.batch_size = batch_size
def __len__(self) :
return (np.ceil(len(self.images) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx) :
batch_x = self.images[idx * self.batch_size : (idx+1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx+1) * self.batch_size]
train_image = []
train_label = []
for i in range(0, len(batch_x)):
img_path = batch_x[i]
label = batch_y[i]
# read method takes image path and label and returns corresponding matrices
image, label_matrix = read(img_path, label)
train_image.append(image)
train_label.append(label_matrix)
return np.array(train_image), np.array(train_label)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment