Traceback (most recent call last): File "D:/SteganoGAN-master/research/psnr.py", line 25, in <module> _main() File "D:/SteganoGAN-master/research/psnr.py", line 18, in _main with tf.Session() as sess: AttributeError: module 'tensorflow' has no attribute 'Session'
时间: 2023-06-27 21:07:32 浏览: 52
这个错误通常是因为 TensorFlow 版本更新导致的。在 TensorFlow 2.0 及以上版本中,Session 已不再使用。相反,您需要使用 tf.compat.v1.Session()。
如果您的代码是基于 TensorFlow 1.x 写的,可以通过以下方式解决此问题:
```
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
```
如果您想使用 TensorFlow 2.x 版本,您可以使用以下方式替换代码:
```
import tensorflow as tf
with tf.compat.v1.Session() as sess:
...
```
或者:
```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
...
sess.close()
```
这两种方法都会将代码转换为兼容 TensorFlow 2.x 版本的代码。
相关问题
Traceback (most recent call last): File "D:/SteganoGAN-master/research/psnr.py", line 25, in <module> _main() File "D:/SteganoGAN-master/research/psnr.py", line 16, in _main t1 = read_img('D:/SteganoGAN-master/research/ruanjian.png') File "D:/SteganoGAN-master/research/psnr.py", line 8, in read_img return tf.image.decode_image(tf.read_file(path)) AttributeError: module 'tensorflow' has no attribute 'read_file'
这个错误提示是说在你的代码中使用了 `tensorflow` 模块中不存在的 `read_file` 属性。
在 `tensorflow` 2.0 版本之后,`read_file` 方法已经被移动到了 `tf.io` 模块中,所以你需要将代码中的 `tf.read_file` 替换为 `tf.io.read_file`。
下面是修改后的代码示例:
```python
import tensorflow as tf
def read_img(path):
return tf.image.decode_image(tf.io.read_file(path))
def _main():
t1 = read_img('D:/SteganoGAN-master/research/ruanjian.png')
t2 = read_img('D:/SteganoGAN-master/research/ruanjian_steg.png')
psnr = tf.image.psnr(t1, t2, max_val=255)
print("PSNR:", psnr)
if __name__ == '__main__':
_main()
```
修改后的代码中使用了 `tf.io.read_file` 方法来读取图像文件,应该可以解决你遇到的问题。
Traceback (most recent call last): File "D:/SteganoGAN-master/research/psnr.py", line 22, in <module> _main() File "D:/SteganoGAN-master/research/psnr.py", line 15, in _main with tf.Session() as sess: AttributeError: module 'tensorflow' has no attribute 'Session'
这个错误是因为你正在使用TensorFlow 2.0或更高版本,而在这些版本中,`tf.Session()`不再被支持。
你可以尝试以下两种方法来解决这个问题:
1. 回退到TensorFlow 1.x版本,这样你就可以使用`tf.Session()`了。可以使用以下命令安装TensorFlow 1.x:
```
pip install tensorflow==1.15
```
2. 改用`tf.compat.v1.Session()`代替`tf.Session()`。如下所示:
```python
import tensorflow.compat.v1 as tf
def _main():
# ...
with tf.Session() as sess:
# ...
```
这样你就可以在TensorFlow 2.x中使用`tf.compat.v1.Session()`来代替`tf.Session()`了。