【问题标题】:Execution of TF1.X code with TF2.2 causes errors使用 TF2.2 执行 TF1.X 代码会导致错误
【发布时间】:2021-04-06 10:00:29
【问题描述】:

如何修复 Tensorflow 2.2 中的以下错误?如果可能的话,我更喜欢将代码转换为兼容 TF2.2 的代码,而不是使用紧凑版本。

AttributeError: module 'tensorflow' has no attribute 'placeholder'

[3306:3298 0:1022] 01:57:24 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ python train.py 
Traceback (most recent call last):
  File "train.py", line 260, in <module>
    train()
  File "train.py", line 96, in train
    pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
  File "/home/mona/research/code/DJ-RN/pointnet/models/pointnet_cls.py", line 13, in placeholder_inputs
    pointclouds_pl = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
AttributeError: module 'tensorflow' has no attribute 'placeholder'

还有

[3306:3298 0:1023] 01:57:31 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.2.0'
>>> quit()
12149/31772MB
[3306:3298 0:1024] 01:59:05 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
12149/31772MB

$ lsb_release -a
LSB Version:    core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

如下所示,占位符不是方法:

>>> tf.compat.v1.summary.
tf.compat.v1.summary.Event(                    tf.compat.v1.summary.get_summary_description(
tf.compat.v1.summary.FileWriter(               tf.compat.v1.summary.histogram(
tf.compat.v1.summary.FileWriterCache(          tf.compat.v1.summary.image(
tf.compat.v1.summary.SessionLog(               tf.compat.v1.summary.initialize(
tf.compat.v1.summary.Summary(                  tf.compat.v1.summary.merge(
tf.compat.v1.summary.SummaryDescription(       tf.compat.v1.summary.merge_all(
tf.compat.v1.summary.TaggedRunMetadata(        tf.compat.v1.summary.scalar(
tf.compat.v1.summary.all_v2_summary_ops(       tf.compat.v1.summary.tensor_summary(
tf.compat.v1.summary.audio(                    tf.compat.v1.summary.text(

我也尝试了以下在论坛和 git 问题中提到的导入,但是它不起作用(它也在用于代码迁移的官方 tensorflow 文档中:https://www.tensorflow.org/guide/migrate):

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

这是我得到的错误:

$ python train.py 
WARNING:tensorflow:From /home/mona/anaconda3/lib/python3.8/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
Traceback (most recent call last):
  File "train.py", line 260, in <module>
    train()
  File "train.py", line 96, in train
    pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
  File "/home/mona/research/code/DJ-RN/pointnet/models/pointnet_cls.py", line 15, in placeholder_inputs
    pointclouds_pl = tf.compact.v1.placeholder(tf.float32, shape=(batch_size, num_point, 3))
AttributeError: module 'tensorflow.compat.v1' has no attribute 'compact'

代码位于此 repo 中:https://github.com/charlesq34/pointnet/issues/265

【问题讨论】:

  • Migration 过程可能容易或困难取决于您使用的型号,无论如何这需要一些时间。使用compat 很便宜,也没有太多时间消耗。唯一不要混合使用import tensorflow.compat.v1 as tf 和其他compats - 如果您要使用 TF1.x 风格,请在任何地方使用它。还更新从 repo 获取最新代码 - 好像他们fixed typo
  • 我安装的版本是和我的CUDA兼容的版本。
  • 会不会是错字?紧凑型可以兼容
  • 是的,这就是模块被称为 tensorflow.compat.v1 的原因,你应该在你的 pointnet 脚本中修复它
  • 如果我这样做对我有用: import tensorflow 然后 import tensorflow.compat.v1 as tf ,然后 tf.placeholder 就在那里。

标签: python tensorflow deep-learning tensorflow2.0


【解决方案1】:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

而不是

import tensorflow as tf

然后像这样使用它:

tf.placeholder()

例如,

pointclouds_ph = tf.placeholder(tf.float32, shape=(batch_size, point_num, 3))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多