【问题标题】:InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be trueInvalidArgumentError: 预期 'tf.Tensor(False, shape=(), dtype=bool)' 为真
【发布时间】:2021-04-05 14:40:24
【问题描述】:

在使用结构相似性指数比较图像之前,我使用 PCA 来减小图像的尺寸。使用 PCA 后,tf.image.ssim 报错。

我在这里比较图像而不使用 PCA。这完美 -

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)

我在这里应用了 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)

这段代码抛出错误 - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' 为真。汇总数据:11、1、1 11

【问题讨论】:

    标签: python python-3.x tensorflow


    【解决方案1】:

    因此,简而言之,发生该错误是因为在tf.image.ssim 中,输入X_pca_zeroX_pca_expanded 的大小与filter_size 不匹配,如果您有filter_size=11,那么X_pca_zeroX_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)
    

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2022-07-21
      • 2021-09-25
      • 2019-06-15
      • 1970-01-01
      • 2021-09-13
      • 2020-12-28
      • 2021-10-17
      • 2018-11-29
      相关资源
      最近更新 更多