|
本帖最后由 Hhhhlnlnlnh 于 2019-5-21 13:49 编辑
rknn-toolkit版本是0.9.9
原始pb模型和rknn模型放在百度云盘里
链接:https://pan.baidu.com/s/1doU0uLZXVBMv9EZ6C_r4rA
提取码:rlsq
keras模型代码
- input_data = Input(name='the_input', shape=(320000, 1), dtype='float32')
- input_wavImage = Reshape((1600, 200, 1), name='Reshape')(input_data)
- layer_h1 = Conv2D(32, (3,3), use_bias=False, activation='relu', padding='same', kernel_initializer='he_normal')(input_wavImage) # 卷积层
- layer_h1 = Dropout(0.05)(layer_h1)
- layer_h2 = Conv2D(32, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h1) # 卷积层
- layer_h3 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h2) # 池化层
- #layer_h3 = Dropout(0.2)(layer_h2) # 随机中断部分神经网络连接,防止过拟合
- layer_h3 = Dropout(0.05)(layer_h3)
- layer_h4 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h3) # 卷积层
- layer_h4 = Dropout(0.1)(layer_h4)
- layer_h5 = Conv2D(64, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h4) # 卷积层
- layer_h6 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h5) # 池化层
- layer_h6 = Dropout(0.1)(layer_h6)
- layer_h7 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h6) # 卷积层
- layer_h7 = Dropout(0.15)(layer_h7)
- layer_h8 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h7) # 卷积层
- layer_h9 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(layer_h8) # 池化层
- layer_h9 = Dropout(0.15)(layer_h9)
- layer_h10 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h9) # 卷积层
- layer_h10 = Dropout(0.2)(layer_h10)
- layer_h11 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h10) # 卷积层
- layer_h12 = MaxPooling2D(pool_size=1, strides=None, padding="valid")(layer_h11) # 池化层
- layer_h12 = Dropout(0.2)(layer_h12)
- layer_h13 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h12) # 卷积层
- layer_h13 = Dropout(0.2)(layer_h13)
- layer_h14 = Conv2D(128, (3,3), use_bias=True, activation='relu', padding='same', kernel_initializer='he_normal')(layer_h13) # 卷积层
- layer_h15 = MaxPooling2D(pool_size=1, strides=None, padding="valid")(layer_h14) # 池化层
- #test=Model(inputs = input_data, outputs = layer_h12)
- #test.summary()
- layer_h16 = Reshape((200, 3200))(layer_h15) #Reshape层
- #layer_h5 = LSTM(256, activation='relu', use_bias=True, return_sequences=True)(layer_h4) # LSTM层
- #layer_h6 = Dropout(0.2)(layer_h5) # 随机中断部分神经网络连接,防止过拟合
- layer_h16 = Dropout(0.3)(layer_h16)
- layer_h17 = Dense(128, activation="relu", use_bias=True, kernel_initializer='he_normal')(layer_h16) # 全连接层
- layer_h17 = Dropout(0.3)(layer_h17)
- layer_h18 = Dense(1422, use_bias=True, kernel_initializer='he_normal')(layer_h17) # 全连接层
rknn模型转换代码,且log中没有error或warning
- from rknn.api import RKNN
- rknn = RKNN(verbose=True, verbose_file='./build.log')
- rknn.config()
- print('--> Loading model')
- rknn.load_tensorflow(tf_pb='./model.pb',
- inputs=['the_input'],
- outputs=['Activation0/truediv'],
- input_size_list=[[320000, 1]])
- print('done')
- # Build Model
- print('--> Building model')
- rknn.build(do_quantization=False)
- print('done')
- rknn.export_rknn('./model.rknn')
- rknn.release()
rknn模型inference代码
- rknn = RKNN()
- rknn.load_rknn('./model.rknn')
- ret = rknn.init_runtime(target='rk3399pro')
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- # x_in.size = (1, 320000, 1)
- x_in, in_len = load_wav_input(filename)
- out = rknn.inference(inputs=[x_in], data_type='float32')
同样的环境原来模型没用Reshape直接输入(1600,200,1)可以inference,但最后softmax后np.sum(out)应该=200,pb模型是这样,但rknn模型np.sum(out)是几千完全不对,因此想输入一维向量在模型中reshape,但出之前的错误
|
|