Beispiel #1
0
 def GetData(self, n_start, n_amount=32, dataType='same'):
     assert (n_amount % CLASS_NUM == 0)
     if dataType == self.pathSameLabel:
         path = self.listSame[n_start][0]
         data_label = np.array([self.listSame[n_start][1]])
     elif dataType == self.pathDistinctLabel:
         path = self.listDistinct[n_start][0]
         data_label = np.array([self.listDistinct[n_start][1]])
     wavsignal, fs = read_wav_data(path)
     if self.feature_type in [
             'spec', 'Spec', 'SPEC', 'Spectrogram', 'SPECTROGRAM'
     ]:
         data_input = GetFrequencyFeatures(wavsignal,
                                           fs,
                                           self.feat_dimension,
                                           self.frame_length,
                                           shift=160)
     elif self.feature_type in ['mfcc', 'MFCC', 'Mfcc']:
         data_input = SimpleMfccFeatures(wavsignal, fs)
     else:
         print('Unknown feature type.')
         assert (0)
     data_input = data_input.reshape(data_input.shape[0],
                                     data_input.shape[1], 1)
     return data_input, data_label
def skstyleDataset(input_folder, check_num, stft_spec=False):

    assert (isinstance(check_num, int))
    feature = []
    target = []
    folders = os.listdir(input_folder)
    # place a self-check
    if len(folders) != check_num:
        print('error occurred when cheking subject numbers.')
        assert (0)
    else:
        for folder in folders:
            subf_bowel = os.path.join(input_folder, folder, 'bowels')
            files = os.listdir(subf_bowel)
            if len(files) in (200, 198):
                for file in files:
                    path = os.path.join(subf_bowel, file)
                    wavsignal, fs = read_wav_data(path)
                    if stft_spec == True:
                        data_input = GetFrequencyFeatures(wavsignal,
                                                          fs,
                                                          200,
                                                          400,
                                                          shift=160)
                    else:
                        data_input = SimpleMfccFeatures(wavsignal, fs)
                    data_input = np.reshape(data_input, [-1])
                    feature.append(data_input)
                    target.append(1)

            subf_non = os.path.join(input_folder, folder, 'non')
            files = os.listdir(subf_non)
            if len(files) in (200, 198):
                for file in files:
                    path = os.path.join(subf_non, file)
                    wavsignal, fs = read_wav_data(path)
                    if stft_spec == True:
                        data_input = GetFrequencyFeatures(wavsignal,
                                                          fs,
                                                          200,
                                                          400,
                                                          shift=160)
                    else:
                        data_input = SimpleMfccFeatures(wavsignal, fs)
                    data_input = np.reshape(data_input, [-1])
                    feature.append(data_input)
                    target.append(0)
            else:
                print('wrong wavfile number.')
                assert (0)

        return np.array(feature), np.array(target)
set_session(tf.Session(config=config))

#1. load image
three = []
results =[]
spath = os.path.join(os.getcwd(),'log&&materials','105660089020341_2018_11_13_08_20_07.wav')
three.append(spath)
spath = os.path.join(os.getcwd(),'log&&materials','20843728498872_2018_01_08_10_42_58.wav')
three.append(spath)
spath = os.path.join(os.getcwd(),'log&&materials','199481741554988_2018_10_20_00_04_25.wav')
three.append(spath)

# n1path = os.path.join(os.getcwd(),'log&&materials','noisy_sounds','199481741554988_2018_10_20_00_00_00.wav')
for spath in three:
    signal,fs = read_wav_data(spath)
    spec = GetFrequencyFeatures(signal,fs)
    img = spec.reshape(1,spec.shape[0], spec.shape[1], 1)
#2. load model
# mpath = os.path.join(os.getcwd(),'CNNevaluation','residual','resi=1','SPEC_Residual_8_epoch14.h5')
# mpath = os.path.join(os.getcwd(),'CNNdesign','spec+inception','10','SPEC_Inception_10_epoch4.h5')
    mpath = os.path.join(os.getcwd(),'CNNevaluation','regular','regi=0','spec_Regular_8_epoch12.h5')
    model = load_model(mpath,custom_objects={'FocalLoss': focal_loss,'focal_loss_fixed': focal_loss()})
    optimizer = optimizers.Adadelta()
    model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)])
#3. try to predictverbose=0
    print(np.argmax(model.predict(img)))
    print(model.predict(img,verbose=1))
# print(np.argmax(model.predict(img)[0]))
# print(model.layers[27])
# print(model.layers[28])
# print(model.layers[29])
Beispiel #4
0
 def GetData(self, n_start, n_amount=32, mode='balanced'):
     '''
     读取数据,返回神经网络输入值和输出值矩阵(可直接用于神经网络训练的那种)
     参数:
         n_start:从编号为n_start数据开始选取数据
         n_amount:选取的数据数量,默认为1,即一次一个wav文件
     返回:
         四个音频四个label,
     '''
     # 随机从四个文件夹中拿一条数据,判断是否大于1s,否就重拿
     assert (n_amount % CLASS_NUM == 0)
     category = (self.list_bowel1, self.list_non0)
     link = ('bowels', 'non')
     label = (1, 0)
     if self.feature_type in [
             'spec', 'Spec', 'SPEC', 'Spectrogram', 'SPECTROGRAM'
     ]:
         if mode == 'balanced':
             data = []
             labels = []
             for genre in range(CLASS_NUM):
                 for file in range(n_amount // CLASS_NUM):
                     filename = category[genre][(n_start + file) %
                                                self.DataNum[genre]]
                     # filename = category[genre][(n_start + file) % min(self.DataNum)]
                     path = self.common_path + link[
                         genre] + self.slash + filename
                     wavsignal, fs = read_wav_data(path)
                     # data_input = SimpleMfccFeatures(wavsignal, fs)
                     data_input = GetFrequencyFeatures(wavsignal,
                                                       fs,
                                                       self.feat_dimension,
                                                       self.frame_length,
                                                       shift=160)
                     data_input = self.shifting(data_input)
                     data_input = data_input.reshape(
                         data_input.shape[0], data_input.shape[1], 1)
                     data.append(data_input)
                     data_label = np.array([label[genre]])
                     labels.append(data_label)
             return data, labels
         if mode == 'non-repetitive':
             path = self.list_path[n_start][0]
             data_label = np.array([self.list_path[n_start][1]])
             wavsignal, fs = read_wav_data(path)
             data_input = GetFrequencyFeatures(wavsignal,
                                               fs,
                                               self.feat_dimension,
                                               self.frame_length,
                                               shift=160)
             data_input = data_input.reshape(data_input.shape[0],
                                             data_input.shape[1], 1)
             return data_input, data_label
     elif self.feature_type in ['mfcc', 'MFCC', 'Mfcc']:
         if mode == 'balanced':
             data = []
             labels = []
             for genre in range(CLASS_NUM):
                 for file in range(n_amount // CLASS_NUM):
                     filename = category[genre][(n_start + file) %
                                                self.DataNum[genre]]
                     # filename = category[genre][(n_start + file) % min(self.DataNum)]
                     path = self.common_path + link[
                         genre] + self.slash + filename
                     wavsignal, fs = read_wav_data(path)
                     data_input = SimpleMfccFeatures(wavsignal, fs)
                     data_label = np.array([label[genre]])
                     data_input = self.shifting(data_input)
                     data_input = data_input.reshape(
                         data_input.shape[0], data_input.shape[1], 1)
                     data.append(data_input)
                     labels.append(data_label)
             return data, labels
         if mode == 'non-repetitive':
             path = self.list_path[n_start][0]
             data_label = np.array([self.list_path[n_start][1]])
             wavsignal, fs = read_wav_data(path)
             data_input = SimpleMfccFeatures(wavsignal, fs)
             data_input = data_input.reshape(data_input.shape[0],
                                             data_input.shape[1], 1)
             return data_input, data_label
     else:
         print('Unknown feature type.')
         assert (0)