如何在 Tensorflow 中使用预训练模型?
How to use a pretrained model with Tensorflow?
我知道以下是一个已经回答的问题,但即使我尝试并尝试了所有建议的解决方案,其中 none 解决了我的问题。
我制作这个网络是为了训练 MNIST 数据集。开始的时候比较深,但是为了突出问题我简化了。
mnist = mnist_data.read_data_sets('MNIST_data', one_hot=True)
# train the net
def train():
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
print("accuracy", sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
if i%100==0:
save_path = saver.save(sess, "./tmp/model.ckpt", global_step = i, write_meta_graph=True)
print("Model saved in file: %s" % save_path)
# evaluate the net
def test(image, label):
true_value = tf.argmax(label, 1)
prediction = tf.argmax(y, 1)
print("true value:", sess.run(true_value))
print("predictions", sess.run(prediction, feed_dict={x:image}))
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
W = tf.Variable(tf.zeros([784,10]), name = "W1")
b = tf.Variable(tf.zeros([10]), name = "B1")
y = tf.nn.softmax(tf.matmul(x,W) + b, name ="Y")
y_ = tf.placeholder("float", shape=[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
saver = tf.train.Saver()
model_to_restore="./tmp/model.ckpt-100.meta"
if os.path.isfile(model_to_restore):
#what i have to do here?????#
else:
#this part works!#
print("Model does not exist: training")
train()
谢谢大家的回答!
此致,
西尔维奥
更新
我都试过了
saver.restore(sess, model_to_restore)
和
saver = tf.train.import_meta_graph(model_to_restore)
saver.restore(sess, model_to_restore)
但在这两种情况下我都从终端收到了这个错误:
DataLossError (see above for traceback): Unable to open table file ./tmp/model.ckpt.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]
我认为您的模型位置可能有误,我建议您尝试以下工作流程。
由于保存的模型包含多个文件,我通常在训练后将它们保存到一个文件夹中:
modelPath = "myMNIST/model"
saved_path = saver.save(sess, os.path.join(modelPath, "model.ckpt"))
print("Model saved in file: ", saved_path)
这还会告诉您它保存的确切位置。
然后我可以在保存的位置启动我的预测器(cd 到 myMNIST)并通过以下方式恢复模型:
ckpt = tf.train.get_checkpoint_state("./model")
if ckpt and ckpt.model_checkpoint_path:
print("Restored Model")
saver.restore(sess, ckpt.model_checkpoint_path)
else:
print("Could not restore model!")
我知道以下是一个已经回答的问题,但即使我尝试并尝试了所有建议的解决方案,其中 none 解决了我的问题。 我制作这个网络是为了训练 MNIST 数据集。开始的时候比较深,但是为了突出问题我简化了。
mnist = mnist_data.read_data_sets('MNIST_data', one_hot=True)
# train the net
def train():
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
print("accuracy", sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
if i%100==0:
save_path = saver.save(sess, "./tmp/model.ckpt", global_step = i, write_meta_graph=True)
print("Model saved in file: %s" % save_path)
# evaluate the net
def test(image, label):
true_value = tf.argmax(label, 1)
prediction = tf.argmax(y, 1)
print("true value:", sess.run(true_value))
print("predictions", sess.run(prediction, feed_dict={x:image}))
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
W = tf.Variable(tf.zeros([784,10]), name = "W1")
b = tf.Variable(tf.zeros([10]), name = "B1")
y = tf.nn.softmax(tf.matmul(x,W) + b, name ="Y")
y_ = tf.placeholder("float", shape=[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
saver = tf.train.Saver()
model_to_restore="./tmp/model.ckpt-100.meta"
if os.path.isfile(model_to_restore):
#what i have to do here?????#
else:
#this part works!#
print("Model does not exist: training")
train()
谢谢大家的回答!
此致,
西尔维奥
更新
我都试过了
saver.restore(sess, model_to_restore)
和
saver = tf.train.import_meta_graph(model_to_restore) saver.restore(sess, model_to_restore)
但在这两种情况下我都从终端收到了这个错误:
DataLossError (see above for traceback): Unable to open table file ./tmp/model.ckpt.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator? [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]
我认为您的模型位置可能有误,我建议您尝试以下工作流程。
由于保存的模型包含多个文件,我通常在训练后将它们保存到一个文件夹中:
modelPath = "myMNIST/model"
saved_path = saver.save(sess, os.path.join(modelPath, "model.ckpt"))
print("Model saved in file: ", saved_path)
这还会告诉您它保存的确切位置。
然后我可以在保存的位置启动我的预测器(cd 到 myMNIST)并通过以下方式恢复模型:
ckpt = tf.train.get_checkpoint_state("./model")
if ckpt and ckpt.model_checkpoint_path:
print("Restored Model")
saver.restore(sess, ckpt.model_checkpoint_path)
else:
print("Could not restore model!")