恢复的模型未初始化(初始V3 /转移学习)

时间:2017-08-26 15:03:24

标签: tensorflow

我使用Inception v3模型并应用了转移学习。培训和评估工作很顺利。

现在我尝试实际使用(已保存)模型。 我在训练期间刚刚保存了模型

print("Saving model...")
saver = tf.train.Saver()
save_path = saver.save(sess, self.MODEL_SAV_PATH)
print("...Saved @ ", save_path)

并尝试使用

恢复它
def load_model(self):     
    tf.reset_default_graph()
    self.input_shape = tf.placeholder(tf.float32, shape=[None, pipeline.height, pipeline.width, pipeline.channels])
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        self.logits, self.end_points = inception.inception_v3(self.input_shape, num_classes=1001, is_training=False)

    self.predictions = self.end_points['Predictions']

    self.sess = tf.Session()
    saver = tf.train.import_meta_graph(META_PATH)
    saver.restore(self.sess, train.latest_checkpoint(CHECKPOINT_PATH))

...

并最终使用

prediction = self.sess.run(self.predictions, feed_dict={self.input_shape: converted_images}).argmax()

但是sess.run会抛出以下错误

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value InceptionV3/Conv2d_1a_3x3/weights
 [[Node: InceptionV3/Conv2d_1a_3x3/weights/read = Identity[T=DT_FLOAT, _class=["loc:@InceptionV3/Conv2d_1a_3x3/weights"], _device="/job:localhost/replica:0/task:0/gpu:0"](InceptionV3/Conv2d_1a_3x3/weights)]]
 [[Node: InceptionV3/Predictions/Reshape_1/_795 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_2278_InceptionV3/Predictions/Reshape_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

所以我想我的恢复过程中出现了一些不正确的事情......

1 个答案:

答案 0 :(得分:1)

由于您使用self.logits, self.end_points = inception.inception_v3创建图表,因此默认图表已包含该图表的所有变量。

# replacing this one
saver = tf.train.import_meta_graph(META_PATH)
# with this saver would work
saver = tf.train.Saver()