1

Looking for help! I have this 'Data.npy' which contains some images and labels. it looks likes this:

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

I want to make a dataset so that I can train.. this is what I did

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

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray). then got this and have no idea what to do, can someone please explain to me how to fix this

lynn
  • 11
  • 1

1 Answers1

0

Instead of passing Img and Label directly to tf.data.Dataset.from_tensor_slices, you should follow the steps mentioned below:

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

Please refer this Stack Overflow Answer for more information about loading a .npy file and preparing the Tensorflow Dataset.