python - 加载 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)
解决方案
推荐阅读
- go - 无法通过远程 ssh 命令在远程服务器上构建 go 程序
- sql - 连接来自多列和多行的字符串
- c# - 使用 WinForms 控件中的 SystemEvents 为具有静态方法的类处理模式
- python - 美式看跌期权定价的迭代方法
- freeradius - FreeRadius LDAP 组检查
- javascript - 如何将预加载器添加到长时间运行的功能
- c - 为什么我不能在 C 中使用连续实现将元素插入到列表中?
- javascript - 未定义的方法 `comment_url' 用于#
- c# - 从 2.2 迁移到 3.1 后 EF Core 出现问题
- vba - 向地址条目添加/删除地址条目