Home > other >  Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) error
Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) error

Time:12-04

I try to train a tensorflow model. But I got error.

Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Here my fit codes:

model.fit(self.datas.trainImages, self.datas.trainLabels,self.datas.batch_size, epochs =self.datas.epochs)

My self.datas.trainImages is numpy.array() its shape is (16,) it has 16 sample and their sizes is 28x28, it is mnist dataset.

self.train_dataset = [[cv2.imread(image0),0],[cv2.imread(image1),1],[cv2.imread(image2),2],[...],[...]]
self.trainDataset = numpy.array(self.train_dataset)
        
self.trainImages, self.trainLabels = numpy.asarray(self.trainDataset[:,0])/255,self.trainDataset[:,1] #.astype(numpy.float32)/



self.val_dataset = [[cv2.imread(image0),0],[cv2.imread(image1),1],[cv2.imread(image2),2],[...],[...]]

self.valDataset = numpy.array(self.val_dataset)#.astype(numpy.float32)
self.valImages, self.valLabels = numpy.asarray(self.valDataset[:,0])/255,self.valDataset[:,1] #.astype(numpy.float32)/255
        

I tried to use astype or numpy.ndarray but I got another errors. I am sure of that all datas in the self.datas.trainImages is float numbers and has same shape.

CodePudding user response:

Would it be possible to print out some type of error output?

Personally, I was having a similar issue and by coating my input with "np.stack()" it added an extra dimension, changed the shape of the array and allowed it to work.

i.e.

images = np.stack(self.data.trainImages)

Furthermore, I'm not sure if you're using a custom Model.fit() method, but I believe for multiple inputs it's best to use square brackets, i.e.

model.fit(x = [input1, input2], y = output1, batch_size = batch_sizing, epochs = epoch_quantity)
  • Related