예제 #1
0
    def TestModel(self,
                  datapath='',
                  str_dataset='dev',
                  data_count=32,
                  out_report=False,
                  show_ratio=True,
                  io_step_print=10,
                  io_step_file=10):
        '''
        测试检验模型效果
        
        io_step_print
            为了减少测试时标准输出的io开销,可以通过调整这个参数来实现
        
        io_step_file
            为了减少测试时文件读写的io开销,可以通过调整这个参数来实现
        
        '''

        data = DataSpeech(self.datapath, str_dataset)
        #data.LoadDataList(str_dataset)
        num_data = data.GetDataNum()  # 获取数据的数量
        if (
                data_count <= 0 or data_count > num_data
        ):  # 当data_count为小于等于0或者大于测试数据量的值时,则使用全部数据来测试(正常使用的是32或者自己传递进来的需要测试的个数)
            data_count = num_data

        try:
            ran_num = random.randint(0, num_data - 1)  # 获取一个随机数(0-num_data)

            words_num = 0  #总得单次数量
            word_error_num = 0  #错误的单次数量

            nowtime = time.strftime('%Y%m%d_%H%M%S', time.localtime(
                time.time()))  # '20190924_103104'  就是时间日期的一个字符串
            if (out_report == True):  #如果说输出开关打开的话
                txt_obj = open('Test_Report_' + str_dataset + '_' + nowtime +
                               '.txt',
                               'w',
                               encoding='UTF-8')  # 打开文件并读入

            txt = '测试报告\n模型编号 ' + ModelName + '\n\n'
            for i in range(data_count):
                data_input, data_labels = data.GetData(
                    (ran_num + i) % num_data)  # 从随机数开始连续向后取一定数量数据

                # 数据格式出错处理 开始
                # 当输入的wav文件长度过长时自动跳过该文件,转而使用下一个wav文件来运行
                num_bias = 0
                while (data_input.shape[0] > self.AUDIO_LENGTH):
                    print('*[Error]', 'wave data lenghth of num',
                          (ran_num + i) % num_data, 'is too long.',
                          '\n A Exception raise when test Speech Model.')
                    num_bias += 1
                    data_input, data_labels = data.GetData(
                        (ran_num + i + num_bias) %
                        num_data)  # 从随机数开始连续向后取一定数量数据
                # 数据格式出错处理 结束

                pre = self.Predict(data_input,
                                   data_input.shape[0] // 8)  #预测的结果

                words_n = data_labels.shape[0]  # 获取每个句子的字数
                words_num += words_n  # 把句子的总字数加上
                edit_distance = GetEditDistance(
                    data_labels, pre)  # 获取编辑距离(预测的结果跟真实的结果之间的编辑距离(整数))
                if (edit_distance <= words_n):  # 当编辑距离小于等于句子字数时
                    word_error_num += edit_distance  # 使用编辑距离作为错误字数
                else:  # 否则肯定是增加了一堆乱七八糟的奇奇怪怪的字
                    word_error_num += words_n  # 就直接加句子本来的总字数就好了(错误率就是100%)

                if ((i % io_step_print == 0 or i == data_count - 1)
                        and show_ratio == True):
                    #print('测试进度:',i,'/',data_count)
                    print('Test Count: ', i, '/', data_count)

                if (out_report == True):
                    if (i % io_step_file == 0 or i == data_count - 1):
                        txt_obj.write(txt)
                        txt = ''

                    txt += str(i) + '\n'
                    txt += 'True:\t' + str(data_labels) + '\n'
                    txt += 'Pred:\t' + str(pre) + '\n'
                    txt += '\n'

            #print('*[测试结果] 语音识别 ' + str_dataset + ' 集语音单字错误率:', word_error_num / words_num * 100, '%')
            print(
                '*[Test Result] Speech Recognition ' + str_dataset +
                ' set word error ratio: ', word_error_num / words_num * 100,
                '%')
            if (out_report == True):  #将错误率进行写入
                txt += '*[测试结果] 语音识别 ' + str_dataset + ' 集语音单字错误率: ' + str(
                    word_error_num / words_num * 100) + ' %'
                txt_obj.write(txt)
                txt = ''
                txt_obj.close()

        except StopIteration:
            print('[Error] Model Test Error. please check data format.')
예제 #2
0
    def TestModel(self,
                  datapath='',
                  str_dataset='dev',
                  data_count=32,
                  out_report=False,
                  show_ratio=True):
        '''
		测试检验模型效果
		'''
        data = DataSpeech(self.datapath, str_dataset)
        #data.LoadDataList(str_dataset)
        num_data = data.GetDataNum()  # 获取数据的数量
        if (data_count <= 0 or data_count >
                num_data):  # 当data_count为小于等于0或者大于测试数据量的值时,则使用全部数据来测试
            data_count = num_data

        try:
            ran_num = random.randint(0, num_data - 1)  # 获取一个随机数

            words_num = 0
            word_error_num = 0

            nowtime = time.strftime('%Y%m%d_%H%M%S',
                                    time.localtime(time.time()))
            if (out_report == True):
                txt_obj = open('Test_Report_' + str_dataset + '_' + nowtime +
                               '.txt',
                               'w',
                               encoding='UTF-8')  # 打开文件并读入

            txt = ''
            for i in range(data_count):
                data_input, data_labels = data.GetData(
                    (ran_num + i) % num_data)  # 从随机数开始连续向后取一定数量数据

                # 数据格式出错处理 开始
                # 当输入的wav文件长度过长时自动跳过该文件,转而使用下一个wav文件来运行
                num_bias = 0
                while (data_input.shape[0] > self.AUDIO_LENGTH):
                    print('*[Error]', 'wave data lenghth of num',
                          (ran_num + i) % num_data, 'is too long.',
                          '\n A Exception raise when test Speech Model.')
                    num_bias += 1
                    data_input, data_labels = data.GetData(
                        (ran_num + i + num_bias) %
                        num_data)  # 从随机数开始连续向后取一定数量数据
                # 数据格式出错处理 结束

                pre = self.Predict(data_input, data_input.shape[0] // 8)

                words_n = data_labels.shape[0]  # 获取每个句子的字数
                words_num += words_n  # 把句子的总字数加上
                edit_distance = GetEditDistance(data_labels, pre)  # 获取编辑距离
                if (edit_distance <= words_n):  # 当编辑距离小于等于句子字数时
                    word_error_num += edit_distance  # 使用编辑距离作为错误字数
                else:  # 否则肯定是增加了一堆乱七八糟的奇奇怪怪的字
                    word_error_num += words_n  # 就直接加句子本来的总字数就好了

                if (i % 10 == 0 and show_ratio == True):
                    print('Test Count: ', i, '/', data_count)

                txt = ''
                if (out_report == True):
                    txt += str(i) + '\n'
                    txt += 'True:\t' + str(data_labels) + '\n'
                    txt += 'Pred:\t' + str(pre) + '\n'
                    txt += '\n'
                    txt_obj.write(txt)

            #print('*[测试结果] 语音识别 ' + str_dataset + ' 集语音单字错误率:', word_error_num / words_num * 100, '%')
            print(
                '*[Test Result] Speech Recognition ' + str_dataset +
                ' set word error ratio: ', word_error_num / words_num * 100,
                '%')
            if (out_report == True):
                txt = '*[测试结果] 语音识别 ' + str_dataset + ' 集语音单字错误率: ' + str(
                    word_error_num / words_num * 100) + ' %'
                txt_obj.write(txt)
                txt_obj.close()

        except StopIteration:
            print('[Error] Model Test Error. please check data format.')