module 'tensorflow' has no attribute 'GPUOptions'解决办法

module 'tensorflow' has no attribute 'GPUOptions'解决办法

根据官方通讯ensorflow 2.x与1.x相比发生了重大变化。

tf.contrib将从核心TensorFlow信息库和构建过程中删除。TensorFlow的contrib模块已经超出了在单个存储库中可以维护和支持的范围。较大的项目最好单独维护,而较小的扩展将逐步扩展到TensorFlow核心代码。

如果要使用tensorflow 1.x功能/方法,请在中保存一个兼容性模块tensorflow 2.x。

解决办法:

tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction)

 

附上本人报错遇到的部分代码:

# 首先调用create_mtcnn创建MTCNN的三个网络
mtcnn_graph = tf.Graph()
with mtcnn_graph.as_default():
    gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=1)
    mtcnn_sess = tf.compat.v1.Session(graph=mtcnn_graph,
                            config=tf.compat.v1.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
    mtcnn_sess.run(tf.compat.v1.global_variables_initializer())
    with mtcnn_sess.as_default():
        pnet, rnet, onet = align.detect_face.create_mtcnn(mtcnn_sess, None)

问题的原因是由于tensorflow的版本问题,导致在使用GPUOptions时出现了AttributeError: module 'tensorflow' has no attribute 'GPUOptions'的错误。 解决这个问题的方法是将import tensorflow as tf修改为import tensorflow.compat.v1 as tf,这样导入的是tensorflow.compat.v1模块,可以解决attribute错误问题。同时,需要注意确保代码中其他文件也改为使用tensorflow.compat.v1模块进行导入。 另外,还需要将代码中的其他tensorflow相关的模块和函数也修改为tensorflow.compat.v1模块中的对应函数。例如,将tensorflow.variable_scope修改为tensorflow.compat.v1.variable_scope,将tensorflow.TFRecordReader修改为tensorflow.compat.v1.TFRecordReader,将tensorflow.placeholder_with_default修改为tensorflow.compat.v1.placeholder_with_default,将tensorflow.flags修改为tensorflow.compat.v1.flags。 通过以上修改,就可以解决AttributeError: module 'tensorflow' has no attribute 'GPUOptions'错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [AttributeError: moduletensorflow‘ has no attribute](https://blog.csdn.net/self_Name_/article/details/112149189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [AttributeError: module tensorflow has no attribute ‘io](https://blog.csdn.net/tsy_0827/article/details/122864512)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值