Ejemplo n.º 1
0
    def initialize_new(self, scp_list, word_mlf, dict, remove_previous=False):
        System.set_log_dir(self.name)
        if remove_previous:
            for f in glob.iglob(System.get_log_dir() + '/*'):
                os.remove(f)

        if not remove_previous and (
                os.path.exists(self.train_files_dir) or
                len(glob.glob(self.model_dir + '/' + self.name + '.*')) > 0):
            raise ExistingFilesException

        if os.path.exists(self.train_files_dir):
            shutil.rmtree(self.train_files_dir)
        for f in glob.iglob(self.model_dir + '/' + self.name + '.*'):
            os.remove(f)
        os.mkdir(self.train_files_dir)

        # handle dictionary
        dic = HTK_dictionary()
        if isinstance(dict, basestring):
            dic.read_dict(dict)
        elif all(isinstance(d, basestring) for d in dict):
            for d in dict:
                dic.read_dict(d)
        else:
            raise TypeError
        dic.write_dict(self.training_dict)

        self.phones = dic.get_phones()

        # handle transcription
        trans = HTK_transcription()
        #        if isinstance(word_mlf,basestring):
        #            trans.read_mlf(word_mlf, HTK_transcription.WORD)
        #        elif all(isinstance(w,basestring) for w in word_mlf):
        #            for w in word_mlf:
        #                trans.read_mlf(w, HTK_transcription.WORD)
        #        else:
        #            raise TypeError
        word_mlf = word_mlf.strip().split(',')
        for w in word_mlf:
            trans.read_mlf(w, HTK_transcription.WORD)

        self.id = 1

        phones_list = self._get_model_name_id() + '.hmmlist'
        with open(phones_list, 'w') as phones_desc:
            for p in self.phones:
                print(p, file=phones_desc)

        # handle scp files
        scp_list = scp_list.strip().split(',')
        #        if isinstance(scp_list,basestring):
        #            scp_list = [scp_list]

        real_trans = HTK_transcription()
        real_trans.transcriptions[real_trans.WORD] = {}

        with open(self.training_scp, 'w') as scp_desc:
            for scp in scp_list:
                for file in open(scp):
                    id = os.path.splitext(os.path.basename(file.strip()))[0]
                    if not file.startswith('/'):
                        file = os.path.join(os.path.dirname(scp), file.strip())

                    ok = True

                    for word in trans.transcriptions[
                            HTK_transcription.WORD][id]:
                        if not dic.word_in_dict(word):
                            print("%s skipped, because has missing word %s" %
                                  (file.strip(), word))
                            ok = False
                            break
                    if ok:
                        print(file.strip(), file=scp_desc)
                        real_trans.transcriptions[real_trans.WORD][
                            id] = trans.transcriptions[real_trans.WORD][id]

        real_trans.write_mlf(self.training_word_mlf,
                             target=HTK_transcription.WORD)
        self.expand_word_transcription()
Ejemplo n.º 2
0
        shuffle(t_files)

        #        if options.num_adaptation_files > 0:
        #            t_files = t_files[:options.num_adaptation_files]

        for t in t_files:
            f = splitext(basename(t))[0]
            mlf.transcriptions[HTK_transcription.WORD][
                "%s_%s" %
                (sp, f)] = mlf.transcriptions[HTK_transcription.WORD][f]

            new_f = join(file_dir, "%s_%s" % (sp, basename(t)))
            symlink(t, new_f)
            print >> transform_desc, new_f

mlf.write_mlf(transform_mlf, target=HTK_transcription.WORD)

recognizer.add_adaptation(transform_scp,
                          transform_mlf,
                          num_speaker_chars=options.transform_speaker_chars)
recognizer.add_adaptation(transform_scp,
                          transform_mlf,
                          num_speaker_chars=options.transform_speaker_chars,
                          num_nodes=options.tree_size)

recognizer.recognize(None, 'neighbour_transform')

if options.dostack:
    recognizer.add_adaptation(scp,
                              recognizer.name + '.neighbour_transform.mlf',
                              num_speaker_chars=options.eval_speaker_chars,
        for n in neighbors:
            t_files.extend(transform_files[n])
        shuffle(t_files)

#        if options.num_adaptation_files > 0:
#            t_files = t_files[:options.num_adaptation_files]

        for t in t_files:
            f = splitext(basename(t))[0]
            mlf.transcriptions[HTK_transcription.WORD]["%s_%s"%(sp,f)] = mlf.transcriptions[HTK_transcription.WORD][f]

            new_f = join(file_dir,"%s_%s"%(sp,basename(t)))
            symlink(t,new_f)
            print >> transform_desc, new_f

mlf.write_mlf(transform_mlf,target=HTK_transcription.WORD)

    

recognizer.add_adaptation(transform_scp,transform_mlf,num_speaker_chars=options.transform_speaker_chars)
recognizer.add_adaptation(transform_scp,transform_mlf,num_speaker_chars=options.transform_speaker_chars,num_nodes=options.tree_size)


recognizer.recognize(None,'neighbour_transform')

if options.dostack:
    recognizer.add_adaptation(scp,recognizer.name+'.neighbour_transform.mlf',num_speaker_chars=options.eval_speaker_chars,files_per_speaker=options.num_adaptation_files)
    recognizer.add_adaptation(scp,recognizer.name+'.neighbour_transform.mlf',num_speaker_chars=options.eval_speaker_chars,num_nodes=64,files_per_speaker=options.num_adaptation_files)
    #
    recognizer.recognize(None,'neighbour_transform_stack.%d'%options.num_adaptation_files)