首页 > 解决方案 > 如何在配色方案中可视化相似度分数?(张量)

问题描述

我在“sims”变量中有一个相似度分数,我想在矩阵中绘制相似度分数,最好是在配色方案中。有关“sims”变量的更多信息是这样的:

  import tensorflow.compat.v2 as tf

  print(sims) #Tensor("ExpandDims:0", shape=(1, 64, 64, 1), dtype=float32)
  print(sims[0]) #Tensor("strided_slice_6:0", shape=(64, 64, 1), dtype=float32)
  tf.print(sims[0][:], output_stream=sys.stderr)

这些行的输出给出了以下屏幕截图。

输出

为了可视化颜色矩阵中的分数,我尝试使用 plt.matshow 方法,但我无法将张量转换为 numpy 数组 (sims[0].numpy()),因为它会给出错误。

AttributeError:“张量”对象没有属性“numpy”。

我也不能使用 plt.matshow。它给出了以下错误:

NotImplementedError:无法将符号张量 (strided_slice_10:0) 转换为 numpy 数组。此错误可能表明您正在尝试将张量传递给 NumPy 调用,这是不受支持的

如何在这样的矩阵(64x64)中可视化这些分数?

相似矩阵

标签: pythontensorflowkeraspytorchsimilarity

解决方案


Tensor您可以通过将数组转换为Numpy数组并按如下方式绘制图像,以一种简单的方式通过几种方法来完成此操作。

使用 PIL:

# sims is a Tensor <tf.Tensor: shape=(64, 64, 1), dtype=float64>

import tensorflow as tf
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
%matplotlib inline  

data = sims.numpy()
img = Image.fromarray(data,'RGB')
img.save('my.png')
img.show()  

使用 Matplot:

data = sims.numpy()
data.resize(64,64)
plt.figure(figsize = (10,10))
plt.imshow(data, interpolation='nearest')
plt.show()

推荐阅读