ncnn:https://github.com/Tencent/ncnn
1、.h5模型保存为TFSaveModel格式
import tensorflow as tf
from keras.models import load_model# 加载Keras模型
model = load_model('encoder_model.h5')# 转换为SavedModel类型
tf.saved_model.save(model, 'TFSaveModel')
2、TFSaveModel格式模型保存为onnx模型
python3 -m tf2onnx.convert --saved-model TFSaveModel --output onnxModel/encoder_model.onnx
打开https://netron.app/来看下网络结构,主要是先看输入部分的维度(网络结构后面会细讲哈)
可以看到输入维度[unk__64、unk__65、62],我们需要将unk__64、unk__65这两个改为具体数值,否则在导出ncnn模型时会报一些op不支持的错误,那么问题来了,要怎么改呢,我也不知道啊!!!
哈哈哈,开完笑的,都写出来了,怎么会不知道,请听我慢慢说来。
其实数据第一个是batch,第二个是输入句子的最大长度,第三个是字符总数量,我们在推理时,batch size一般为1,所以这个input_1的shape就是[1,max_encoder_seq_length, num_encoder_tokens]
max_encoder_seq_length, num_encoder_tokens 这两个参数可以在训练的时候获取到了,拿到这个input shape 之后,对onnx模型进行simplify,我训练出来的模型时得到的shape是[1,16,62],因此执行以下命令:
python3 -m onnxsim onnxModel/encoder_model.onnx onnxModel/encoder_model-sim.onnx --overwrite-input-shape 1,16,62
可得到简化后的onnx模型啦
这个时候,我们再用https://netron.app打开encoder_model-sim.onnx,可以看到encoder模型的输出了,有两个输出,均为[1,256]的维度
然后我们需要对decoder_model.h5也进行转换,
import tensorflow as tf
from keras.models import load_model# 加载Keras模型
model = load_model('decoder_model.h5')# 转换为SavedModel类型
tf.saved_model.save(model, 'TFSaveModel')
python3 -m tf2onnx.convert --saved-model TFSaveModel --output onnxModel/decoder_model.onnx
同样打开模型来看,能看到一共有三个输入,其中的input_3:[unk__57,256],input_4:[unk__58,256],为encoder的输出,因此可以得到这两个输入维度均为[1,256],那 input_2:[unk__55,unk__56,849]的维度是多少呢,我们接着往下看。
我们想一想,解码器除了接受编码器的数据,还有什么数据没给它呢,没有错,就是target_characters的特征,对于英翻中而言就是中文的字符,要解码器解出中文,肯定要把中文数据给它,要不然你让解码器去解空气嘛,实际上这个 input_2的维度就是
target_seq = np.zeros((1, 1, num_decoder_tokens))
num_decoder_tokens同样可以在训练的时候获取到(至于不知道怎么来的,可以看这个系列文章的第一、二篇),我这边得到的num_decoder_tokens是849,当然实际上这个模型的 input_2:[unk__55,unk__56,849]已经给了num_decoder_tokens,我们只需要把unk__55,unk__56都改为1就可以了,即[1,1,849],那么对onnx进行simplify
python3 -m onnxsim onnxModel/decoder_model.onnx onnxModel/decoder_model-sim.onnx --overwrite-input-shape input_2:1,1,849 input_3:1,256 input_4:1,256
成功完成simplify可得到:
完成了onnx模型的转换之后,我们要做的就是将模型转换为ncnn模型
3、onnx模型转换为ncnn
onnx2ncnn onnxModel/encoder_model-sim.onnx ncnnModel/encoder_model.param ncnnModel/encoder_model.bin
onnx2ncnn onnxModel/decoder_model-sim.onnx ncnnModel/decoder_model.param ncnnModel/decoder_model.bin
转换成功可以看到:
ncnnoptimize ncnnModel/encoder_model.param ncnnModel/encoder_model.bin ncnnModel/encoder_model.param ncnnModel/encoder_model.bin 1
ncnnoptimize ncnnModel/decoder_model.param ncnnModel/decoder_model.bin ncnnModel/decoder_model.param ncnnModel/decoder_model.bin 1
4、ncnn模型加载与推理(python版)
有点问题,先把调试代码贴在下面吧
import numpy as np
import ncnn# 加载字符
# 从 input_words.txt 文件中读取字符串
with open('config/input_words.txt', 'r') as f:input_words = f.readlines()input_characters = [line.rstrip('\n') for line in input_words]# 从 target_words.txt 文件中读取字符串
with open('config/target_words.txt', 'r', newline='') as f:target_words = [line.strip() for line in f.readlines()]target_characters = [char.replace('\\t', '\t').replace('\\n', '\n') for char in target_words]#字符处理,以方便进行编码
input_token_index = dict([(char, i) for i, char in enumerate(input_characters)])
target_token_index = dict([(char, i) for i, char in enumerate(target_characters)])# something readable.
reverse_input_char_index = dict((i, char) for char, i in input_token_index.items())
reverse_target_char_index = dict((i, char) for char, i in target_token_index.items())
num_encoder_tokens = len(input_characters) # 英文字符数量
num_decoder_tokens = len(target_characters) # 中文文字数量import json
with open('config/config.json', 'r') as file:loaded_data = json.load(file)# 从加载的数据中获取max_encoder_seq_length和max_decoder_seq_length的值
max_encoder_seq_length = loaded_data["max_encoder_seq_length"]
max_decoder_seq_length = loaded_data["max_decoder_seq_length"]encoder_model = ncnn.Net()encoder_model.load_param("ncnnModel/encoder_model.param")
encoder_model.load_model("ncnnModel/encoder_model.bin")decoder_model = ncnn.Net()
decoder_model.load_param("ncnnModel/decoder_model.param")
decoder_model.load_model("ncnnModel/decoder_model.bin")def decode_sequence(input_seq):# Encode the input as state vectors.ex_encoder = encoder_model.create_extractor()ex_encoder.input("input_1", ncnn.Mat(input_seq))_, LSTM_1 = ex_encoder.extract("LSTM__31:1")_, LSTM_2 = ex_encoder.extract("LSTM__31:2")print(LSTM_1)print(LSTM_2)# Generate empty target sequence of length 1.target_seq = np.zeros((1, 1, 849))# Populate the first character of target sequence with the start character.target_seq[0, 0, target_token_index['\t']] = 1.# this target_seq you can treat as initial state# Sampling loop for a batch of sequences# (to simplify, here we assume a batch of size 1).stop_condition = Falsedecoded_sentence = ''while not stop_condition:ex_decoder = decoder_model.create_extractor()print(ncnn.Mat(target_seq))print("---------")ex_decoder.input("input_2", ncnn.Mat(target_seq))ex_decoder.input("input_3", LSTM_1)ex_decoder.input("input_4", LSTM_2)_, output_tokens = ex_decoder.extract("dense")_, h = ex_decoder.extract("lstm_1")_, c = ex_decoder.extract("lstm_1_1")print(output_tokens)print(h)print(c)print(fdsf)output_tokens = np.array(output_tokens)h = np.array(h)c = np.array(c)print(output_tokens.shape)print(output_tokens.shape)print(h.shape)print(c.shape)#print(gfdgd)#output_tokens, h, c = decoder_model.predict([target_seq] + states_value)# Sample a token# argmax: Returns the indices of the maximum values along an axis# just like find the most possible charsampled_token_index = np.argmax(output_tokens[0, -1, :])# find char using indexsampled_char = reverse_target_char_index[sampled_token_index]# and append sentencedecoded_sentence += sampled_char# Exit condition: either hit max length# or find stop character.if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length):stop_condition = True# Update the target sequence (of length 1).# append then ?# creating another new target_seq# and this time assume sampled_token_index to 1.0target_seq = np.zeros((1, 1, num_decoder_tokens))target_seq[0, 0, sampled_token_index] = 1.# Update states# update states, frome the front partsstates_value = [h, c]return decoded_sentenceimport numpy as npinput_text = "Call me."
encoder_input_data = np.zeros((1,max_encoder_seq_length, num_encoder_tokens),dtype='float32')
for t, char in enumerate(input_text):print(char)# 3D vector only z-index has char its value equals 1.0encoder_input_data[0,t, input_token_index[char]] = 1.input_seq = encoder_input_data
decoded_sentence = decode_sequence(input_seq)
print('-')
print('Input sentence:', input_text)
print('Decoded sentence:', decoded_sentence)