What's new? |
keras 사용 노트 - 컴퓨터 |
1. 기존 keras 쓰는 방식과 Tensroflow 2.0 간 차이라는 것은 import keras.... 과 tf.keras 가 다른 다는 것이고 호환이 잘 안됨. tensorflow 2.0 예시는 모두 후자.
2. keras.Model 을 만들어서 컴파일 해서 가거나 Model 자체를 inheritance 해서 사용하는 경우가 있는데 tensorflow 2.0 예시에는 후자가 더 많음
3. TFRecord 1.0 쓸때는 iterator 도 설정해줬는데, keras 를 써서인지 2.0 이어서 인지 그냥 model.fit 에 dataset을 넣어주면 되고 뭔가 적용하고 싶다면 map 함수에 python 함수를 넣어주면 됨;; 내 경우는 feature 가 time, frequency 라서 2D 인데 tfrecord 넣을때는 1d 로 넣어야 하니까 padding 다하고 나서 그뒤에 reshape 용도로 사용함.
4. custom layer 는 lambda 로 만들기만 하면 문제없음.
5. 데이터는 TF Record 형태로 kaldi 의 ark 처럼 저장 해뒀다가 쓰고 cmvn 만 별도로 numpy to constant tensor 로 바꿔서 넣어주고 빼고 나누면 됨.
원래는 VAD 로 음성구간을 추려서 CMVN 을 한다고도 하는데 지금까지는 쿨하게 음원 전체의 feature mean, std 로 함. from_generator 인가 하는 걸로 만드는 것 보다 이게 훨씬 빠를 줄 알았으나.. GPU 사용률이 40 대 맴돌던게 60대까지 올라온 정도고 중간에 lack 걸리듯 느려질때가 더러 있음.. TF Record 만의 문제는 아닐듯. 모델은 Transformer.
2. keras.Model 을 만들어서 컴파일 해서 가거나 Model 자체를 inheritance 해서 사용하는 경우가 있는데 tensorflow 2.0 예시에는 후자가 더 많음
3. TFRecord 1.0 쓸때는 iterator 도 설정해줬는데, keras 를 써서인지 2.0 이어서 인지 그냥 model.fit 에 dataset을 넣어주면 되고 뭔가 적용하고 싶다면 map 함수에 python 함수를 넣어주면 됨;; 내 경우는 feature 가 time, frequency 라서 2D 인데 tfrecord 넣을때는 1d 로 넣어야 하니까 padding 다하고 나서 그뒤에 reshape 용도로 사용함.
4. custom layer 는 lambda 로 만들기만 하면 문제없음.
5. 데이터는 TF Record 형태로 kaldi 의 ark 처럼 저장 해뒀다가 쓰고 cmvn 만 별도로 numpy to constant tensor 로 바꿔서 넣어주고 빼고 나누면 됨.
원래는 VAD 로 음성구간을 추려서 CMVN 을 한다고도 하는데 지금까지는 쿨하게 음원 전체의 feature mean, std 로 함. from_generator 인가 하는 걸로 만드는 것 보다 이게 훨씬 빠를 줄 알았으나.. GPU 사용률이 40 대 맴돌던게 60대까지 올라온 정도고 중간에 lack 걸리듯 느려질때가 더러 있음.. TF Record 만의 문제는 아닐듯. 모델은 Transformer.
written time : 2019-12-03 23:13:00.0
feature extraction in python (fbank, mfcc) - 컴퓨터 |
@staticmethod
def get_fbanks(path_file, frame_size=0.025, frame_stride=0.01, n_filt=40, num_ceps=-1):
"""
I borrowed this feature extraction code from
https://www.kaggle.com/ybonde/log-spectrogram-and-mfcc-filter-bank-example
https://haythamfayek.com/2016/04/21/speech-processing-for-machine
-learning.html
:param n_filt: applying triangular n_filt filsters filters, typically 40 filters,
nfilt = 40 on a Mel-scale to the power spectrum
to extract frequency bands. The Mel- scale aims to mimic the non-linear human ear
perception of sound, by being more discriminative at lower frequencies and less
discriminative at higher frequencies. The final step to computing filter banks is
applying triangular filters, typically 40 filters, nfilt = 40 on a Mel-scale to
the power spectrum to extract frequency bands. The Mel-scale aims to mimic the
non-linear human ear perception of sound, by being more discriminative at lower
frequencies and less discriminative at higher frequencies.
:param path_file: path for a speech file
:param frame_size: the length for frame (default = 0.05, it means 50 ms)
:param frame_stride: the length for striding (default = 0.03, it means 30
ms)
:param num_ceps: mfcc dimension, if it is bigger than 0 then it returns mfcc
:return: fbank features
"""
sample_rate, signal = wavfile.read(path_file)
pre_emphasis = 0.97
emphasized_signal = np.append(signal[0],
signal[1:] - pre_emphasis * signal[:-1])
# params
# Convert from seconds to samples
frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate # Convert from seconds to samples
signal_length = len(emphasized_signal)
frame_length = int(round(frame_length))
frame_step = int(round(frame_step))
num_frames = int(np.ceil(float(np.abs(
signal_length - frame_length)) / frame_step)) # Make sure that we have at least 1 frame
pad_signal_length = num_frames * frame_step + frame_length
z = np.zeros((pad_signal_length - signal_length))
pad_signal = np.append(emphasized_signal,
z) # Pad Signal to make sure that all frames have equal number of samples without truncating any samples from the original signal
indices = np.tile(np.arange(0, frame_length),
&
def get_fbanks(path_file, frame_size=0.025, frame_stride=0.01, n_filt=40, num_ceps=-1):
"""
I borrowed this feature extraction code from
https://www.kaggle.com/ybonde/log-spectrogram-and-mfcc-filter-bank-example
https://haythamfayek.com/2016/04/21/speech-processing-for-machine
-learning.html
:param n_filt: applying triangular n_filt filsters filters, typically 40 filters,
nfilt = 40 on a Mel-scale to the power spectrum
to extract frequency bands. The Mel- scale aims to mimic the non-linear human ear
perception of sound, by being more discriminative at lower frequencies and less
discriminative at higher frequencies. The final step to computing filter banks is
applying triangular filters, typically 40 filters, nfilt = 40 on a Mel-scale to
the power spectrum to extract frequency bands. The Mel-scale aims to mimic the
non-linear human ear perception of sound, by being more discriminative at lower
frequencies and less discriminative at higher frequencies.
:param path_file: path for a speech file
:param frame_size: the length for frame (default = 0.05, it means 50 ms)
:param frame_stride: the length for striding (default = 0.03, it means 30
ms)
:param num_ceps: mfcc dimension, if it is bigger than 0 then it returns mfcc
:return: fbank features
"""
sample_rate, signal = wavfile.read(path_file)
pre_emphasis = 0.97
emphasized_signal = np.append(signal[0],
signal[1:] - pre_emphasis * signal[:-1])
# params
# Convert from seconds to samples
frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate # Convert from seconds to samples
signal_length = len(emphasized_signal)
frame_length = int(round(frame_length))
frame_step = int(round(frame_step))
num_frames = int(np.ceil(float(np.abs(
signal_length - frame_length)) / frame_step)) # Make sure that we have at least 1 frame
pad_signal_length = num_frames * frame_step + frame_length
z = np.zeros((pad_signal_length - signal_length))
pad_signal = np.append(emphasized_signal,
z) # Pad Signal to make sure that all frames have equal number of samples without truncating any samples from the original signal
indices = np.tile(np.arange(0, frame_length),
&