hello, Parth Dedhia. Thanks for making the blog and this repo.
i'm working on my thesis, i need the Triplet Generator so that i come across to your blog and this repo, but i got some issues. can you help me, please?
def align_image(img):
return alignment.align(96, img, alignment.getLargestFaceBoundingBox(img),
landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE)
class DataGenerator(tf.keras.utils.Sequence):
def __init__(self, dataset_path, batch_size=32, shuffle=True):
self.dataset = self.__curate_dataset(dataset_path)
self.dataset_path = dataset_path
self.shuffle = shuffle
self.batch_size = batch_size
self.no_of_people = len(list(self.dataset.keys()))
self.on_epoch_end()
def __getitem__(self, index):
people = list(self.dataset.keys())[index * self.batch_size: (index + 1) * self.batch_size]
P = []
A = []
N = []
for person in people:
anchor_index = random.randint(0, len(self.dataset[person])-1)
a = self.__get_image(person, anchor_index)
positive_index = random.randint(0, len(self.dataset[person])-1)
while positive_index == anchor_index:
positive_index = random.randint(0, len(self.dataset[person])-1)
p = self.__get_image(person, positive_index)
negative_person_index = random.randint(0, self.no_of_people - 1)
negative_person = list(self.dataset.keys())[negative_person_index]
while negative_person == person:
negative_person_index = random.randint(0, self.no_of_people - 1)
negative_person = list(self.dataset.keys())[negative_person_index]
negative_index = random.randint(0, len(self.dataset[negative_person])-1)
n = self.__get_image(negative_person, negative_index)
P.append(p)
A.append(a)
N.append(n)
A = np.asarray(A)
N = np.asarray(N)
P = np.asarray(P)
return [A, P, N]
def __len__(self):
return self.no_of_people // self.batch_size
def __curate_dataset(self, dataset_path):
with open(os.path.join(dataset_path, 'list.txt'), 'r') as f:
dataset = {}
image_list = f.read().splitlines()
for image in image_list:
folder_name, file_name = image.split('/')
if folder_name in dataset.keys():
dataset[folder_name].append(file_name)
else:
dataset[folder_name] = [file_name]
return dataset
def on_epoch_end(self):
if self.shuffle:
keys = list(self.dataset.keys())
random.shuffle(keys)
dataset_ = {}
for key in keys:
dataset_[key] = self.dataset[key]
self.dataset = dataset_
def __get_image(self, person, index):
img = cv2.imread(os.path.join(self.dataset_path, os.path.join('images/' + person, self.dataset[person][index])))
img = align_image(img) #my own custom function
img = np.asarray(img, dtype=np.float64)
img = preprocess_input(img)
return img
data_generator = DataGenerator(dataset_path='./dataset/')
when i was passing the data_generator to model.fit_generator().
nn4_small2_train.compile(loss=None, optimizer='adam')
nn4_small2_train.fit_generator(data_generator, epochs=5, steps_per_epoch=20)
i don't want to loop the data generator like the way you did. i want to pass through the model.fit_generator()
immediately