I've been working on a CNN for image classification and I keep getting the same error, my data is being loaded into a dataframe and I can't convert it to a tensor to feed it into the CNN. As you can see I used this code to load the pictures into a dataframe:
for i in range(len(merged)):
full_path = merged.iloc[i]['Image Path Rel']
filename = full_path[-22:-1] + 'G'
try:
img = img_to_array(load_img('D:/Serengeti_Data/Compressed/Compressed/' + filename, target_size=(32,32,3)))
except:
img = np.zeros((32,32,3), dtype=np.float32)
images = images.append({'Capture Id' : merged.iloc[i]['Capture Id'],'Image' : img}, ignore_index = True)
else:
images = images.append({'Capture Id' : merged.iloc[i]['Capture Id'],'Image' : img}, ignore_index = True)
Then once I had the images loaded by using load_img() and img_to_array() I did a reshape to get the desired shape of (32,32,3). Also normalized the values by dividing the Image column by 255.
Then I'm doing this to try to get it into a tensor:
train_tf = tf.data.Dataset.from_tensor_slices(images['Image'])
# Also tried this, but didn't got the same results:
# train_tf = tf.convert_to_tensor(train_df['Image'])
But keep getting the error:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
I also tried skipping that and tried to fit to our model right away, but got the same exact error:
trying_df = pd.DataFrame(images['Image'])
target_df = pd.DataFrame(targets)
animal_model = models.Sequential()
animal_model.add(layers.Conv2D(30, kernel_size = (3,3), padding = 'valid', activation = 'relu', input_shape =(32,32,3)))
animal_model.add(layers.MaxPooling2D(pool_size=(1,1)))
animal_model.add(layers.Conv2D(60,kernel_size=(1,1),activation = 'relu'))
animal_model.add(layers.Flatten())
animal_model.add(layers.Dense(100, activation = 'relu'))
animal_model.add(layers.Dense(10, activation = 'softmax'))
## compiler to model
animal_model.compile(loss = 'categorical_crossentropy', metrics = ['accuracy'], optimizer ='adam')
## training the model
animal_model.fit(trying_df,target_df, batch_size = 128, epochs = 15)
animal_model.summary()
TensorFlow Version: 2.4.1
Numpy Version: 1.19.5
Pandas Version: 1.0.1