Last active
April 28, 2019 02:48
-
-
Save tam17aki/715d2fc1f0ba11674137e4842f9a8c89 to your computer and use it in GitHub Desktop.
dnnclient.py for Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Julius Dictation Kit | |
# License Agreement | |
# Copyright (c) 1991-2016 Kawahara Lab., Kyoto University | |
# Copyright (c) 2005-2016 Lee Lab., Nagoya Institute of Technology | |
# "Julius Dictation Kit" has been developed at Kawahara Lab., Kyoto | |
# University and Lee Lab., Nagoya Institute of Technology (collectively | |
# referred to herein as the "Licensers"). | |
# The Licensers reserve the copyright thereto. However, as long as you | |
# accept and remain in strict compliance with the terms and conditions | |
# of the license set forth herein, you are hereby granted a royalty-free | |
# license to use this software program including the source code thereof | |
# and the documentation thereto (collectively referred to herein as the | |
# "Software"). Use by you of the Software shall constitute acceptance | |
# by you of all terms and conditions of the license set forth herein. | |
# TERMS AND CONDITIONS OF LICENSE | |
# (Grant of License) | |
# 1. So long as you accept and strictly comply with the terms and | |
# conditions of the license set forth herein, the Licensers will not | |
# enforce the copyright or moral rights in respect of the Software, in | |
# connection with the use, copying, duplication, adaptation, | |
# modification, preparation of a derivative work, aggregation with | |
# another program, or insertion into another program of the Software and | |
# the distribution or transmission of the Software. | |
# (Copyright Notice) | |
# 2. In the event you provide to any third party all or any portion of | |
# the Software, you shall affix the above copyright notice and all terms | |
# and conditions of this license (both the Japanese original and English | |
# translation). Moreover, in the event you revise all or any portion of | |
# the Software for distribution, a notice shall be affixed indicating | |
# that the Software has been revised, together with the revision date | |
# and the name of the person or entity that made the revision. | |
# (Publication) | |
# 3. When you publish or present any results by using the Software, you | |
# must explicitly mention your use of "Julius Dictation Kit". | |
# 3-1. The language model included in this Software has been developed | |
# by using the "Balanced Corpus of Contemporary Written Japanese | |
# (BCCWJ)" compiled and owned by the National Institute for Japanese | |
# Language and Linguistics. When you publish or present any results by | |
# using the Software, you must explicitly mention this fact. | |
# 3-2. The acoustic model included in this Software has been developed | |
# by using the "ASJ Continuous Speech Corpus -- Japanese Newspaper | |
# Article Sentences (JNAS)" and the "Corpus of Spontaneous Japanese | |
# (CSJ)". | |
# (Disclaimer of Warranty) | |
# 4. The Licensers make no warranty of any kind and nature, whether | |
# express, implied or statutory on the Software and any matters relating | |
# thereto. THE LICENSERS SPECIFICALLY DISCLAIMS ALL IMPLIED WARRANTIES, | |
# DUTIES OR CONDITIONS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR PURPOSE, OF | |
# ACCURACY OR COMPLETENESS OF RESPONSES AND RESULTS, AND THAT THE USE OF | |
# THE SOFTWARE WILL NOT INFRINGE ANY PATENT, TRADEMARK, TRADE-SECRETS OR | |
# OTHER PROPRIETARY RIGHTS OF ANY THIRD PARTY. | |
# (No Liability) | |
# 5. The Licensers shall not be liable to you for any general or direct, | |
# special, incidental, consequential or other indirect damages | |
# whatsoever (including, but not limited to, damages for loss of profits | |
# or savings) arising out of or in any way connected with the use of or | |
# inability to use the Software, or otherwise under or in connection | |
# with any provisions of this License, even in the event of the fault, | |
# tort (including negligence), misrepresentation, strict liability | |
# (including product liability), breach of contract or breach of | |
# warranty of the Licensers, and even if the Licensers has been advised | |
# of possibility of such damages. | |
# (Choice of Law and Exclusive Forum) | |
# 6. This license of use of the Software shall be governed by the laws | |
# of Japan, and the Kyoto District Court shall have exclusive primary | |
# jurisdiction with respect to all disputes arising with respect | |
# thereto. | |
# (Contact) | |
# 7. Inquiries about support or maintenance of the Software, or | |
# inquiries concerning the license of use besides the conditions above, | |
# may be sent to Kawahara Lab., Kyoto University or Lee Lab., Nagoya | |
# Institute of Technology. | |
import sys | |
import numpy as np | |
import socket | |
import struct | |
adinserver_host = 'localhost' | |
adinserver_port = 5532 | |
julius_host = 'localhost' | |
julius_port = 5531 | |
num_raw = 120 | |
num_input = 1320 | |
num_hid = 2048 | |
num_output = 2004 | |
num_context = 11 # 1320 / 120 | |
batchsize = 32 | |
w_filename = ["dnn_sample/W_l1.npy", "dnn_sample/W_l2.npy", | |
"dnn_sample/W_l3.npy", "dnn_sample/W_l4.npy", | |
"dnn_sample/W_l5.npy", "dnn_sample/W_l6.npy", | |
"dnn_sample/W_l7.npy", "dnn_sample/W_output.npy"] | |
b_filename = ["dnn_sample/bias_l1.npy", "dnn_sample/bias_l2.npy", | |
"dnn_sample/bias_l3.npy", "dnn_sample/bias_l4.npy", | |
"dnn_sample/bias_l5.npy", "dnn_sample/bias_l6.npy", | |
"dnn_sample/bias_l7.npy", "dnn_sample/bias_output.npy"] | |
prior_filename = "dnn_sample/seedhmm.cluster.prior" | |
if len(sys.argv) > 1: | |
conffile = sys.argv[1] | |
f = open(conffile) | |
for line in f: | |
linebuf = line.strip().split(' ') | |
if linebuf[0] == "--adinserver_host": | |
adinserver_host = linebuf[1] | |
elif linebuf[0] == "--adinserver_port": | |
adinserver_port = int(linebuf[1]) | |
elif linebuf[0] == "--julius_host": | |
julius_host = linebuf[1] | |
elif linebuf[0] == "--julius_port": | |
julius_port = int(linebuf[1]) | |
elif linebuf[0] == "--num_raw": | |
num_raw = int(linebuf[1]) | |
elif linebuf[0] == "--num_input": | |
num_input = int(linebuf[1]) | |
elif linebuf[0] == "--num_hid": | |
num_hid = int(linebuf[1]) | |
elif linebuf[0] == "--num_output": | |
num_output = int(linebuf[1]) | |
elif linebuf[0] == "--num_context": | |
num_context = int(linebuf[1]) | |
elif linebuf[0] == "--batchsize": | |
batchsize = int(linebuf[1]) | |
elif linebuf[0] == "--prior_filename": | |
prior_filename = linebuf[1] | |
elif linebuf[0] == "--w_filename": | |
for i in range(1, len(linebuf)): | |
w_filename[i - 1] = linebuf[i] | |
elif linebuf[0] == "--b_filename": | |
for i in range(1, len(linebuf)): | |
b_filename[i - 1] = linebuf[i] | |
elif linebuf[0] == "#": | |
pass | |
else: | |
print("unkown switch") | |
sys.exit() | |
f.close() | |
w1 = np.load(w_filename[0]) | |
w2 = np.load(w_filename[1]) | |
w3 = np.load(w_filename[2]) | |
w4 = np.load(w_filename[3]) | |
w5 = np.load(w_filename[4]) | |
w6 = np.load(w_filename[5]) | |
w7 = np.load(w_filename[6]) | |
wo = np.load(w_filename[7]) | |
b1 = np.load(b_filename[0]) | |
b2 = np.load(b_filename[1]) | |
b3 = np.load(b_filename[2]) | |
b4 = np.load(b_filename[3]) | |
b5 = np.load(b_filename[4]) | |
b6 = np.load(b_filename[5]) | |
b7 = np.load(b_filename[6]) | |
bo = np.load(b_filename[7]) | |
state_prior = np.zeros((bo.shape[0], 1)) | |
prior_factor = 1.0 | |
for line in open(prior_filename): | |
state_id, state_p = line[:-1].split(' ') | |
state_id = int(state_id) | |
state_p = float(state_p) * prior_factor | |
state_prior[state_id][0] = state_p | |
def ff(x0): | |
x1 = 1. / (1 + np.exp(-(np.dot(w1.T, x0) + b1))) | |
x2 = 1. / (1 + np.exp(-(np.dot(w2.T, x1) + b2))) | |
x3 = 1. / (1 + np.exp(-(np.dot(w3.T, x2) + b3))) | |
x4 = 1. / (1 + np.exp(-(np.dot(w4.T, x3) + b4))) | |
x5 = 1. / (1 + np.exp(-(np.dot(w5.T, x4) + b5))) | |
x6 = 1. / (1 + np.exp(-(np.dot(w6.T, x5) + b6))) | |
x7 = 1. / (1 + np.exp(-(np.dot(w7.T, x6) + b7))) | |
tmp = np.dot(wo.T, x7) + bo | |
np.exp(tmp, tmp) | |
tmp /= np.sum(tmp, axis=0) | |
tmp /= state_prior | |
np.log10(tmp, tmp) | |
return tmp | |
adinserversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
adinserversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
adinserversock.bind((adinserver_host, adinserver_port)) | |
adinserversock.listen(1) | |
juliusclientsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
juliusclientsock.connect((julius_host, julius_port)) | |
sendconf = 0 | |
print('Waiting for connections...') | |
adinclientsock, adinclient_address = adinserversock.accept() | |
splice_feature = np.zeros(num_input) | |
buf_splice_feature = None | |
fnum = 0 | |
while True: | |
rcvmsg = adinclientsock.recv(4) | |
nbytes = struct.unpack('=i', rcvmsg)[0] | |
if nbytes == 12: | |
rcvmsg = adinclientsock.recv(12) | |
fbank_vecdim, fbank_shift, fbank_outprob_p = struct.unpack( | |
'=iii', rcvmsg) | |
c_msg = struct.pack('=iiii', 12, num_output, 10, 1) | |
juliusclientsock.sendall(c_msg) | |
sendconf = 1 | |
elif nbytes == num_raw * 4: | |
buffer = '' | |
while len(buffer) < nbytes: | |
tmpdata = adinclientsock.recv(nbytes - len(buffer)) | |
tmpdata = tmpdata.decode('ISO-8859-1') | |
if not tmpdata: | |
break | |
buffer += tmpdata | |
rcvmsg = buffer | |
val = struct.unpack("=" + "f" * num_raw, rcvmsg.encode('ISO-8859-1')) | |
splice_feature = np.r_[splice_feature[num_raw:num_input], val] | |
if fnum >= num_context: | |
if buf_splice_feature is not None: | |
buf_splice_feature = np.hstack( | |
(buf_splice_feature, splice_feature[:, np.newaxis])) | |
else: | |
buf_splice_feature = splice_feature[:, np.newaxis] | |
if buf_splice_feature is not None and buf_splice_feature.shape[1] == batchsize: | |
xo = ff(buf_splice_feature) | |
for i in range(xo.shape[1]): | |
r_feature = xo[:, i] | |
r_msg = struct.pack('=i', num_output * 4) | |
juliusclientsock.sendall(r_msg) | |
r_msg = struct.pack("=" + "f" * num_output, *r_feature) | |
juliusclientsock.sendall(r_msg) | |
buf_splice_feature = None | |
fnum = fnum + 1 | |
elif nbytes == 0: | |
if buf_splice_feature is not None: | |
xo = ff(buf_splice_feature) | |
for i in range(xo.shape[1]): | |
r_feature = xo[:, i] | |
r_msg = struct.pack('=i', num_output * 4) | |
juliusclientsock.sendall(r_msg) | |
r_msg = struct.pack("=" + "f" * num_output, *r_feature) | |
juliusclientsock.sendall(r_msg) | |
r_msg = struct.pack('=i', 0) | |
juliusclientsock.sendall(r_msg) | |
splice_feature = np.zeros(num_input) | |
buf_splice_feature = None | |
fnum = 0 | |
r_msg = struct.pack('=i', 0) | |
juliusclientsock.sendall(r_msg) | |
r_msg = struct.pack('=i', -1) | |
juliusclientsock.sendall(r_msg) | |
adinclientsock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment