如何在Tensorflow中加载.pb模型

时间:2019-05-20 16:29:46

标签: python tensorflow

我拼命试图将一个tensorflow模型另存为.pb文件,然后再次加载并用于预测。我花了10到15个小时尝试搜索互联网,但我根本无法正常工作。我用于模型的代码是:

from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
import numpy as np

model_version = "465555555"
epoch = 100
tensorboard = TensorBoard(log_dir='./logs', histogram_freq = 0, write_graph = True, write_images = False)

sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(0)

X = np.array([[0,0], [0,1], [1,0], [1,1]])
Y = np.array([[0],[1],[1],[0]])

model = Sequential()
model.add(Dense(8, input_dim=2))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
sgd = SGD(lr=0.1)

model.compile(loss='binary_crossentropy', optimizer=sgd)
model.fit(X, Y, batch_size=1, nb_epoch=epoch, callbacks = [tensorboard])

x = model.input
y = model.output
a = model.predict(np.array([[0,0],[0,1],[1,0],[1,1]]))

prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def({"inputs": x}, {"prediction":y})
builder = saved_model_builder.SavedModelBuilder('./'+model_version)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
      sess, [tag_constants.SERVING],
      signature_def_map={
           signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:prediction_signature,
      },
      legacy_init_op=legacy_init_op)

我想从pb读取模型。文件并将其用于例如。做model.predict(np.array([[0,0],[0,1],[1,0],[1,1]]))

0 个答案:

没有答案