def test_merger(): merger = joint.Merger() pipe1 = base.PIPE() pipe2 = base.PIPE() for i in range(5): pipe1.put( base.Packet({"mfcc": np.ones([ 5, ], dtype="float32")}, cid=i, idmaker=0)) pipe2.put( base.Packet({"fbank": np.ones([ 4, ], dtype="float32")}, cid=i, idmaker=0)) pipe1.stop() pipe2.stop() merger.link(inPIPE=[pipe1, pipe2]) merger.start() merger.wait() print(merger.outPIPE) print(merger.outPIPE[0].size()) print(merger.outPIPE[0].get().items()) #test_merger()
def test_sender(): pipe = base.PIPE() for i in range(5): pipe.put(base.Packet({"element": 1}, cid=i, idmaker=0)) for i in range(5, 10): pipe.put( base.Packet({"vector": np.ones([ 5, ], dtype="float32")}, cid=i, idmaker=0)) for i in range(10, 15): pipe.put( base.Packet({"matrix": np.ones([5, 10], dtype="float32")}, cid=i, idmaker=0)) for i in range(15, 20): pipe.put(base.Packet({"string": "this is a test"}, cid=i, idmaker=0)) pipe.stop() # define a packet sender sender = transmit.PacketSender(thost="192.168.1.11", tport=9509) sender.start(inPIPE=pipe) sender.wait()
def test_combiner(): pipe1 = base.PIPE() pipe2 = base.PIPE() for i in range(5): pipe1.put( base.Packet({"mfcc": np.ones([ 5, ], dtype="float32")}, cid=i, idmaker=0)) pipe2.put( base.Packet({"fbank": np.zeros([ 5, ], dtype="float32")}, cid=i, idmaker=0)) pipe1.stop() pipe2.stop() def combine_function(items): return { "mixedFeat": np.concatenate([items[0]["mfcc"], items[1]["fbank"]]) } combiner = joint.Combiner(func=combine_function) combiner.link(inPIPE=[pipe1, pipe2]) combiner.start() combiner.wait() print(combiner.outPIPE) print(combiner.outPIPE[0].get().items())
def test_estimator(): pipe = base.PIPE() for i in range(5): pipe.put(base.Packet({"data": np.ones([10, 13])}, cid=i, idmaker=0)) pipe.stop() def test_func(frames): return np.zeros_like(frames) estimator = decode.AcousticEstimator( func=test_func, leftContext=3, rightContext=3, applySoftmax=False, applyLog=False, ) estimator.start(inPIPE=pipe) estimator.wait() print(estimator.outPIPE.size()) print(estimator.outPIPE.get()["data"].shape)
def test_processor_cmvn(): wavData = stream.read(wavPath).value frames = stream.cut_frames(wavData) pipe = base.PIPE() for i in range(10): pipe.put( base.Packet( {"rawWave":frames[i*50:(i+1)*50]}, cid=i, idmaker=0 ) ) pipe.stop() extractor = feature.MfccExtractor(minParallelSize=100,oKey="mfcc") processor = feature.MatrixFeatureProcessor( spliceLeft=3, spliceRight=3, cmvNormalizer=feature.FrameSlideCMVNormalizer(), oKey="mfcc", ) extractor.start(inPIPE=pipe) processor.start(inPIPE=extractor.outPIPE,iKey="mfcc") processor.wait() print( processor.outPIPE.size() ) packet = processor.outPIPE.get() print( packet.keys() ) print( packet.mainKey ) print( packet["mfcc"].shape ) # 273 = 13 * 3 * 7
def test_spliter(): pipe = base.PIPE() for i in range(5): pipe.put( base.Packet(items={ "mfcc": np.ones([ 5, ], dtype="float32"), "fbank": np.ones([ 5, ], dtype="float32"), }, cid=i, idmaker=0)) pipe.stop() def split_function(items): return [{key: value} for key, value in items.items()] spliter = joint.Spliter(func=split_function, outNums=2) spliter.start(inPIPE=pipe) spliter.wait() print(spliter.outPIPE) print(spliter.outPIPE[0].get().keys()) print(spliter.outPIPE[1].get().keys())
def test_mixture_feature_parallel(): wavData = stream.read(wavPath).value frames = stream.cut_frames(wavData) pipe = base.PIPE() for i in range(10): pipe.put( base.Packet({"rawWave": frames[i * 50:(i + 1) * 50]}, cid=i, idmaker=0)) pipe.stop() extractor = feature.MixtureExtractor(mixType=["fbank", "mfcc"], ) # Split packets def split_rule(items): return {"fbank": items["fbank"]}, {"mfcc": items["mfcc"]} spliter = joint.Spliter(split_rule, outNums=2) # use a processor to transform fbank feature processor1 = feature.MatrixFeatureProcessor( spliceLeft=2, spliceRight=2, cmvNormalizer=feature.FrameSlideCMVNormalizer(), oKey="fbank", ) # use a processor to transform mfcc feature processor2 = feature.MatrixFeatureProcessor( spliceLeft=3, spliceRight=3, cmvNormalizer=feature.FrameSlideCMVNormalizer(), oKey="mfcc", ) # combine packets def combine_rule(items): return { "feat": np.concatenate([items[0]["fbank"], items[1]["mfcc"]], axis=1) } combiner = joint.Combiner(combine_rule) extractor.start(inPIPE=pipe) spliter.start(inPIPE=extractor.outPIPE) processor1.start( inPIPE=spliter.outPIPE[0]) # specify which key you want to process processor2.start( inPIPE=spliter.outPIPE[1]) # specify which key you want to process combiner.start(inPIPE=[processor1.outPIPE, processor2.outPIPE]) combiner.wait() print(combiner.outPIPE[0].size()) packet = combiner.outPIPE[0].get() print(packet.keys()) print(packet["feat"].shape) # 211 = 120 + 91
def test_mfcc_extractor(): # get wave data wavData = stream.read(wavPath).value frames = stream.cut_frames(wavData) # define an input pipe pipe = base.PIPE() for i in range(10): pipe.put( base.Packet( {"rawWave":frames[i*50:(i+1)*50]}, cid=i, idmaker=0 ) ) pipe.stop() print( pipe.size() ) # run a mfcc extractor extractor = feature.MfccExtractor(oKey="mfcc") extractor.start(inPIPE=pipe) extractor.wait() print( extractor.outPIPE.size() ) packet = extractor.outPIPE.get() print( packet.keys() ) print( packet.mainKey ) print( packet["mfcc"].shape )
def test_decoder(): prob = np.load("../examples/1272-135031-0000_mlp.npy") frames = prob.shape[0] dim = prob.shape[1] batchSize = 50 N = int(math.ceil(frames / batchSize)) buffer = np.zeros([N * batchSize, dim], dtype="float32") buffer[0:frames] = prob pipe = base.PIPE() for i in range(N): s = i * batchSize e = (i + 1) * batchSize pipe.put(base.Packet({"data": buffer[s:e]}, cid=i, idmaker=0)) pipe.stop() print(pipe.size()) decoder = decode.WfstDecoder( symbolTable= "/Work18/wangyu/kaldi/egs/mini_librispeech/s5/exp/tri3b/graph_tgsmall/words.txt", silencePhones="1:2:3:4:5:6:7:8:9:10", frameShiftSec=0.01, tmodel= "/Work18/wangyu/kaldi/egs/mini_librispeech/s5/exp/tri3b_ali_dev_clean_2/final.mdl", graph= "/Work18/wangyu/kaldi/egs/mini_librispeech/s5/exp/tri3b/graph_tgsmall/HCLG.fst", beam=10, latticeBeam=8, minActive=200, maxActive=7000, acousticScale=0.1, maxBatchSize=50, ) decoder.start(inPIPE=pipe) base.dynamic_display(decoder.outPIPE) #decoder.wait() #print( decoder.outPIPE.size() ) #result = decode.dump_text_PIPE(decoder.outPIPE) #print( result ) # online : BECAUSE YOU ARE A SLEEPING IN SOME OF CONQUERING THE LOVELY RUSE PRINCES WAS PUT TO A FATAL WITHOUT A BULL OLD TORE SHAGGY SINCERE OR COOING DOVE # offline: BECAUSE YOU ARE A SLEEPING IN SOME OF CONQUERING THE LOVELY RUSE PRINCES WAS PUT TO A FATAL WITHOUT A BULL OLD TORE SHAGGY SINCERE OR COOING DOVE #test_decoder()
def test_mapper(): pipe = base.PIPE() for i in range(5): pipe.put( base.Packet({"mfcc":np.ones([5,],dtype="float32")},cid=i,idmaker=0) ) pipe.stop() def map_function(items): return { "scaledMFCC": 2*items["mfcc"] } mapper = joint.Mapper(mapFunc=map_function) mapper.start(inPIPE=pipe) mapper.wait() print( mapper.outPIPE.size() ) print( mapper.outPIPE.get()["scaledMFCC"] )
def test_replicator(): replicator = joint.Replicator(outNums=2) pipe = base.PIPE() for i in range(5): pipe.put( base.Packet({"mfcc":np.ones([5,],dtype="float32")},cid=i,idmaker=0) ) pipe.stop() replicator.link(inPIPE=pipe) replicator.start() replicator.wait() print( replicator.outPIPE ) print( replicator.outPIPE[0].get().items() ) print( replicator.outPIPE[1].get().items() )
def test_chain(): # Define a component class MyComponent(base.Component): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) def core_loop(self): while True: if self.inPIPE.is_empty(): break pack = self.get_packet() self.put_packet(pack) component = MyComponent() # Define a joint def copy_packet(items): return items[0], copy.deepcopy(items[0]) joint = base.Joint(copy_packet, outNums=2) # define the input PIPE pipe = base.PIPE() for i in range(5): pipe.put( base.Packet( {"mfcc":np.ones([5,],dtype="float32") },cid=i, idmaker=0) ) pipe.stop() # Define a chain (container) chain = base.Chain() # Add a component chain.add( component, inPIPE=pipe ) # Add a joint. chain will link the joint to the output of component automatically. chain.add( joint ) # Start and Wait chain.start() chain.wait() # Chain has similiar API with component and joint print( chain.outPIPE ) print( chain.outPIPE[0].size() ) print( chain.outPIPE[1].size() )
def test_mixture_feature_series(): wavData = stream.read(wavPath).value frames = stream.cut_frames(wavData) pipe = base.PIPE() for i in range(10): pipe.put( base.Packet({"rawWave": frames[i * 50:(i + 1) * 50]}, cid=i, idmaker=0)) pipe.stop() extractor = feature.MixtureExtractor(mixType=["fbank", "mfcc"], ) # use a processor to transform fbank feature processor1 = feature.MatrixFeatureProcessor( spliceLeft=2, spliceRight=2, cmvNormalizer=feature.FrameSlideCMVNormalizer(), oKey="fbank", ) # use a processor to transform mfcc feature processor2 = feature.MatrixFeatureProcessor( spliceLeft=3, spliceRight=3, cmvNormalizer=feature.FrameSlideCMVNormalizer(), oKey="mfcc", ) extractor.start(inPIPE=pipe) processor1.start(inPIPE=extractor.outPIPE, iKey="fbank") # specify which key you want to process processor2.start(inPIPE=processor1.outPIPE, iKey="mfcc") # specify which key you want to process processor2.wait() print(processor2.outPIPE.size()) packet = processor2.outPIPE.get() print(packet.keys()) print(packet["fbank"].shape) # 120 = 24 * 5 print(packet["mfcc"].shape) # 91 = 13 * 7
def test_joint(): # Define a joint: it is used to separate different data. def combine(items): # items is a list including multiple input items (dict objects) return {"mfcc":items[0]["mfcc"]}, {"fbank":items[0]["fbank"]} joint = base.Joint(jointFunc=combine,outNums=2) # Define an input PIPE which the joint will get packets from it. pipe = base.PIPE() for i in range(5): packet = base.Packet( items={"mfcc":np.ones([5,],dtype="float32"), "fbank":np.ones([4,],dtype="float32") }, cid=i, idmaker=0 ) pipe.put( packet ) pipe.stop() # then link joint with this input PIPE joint.link(inPIPE=pipe) # start and wait joint.start() joint.wait() # there should be two output PIPEs print( joint.outPIPE ) # the first PIPE should only include "mfcc" date # the second PIPE should only include "fbank" date print( joint.outPIPE[0].get().items() ) print( joint.outPIPE[1].get().items() )
def test_packet(): # Define a packet which carry one frame of mfcc features. # Each packet must have a unique chunk id, which is made by a component or joint. packet = base.Packet( items={"mfcc":np.ones([13,],dtype="float32") }, cid=0, idmaker=0 ) # Each packet has a mainKey and you can check all keys in this packet. # This main key is decided automatically when you initialize packet object. print( packet.keys(), packet.mainKey ) # It has some similar function with Python doct object. print( packet.values() ) print( packet.items() ) print( packet["mfcc"] ) # but __setitems__ is unavaliable. # If you want add a new record in this packet. # You can set it as main key. packet.add( key="fbank", data=np.ones([24,],dtype="float32"), asMainKey=True ) print( packet.keys(), packet.mainKey ) # Packet object can be convert to bytes object for, such as remote transmission. bpack = packet.encode() print( bpack ) # A packet can be restored from the corrsponded bytes object. npack = base.Packet.decode( bpack ) print( npack ) print( npack.keys() ) # Endpoint is a special packet the mark speech endpoint. # It defaultly is an empty packet but also can carries data. ENDPOINT = base.Endpoint( cid=0, idmaker=0 ) bENDPOINT = ENDPOINT.encode() print(bENDPOINT) nENDPOINT = base.Packet.decode( bENDPOINT ) print(nENDPOINT)
def test_pipe(): # Define a PIPE and put a packet int it. # pipe has 5 states: # 0 -> silent, 1 -> active, 2 -> terminated, 3 -> wrong, 4 -> stranded (more marks in exkaldirt.base.mark) # Different states can allow different operations, # for examples, you can not add a new packet into a terminated PIPE. pipe = base.PIPE() print( pipe.state ) pipe.put( base.Packet( items={"stream":100}, cid=0, idmaker=0 ) ) print( pipe.size() ) # If the input port or/and output port is locked, # password is necessary if others want to access it. # password is a random int number. password = pipe.lock_in() print( password ) print( pipe.is_inlocked() ) pipe.put( base.Packet( items={"stream":101}, cid=1, idmaker=0 ), password=password ) print( pipe.size() ) # When a pipe is locked by a specified component, # you can not get packet from it unless you know the password. # you can release it. pipe.release_in(password=password) # Note that, we will forcely remove continuous endpoints. # If the endpoint had data, it will be lost. # No matter how many endpoints you added, only keep the first one. pipe.put( base.Endpoint( cid=2, idmaker=0 ) ) print( pipe.size() ) pipe.put( base.Endpoint( cid=3, idmaker=0 ) ) print( pipe.size() ) pipe.put( base.Endpoint( cid=4, idmaker=0 ) ) print( pipe.size() ) # For an active pipe, you can: # pause it: the state will become "stranded"; # stop it: the state will become "terminated", and an endpoint packet will be appended at the last automatically; # kill it: the state will become "wrong". pipe.stop() print( pipe.state ) # PIPE is actually a LILO queue. # You can get a packet from head. print( pipe.get() ) # If the pipe is "wrong" or "terminated", it can be converted to lists devided by endpoints. # you can design the convert rule. # defaultly, it only get the main key. result = pipe.to_list() print(result) # If the pipe is "wrong" or "terminated", it can be reset. pipe.reset() # PIPE can add callback functions. # when a packet is appended in PIPE, these functions will work. def call_func_1(pac): print("feature shape:", pac[pac.mainKey].shape ) def call_func_2(pac): print("feature dtype:", pac[pac.mainKey].dtype ) pipe.callback( call_func_1 ) pipe.callback( call_func_2 ) for i in range(5): pipe.put( base.Packet({"data":np.ones([5,],dtype="float32")},cid=i,idmaker=0) ) # The time info will be recorded when packets are appended and picked out. pipe.get() print( pipe.report_time() )
def PF_func1(pipe): for i in range(5): pipe.put( base.Packet({"data":1},cid=i,idmaker=0) ) pipe.stop()