def get_example(self): if self.mix: # Training phase of BC learning # Select two training examples while True: sound1, label1 = self.base[random.randint( 0, len(self.base) - 1)] sound2, label2 = self.base[random.randint( 0, len(self.base) - 1)] if label1 != label2: break sound1 = self.preprocess(sound1) sound2 = self.preprocess(sound2) # Mix two examples r = np.array(random.random()) sound = U.mix(sound1, sound2, r, self.opt.fs).astype(np.float32) eye = np.eye(self.opt.nClasses) label = (eye[label1] * r + eye[label2] * (1 - r)).astype( np.float32) else: # Training phase of standard learning or testing phase sound, label = self.base[i] sound = self.preprocess(sound).astype(np.float32) label = np.array(label, dtype=np.int32) if self.train and self.opt.strongAugment: sound = U.random_gain(6)(sound).astype(np.float32) return sound, label
def _generate_sample(self, fIdA, fIdB): ''' Takes 2 audio fileidx, preprocess them, mix them (if needed) ''' sound1, label1 = self.fId_to_sound(fIdA) sound2, label2 = self.fId_to_sound(fIdB) if self.n_classes == 10: lbl_indexes = {0:0, 1:1, 10:2, 11:3, 12:4, 20:5, 21:6, 38:7, 40:8, 41:9} label1 = lbl_indexes[label1] label2 = lbl_indexes[label2] if self.mix: # Mix two examples r = np.array(random.random()) sound = U.mix(sound1, sound2, r, self.audio_rate) # delays sound = sound.astype(np.float32) eye = np.eye(self.n_classes) label = (eye[label1] * r + eye[label2] * (1 - r)) label = label.astype(np.float32) else: sound, label = sound1, label1 if self.strongAugment: sound = U.random_gain(6)(sound).astype(np.float32) sound = sound[:, np.newaxis] return sound, label
def _data_gen(self): self.stop = False while not self.stop: idxs1 = list(self.df.index) idxs2 = list(self.df.index) if self.randomize: random.shuffle(idxs1) random.shuffle(idxs2) for idx1, idx2 in zip(idxs1, idxs2): fname1 = self.df.filename[idx1] fname2 = self.df.filename[idx2] sound1 = self.fname_to_wav(fname1) sound2 = self.fname_to_wav(fname2) sound1 = self.preprocess(sound1) sound2 = self.preprocess(sound2) label1 = self.df.target[idx1] label2 = self.df.target[idx2] if self.n_classes == 10: lbl_indexes = [0, 1, 10, 11, 12, 20, 21, 38, 40, 41] label1 = lbl_indexes.index(label1) label2 = lbl_indexes.index(label2) if self.mix: # Mix two examples r = np.array(random.random()) sound = U.mix(sound1, sound2, r, self.audio_rate) sound = sound.astype(np.float32) eye = np.eye(self.n_classes) label = (eye[label1] * r + eye[label2] * (1 - r)) label = label.astype(np.float32) else: sound, label = sound1, label1 if self.strongAugment: sound = U.random_gain(6)(sound).astype(np.float32) sound = sound[:, np.newaxis] yield sound, label