Beispiel #1
0
    def test_interpolate_hmm(self):
        acmodel1 = AcModel()
        acmodel1.load_htk( os.path.join(HERE, "1-hmmdefs") )
        acmodel2 = AcModel()
        acmodel2.load_htk( os.path.join(HERE,"2-hmmdefs") )
        ahmm1=acmodel1.get_hmm('a')
        ahmm2=acmodel2.get_hmm('a')

        # transitions
        # (notice that the transition of 'a' in acmodel1 is in a macro.)
        a1transition = [macro["transition"] for macro in acmodel1.macros if macro.get('transition',None)][0]
        transitions = [ a1transition['definition'],ahmm2.definition['transition'] ]
        trs = self.lin.linear_transitions( transitions, [1,0])
        compare(trs,a1transition['definition'])
        self.assertTrue(compare(trs,a1transition['definition']))

        acmodel1.fill_hmms()

        transitions = [ ahmm1.definition['transition'],ahmm2.definition['transition'] ]
        trs = self.lin.linear_transitions( transitions, [1,0])
        self.assertTrue(compare(trs,ahmm1.definition['transition']))

        trs = self.lin.linear_transitions( transitions, [0,1])
        self.assertTrue(compare(trs,ahmm2.definition['transition']))

        # states
        # (notice that the state 2 of 'a' in acmodel1 is in a macro.)
        states = [ ahmm1.definition['states'],ahmm2.definition['states'] ]
        sts = self.lin.linear_states( states, [1,0])
        compare(sts,ahmm1.definition['states'],verbose=True)
        self.assertTrue(compare(sts,ahmm1.definition['states']))
        sts = self.lin.linear_states( states, [0,1])
        self.assertTrue(compare(sts,ahmm2.definition['states']))
Beispiel #2
0
    def test_monophones(self):
        acmodel1 = AcModel()
        acmodel1.load( os.path.join(MODEL_PATH,"models-fra") )

        acmodel2 = acmodel1.extract_monophones()
        acmodel2.save(os.path.join(HERE,'fra-mono'))
        self.assertTrue(  os.path.isfile( os.path.join(HERE, 'fra-mono','hmmdefs')) )
        self.assertTrue(  os.path.isfile( os.path.join(HERE, 'fra-mono','monophones.repl')) )
        self.assertFalse( os.path.isfile( os.path.join(HERE, 'fra-mono','tiedlist')) )
        os.remove( os.path.join(HERE, 'fra-mono','hmmdefs') )
        os.remove( os.path.join(HERE, 'fra-mono','monophones.repl') )
        os.rmdir( os.path.join(HERE,'fra-mono') )
        self.assertEqual( len(acmodel2.hmms), 38 )
Beispiel #3
0
    def test_merge(self):
        acmodel1 = AcModel()
        acmodel1.load_htk( os.path.join(HERE,"1-hmmdefs") )
        acmodel2 = AcModel()
        acmodel2.load_htk( os.path.join(HERE,"2-hmmdefs") )

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(acmodel1,gamma=0.5)
        self.assertEqual(interpolated, 2) # acopy, a
        self.assertEqual(appended, 1)     # i
        self.assertEqual(keeped, 1)       # e
        self.assertEqual(changed, 0)

        self.__test_states( acmodel2.get_hmm('a').definition['states'] )
        self.__test_transition( acmodel2.get_hmm('a').definition['transition'] )
Beispiel #4
0
    def _test_load_save(self, acmodel):

        # Save temporary the loaded model into a file
        tmpfile = self.hmmdefs+".copy"
        self.acmodel.save_htk( tmpfile )

        # Load the temporary file into a new model
        acmodelcopy = AcModel()
        acmodelcopy.load_htk(tmpfile)

        # Compare original and copy
        self.assertEqual(len(self.acmodel.hmms),len(acmodelcopy.hmms))
        for hmm,hmmcopy in zip(self.acmodel.hmms,acmodelcopy.hmms):
            self.assertEqual(hmm.name,hmmcopy.name)
            self.assertTrue(compare(hmm.definition,hmmcopy.definition))
        self.assertTrue(compare(self.acmodel.macros,acmodelcopy.macros))

        os.remove(tmpfile)
Beispiel #5
0
    def testMix(self):
        acmodel1 = AcModel()
        hmm1 = HMM()
        hmm1.create_proto( 25 )
        hmm1.name = "y"
        acmodel1.append_hmm( hmm1 )
        acmodel1.repllist.add("y","j")

        acmodel2 = AcModel()
        hmm2 = HMM()
        hmm2.create_proto( 25 )
        hmm2.name = "j"
        hmm3 = HMM()
        hmm3.create_proto( 25 )
        hmm3.name = "y"
        acmodel2.hmms.append( hmm2 )
        acmodel2.hmms.append( hmm3 )
        acmodel2.repllist.add("y","y")
        acmodel2.repllist.add("j","j")

        modelmixer = ModelMixer()
        modelmixer.set_models( acmodel1,acmodel2 )

        outputdir = os.path.join(MODELDIR, "models-test")
        modelmixer.mix( outputdir, gamma=1. )
        mixedh1 = AcModel()
        mixedh1.load( outputdir )
        shutil.rmtree( outputdir )
Beispiel #6
0
    def test_fill(self):
        acmodel1 = AcModel()
        acmodel1.load_htk( os.path.join(HERE,"1-hmmdefs") )
        ahmm1=acmodel1.get_hmm('a')
        a1transition = [macro["transition"] for macro in acmodel1.macros if macro.get('transition',None)][0]

        acmodel1.fill_hmms()
        self.assertTrue(compare(ahmm1.definition['transition'],a1transition['definition']))
Beispiel #7
0
 def testMixData(self):
     modelmixer = ModelMixer()
     modelmixer.load(self._modelL2dir, self._modelL1dir )
     outputdir = os.path.join(MODELDIR, "models-eng-fra")
     modelmixer.mix( outputdir, gamma=0.5 )
     self.assertTrue( os.path.exists( outputdir ) )
     acmodel1 = AcModel()
     acmodel1.load_htk( os.path.join(self._modelL2dir, "hmmdefs") )
     acmodel1 = acmodel1.extract_monophones()
     acmodel2 = AcModel()
     acmodel2.load_htk( os.path.join(os.path.join(MODELDIR, "models-eng-fra"), "hmmdefs") )
     shutil.rmtree( outputdir )
Beispiel #8
0
    def test_initializer_without_corpus(self):
        corpus  = TrainingCorpus()
        workdir = os.path.join(HERE,"working")

        os.mkdir( workdir )
        shutil.copy( os.path.join(HERE, "protos","vFloors"), workdir )

        initial = HTKModelInitializer( corpus, workdir )

        corpus.datatrainer.protodir = os.path.join(HERE, "protos")
        initial.create_model()

        hmm1 = HMM()
        hmm2 = HMM()

        hmm1.load( os.path.join( workdir, "@@.hmm") )
        hmm2.load( os.path.join(HERE, "protos", "@@.hmm") )
        self.assertTrue(compare(hmm1.definition,hmm2.definition))

        hmm1.load( os.path.join( workdir, "sil.hmm") )
        hmm2.load( os.path.join(HERE, "protos", "sil.hmm") )

        corpus.datatrainer.fix_proto(protofilename=os.path.join(HERE, "protos", "proto.hmm"))
        hmm2.load( os.path.join(HERE, "protos", "proto.hmm") )

        hmm1.load( os.path.join(workdir, "gb.hmm") )
        self.assertTrue(compare(hmm1.definition,hmm2.definition))

        hmm1.load( os.path.join(workdir, "dummy.hmm") )
        self.assertTrue(compare(hmm1.definition,hmm2.definition))

        acmodel = AcModel()
        acmodel.load_htk( os.path.join( workdir,"hmmdefs") )

        shutil.rmtree( workdir )
        os.remove( os.path.join(HERE, "protos", "proto.hmm") )
Beispiel #9
0
    def load(self, modelTextDir, modelSpkDir):
        """
        Load the acoustic models from their directories.

        @param modelTextDir (str)
        @param modelSpkDir (str)

        """
        modelText = AcModel()
        modelSpk  = AcModel()

        # Load the acoustic models.
        modelText.load( modelTextDir )
        modelSpk.load( modelSpkDir )

        self.set_models(modelText, modelSpk)
Beispiel #10
0
args = parser.parse_args()

# ----------------------------------------------------------------------------

if not os.path.isdir( args.o ):
    print "Error:",args.o,"must be an existing directory."
    sys.exit(1)

if not os.path.isfile( args.i ):
    print "Error:",args.i,"must be an acoutic model file (HTK-ASCII format)."
    sys.exit(1)

if not args.quiet is True:
    print "Loading AC:",
acmodel1 = AcModel()
acmodel1.load_htk( args.i )
if not args.quiet is True:
    print "... done"

# ----------------------------------------------------------------------------

acmodel = acmodel1.extract_monophones()

for hmm in acmodel.hmms:
   
    filename = os.path.join( args.o, hmm.name )
    filename = filename + ".hmm"
    if not args.quiet is True:
        print hmm.name,filename
    hmm.save( filename )
Beispiel #11
0
    def test_replace_phones(self):
        acmodel1 = AcModel()
        acmodel1.load( os.path.join(MODEL_PATH,"models-fra") )
        acmodel1.replace_phones( reverse=False )
        acmodel1.replace_phones( reverse=True )

        acmodel2 = AcModel()
        acmodel2.load( os.path.join(MODEL_PATH,"models-fra") )

        for h1 in acmodel1.hmms:
            h2 = acmodel2.get_hmm( h1.name )
            self.assertTrue(compare(h1.definition['transition'],h2.definition['transition']))
            self.assertTrue(compare(h1.definition['states'],h2.definition['states']))
Beispiel #12
0
    def test_no_merge(self):
        nbhmms = len(self.acmodel.hmms)

        # Try to merge with the same model!
        acmodel2 = AcModel()
        acmodel2.load_htk(self.hmmdefs)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=1.)
        self.assertEqual(interpolated, 0)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, nbhmms)
        self.assertEqual(changed, 0)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=0.5)
        self.assertEqual(interpolated, nbhmms)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, 0)
        self.assertEqual(changed, 0)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=0.)
        self.assertEqual(interpolated, 0)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, 0)
        self.assertEqual(changed, nbhmms)

        # Try to merge with a different MFCC parameter kind model...
        acmodel2 = AcModel()
        acmodel2.load_htk( os.path.join(MODEL_PATH,"models-cat","hmmdefs") )
        with self.assertRaises(TypeError):
            acmodel2.merge_model(self.acmodel,gamma=1.)
Beispiel #13
0
 def setUp(self):
     self.hmmdefs = os.path.join(MODEL_PATH,"models-jpn","hmmdefs")
     self.acmodel = AcModel()
     self.acmodel.load_htk( self.hmmdefs )
Beispiel #14
0
class TestAcModel(unittest.TestCase):

    #This one takes too much time to be tested each time....
#     def test_load_all_models(self):
#         models = glob.glob(os.path.join(MODEL_PATH,"models-*","hmmdefs"))
#         for hmmdefs in models:
#             acmodel = AcModel( hmmdefs )
#             self._test_load_save( acmodel )

    def setUp(self):
        self.hmmdefs = os.path.join(MODEL_PATH,"models-jpn","hmmdefs")
        self.acmodel = AcModel()
        self.acmodel.load_htk( self.hmmdefs )


    def test_load_save(self):
        self._test_load_save( self.hmmdefs )


    def _test_load_save(self, acmodel):

        # Save temporary the loaded model into a file
        tmpfile = self.hmmdefs+".copy"
        self.acmodel.save_htk( tmpfile )

        # Load the temporary file into a new model
        acmodelcopy = AcModel()
        acmodelcopy.load_htk(tmpfile)

        # Compare original and copy
        self.assertEqual(len(self.acmodel.hmms),len(acmodelcopy.hmms))
        for hmm,hmmcopy in zip(self.acmodel.hmms,acmodelcopy.hmms):
            self.assertEqual(hmm.name,hmmcopy.name)
            self.assertTrue(compare(hmm.definition,hmmcopy.definition))
        self.assertTrue(compare(self.acmodel.macros,acmodelcopy.macros))

        os.remove(tmpfile)


    def test_get_hmm(self):
        with self.assertRaises(ValueError):
            self.acmodel.get_hmm('Q')
        Nhmm = self.acmodel.get_hmm('N')
        self.__test_states( Nhmm.definition['states'] )
        self.__test_transition( Nhmm.definition['transition'] )


    def test_append_hmm(self):
        with self.assertRaises(TypeError):
            self.acmodel.append_hmm({'toto':None})
        hmm = HMM()

        with self.assertRaises(TypeError):
            self.acmodel.append_hmm(hmm)

        Nhmm = self.acmodel.get_hmm('N')
        with self.assertRaises(ValueError):
            self.acmodel.append_hmm(Nhmm)

        Newhmm = copy.deepcopy(Nhmm)
        Newhmm.name = "NewN"
        self.acmodel.append_hmm(Newhmm)


    def test_pop_hmm(self):
        self.acmodel.pop_hmm("N")
        with self.assertRaises(ValueError):
            self.acmodel.get_hmm( "N" )


    def test_load_hmm(self):
        hmm = HMM()
        hmm.load( os.path.join(HERE,"N-hmm") )
        self.__test_states( hmm.definition['states'] )
        self.__test_transition( hmm.definition['transition'] )


    def test_save_hmm(self):
        hmm = HMM()
        hmm.load( os.path.join(HERE,"N-hmm") )
        hmm.save(os.path.join(HERE,"N-hmm-copy"))
        newhmm = HMM()
        newhmm.load(os.path.join(HERE,"N-hmm-copy"))
        os.remove(os.path.join(HERE,'N-hmm-copy'))
        self.assertEqual(hmm.name,newhmm.name)
        self.assertTrue(compare(hmm.definition,newhmm.definition))


    def test_fill(self):
        acmodel1 = AcModel()
        acmodel1.load_htk( os.path.join(HERE,"1-hmmdefs") )
        ahmm1=acmodel1.get_hmm('a')
        a1transition = [macro["transition"] for macro in acmodel1.macros if macro.get('transition',None)][0]

        acmodel1.fill_hmms()
        self.assertTrue(compare(ahmm1.definition['transition'],a1transition['definition']))


    def test_no_merge(self):
        nbhmms = len(self.acmodel.hmms)

        # Try to merge with the same model!
        acmodel2 = AcModel()
        acmodel2.load_htk(self.hmmdefs)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=1.)
        self.assertEqual(interpolated, 0)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, nbhmms)
        self.assertEqual(changed, 0)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=0.5)
        self.assertEqual(interpolated, nbhmms)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, 0)
        self.assertEqual(changed, 0)

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(self.acmodel,gamma=0.)
        self.assertEqual(interpolated, 0)
        self.assertEqual(appended, 0)
        self.assertEqual(keeped, 0)
        self.assertEqual(changed, nbhmms)

        # Try to merge with a different MFCC parameter kind model...
        acmodel2 = AcModel()
        acmodel2.load_htk( os.path.join(MODEL_PATH,"models-cat","hmmdefs") )
        with self.assertRaises(TypeError):
            acmodel2.merge_model(self.acmodel,gamma=1.)


    def test_merge(self):
        acmodel1 = AcModel()
        acmodel1.load_htk( os.path.join(HERE,"1-hmmdefs") )
        acmodel2 = AcModel()
        acmodel2.load_htk( os.path.join(HERE,"2-hmmdefs") )

        (appended,interpolated,keeped,changed) = acmodel2.merge_model(acmodel1,gamma=0.5)
        self.assertEqual(interpolated, 2) # acopy, a
        self.assertEqual(appended, 1)     # i
        self.assertEqual(keeped, 1)       # e
        self.assertEqual(changed, 0)

        self.__test_states( acmodel2.get_hmm('a').definition['states'] )
        self.__test_transition( acmodel2.get_hmm('a').definition['transition'] )


    def test_replace_phones(self):
        acmodel1 = AcModel()
        acmodel1.load( os.path.join(MODEL_PATH,"models-fra") )
        acmodel1.replace_phones( reverse=False )
        acmodel1.replace_phones( reverse=True )

        acmodel2 = AcModel()
        acmodel2.load( os.path.join(MODEL_PATH,"models-fra") )

        for h1 in acmodel1.hmms:
            h2 = acmodel2.get_hmm( h1.name )
            self.assertTrue(compare(h1.definition['transition'],h2.definition['transition']))
            self.assertTrue(compare(h1.definition['states'],h2.definition['states']))


    def test_monophones(self):
        acmodel1 = AcModel()
        acmodel1.load( os.path.join(MODEL_PATH,"models-fra") )

        acmodel2 = acmodel1.extract_monophones()
        acmodel2.save(os.path.join(HERE,'fra-mono'))
        self.assertTrue(  os.path.isfile( os.path.join(HERE, 'fra-mono','hmmdefs')) )
        self.assertTrue(  os.path.isfile( os.path.join(HERE, 'fra-mono','monophones.repl')) )
        self.assertFalse( os.path.isfile( os.path.join(HERE, 'fra-mono','tiedlist')) )
        os.remove( os.path.join(HERE, 'fra-mono','hmmdefs') )
        os.remove( os.path.join(HERE, 'fra-mono','monophones.repl') )
        os.rmdir( os.path.join(HERE,'fra-mono') )
        self.assertEqual( len(acmodel2.hmms), 38 )


    def test_proto(self):
        h1 = HtkIO()
        h1.write_hmm_proto( 25, os.path.join(HERE,"proto_from_htkio") )

        h2 = HMM()
        h2.create_proto( 25 )
        h2.save( os.path.join(HERE,"proto_from_hmm") )

        m1 = HMM()
        m1.load( os.path.join(HERE,"proto_from_htkio") )

        m2 = HMM()
        m2.load( os.path.join(HERE,"proto_from_hmm") )

        self.assertTrue(compare(m1.definition['transition'],m2.definition['transition']))
        self.assertTrue(compare(m1.definition['states'],m2.definition['states']))

        os.remove( os.path.join(HERE,"proto_from_hmm") )
        os.remove( os.path.join(HERE,"proto_from_htkio") )


    def __test_transition(self, transition):
        self.assertEqual(transition['dim'], 5)
        matrix = transition['matrix']
        for i in range(len(matrix)-1):
            # the last vector is always 0.!
            vector=matrix[i]
            self.assertEqual(1.0, round(sum(vector),4))

    def __test_states(self, states):
        for item in states: # a dict
            state = item['state']
            streams = state['streams']
            for s in streams: # a list
                mixtures = s['mixtures']
                for mixture in mixtures: # a list of dict
                    #self.assertEqual(type(mixture['weight']),float)
                    pdf = mixture['pdf']
                    self.assertEqual(pdf['mean']['dim'], 25)
                    self.assertEqual(len(pdf['mean']['vector']),25)
                    self.assertEqual(pdf['covariance']['variance']['dim'], 25)
                    self.assertEqual(len(pdf['covariance']['variance']['vector']), 25)
                    self.assertEqual(type(pdf['gconst']),float)
Beispiel #15
0
mxg = parser.add_mutually_exclusive_group(required=True)
mxg.add_argument("-o", metavar="file", required=False,  help='Output file name')
mxg.add_argument("-O", metavar="file", required=False,  help='Output directory name')


if len(sys.argv) <= 1:
    sys.argv.append('-h')

args = parser.parse_args()

# ----------------------------------------------------------------------------

if not args.quiet is True:
    print "Loading AC 1:",
acmodel1 = AcModel()
if os.path.isfile( args.i ):
    acmodel1.load_htk( args.i )
else:
    acmodel1.load( args.i )
if not args.quiet is True:
    print "... done"

if not args.quiet is True:
    print "Loading AC 2:",
acmodel2 = AcModel()
if os.path.isfile( args.I ):
    acmodel2.load_htk( args.I )
else:
    acmodel2.load( args.I )
if not args.quiet is True: