'ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).in jupyternotebook

i write the code in jupyter

print('---------------Data Gathering------------------')
# put The address of test images
directory_location_test = "G:\\ml\\test"
data_test, labels_test = data_extraction(directory_location_test)
testX = np.array(data_test)
#testX = tf.convert_to_tensor(testX)
#testX = testX.astype('int32')

testY = labels_test

but in my code show

<ipython-input-5-9ba842610e7d>:60: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  testX = np.array(data_test)

and error: please me to solve



Solution 1:[1]

It seems that data loaded using data_extraction() is not of uniform size like the deprecation error says: Creating an ndarray from ragged nested sequences.

The input data has probably varying length along some axis eg. data = [[1], [1,2]]

Can't really help without information what `data_extraction()' is.

Solution 2:[2]

tanks

            # image_resize
#######################################################
def image_resize(folder):
   
    images = []
    num_images = 0
    location = folder
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder, filename))
        if img is not None:
            new_img = np.array(Image.fromarray(img).resize((96,96), Image.ANTIALIAS))  # Resize the images to 50 X 50
            images.append(new_img)
            num_images += 1
            cv2.imwrite("{}/{}".format(location, filename), new_img)
        print("_image:{0} complete".format(filename))
    return None


print('____________________Start Program__________________________')
directory_location_train = "G:\\ml\\train"
for folder in os.listdir(directory_location_train):
    print("image_resize on folder: {0}".format(folder))
    image_resize(directory_location_train+"/"+folder) and the code -- is:
########################################## Define Functions ############################################################
def data_extraction(directory):
    images = []
    label = []
    num_images = 0
    for folder in os.listdir(directory):
        print(folder)
        for filename in os.listdir("{}/{}".format(directory, folder)):
            img = cv2.imread(os.path.join(directory, folder, filename))                   
            if img is not None:
                images.append(img)
                label.append(folder)
                num_images += 1
    le = LabelEncoder()
    label = le.fit_transform(label)
    return images, label
#############################################
def swish(x, beta = 2):
    return (x * sigmoid(beta * x))

get_custom_objects().update({'swish': Activation(swish)})


######################################### Get train and test input #####################################################
print('---------------Data Gathering------------------')
# put The address of test images
directory_location_test = "G:\\ml\\test"
data_test, labels_test = data_extraction(directory_location_test)
testX = np.array(data_test)
testY = labels_test

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Micha? Darowny
Solution 2 ali