我想从keras模型创建PB文件,并使用DnnInvoke.readnetfromTensorflow
将其提供给EmguCV(或至少使用opencv,最好使用EmguCV)
我使用代码创建网络:
from keras import backend as K
from keras.callbacks import *
from keras.layers import *
from keras.models import *
from keras.utils import *
from keras.optimizers import Adadelta,RMSprop,Adam,SGD
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
from config import *
def ctc_lambda_func(args):
iy_pred,ilabels,iinput_length,ilabel_length = args
# the 2 is critical here since the first couple outputs of the RNN
# tend to be garbage:
iy_pred = iy_pred[:,2:,:] # no such influence
return K.ctc_batch_cost(ilabels,iy_pred,ilabel_length)
def CRNN_model(is_training=True):
inputShape = Input((width,height,1),name='input') # base on Tensorflow backend
conv_1 = Conv2D(64,(3,3),activation='relu',padding='same')(inputShape)
conv_2 = Conv2D(64,padding='same')(conv_1)
#batchnorm_2 = BatchNormalization()(conv_2)
pool_2 = MaxPooling2D(pool_size=(2,2))(conv_2)
conv_3 = Conv2D(64,padding='same')(pool_2)
conv_4 = Conv2D(128,padding='same')(conv_3)
#batchnorm_4 = BatchNormalization()(conv_4)
pool_4 = MaxPooling2D(pool_size=(2,2))(conv_4)
conv_5 = Conv2D(128,padding='same')(pool_4)
conv_6 = Conv2D(128,padding='same')(conv_5)
pool_5 = MaxPool2D(pool_size=(2,2))(conv_6)
#batchnorm_6 = BatchNormalization()(conv_6)
#bn_shape = batchnorm_6.get_shape()
#print(bn_shape)
#x_reshape = Reshape(target_shape=(int(bn_shape[1]),int(bn_shape[2] * bn_shape[3])))(batchnorm_6)
#drop_reshape = Dropout(0.25,name='d1')(x_reshape)
fl_1 = flatten()(pool_5)
fc_1 = Dense(256,activation='relu')(fl_1)
#print(x_reshape.get_shape())
#print(fc_1.get_shape())
bi_LSTM_1 = Bidirectional(LSTM(256,return_sequences=True,kernel_initializer='he_normal'),merge_mode='sum')(fc_1)
bi_LSTM_2 = Bidirectional(LSTM(128,merge_mode='concat')(bi_LSTM_1)
#drop_rnn = Dropout(0.3,name='d2')(bi_LSTM_2)
fc_2 = Dense(label_classes,kernel_initializer='he_normal',activation='softmax')(bi_LSTM_2)
base_model = Model(inputs=[inputShape],outputs=fc_2)
labels = Input(name='the_labels',shape=[label_len],dtype='float32')
input_length = Input(name='input_length',shape=[1],dtype='int64')
label_length = Input(name='label_length',dtype='int64')
loss_out = Lambda(ctc_lambda_func,output_shape=(1,),name='ctc')([fc_2,labels,input_length,label_length])
if is_training:
return Model(inputs=[inputShape,label_length],outputs=[loss_out]),base_model
else:
return base_model
并且我使用下面的代码来创建.pb文件:
import tensorflow as tf
mfname = './models/weights.01-0.080-0.007.hdf5' # FIXME
tf.keras.backend.set_learning_phase(0)
sess = tf.keras.backend.get_session()
sess.as_default()
model = tf.keras.models.load_model(mfname,compile=False)
constant_graph = tf.graph_util.convert_variables_to_constants(
sess,sess.graph.as_graph_def(),[out.op.name for out in model.outputs],)
tf.train.write_graph(constant_graph,'',mfname[:-4] + '_graph.pb',as_text=False)
但是当我从Tensorflow调用DnnInvoke.readnet时,它显示此错误:
Emgu.CV.Util.CvException:'OpenCV:找不到输入层: density_1 / Tensordot / free'
我该如何解决这个问题?