desert_ranger
desert_ranger

Reputation: 1737

Why does tf.image.ssim always return AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

I am trying to use tf.image.ssim to get the similarity between 2 images, however, it returns an attribute error. Since I am just directly using the TensorFlow code, I don't see any way to debug this issue.

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
)

Upvotes: 1

Views: 509

Answers (1)

yudhiesh
yudhiesh

Reputation: 6799

MNIST returns a grayscale image which is in 2D, SSIM requires the image to be in 3D. So just expand the dims of the returned image that you want to compare and apply SSIM on it.

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))

It returns the following:

tf.Tensor(1.0, shape=(), dtype=float32)

The returned tensor contains an MS-SSIM value for each image in batch. The values are in the range [0, 1] and the example returns a value of 1 indicating both images are identical.

Upvotes: 2

Related Questions