[英]InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true
I am using PCA to reduce the dimensions of images before comparing them using the Structural Similarity Index.在使用结构相似性指数进行比较之前,我使用 PCA 来减小图像的尺寸。 After using PCA, tf.image.ssim throws an error.使用 PCA 后,tf.image.ssim 会抛出错误。
I am comparing images here without the use of PCA.我在这里比较图像而不使用 PCA。 This works perfectly -这完美地工作 -
import numpy as np
import tensorflow as tf
import time
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
start = time.time()
for i in range(1,6000):
x_train_zero = np.expand_dims(x_train[0], axis=2)
x_train_expanded = np.expand_dims(x_train[i], axis=2)
print(tf.image.ssim(x_train_zero, x_train_expanded, 255))
print(time.time()-start)
I have applied PCA here to reduce the dimensions of images, so that SSIM takes lesser time to compare images -我在这里应用了 PCA 来减小图像的尺寸,这样 SSIM 比较图像所需的时间更少——
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
x_train = x_train.reshape(60000,-1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(x_train)
pca = PCA()
pca = PCA(n_components = 11)
X_pca = pca.fit_transform(X_scaled).reshape(60000,11,1)
start = time.time()
for i in range(1,6000):
X_pca_zero = np.expand_dims(X_pca[0], axis=2)
X_pca_expanded = np.expand_dims(X_pca[i], axis=2)
print(tf.image.ssim(X_pca_zero, X_pca_expanded, 255))
print(time.time()-start)
This chunk of code throws the error - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true.这段代码会引发错误 - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true。 Summarized data: 11, 1, 1 11汇总数据:11、1、1 11
So, in short, that error happen because in tf.image.ssim
, the inputs X_pca_zero
and X_pca_expanded
size doesn't match filter_size
, if you have filter_size=11
then the X_pca_zero
and X_pca_expanded
must be at least 11x11 , example of how you could change your code:因此,简而言之,发生该错误是因为在tf.image.ssim
中,输入X_pca_zero
和X_pca_expanded
大小与filter_size
不匹配,如果您有filter_size=11
那么X_pca_zero
和X_pca_expanded
必须至少为11x11 ,例如您可以更改您的代码:
import tensorflow as tf
import time
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
x_train = x_train.reshape(60000,-1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(x_train)
pca = PCA()
pca = PCA(n_components = 16) # or 12 -> 3, 4 filter_size=3
X_pca = pca.fit_transform(X_scaled).reshape(60000, 4, 4, 1)
start = time.time()
X_pca_zero = X_pca[0]
for i in range(1,6000):
X_pca_expanded = X_pca[i]
print(tf.image.ssim(X_pca_zero, X_pca_expanded, 255, filter_size=4))
print(time.time()-start)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.