首页 > 解决方案 > 加载 TensorFlow 1.11 模型后得到验证损失

问题描述

我已经使用 TensorFlow 1.11 训练并保存了一个模型,即它以 .index、.meta 和 .data 格式保存。当我训练模型时,我计算了损失(我使用的是 softmax_cross_entropy_with_logits)并且效果很好。

但是,我想加载模型并在一些验证图片上进行测试。到目前为止,一切都很好。

我还想计算验证损失。但是,每当我这样做时,我总是得到 0。我不确定我做错了什么。

这是一个代码片段:

[...]
def validate(mylabels):
  [...]
  session = tf.Session()

  labels = np.zeros((1, 3))

  mode_folder = "mymodelfolder"
  saver = tf.train.import_meta_graph(os.path.join(model_folder,'.meta'))
  saver.restore(session, tf.train.latest_checkpoint(model_folder + "/./"))

  graph = tf.get_default_graph()

  network = graph.get_tensor_by_name("add_6:0")

  im_ph = graph.get_tensor_by_name("Placeholder:0")
  label_ph = graph.get_tensor_by_name("Placeholder_1:0")

  network=tf.nn.sigmoid(network)

  [...]

  cost = tf.reduce_mean(graph.get_tensor_by_name("softmax_cross_entropy_with_logits:0")) * 100

  imagepath = "myimages"
  for filename in os.listdir(imagepath):
    img = cv2.imread(imagepath + "/" + filename)
    img = img.reshape(1, height, width, 3)
    feed_dict_testing = {im_ph: img, label_ph: labels}
    result, loss = session.run([network, cost], feed_dict = feed_dict_testing)



标签: pythontensorflowmachine-learningcross-entropy

解决方案


推荐阅读