Пример #1
0
def do_inference(num_tests, concurrency=1):
    channel = implementations.insecure_channel(host, int(port))
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    coord = _Coordinator(num_tests, concurrency)

    for _ in range(num_tests):
        # dummy audio
        duration, sr, n_fft, win_length, hop_length, n_mels, max_db, min_db = 4, 16000, 512, 512, 128, 80, 35, -55
        filename = librosa.util.example_audio_file()
        wav = read_wav(filename, sr=sr, duration=duration)
        mel = wav2melspec_db(wav, sr, n_fft, win_length, hop_length, n_mels)
        mel = normalize_db(mel, max_db=max_db, min_db=min_db)
        mel = mel.astype(np.float32)
        mel = np.expand_dims(mel, axis=0)  # single batch
        n_timesteps = sr / hop_length * duration + 1

        # build request
        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'voice_vector'
        request.model_spec.signature_name = 'predict'
        request.inputs['x'].CopyFrom(
            tf.contrib.util.make_tensor_proto(mel,
                                              shape=[1, n_timesteps, n_mels]))

        coord.throttle()

        # send asynchronous response (recommended. use this.)
        result_future = stub.Predict.future(request, 10.0)  # timeout
        result_future.add_done_callback(_create_rpc_callback(coord))

        # send synchronous response (NOT recommended)
        # result = stub.Predict(request, 5.0)

    coord.wait_all_done()
Пример #2
0
def wav2melspec_db(wav,
                   sr,
                   n_fft,
                   win_length,
                   hop_length,
                   n_mels,
                   normalize=False,
                   max_db=None,
                   min_db=None,
                   time_first=True,
                   **kwargs):

    # Mel-spectrogram
    mel_spec = wav2melspec(wav,
                           sr,
                           n_fft,
                           win_length,
                           hop_length,
                           n_mels,
                           time_first=False,
                           **kwargs)

    # Decibel
    mel_db = librosa.amplitude_to_db(mel_spec)

    # Normalization
    mel_db = normalize_db(mel_db, max_db, min_db) if normalize else mel_db

    # Time-axis first
    if time_first:
        mel_db = mel_db.T  # (t, n_mels)

    return mel_db
Пример #3
0
 def get_data(self):
     while True:
         speaker_id = random.choice(list(self.speaker_dict.keys()))
         wav = self._load_random_wav(speaker_id)
         mel_spec = wav2melspec_db(wav, hp.signal.sr, hp.signal.n_fft,
                                   hp.signal.win_length,
                                   hp.signal.hop_length, hp.signal.n_mels)
         mel_spec = normalize_db(mel_spec,
                                 max_db=hp.signal.max_db,
                                 min_db=hp.signal.min_db)
         yield wav, mel_spec, speaker_id