def write_model_to_kaldi(self, file_path, with_softmax = True): # determine whether it's BNF based on layer sizes print "[Debug] write_model_to_kaldi" fout = smart_open(file_path, 'wb') params = lasagne.layers.get_all_params(self.network) for i in range(0, len(params), 2): activation_text = '<' + self.cfg.activation_text + '>' if i == (len(params)-2) and with_softmax: # we assume that the last layer is a softmax layer activation_text = '<softmax>' W_mat = params[i].get_value() b_vec = params[i+1].get_value() input_size, output_size = W_mat.shape W_layer = []; b_layer = '' for rowX in xrange(output_size): W_layer.append('') for x in xrange(input_size): for t in xrange(output_size): W_layer[t] = W_layer[t] + str(W_mat[x][t]) + ' ' for x in xrange(output_size): b_layer = b_layer + str(b_vec[x]) + ' ' fout.write('<affinetransform> ' + str(output_size) + ' ' + str(input_size) + '\n') fout.write('[' + '\n') for x in xrange(output_size): fout.write(W_layer[x].strip() + '\n') fout.write(']' + '\n') fout.write('[ ' + b_layer.strip() + ' ]' + '\n') if activation_text == '<maxout>': fout.write(activation_text + ' ' + str(output_size/self.pool_size) + ' ' + str(output_size) + '\n') else: fout.write(activation_text + ' ' + str(output_size) + ' ' + str(output_size) + '\n') fout.close()
def _cnn2file(conv_layers, filename='nnet.out', input_factor=1.0, factor=[]): n_layers = len(conv_layers) nnet_dict = {} for i in xrange(n_layers): conv_layer = conv_layers[i] filter_shape = conv_layer.filter_shape dropout_factor = 0.0 if i == 0: dropout_factor = input_factor if i > 0 and len(factor) > 0: dropout_factor = factor[i - 1] for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): dict_a = 'W' + str(i) + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[dict_a] = array_2_string( dropout_factor * (conv_layer.W.get_value())[next_X, this_X]) dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(conv_layer.b.get_value()) with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys=True) fp.flush()
def save_lrate(lrate, lrate_file): file_open = smart_open(lrate_file, 'w') # always overwrite file_open.write(str(lrate.epoch) + '\n') file_open.write(str(lrate.rate) + '\n') file_open.write(str(lrate.lowest_error) + '\n') file_open.write(str(int(lrate.decay)) + '\n') file_open.close()
def read_next_utt(self): self.scp_cur_pos = self.scp_file_read.tell() next_scp_line = self.scp_file_read.readline() if next_scp_line == '' or next_scp_line == None: # we are reaching the end of one epoch return '', None utt_id, path_pos = next_scp_line.replace('\n','').split(' ') path, pos = path_pos.split(':') ark_read_buffer = smart_open(path, 'rb') ark_read_buffer.seek(int(pos),0) # now start to read the feature matrix into a numpy matrix header = struct.unpack('<xcccc', ark_read_buffer.read(5)) if header[0] != "B": print "Input .ark file is not binary"; exit(1) rows = 0; cols= 0 m, rows = struct.unpack('<bi', ark_read_buffer.read(5)) n, cols = struct.unpack('<bi', ark_read_buffer.read(5)) tmp_mat = numpy.frombuffer(ark_read_buffer.read(rows * cols * 4), dtype=numpy.float32) utt_mat = numpy.reshape(tmp_mat, (rows, cols)) ark_read_buffer.close() return utt_id, utt_mat
def load_next_partition(self, shared_xy): pfile_path = self.pfile_path_list[self.cur_pfile_index] if self.feat_mat is None or len(self.pfile_path_list) > 1: fopen = smart_open(pfile_path, 'rb') test = cPickle.load(fopen) if len(test) == 2: self.feat_mat, self.label_vec = test elif len(test) == 3: self.feat_mat, self.label_vec, unused = test fopen.close() shared_x, shared_y = shared_xy self.feat_mat, self.label_vec = \ preprocess_feature_and_label(self.feat_mat, self.label_vec, self.read_opts) if self.read_opts['random']: shuffle_feature_and_label(self.feat_mat, self.label_vec) shared_x.set_value(self.feat_mat, borrow=True) shared_y.set_value(self.label_vec, borrow=True) self.cur_frame_num = len(self.feat_mat) self.cur_pfile_index += 1 if self.cur_pfile_index >= len( self.pfile_path_list): # the end of one epoch self.end_reading = True self.cur_pfile_index = 0
def read_next_utt(self): next_scp_line = self.scp_file_read.readline() if next_scp_line == '' or next_scp_line == None: return '', None utt_id, path_pos = next_scp_line.replace('\n', '').split(' ') path, pos = path_pos.split(':') ark_read_buffer = smart_open(path, 'rb') ark_read_buffer.seek(int(pos), 0) header = struct.unpack('<xcccc', ark_read_buffer.read(5)) if header[0] != "B": print "Input .ark file is not binary" exit(1) rows = 0 cols = 0 m, rows = struct.unpack('<bi', ark_read_buffer.read(5)) n, cols = struct.unpack('<bi', ark_read_buffer.read(5)) tmp_mat = numpy.frombuffer(ark_read_buffer.read(rows * cols * 4), dtype=numpy.float32) utt_mat = numpy.reshape(tmp_mat, (rows, cols)) ark_read_buffer.close() return utt_id, utt_mat
def load_next_partition(self, shared_xy): pfile_path = self.pfile_path_list[self.cur_pfile_index] print("pfile_path:", pfile_path) if self.feat_mat is None or len(self.pfile_path_list) > 1: fopen = smart_open(pfile_path, "rb") test = cPickle.load(fopen) if len(test) == 2: self.feat_mat, self.label_vec = test elif len(test) == 3: self.feat_mat, self.label_vec, unused = test fopen.close() shared_x, shared_y = shared_xy self.feat_mat, self.label_vec = preprocess_feature_and_label(self.feat_mat, self.label_vec, self.read_opts) if self.read_opts["random"]: shuffle_feature_and_label(self.feat_mat, self.label_vec) shared_x.set_value(self.feat_mat, borrow=True) shared_y.set_value(self.label_vec.astype(theano.config.floatX), borrow=True) self.cur_frame_num = len(self.feat_mat) print("self.cur_frame_num is;", self.cur_frame_num) self.cur_pfile_index += 1 if self.cur_pfile_index >= len(self.pfile_path_list): # the end of one epoch self.end_reading = True self.cur_pfile_index = 0
def _nnet2file(layers, set_layer_num = -1, filename='nnet.out', start_layer = 0, input_factor = 0.0, factor=[]): n_layers = len(layers) nnet_dict = {} if set_layer_num == -1: set_layer_num = n_layers for i in range(start_layer, set_layer_num): layer = layers[i] dict_a = 'W' + str(i) dropout_factor = 0.0 if i == 0: dropout_factor = input_factor if i > 0 and len(factor) > 0: dropout_factor = factor[i-1] if layer.type == 'fc': nnet_dict[dict_a] = array_2_string((1.0 - dropout_factor) * layer.W.get_value()) elif layer.type == 'conv': filter_shape = layer.filter_shape for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[new_dict_a] = array_2_string((1.0-dropout_factor) * (layer.W.get_value())[next_X, this_X]) dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(layer.b.get_value()) with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys = True) fp.flush()
def load_next_partition(self, shared_xy): print 'load_next_partition!' pfile_path = self.pfile_path_list[self.cur_pfile_index] if self.feat_mat is None or len(self.pfile_path_list) > 1: fopen = smart_open(pfile_path, 'rb') self.feat_mat, self.label_vec = cPickle.load(fopen) fopen.close() shared_x, shared_y = shared_xy #TODO no longer label_vec, is array self.feat_mat, self.label_vec = \ preprocess_feature_and_label(self.feat_mat, self.label_vec, self.read_opts) if self.read_opts['random']: shuffle_feature_and_label(self.feat_mat, self.label_vec) shared_x.set_value(self.feat_mat, borrow=True) #TODO types wrong here? Maybe? shared_y.set_value(self.label_vec.astype(theano.config.floatX), borrow=True) self.cur_frame_num = len(self.feat_mat) print len(self.feat_mat), len(self.label_vec), self.feat_mat.shape self.cur_pfile_index += 1 if self.cur_pfile_index >= len(self.pfile_path_list): # the end of one epoch self.end_reading = True self.cur_pfile_index = 0
def read_next_utt(next_scp_line): # this shouldn't happen if next_scp_line == '' or next_scp_line == None: # we are reaching the end of one epoch return '', None utt_id, path_pos = next_scp_line.replace('\n', '').split(' ') path, pos = path_pos.split(':') ark_read_buffer = smart_open(path, 'rb') ark_read_buffer.seek(int(pos), 0) endian = '<' binary_flag = ark_read_buffer.read(4) assert isinstance(binary_flag, binary_type), type(binary_flag) ark_read_buffer.seek(int(pos), 0) # Load as binary if binary_flag[:2] == b'\0B': array, size = read_matrix_or_vector(ark_read_buffer, endian=str(endian), return_size=True) # Load as ascii else: array, size = read_ascii_mat(ark_read_buffer, return_size=True) return utt_id, array
def write_model_to_kaldi(self, file_path, with_softmax=True): # determine whether it's BNF based on layer sizes output_layer_number = -1 for layer_index in range(1, self.hidden_layers_number - 1): cur_layer_size = self.hidden_layers_sizes[layer_index] prev_layer_size = self.hidden_layers_sizes[layer_index - 1] next_layer_size = self.hidden_layers_sizes[layer_index + 1] if cur_layer_size < prev_layer_size and cur_layer_size < next_layer_size: output_layer_number = layer_index + 1 break layer_number = len(self.layers) if output_layer_number == -1: output_layer_number = layer_number fout = smart_open(file_path, 'wb') for i in xrange(output_layer_number): # decide the dropout factor for this layer dropout_factor = 0.0 if i == 0: dropout_factor = self.input_dropout_factor if i > 0 and len(self.dropout_factor) > 0: dropout_factor = self.dropout_factor[i - 1] activation_text = '<' + self.cfg.activation_text + '>' if i == ( layer_number - 1 ) and with_softmax: # we assume that the last layer is a softmax layer activation_text = '<softmax>' W_mat = (1.0 - dropout_factor) * self.layers[i].W.get_value() b_vec = self.layers[i].b.get_value() input_size, output_size = W_mat.shape W_layer = [] b_layer = '' for rowX in xrange(output_size): W_layer.append('') for x in xrange(input_size): for t in xrange(output_size): W_layer[t] = W_layer[t] + str(W_mat[x][t]) + ' ' for x in xrange(output_size): b_layer = b_layer + str(b_vec[x]) + ' ' fout.write('<affinetransform> ' + str(output_size) + ' ' + str(input_size) + '\n') fout.write('[' + '\n') for x in xrange(output_size): fout.write(W_layer[x].strip() + '\n') fout.write(']' + '\n') fout.write('[ ' + b_layer.strip() + ' ]' + '\n') if activation_text == '<maxout>': fout.write(activation_text + ' ' + str(output_size / self.pool_size) + ' ' + str(output_size) + '\n') else: fout.write(activation_text + ' ' + str(output_size) + ' ' + str(output_size) + '\n') fout.close()
def load_river_network(nnet_param = 'neural_network/river_network_params', nnet_cfg = 'neural_network/river_network_cfg'): cfg = cPickle.load(smart_open(nnet_cfg,'r')) cfg.init_activation() model = DNN(numpy_rng=numpy_rng, cfg = cfg) _file2nnet(model.layers, filename = nnet_param) get_river_probs = model.build_extract_feat_function(-1) return get_river_probs
def _file2nnet(layers, set_layer_num = -1, filename='nnet.in', factor=1.0): n_layers = len(layers) nnet_dict = {} if set_layer_num == -1: set_layer_num = n_layers #with open('myfile', 'w') as f: # f.write('') with smart_open(filename, 'rb') as fp: nnet_dict = json.load(fp) for i in xrange(set_layer_num): dict_a = 'W' + str(i) #with open('myfile', 'a') as f: # f.write('*'*30+dict_a +'\n'+nnet_dict[dict_a]) layer = layers[i] if layer.type == 'fc': layer.W.set_value(factor * np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX)) elif layer.type == 'conv': filter_shape = layer.filter_shape W_array = layer.W.get_value() for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) W_array[next_X, this_X, :, :] = factor * np.asarray(string_2_array(nnet_dict[new_dict_a]), dtype=theano.config.floatX) layer.W.set_value(W_array) dict_a = 'b' + str(i) layer.b.set_value(np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX))
def _file2nnet(layers, set_layer_num=-1, filename='nnet.in', factor=1.0): n_layers = len(layers) nnet_dict = {} if set_layer_num == -1: set_layer_num = n_layers with smart_open(filename, 'rb') as fp: nnet_dict = json.load(fp) for i in xrange(set_layer_num): dict_a = 'W' + str(i) layer = layers[i] if layer.type == 'fc': mat_shape = layer.W.get_value().shape layer.W.set_value( factor * np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX).reshape(mat_shape)) elif layer.type == 'conv': filter_shape = layer.filter_shape W_array = layer.W.get_value() for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) mat_shape = W_array[next_X, this_X, :, :].shape W_array[next_X, this_X, :, :] = factor * np.asarray( string_2_array(nnet_dict[new_dict_a]), dtype=theano.config.floatX).reshape(mat_shape) layer.W.set_value(W_array) dict_a = 'b' + str(i) layer.b.set_value( np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX))
def read_nocompression_next_utt(next_scp_line): # this shouldn't happen if next_scp_line == '' or next_scp_line == None: # we are reaching the end of one epoch return '', None utt_id, path_pos = next_scp_line.replace('\n', '').split(' ') path, pos = path_pos.split(':') ark_read_buffer = smart_open(path, 'rb') ark_read_buffer.seek(int(pos), 0) # now start to read the feature matrix into a numpy matrix header = struct.unpack('<xcccc', ark_read_buffer.read(5)) if header[0] != "B" and header[0] != b'B': print("Input .ark file is not binary") exit(1) rows = 0 cols = 0 m, rows = struct.unpack('<bi', ark_read_buffer.read(5)) n, cols = struct.unpack('<bi', ark_read_buffer.read(5)) tmp_mat = numpy.frombuffer(ark_read_buffer.read(rows * cols * 4), dtype=numpy.float32) utt_mat = numpy.reshape(tmp_mat, (rows, cols)) ark_read_buffer.close() return utt_id, utt_mat
def initialize_read(self, first_time_reading = False): self.pfile_path = self.pfile_path_list[self.cur_pfile_index] self.file_read = smart_open(self.pfile_path) self.read_pfile_info() self.end_reading = False self.file_read.seek(self.header_size, 0) self.frame_to_read = self.total_frame_num
def read_two_integers(file): file_open = smart_open(file, 'r') line = file_open.readline().replace('\n', '') int1 = int(line) line = file_open.readline().replace('\n', '') int2 = int(line) file_open.close() return int1, int2
def read_two_integers(file): file_open = smart_open(file, 'r') line = file_open.readline().replace('\n','') int1 = int(line) line = file_open.readline().replace('\n','') int2 = int(line) file_open.close() return int1, int2
def _nnet2file(model, set_layer_num=-1, filename='nnet.out', start_layer=0, input_factor=0.0, factor=[]): print "[Debug] _nnet2file" nnet_dict = {} weight = [] b = [] layers = model.layers params = lasagne.layers.get_all_params(model.network, trainable=True) for i in range(len(params)): if params[i].ndim > 1: weight.append(params[i].get_value()) else: b.append(params[i].get_value()) print "[Debug] len(weight): ", len(weight) print "[Debug] len(b): ", len(b) index_w = 0 index_b = 0 for i in range(len(layers)): #dict_a = 'W' + str(i) dropout_factor = 0.0 if layers[i].type == 'fc': # Have 2-D Weight and Bias print "[Debug-fc] w.shape: ", weight[index_w].shape dict_a = 'W' + str(i) nnet_dict[dict_a] = array_2_string( (1.0 - dropout_factor) * weight[index_w]) index_w += 1 dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(b[index_b]) index_b += 1 elif layers[i].type == 'conv': # Have 4-D Weight and Bias dict_a = 'W' + str(i) filter_shape = layers[i].filter_shape print "[Debug-conv] filter_shape: ", filter_shape print "[Debug-conv] w.shape: ", weight[index_w].shape for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[new_dict_a] = array_2_string( weight[index_w][next_X, this_X]) index_w += 1 dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(b[index_b]) index_b += 1 elif layers[i].type == 'sum': print "[Debug-sum]" with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys=True) fp.flush()
def initialize_read(self, first_time_reading = False): self.scp_file_read = smart_open(self.scp_file, 'r') if first_time_reading: utt_id, utt_mat = self.read_next_utt() self.original_feat_dim = utt_mat.shape[1] self.scp_file_read = smart_open(self.scp_file, 'r') # compute the feature dimension self.feat_dim = (self.read_opts['lcxt'] + 1 + self.read_opts['rcxt']) * self.original_feat_dim # allocate the feat matrix according to the specified partition size self.max_frame_num = self.read_opts['partition'] / (self.feat_dim * 4) self.feats = numpy.zeros((self.max_frame_num, self.feat_dim), dtype=theano.config.floatX) if self.ali_provided: self.read_alignment() self.labels = numpy.zeros((self.max_frame_num,), dtype=numpy.int32) self.end_reading = False
def _cfg2file(cfg, filename='cfg.out'): cfg.lrate = None cfg.train_sets = None; cfg.train_xy = None; cfg.train_x = None; cfg.train_y = None cfg.valid_sets = None; cfg.valid_xy = None; cfg.valid_x = None; cfg.valid_y = None cfg.activation = None # saving the rectifier function causes errors; thus we don't save the activation function # the activation function is initialized from the activation text ("sigmoid") when the network # configuration is loaded with smart_open(filename, "wb") as output: cPickle.dump(cfg, output, cPickle.HIGHEST_PROTOCOL)
def _file2nnet(model, set_layer_num=-1, filename='nnet.in', factor=1.0): print "[Debug] _file2nnet" nnet_dict = {} old_params = lasagne.layers.get_all_params(model.network, trainable=True) new_params = [] index = 0 with smart_open(filename, 'rb') as fp: nnet_dict = json.load(fp) for i in xrange(len(model.layers)): #dict_a = 'W' + str(i) layer = model.layers[i] if layer.type == 'fc': print "[fc] W and b" dict_a = 'W' + str(i) mat_shape = old_params[index].get_value().shape print "[Debug-fc] mat_shape ", mat_shape, layer.filter_shape W_mat = factor * np.asarray( string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX).reshape(mat_shape) new_params.append(W_mat) dict_a = 'b' + str(i) b_vec = np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX) new_params.append(b_vec) index += 2 elif layer.type == 'conv': print "[conv] W and b" dict_a = 'W' + str(i) filter_shape = layer.filter_shape W_array = old_params[index].get_value() for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) mat_shape = W_array[next_X, this_X, :, :].shape W_array[next_X, this_X, :, :] = factor * np.asarray( string_2_array(nnet_dict[new_dict_a]), dtype=theano.config.floatX).reshape(mat_shape) new_params.append(W_array) dict_a = 'b' + str(i) b_vec = np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX) new_params.append(b_vec) index += 2 elif layer.type == 'sum': print "[sum] no trainable params" # update params to network #print "Update the trainable params to network: ", new_params lasagne.layers.set_all_param_values(model.network, new_params, trainable=True)
def initialize_read(self, first_time_reading = False): self.pfile_path = self.pfile_path_list[self.cur_pfile_index] self.file_read = smart_open(self.pfile_path) self.read_pfile_info() self.end_reading = False self.file_read.seek(self.header_size, 0) self.sentence_index = 0 self.feat_buffer = numpy.zeros((0, self.feat_dim), dtype = theano.config.floatX) self.label_buffer = numpy.zeros((0,), dtype = 'int')
def ReadScp(self, scp_line): if scp_line is None: return None self._key, path_pos = scp_line.replace('\n', '').split(' ') path, pos = path_pos.split(':') latfp = smart_open(path, 'rb') latfp.seek(int(pos), 0) Fst.Read(self, latfp) latfp.close() return self._key
def write_conv_config(self, file_path_prefix): for i in xrange(len(self.conv_layer_configs)): self.conv_layer_configs[i][ 'activation'] = self.cfg.conv_activation_text with smart_open(file_path_prefix + '.' + str(i), 'wb') as fp: json.dump(self.conv_layer_configs[i], fp, indent=2, sort_keys=True) fp.flush()
def resume_lrate(lrate, lrate_file): file_open = smart_open(lrate_file, 'r') line = file_open.readline().replace('\n','') lrate.epoch = int(line) line = file_open.readline().replace('\n','') lrate.rate = float(line) line = file_open.readline().replace('\n','') lrate.lowest_error = float(line) line = file_open.readline().replace('\n','') lrate.decay = bool(int(line)) file_open.close()
def _nnet2file(layers, set_layer_num=-1, filename='nnet.out', start_layer=0, input_factor=0.0, factor=[]): n_layers = len(layers) nnet_dict = {} if set_layer_num == -1: set_layer_num = n_layers for i in range(start_layer, set_layer_num): layer = layers[i] dict_a = 'W' + str(i) dropout_factor = 0.0 if i == 0: dropout_factor = input_factor if i > 0 and len(factor) > 0: dropout_factor = factor[i - 1] if layer.type in ['fc', 'factored', 'wfactored']: if type(layer.W) != list: nnet_dict[dict_a] = \ array_2_string((1.0 - dropout_factor) * layer.W.get_value()) else: for j in range(len(layer.W)): dict_a = 'W{}_{}'.format(i, j) nnet_dict[dict_a] = \ array_2_string((1.0 - dropout_factor) * layer.W[j].get_value()) if layer.type == 'factored': for j in range(len(layer.side_Ws)): dict_a = 'side_W{}_{}'.format(i, j) nnet_dict[dict_a] = array_2_string( layer.side_Ws[j].get_value()) for j in range(len(layer.side_bs)): dict_a = 'side_b{}_{}'.format(i, j) nnet_dict[dict_a] = array_2_string( layer.side_bs[j].get_value()) elif layer.type == 'conv': filter_shape = layer.filter_shape for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[new_dict_a] = array_2_string( (1.0 - dropout_factor) * (layer.W.get_value())[next_X, this_X]) dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(layer.b.get_value()) with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys=True) fp.flush()
def resume_lrate(lrate, lrate_file): file_open = smart_open(lrate_file, 'r') line = file_open.readline().replace('\n', '') lrate.epoch = int(line) line = file_open.readline().replace('\n', '') lrate.rate = float(line) line = file_open.readline().replace('\n', '') lrate.lowest_error = float(line) line = file_open.readline().replace('\n', '') lrate.decay = bool(int(line)) file_open.close()
def initialize_read(self, first_time_reading = False): pfile_path = self.pfile_path_list[self.cur_pfile_index] self.file_read = smart_open(pfile_path, 'rb') if first_time_reading or len(self.pfile_path_list) > 1: self.feat_mats = [] self.label_vecs = [] self.read_pfile_info() self.read_pfile_data() self.end_reading = False self.partition_index = 0
def initialize_read(self, first_time_reading=False): pfile_path = self.pfile_path_list[self.cur_pfile_index] self.file_read = smart_open(pfile_path, 'rb') if first_time_reading or len(self.pfile_path_list) > 1: self.feat_mats = [] self.label_vecs = [] self.read_pfile_info() self.read_pfile_data() self.end_reading = False self.partition_index = 0
def read_alignment(self): f_read = smart_open(self.ali_file, 'r') for line in f_read: line = line.replace('\n','').strip() if len(line) < 1: # this is an empty line, skip continue [utt_id, utt_ali] = line.split(' ', 1) # this utterance has empty alignment, skip if len(utt_ali) < 1: continue self.alignment[utt_id] = numpy.fromstring(utt_ali, dtype=numpy.int32, sep=' ') f_read.close()
def read_ark(ark_file, endian='<', return_position=False): assert str(endian) in ('<', '>'), endian size = 0 fd = smart_open(ark_file, 'rb') while True: key = read_token(fd) if key is None: break size += len(key) + 1 array, _size = read_kaldi(fd, str(endian), return_size=True) print(key, array) size += _size
def _file2nnet(layers, set_layer_num=-1, filename='nnet.in', factor=1.0, exc_layers=[]): """ Don't load params for layers in `exc_layers`. """ n_layers = len(layers) nnet_dict = {} if set_layer_num == -1: set_layer_num = n_layers with smart_open(filename, 'rb') as fp: nnet_dict = json.load(fp) for i in xrange(set_layer_num): if i in exc_layers: continue dict_a = 'W' + str(i) layer = layers[i] if layer.type == 'fc' or layer.type == 'factored': mat_shape = layer.W.get_value().shape layer.W.set_value( factor * np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX).reshape(mat_shape)) if layer.type == 'factored': for j in range(len(layer.side_Ws)): dict_a = 'side_W{}_{}'.format(i, j) mat_shape = layer.side_Ws[j].get_value().shape layer.side_Ws[j].set_value( np.asarray( string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX).reshape(mat_shape)) for j in range(len(layer.side_bs)): dict_a = 'side_b{}_{}'.format(i, j) layer.side_bs[j].set_value( np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX)) elif layer.type == 'conv': filter_shape = layer.filter_shape W_array = layer.W.get_value() for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) mat_shape = W_array[next_X, this_X, :, :].shape W_array[next_X, this_X, :, :] = factor * np.asarray( string_2_array(nnet_dict[new_dict_a]), dtype=theano.config.floatX).reshape(mat_shape) layer.W.set_value(W_array) dict_a = 'b' + str(i) layer.b.set_value( np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX))
def ReadScp(scp_file): scp_dict = {} f_read = smart_open(scp_file, 'r') for line in f_read: line = line.replace('\n','').strip() if len(line) < 1: # this is an empty line, skip continue [utt_id, utt_val] = line.split() # this utterance has empty alignment, skip scp_dict[utt_id] = line f_read.close() return scp_dict
def write_model_to_kaldi(self, file_path, with_softmax = True): # determine whether it's BNF based on layer sizes output_layer_number = -1; for layer_index in range(1, self.hidden_layers_number - 1): cur_layer_size = self.hidden_layers_sizes[layer_index] prev_layer_size = self.hidden_layers_sizes[layer_index-1] next_layer_size = self.hidden_layers_sizes[layer_index+1] if cur_layer_size < prev_layer_size and cur_layer_size < next_layer_size: output_layer_number = layer_index+1; break layer_number = len(self.layers) if output_layer_number == -1: output_layer_number = layer_number fout = smart_open(file_path, 'wb') for i in range(output_layer_number): # decide the dropout factor for this layer dropout_factor = 0.0 if i == 0: dropout_factor = self.input_dropout_factor if i > 0 and len(self.dropout_factor) > 0: dropout_factor = self.dropout_factor[i-1] activation_text = '<' + self.cfg.activation_text + '>' if i == (layer_number-1) and with_softmax: # we assume that the last layer is a softmax layer activation_text = '<softmax>' W_mat = (1.0 - dropout_factor) * self.layers[i].W.get_value() b_vec = self.layers[i].b.get_value() input_size, output_size = W_mat.shape W_layer = []; b_layer = '' for rowX in range(output_size): W_layer.append('') for x in range(input_size): for t in range(output_size): W_layer[t] = W_layer[t] + str(W_mat[x][t]) + ' ' for x in range(output_size): b_layer = b_layer + str(b_vec[x]) + ' ' fout.write('<affinetransform> ' + str(output_size) + ' ' + str(input_size) + '\n') fout.write('[' + '\n') for x in range(output_size): fout.write(W_layer[x].strip() + '\n') fout.write(']' + '\n') fout.write('[ ' + b_layer.strip() + ' ]' + '\n') if activation_text == '<maxout>': fout.write(activation_text + ' ' + str(output_size/self.pool_size) + ' ' + str(output_size) + '\n') else: fout.write(activation_text + ' ' + str(output_size) + ' ' + str(output_size) + '\n') fout.close()
def read_alignment(ali_file): alignment = {} f_read = smart_open(ali_file, 'r') for line in f_read: line = line.replace('\n','').strip() if len(line) < 1: # this is an empty line, skip continue [utt_id, utt_ali] = line.split(' ', 1) # this utterance has empty alignment, skip if len(utt_ali) < 1: continue alignment[utt_id] = numpy.fromstring(utt_ali, dtype=numpy.int32, sep=' ') f_read.close() return alignment
def _cfg2file(cfg, filename='cfg.out'): cfg.lrate = None cfg.train_sets = None cfg.train_xy = None cfg.train_x = None cfg.train_y = None cfg.valid_sets = None cfg.valid_xy = None cfg.valid_x = None cfg.valid_y = None cfg.activation = None # saving the rectifier function causes errors; thus we don't save the activation function # the activation function is initialized from the activation text ("sigmoid") when the network # configuration is loaded with smart_open(filename, "wb") as output: cPickle.dump(cfg, output, cPickle.HIGHEST_PROTOCOL)
def initialize_read(self, first_time_reading=False): self.scp_file_read = smart_open(self.scp_file, 'r') if first_time_reading: utt_id, utt_mat = self.read_next_utt() self.original_feat_dim = utt_mat.shape[1] self.scp_file_read = smart_open(self.scp_file, 'r') # compute the feature dimension self.feat_dim = (self.read_opts['lcxt'] + 1 + self.read_opts['rcxt']) * self.original_feat_dim # allocate the feat matrix according to the specified partition size self.max_frame_num = self.read_opts['partition'] / (self.feat_dim * 4) self.feats = numpy.zeros((self.max_frame_num, self.feat_dim), dtype=theano.config.floatX) if self.ali_provided: self.read_alignment() self.labels = numpy.zeros(self.max_frame_num, dtype=numpy.int32) self.end_reading = False self.feat_buffer = None self.label_buffer = None
def _rbm2file(rbm_layers, filename='rbm.out'): rbm_dict = {} for i in range(len(rbm_layers)): layer = rbm_layers[i] # W kW = 'W' + str(i) rbm_dict[kW] = array_2_string(layer.W.get_value()) # hbias khb = 'hbias' + str(i) rbm_dict[khb] = array_2_string(layer.hbias.get_value()) # vbias kvb = 'vbias' + str(i) rbm_dict[kvb] = array_2_string(layer.vbias.get_value()) with smart_open(filename, 'wb') as fp: json.dump(rbm_dict, fp, indent=2, sort_keys=True) fp.flush()
def initialize_read(self, first_time_reading=False): self.pfile_path = self.pfile_path_list[self.cur_pfile_index] self.file_read = smart_open(self.pfile_path) self.read_pfile_info() self.end_reading = False self.file_read.seek(self.header_size, 0) self.sentence_index = 0 if first_time_reading: self.feat = numpy.zeros((self.frame_per_partition, self.feat_dim), dtype=theano.config.floatX) self.label = numpy.zeros(self.frame_per_partition, dtype=numpy.int32) self.feat_buffer = None self.label_buffer = None
def write_model_to_kaldi(self, file_path, with_softmax=True): # determine whether it's BNF based on layer sizes print "[Debug] write_model_to_kaldi" fout = smart_open(file_path, 'wb') params_now = lasagne.layers.get_all_params(self.network, trainable=True) print "[Debug] params_now: ", params_now num_fc = len(self.cfg.hidden_layers_sizes) + 1 start = len(params_now) - 2 * num_fc end = len(params_now) print "[Debug] num_fc ", num_fc print "[Debug] start, end : ", start, end for i in range(start, end, 2): #if self.layers[i].type == 'fc': activation_text = '<' + self.cfg.activation_text + '>' if i == ( end - 2 ) and with_softmax: # we assume that the last layer is a softmax layer activation_text = '<softmax>' W_mat = params_now[i].get_value() b_vec = params_now[i + 1].get_value() input_size, output_size = W_mat.shape W_layer = [''] * output_size b_layer = '' for t in xrange(output_size): b_layer = b_layer + str(b_vec[t]) + ' ' W_layer[t] = ' '.join(map(str, W_mat[:, t])) + ' ' fout.write('<affinetransform> ' + str(output_size) + ' ' + str(input_size) + '\n') fout.write('[' + '\n') for x in xrange(output_size): fout.write(W_layer[x].strip() + '\n') fout.write(']' + '\n') fout.write('[ ' + b_layer.strip() + ' ]' + '\n') if activation_text == '<maxout>': fout.write(activation_text + ' ' + str(output_size / self.pool_size) + ' ' + str(output_size) + '\n') else: fout.write(activation_text + ' ' + str(output_size) + ' ' + str(output_size) + '\n') fout.close()
def _file2cnn(conv_layers, filename='nnet.in', factor=1.0): n_layers = len(conv_layers) nnet_dict = {} with smart_open(filename, 'rb') as fp: nnet_dict = json.load(fp) for i in xrange(n_layers): conv_layer = conv_layers[i] filter_shape = conv_layer.filter_shape W_array = conv_layer.W.get_value() for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): dict_a = 'W' + str(i) + ' ' + str(next_X) + ' ' + str(this_X) W_array[next_X, this_X, :, :] = factor * np.asarray(string_2_array(nnet_dict[dict_a])) conv_layer.W.set_value(W_array) dict_a = 'b' + str(i) conv_layer.b.set_value(np.asarray(string_2_array(nnet_dict[dict_a]), dtype=theano.config.floatX))
def _file2rbm(rbm_layers, filename='rbm.in'): with smart_open(filename, 'rb') as fp: rbm_dict = json.load(fp) for i in range(len(rbm_layers)): layer = rbm_layers[i] # W kW = 'W' + str(i) shape = layer.W.get_value().shape layer.W.set_value( np.asarray(string_2_array(rbm_dict[kW]), dtype=theano.config.floatX).reshape(shape)) # hbias khb = 'hbias' + str(i) layer.hbias.set_value( np.asarray(string_2_array(rbm_dict[khb]), dtype=theano.config.floatX)) # vbias kvb = 'vbias' + str(i) layer.vbias.set_value( np.asarray(string_2_array(rbm_dict[kvb]), dtype=theano.config.floatX))
def _nnet2file(model, set_layer_num=-1, filename='nnet.out', start_layer=0, input_factor=0.0, factor=[]): print "[Debug] _nnet2file" nnet_dict = {} weight = [] b = [] layers = model.layers params = lasagne.layers.get_all_params(model.network) for i in range(0, len(params), 2): weight.append(params[i].get_value()) b.append(params[i + 1].get_value()) for i in range(len(layers)): dict_a = 'W' + str(i) dropout_factor = 0.0 print "[Debug] w.shape: ", weight[i].shape if layers[i].type == 'fc': nnet_dict[dict_a] = array_2_string( (1.0 - dropout_factor) * weight[i]) elif layers[i].type == 'conv': filter_shape = layers[i].filter_shape print "[Debug] filter_shape: ", filter_shape for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): new_dict_a = dict_a + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[new_dict_a] = array_2_string(weight[i][next_X, this_X]) dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(b[i]) with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys=True) fp.flush()
def write_model_to_kaldi(self, file_path, with_softmax=True): # determine whether it's BNF based on layer sizes print "[Debug] write_model_to_kaldi" fout = smart_open(file_path, 'wb') params = lasagne.layers.get_all_params(self.network, trainable=True) activation_text = '<softmax>' W_mat = params[-2].get_value() b_vec = params[-1].get_value() input_size, output_size = W_mat.shape W_layer = [] b_layer = '' for rowX in xrange(output_size): W_layer.append('') for x in xrange(input_size): for t in xrange(output_size): W_layer[t] = W_layer[t] + str(W_mat[x][t]) + ' ' for x in xrange(output_size): b_layer = b_layer + str(b_vec[x]) + ' ' fout.write('<affinetransform> ' + str(output_size) + ' ' + str(input_size) + '\n') fout.write('[' + '\n') for x in xrange(output_size): fout.write(W_layer[x].strip() + '\n') fout.write(']' + '\n') fout.write('[ ' + b_layer.strip() + ' ]' + '\n') if activation_text == '<maxout>': fout.write(activation_text + ' ' + str(output_size / self.pool_size) + ' ' + str(output_size) + '\n') else: fout.write(activation_text + ' ' + str(output_size) + ' ' + str(output_size) + '\n') fout.close()
def load_next_partition(self, shared_xy): pfile_path = self.pfile_path_list[self.cur_pfile_index] if self.feat_mat is None or len(self.pfile_path_list) > 1: fopen = smart_open(pfile_path, 'rb') self.feat_mat, self.label_vec = load(fopen) fopen.close() shared_x, shared_y = shared_xy self.feat_mat, self.label_vec = \ preprocess_feature_and_label(self.feat_mat, self.label_vec, self.read_opts) if self.read_opts['random']: shuffle_feature_and_label(self.feat_mat, self.label_vec) shared_x.set_value(self.feat_mat, borrow=True) shared_y.set_value(self.label_vec.astype(theano.config.floatX), borrow=True) self.cur_frame_num = len(self.feat_mat) self.cur_pfile_index += 1 if self.cur_pfile_index >= len(self.pfile_path_list): # the end of one epoch self.end_reading = True self.cur_pfile_index = 0
def load_next_partition(self, shared_xy): pfile_path = self.pfile_path_list[self.cur_pfile_index] if self.feat_mat is None or len(self.pfile_path_list) > 1: fopen = smart_open(pfile_path, 'rb') self.feat_mat, self.label_vec = cPickle.load(fopen) fopen.close() shared_x, shared_y = shared_xy if self.read_opts['random']: # randomly shuffle features and labels in the *same* order numpy.random.seed(18877) numpy.random.shuffle(self.feat_mat) numpy.random.seed(18877) numpy.random.shuffle(self.label_vec) shared_x.set_value(self.feat_mat, borrow=True) shared_y.set_value(self.label_vec.astype(numpy.float32), borrow=True) self.cur_frame_num = len(self.feat_mat) self.cur_pfile_index += 1 if self.cur_pfile_index >= len(self.pfile_path_list): # the end of one epoch self.end_reading = True self.cur_pfile_index = 0
def read_next_utt(self): next_scp_line = self.scp_file_read.readline() if next_scp_line == '' or next_scp_line == None: return '', None utt_id, path_pos = next_scp_line.replace('\n','').split(' ') path, pos = path_pos.split(':') ark_read_buffer = smart_open(path, 'rb') ark_read_buffer.seek(int(pos),0) header = struct.unpack('<xcccc', ark_read_buffer.read(5)) if header[0] != "B": print("Input .ark file is not binary"); exit(1) rows = 0; cols= 0 m, rows = struct.unpack('<bi', ark_read_buffer.read(5)) n, cols = struct.unpack('<bi', ark_read_buffer.read(5)) tmp_mat = numpy.frombuffer(ark_read_buffer.read(rows * cols * 4), dtype=numpy.float32) utt_mat = numpy.reshape(tmp_mat, (rows, cols)) ark_read_buffer.close() return utt_id, utt_mat
def _cnn2file(conv_layers, filename='nnet.out', input_factor = 1.0, factor=[]): n_layers = len(conv_layers) nnet_dict = {} for i in xrange(n_layers): conv_layer = conv_layers[i] filter_shape = conv_layer.filter_shape dropout_factor = 0.0 if i == 0: dropout_factor = input_factor if i > 0 and len(factor) > 0: dropout_factor = factor[i-1] for next_X in xrange(filter_shape[0]): for this_X in xrange(filter_shape[1]): dict_a = 'W' + str(i) + ' ' + str(next_X) + ' ' + str(this_X) nnet_dict[dict_a] = array_2_string(dropout_factor * (conv_layer.W.get_value())[next_X, this_X]) dict_a = 'b' + str(i) nnet_dict[dict_a] = array_2_string(conv_layer.b.get_value()) with smart_open(filename, 'wb') as fp: json.dump(nnet_dict, fp, indent=2, sort_keys = True) fp.flush()
# check the arguments arg_elements = [sys.argv[i] for i in range(1, len(sys.argv))] arguments = parse_arguments(arg_elements) required_arguments = ['in_scp_file', 'out_ark_file', 'cnn_param_file', 'cnn_cfg_file'] for arg in required_arguments: if arguments.has_key(arg) == False: print "Error: the argument %s has to be specified" % (arg); exit(1) # mandatory arguments in_scp_file = arguments['in_scp_file'] out_ark_file = arguments['out_ark_file'] cnn_param_file = arguments['cnn_param_file'] cnn_cfg_file = arguments['cnn_cfg_file'] # network structure cfg = cPickle.load(smart_open(cnn_cfg_file,'r')) conv_configs = cfg.conv_layer_configs conv_layer_number = len(conv_configs) for i in xrange(conv_layer_number): conv_configs[i]['activation'] = cfg.conv_activation # whether to use the fast mode use_fast = cfg.use_fast if arguments.has_key('use_fast'): use_fast = string_2_bool(arguments['use_fast']) kaldiread = KaldiReadIn(in_scp_file) kaldiwrite = KaldiWriteOut(out_ark_file)
arg_elements = [sys.argv[i] for i in range(1, len(sys.argv))] arguments = parse_arguments(arg_elements) required_arguments = ['in_scp_file', 'out_ark_file', 'nnet_param', 'nnet_cfg', 'layer_index'] for arg in required_arguments: if arguments.has_key(arg) == False: print "Error: the argument %s has to be specified" % (arg); exit(1) # mandatory arguments in_scp_file = arguments['in_scp_file'] out_ark_file = arguments['out_ark_file'] nnet_param = arguments['nnet_param'] nnet_cfg = arguments['nnet_cfg'] layer_index = int(arguments['layer_index']) # load network configuration cfg = cPickle.load(smart_open(nnet_cfg,'r')) cfg.init_activation() # set up the model with model config log('> ... setting up the model and loading parameters') numpy_rng = numpy.random.RandomState(89677) theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) cfg = cPickle.load(smart_open(nnet_cfg,'r')) model = None if cfg.model_type == 'DNN': model = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg) elif cfg.model_type == 'CNN': model = CNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg, testing = True) # load model parameters _file2nnet(model.layers, filename = nnet_param)
# mandatory arguments data_spec = arguments['data'] nnet_param = arguments['nnet_param'] nnet_cfg = arguments['nnet_cfg'] output_path = arguments['output_path'] batch_size = float(arguments['batch_size']) log("Dump original data") countItems,initialDim = dumpInput(output_path,perplexity,data_spec) log("Original data dumped. Items="+str(countItems)+" initialDim="+str(initialDim)) # load network configuration and set up the model log('> ... setting up the model and loading parameters') cfg = pickle.load(smart_open(nnet_cfg,'rb')) layerNr = cfg.totalNumerOfLayers() log('Total number of layers '+str(layerNr)) for i in range(0,layerNr): count = 0 log("Going to output layer="+str(i)) files = [] layer_index = i numpy_rng = numpy.random.RandomState(89677) theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) cfg = pickle.load(smart_open(nnet_cfg,'rb')) cfg.init_activation() model = None if cfg.model_type == 'DNN': model = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg)
def save_two_integers(integers, file): file_open = smart_open(file, 'w') # always overwrite file_open.write(str(integers[0]) + '\n') file_open.write(str(integers[1]) + '\n') file_open.close()
def __init__(self, scp_path): self.scp_path = scp_path self.scp_file_read = smart_open(self.scp_path,"r")
def write_conv_config(self, file_path_prefix): for i in xrange(len(self.conv_layer_configs)): self.conv_layer_configs[i]["activation"] = self.cfg.conv_activation_text with smart_open(file_path_prefix + "." + str(i), "wb") as fp: json.dump(self.conv_layer_configs[i], fp, indent=2, sort_keys=True) fp.flush()
if arguments.has_key(arg) == False: print "Error: the argument %s has to be specified" % (arg); exit(1) # mandatory arguments data_spec = arguments['data'] nnet_param = arguments['nnet_param'] nnet_cfg = arguments['nnet_cfg'] output_file = arguments['output_file'] layer_index = int(arguments['layer_index']) batch_size = float(arguments['batch_size']) # load network configuration and set up the model log('> ... setting up the model and loading parameters') numpy_rng = numpy.random.RandomState(89677) theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) cfg = cPickle.load(smart_open(nnet_cfg,'r')) cfg.init_activation() model = None if cfg.model_type == 'DNN': model = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg) elif cfg.model_type == 'CNN': model = CNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg, testing = True) # load model parameters _file2nnet(model.layers, filename = nnet_param) # initialize data reading cfg.init_data_reading_test(data_spec) # get the function for feature extraction log('> ... getting the feat-extraction function')
def __init__(self, ark_path): self.ark_path = ark_path self.ark_file_write = smart_open(ark_path,"wb")
def reopen_file(self): self.file_read = smart_open(self.pfile_path, 'rb') self.read_pfile_info() self.initialize_read()