tensorflow - 为什么 tf.image.ssim 总是返回 AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'
问题描述
我正在尝试使用tf.image.ssim
来获取 2 个图像之间的相似性,但是,它返回一个属性错误。由于我只是直接使用 TensorFlow 代码,因此我看不到任何调试此问题的方法。
import tensorflow as tf
from sklearn import datasets
import matplotlib.pyplot as plt
iris = datasets.load_iris()
x_train, y= tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
tf.image.ssim(
x_train[0][0], x_train[0][0], 255
)
解决方案
MNIST 返回 2D 灰度图像,SSIM 要求图像为 3D。因此,只需扩展要比较的返回图像的暗度并在其上应用 SSIM。
import numpy as np
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
x_train_expanded = np.expand_dims(x_train[0], axis=2)
print(tf.image.ssim(x_train_expanded, x_train_expanded, 255))
它返回以下内容:
tf.Tensor(1.0, shape=(), dtype=float32)
返回的张量包含批量每个图像的 MS-SSIM 值。这些值在 [0, 1] 范围内,示例返回值 1,表示两个图像相同。
推荐阅读
- md5 - 可以使用 Snowflake UDF 即时创建 MD5 吗?
- sorting - Java 8 排序自定义排序
- c++ - c++ winapi 代码块有效,Visual Studio 社区无效
- python-3.x - 如何使用生成器函数一次从 csv 文件中检索一行
- python - 为什么我的 dict 在 dict 理解构造函数中被删除?
- python-3.x - 有没有办法从 Tkinter 中的文本小部件存储和加载格式化文本?
- typescript - 在不丢失类型的情况下对 const 字面量的键进行类型检查
- docker - Enabling Kubernetes on Docker Desktop breaks access to external service
- python - python:重载运算符>具有不同类型
- firebase - 始终使用邮递员在 Firebase REST API 中返回 null