“ValueError : Tensor Tensor("predictions/Softmax:0", shape=(?, 2), dtype=float32) is not an element

在使用Keras和TensorFlow框架训练神经网络后,尝试在C#多线程环境中调用Python代码进行预测时遇到错误:`ValueError: Tensor Tensor("predictions/Softmax:0", shape=(?, 2), dtype=float32) is not an element`。解决方案是加载模型后,设置默认图形上下文`tf.get_default_graph()`,并在预测前使用`with graph.as_default():`。通过这些修改,可以成功在多线程子线程中运行预测。" 121611224,2562453,Ghidra逆向分析:二进制程序解析与实战,"['网络安全', '渗透测试', '逆向分析', '软件安全', 'Ghidra工具']
摘要由CSDN通过智能技术生成

原始问题及解决方案
https://github.com/keras-team/keras/issues/2397#issuecomment-254919212

问题描述:
在keras+tensorflow框架下训练神经网络并得到权重h5文件。
在之后需要调用的python代码中读取权重和图片并预测
在C#多线程的子线程调用python代码时出现以下报错
“ValueError : Tensor Tensor(“predictions/Softmax:0”, shape=(?, 2), dtype=float32) is not an element

解决方法:
主要是在读取权重后增加一行self.graph = tf.get_default_graph()
self.model=tf.keras.models.load_model(‘C:\Users\18367\Desktop\corner_test\0324_lug_irv2.h5’)
self.graph = tf.get_default_graph()
并在需要预测时前加 with self.graph.as_default():

原始py文件代码

# coding: utf-8

# In[1]:


import numpy as np
import tensorflow as tf
import cv2
import os

#model = tf.keras.models.load_model('C:/Users/18367/Desktop/corner_test/test_0430.h5')
class MODEL_lug(object):
    def __init__(self):
        self.model=tf.
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值