Variable darknet53/conv2d_1/kernel does not exist, or was not was not created with tf.get_variable()

本文介绍了解决在TensorFlow中重复调用同一模型进行预测时出现的变量不存在错误的方法。通过使用tf.get_variable()或在变量作用域中设置reuse参数为tf.AUTO_REUSE,可以避免因静态图特性导致的连续调用失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow训练好的模型加载图片或者视频流,类似下面多次的调用模型的时候如果会报错

Variable darknet53/conv2d_1/kernel does not exist, or was not was not created with tf.get_variable()
import predict

model = './ckpt/NYU_FCRN.ckpt'

image_path1 = './test_image/test1.jpg'
png_path1 = './test1.png'

image_path2 = './test_image/test2.jpg'
png_path2 = './test2.png'

image_path3 = './test_image/test3.jpg'
png_path3 = './test3.png'

x, y = predict_new_new.predict(model, image_path1, png_path1)
print(x, y)
print('\n')


x, y = predict_new_new.predict(model, image_path2, png_path2)
print(x, y)
print('\n')


x, y = predict_new_new.predict(model, image_path3, png_path3)
print(x, y)
print('\n')

错误原因:因为tensorflow是静态图所以在连续调用的时候如果你没有在模型中设置变量重用那么会出现以上的错误
解决办法:如果模型比较小调试方便的话可以再定义变量的时候使用tf.get_variable()代替tf.variable(),如果模型比较大而且是用预训练模型的话就使用下面的方法,我以yolo3为例:
用with tf.variable_scope('darknet53',reuse=tf.AUTO_REUSE):代替with tf.variable_scope('darknet53'):
reuse:  True、None或tf.AUTO_REUSE;如果为真,则进入此范围以及所有子范围的重用模式;如果特遣部队。AUTO_REUSE,如果变量不存在,我们创建变量,否则返回;如果没有,则继承父范围的重用标志。当启用了即时执行时,这个参数总是强制为tf.AUTO_REUSE。
这样就可以解决同一个模型不能连续调用两次的bug了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值