【问题标题】:trying to make datasets from *.npy file . Failed to convert a NumPy array to a Tensor试图从 *.npy 文件制作数据集。无法将 NumPy 数组转换为张量
【发布时间】:2020-11-13 22:04:17
【问题描述】:

寻求帮助! 我有这个包含一些图像和标签的“Data.npy”。它看起来像这样:

print(Data)
[[array([[0.57647059, 0.68235294, 1.        , ..., 0.92156863, 0.92156863,
        0.92156863],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.92156863, 0.92156863,
        0.92156863],
       [0.6       , 0.81176471, 0.81176471, ..., 0.92156863, 0.92156863,
        0.92156863],
       ...,
       [0.37254902, 0.36078431, 0.36078431, ..., 0.52941176, 0.5254902 ,
        0.5254902 ],
       [0.37254902, 0.36862745, 0.36470588, ..., 0.51372549, 0.51372549,
        0.50980392],
       [0.37647059, 0.36862745, 0.36862745, ..., 0.50588235, 0.50588235,
        0.49411765]])
  array([[0.57647059, 0.68235294, 1.        , ..., 0.52156863, 0.52156863,
        0.52156863],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.52156863, 0.52156863,
        0.52156863],
       [0.6       , 0.81176471, 0.81176471, ..., 0.52156863, 0.52156863,
        0.52156863],
       ...,
       [0.38823529, 0.38823529, 0.38431373, ..., 0.52941176, 0.52156863,
        0.5254902 ],
       [0.39215686, 0.39215686, 0.38823529, ..., 0.51372549, 0.50980392,
        0.51372549],
       [0.39607843, 0.39215686, 0.39215686, ..., 0.51372549, 0.51372549,
        0.51372549]])
  array([[0.57647059, 0.68235294, 1.        , ..., 0.51372549, 0.51372549,
        0.51372549],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.51372549, 0.51372549,
        0.51372549],
       [0.6       , 0.81176471, 0.81176471, ..., 0.51372549, 0.51372549,
        0.51372549],
       ...,
       [0.4627451 , 0.45882353, 0.45882353, ..., 0.43921569, 0.43921569,
        0.43921569],
       [0.47058824, 0.46666667, 0.46666667, ..., 0.42745098, 0.42745098,
        0.42745098],
       [0.47843137, 0.47843137, 0.4745098 , ..., 0.41568627, 0.41568627,
        0.41568627]])
  ...
  array([[0.57647059, 0.68235294, 1.        , ..., 0.88627451, 0.88627451,
        0.88627451],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.88235294, 0.88235294,
        0.88235294],
       [0.6       , 0.81176471, 0.81176471, ..., 0.8745098 , 0.8745098 ,
        0.8745098 ],
       ...,
       [0.38823529, 0.38431373, 0.38431373, ..., 0.24313725, 0.24705882,
        0.25098039],
       [0.39215686, 0.39215686, 0.39215686, ..., 0.2745098 , 0.28627451,
        0.29019608],
       [0.39607843, 0.4       , 0.4       , ..., 0.29803922, 0.32156863,
        0.32941176]])
  array([[0.57647059, 0.68235294, 1.        , ..., 0.72156863, 0.72156863,
        0.72156863],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.71764706, 0.71372549,
        0.71372549],
       [0.6       , 0.81176471, 0.81176471, ..., 0.70588235, 0.70588235,
        0.70588235],
       ...,
       [0.24313725, 0.24313725, 0.24705882, ..., 0.28627451, 0.29803922,
        0.31372549],
       [0.24705882, 0.25098039, 0.25490196, ..., 0.2745098 , 0.28627451,
        0.29803922],
       [0.25098039, 0.25490196, 0.25882353, ..., 0.2627451 , 0.2745098 ,
        0.28235294]])
  array([[0.57647059, 0.68235294, 1.        , ..., 0.48627451, 0.48627451,
        0.48627451],
       [0.57647059, 0.7372549 , 0.85490196, ..., 0.48627451, 0.48627451,
        0.48627451],
       [0.6       , 0.81176471, 0.81176471, ..., 0.48235294, 0.48235294,
        0.48235294],
       ...,
       [0.37647059, 0.37647059, 0.37647059, ..., 0.39607843, 0.38039216,
        0.3254902 ],
       [0.37647059, 0.37647059, 0.37647059, ..., 0.44705882, 0.44313725,
        0.36862745],
       [0.38039216, 0.37647059, 0.37647059, ..., 0.50588235, 0.50196078,
        0.40784314]])]
 [list([0, 1, 1, 0]) list([0, 1, 0, 0]) list([1, 0, 1, 0]) ...
  list([1, 0, 0, 0]) list([1, 0, 1, 0]) list([0, 1, 0, 0])]]

我想制作一个数据集,以便我可以训练.. 这就是我所做的

Img,Label=Data
dataSet=tf.data.Dataset.from_tensor_slices((Img,Label))

ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。 然后得到了这个,不知道该怎么办,有人可以向我解释如何解决这个问题

【问题讨论】:

  • 将那些嵌套的 list 对象(如第一个 sn-p 的底部所示)转换为 NumPy 数组。

标签: python numpy tensorflow


【解决方案1】:

不要将ImgLabel 直接传递给tf.data.Dataset.from_tensor_slices,您应该按照以下步骤操作:

# Load the training data into two NumPy arrays, for example using `np.load()`.
with np.load("Data.npy") as data:
  Img = data["features"]
  Label = data["labels"]

# Assume that each row of `features` corresponds to the same row as `labels`.
assert Img.shape[0] == Label.shape[0]

dataset = tf.data.Dataset.from_tensor_slices((Img, Label))

有关加载.npy 文件和准备Tensorflow Dataset 的更多信息,请参阅此Stack Overflow Answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2018-11-28
    • 2021-06-26
    • 2021-05-31
    相关资源
    最近更新 更多