0

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.

5
  • What were the "another errors". What's the trainImages.dtype? Commented Dec 3, 2021 at 19:36
  • @hpaulj I checked it is object, but its values is number. Commented Dec 3, 2021 at 20:20
  • @hpaulj if I convert dtype into numpy.float 32, I get this error : setting an array element with a sequence. Commented Dec 3, 2021 at 20:22
  • That tells me that the array is object dtype, with elements that vary in shape. It can't be converted to a multidimensional numeric dtype array. Which is the root of the tensor conversion error. Commented Dec 3, 2021 at 20:33
  • @hpaulj I checked the element shapes in the self.trainImages, and all of them have same size (28,28) Commented Dec 4, 2021 at 6:56

1 Answer 1

2

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)

EDIT: Removed irrelevant information

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, your first answer is solved my problem. But it is weird because I used my same method before maybe 4-6 months and it was worked and I have not updated my tensorflow version. I got another error, I will open new post for new error.
Sorry, I mis-read the fit() call you did, the second part of my answer was for 2 inputs 1 output.
Not problem, I got what you mean, thanks again everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.