def train_dbn(X): dbn_network = UnsupervisedDBN( hidden_layers_structure=[337, 260, 170, 85, 45], activation_function='relu', learning_rate_rbm=5 * 1e-3, n_epochs_rbm=200) dbn_network.fit(X) dbn_network.save(DBN_NETWORK_FILENAME)
class DBNFeatureExtractor(object): def __init__(self, load=False): if load: self.bands = pickle.load(open("bands.pickle", "rb")) self.dbn = UnsupervisedDBN.load("dbn.pickle") self.prepca = pickle.load(open("prepca.pickle", "rb")) self.pca = pickle.load(open("pca.pickle", "rb")) else: self.dbn = UnsupervisedDBN(hidden_layers_structure=[50, 50, 50], batch_size=1024, learning_rate_rbm=0.001, n_epochs_rbm=5, contrastive_divergence_iter=2, activation_function='sigmoid') self.bands = [ {"start": 0, "end": 150, "pca_components":30, "energy_factor": 5}, {"start": 120, "end":300, "pca_components":70, "energy_factor": 1}, {"start": 250, "end":513, "pca_components":60, "energy_factor": 0.5} ] self.prepca = [PCA(n_components=band["pca_components"]) for band in self.bands] self.pca = PCA(n_components=16) def save(self): self.dbn.save("dbn.pickle") pickle.dump(self.bands, open("bands.pickle", "wb")) pickle.dump(self.prepca, open("prepca.pickle", "wb")) pickle.dump(self.pca, open("pca.pickle", "wb")) def normalize_energy(self, banded_frames): bands_energy = [band["energy_factor"] * np.sum(np.square(banded_frames[bandIndex]))/(band["end"] - band["start"]) for bandIndex, band in enumerate(self.bands)] return [bf/math.sqrt(be) for bf,be in zip(banded_frames, bands_energy)] def train(self, frames): banded_frames = [self.prepca[bandIndex].fit_transform(frames[:,band["start"]:band["end"]]) for bandIndex, band in enumerate(self.bands)] banded_frames = self.normalize_energy(banded_frames) frames = np.concatenate(banded_frames, axis=1) print(frames.shape) self.dbn.fit(frames) raw_features = self.dbn.transform(frames) self.pca.fit(raw_features) def extractFeatures(self, frames): banded_frames = [self.prepca[bandIndex].transform(frames[:,band["start"]:band["end"]]) for bandIndex, band in enumerate(self.bands)] banded_frames = self.normalize_energy(banded_frames) frames = np.concatenate(banded_frames, axis=1) raw_features = self.dbn.transform(frames) return self.pca.transform(raw_features)