def valid(self): chopper = Chopper() params = eval(chopper.params) params["upper"] = False chopper.name = "sliding_full" params["step"] = 32 params["scale"] = 128 chopper.params = str(params) chop = chopper.get() x_valid, y_valid = self.prepare_data(chop, self.validation_tracks) x_valid = remove_track_boundaries(x_valid) y_valid = remove_track_boundaries(y_valid) return x_valid, y_valid
def random(self): chopper = Chopper() # only one slices at a time is needed if chopper.params: chopparams = eval(chopper.params) else: chopparams = {} chopparams["slices"] = 1 chopper.params = str(chopparams) # get full or partial depending on chopper if "full" in chopper.name: chopper.name = "random_full" else: chopper.name = "random" chop = chopper.get() def generator(features, labels, batch_size): shape = self._calculate_shape(features[0].shape) # Create empty arrays to contain batch of features and labels batch_features = np.zeros((batch_size, *shape)) batch_labels = np.zeros((batch_size, *shape)) n_tracks = len(features) while True: for i in range(batch_size): # get random track t = random.randrange(n_tracks) x_track = features[t] y_track = labels[t] feature, label = chop(x_track, y_track) batch_features[i] = feature[0] batch_labels[i] = label[0] yield batch_features, batch_labels return generator
def process_spectrogram(self, spectrogram, channels=1): chopper = Chopper() chopper.name = "infer" chopper.params = "{'scale': %d}" % self.config.inference_slice chop = chopper.get(both=False) slices = chop(spectrogram) normalizer = Normalizer() normalize = normalizer.get(both=False) denormalize = normalizer.get_reverse() new_spectrogram = np.zeros((spectrogram.shape[0], 0, channels)) for slice in slices: # normalize slice, norm = normalize(slice) epanded_spectrogram = conversion.expand_to_grid( slice, self.peakDownscaleFactor, channels) epanded_spectrogram_with_batch_and_channels = \ epanded_spectrogram[np.newaxis, :, :] predicted_spectrogram_with_batch_and_channels = self.model.predict( epanded_spectrogram_with_batch_and_channels) # o /// o predicted_spectrogram = \ predicted_spectrogram_with_batch_and_channels[0, :, :, :] local_spectrogram = predicted_spectrogram[:slice.shape[0], :slice. shape[1], :] # de-normalize local_spectrogram = denormalize(local_spectrogram, norm) new_spectrogram = np.concatenate( (new_spectrogram, local_spectrogram), axis=1) console.log("Processed spectrogram") return spectrogram, new_spectrogram