Why do I get this error only for x_train? On commenting x_train out, no errors come.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-19-86f84f4d44b9> in <cell line: 1>()
----> 1 x_train = np.array(x_train, dtype = np.float16) / 255
2 x_val = np.array(x_val, dtype = np.float16) / 255.0
3 x_test = np.array(x_test, dtype = np.float16) / 255.0
4 x_train = x_train.reshape(-1, 224, 224, 3)
5 x_val = x_val.reshape(-1, 224, 224, 3)
ValueError: setting an array element with a sequence.
Here is the relevant code I am running
def convert_image_to_array(image_dir):
try:
image = cv2.imread(image_dir)
if image is not None:
image = cv2.resize(image, (224,224))
return img_to_array(image)
else:
return np.array([])
except Exception as e:
print(f"Error : {e}")
return None
image_list_train, label_list_train = [], []
all_labels = [
'Healthy Potato',
'Potato early blight',
'Rice neck blast',
'Wheat leaf septoria',
'Healthy Rice',
'Potato late blight',
'Tomato Early blight leaf',
'Wheat leaf stripe rust',
'Healthy Tomato',
'Rice brown spot',
'Tomato leaf late blight',
'Healthy Wheat',
'Rice leaf blast',
'Wheat brown rust'
]
binary_labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
temp = -1
for directory in all_labels:
plant_image_list = listdir(f"{dir_train}/{directory}")
temp += 1
for files in plant_image_list:
image_path = f"{dir_train}/{directory}/{files}"
image_list_train.append(convert_image_to_array(image_path))
label_list_train.append(binary_labels[temp])
x_train, x_val, y_train, y_val = train_test_split(image_list_train, label_list_train, test_size = 0.2)
x_train = np.array(x_train, dtype = np.float16) / 255
x_val = np.array(x_val, dtype = np.float16) / 255.0
x_test = np.array(x_test, dtype = np.float16) / 255.0
x_train = x_train.reshape(-1, 224, 224, 3)
x_val = x_val.reshape(-1, 224, 224, 3)
x_test = x_test.reshape(-1, 224, 224, 3)
Additionally, I tried using dtype = object for all instead. Now different error comes during reshaping. Code:
x_train = np.array(x_train, dtype = object) / 255
x_val = np.array(x_val, dtype = object) / 255.0
x_test = np.array(x_test, dtype = object) / 255.0
x_train = x_train.reshape(-1, 224, 224, 3)
x_val = x_val.reshape(-1, 224, 224, 3)
x_test = x_test.reshape(-1, 224, 224, 3)
Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-3411c6f1375e> in <cell line: 4>()
2 x_val = np.array(x_val, dtype = object) / 255.0
3 x_test = np.array(x_test, dtype = object) / 255.0
----> 4 x_train = x_train.reshape(-1, 224, 224, 3)
5 x_val = x_val.reshape(-1, 224, 224, 3)
6 x_test = x_test.reshape(-1, 224, 224, 3)
ValueError: cannot reshape array of size 1374 into shape (224,224,3)
Again, for the rest, no problems occur as shown in the shapes: Output:
x train shape: (1374,)
x val shape: (344, 224, 224, 3)
x test shape: (141, 224, 224, 3)
Found out that one of the elements didnt have a similar shape which was causing the problem.
I fixed the single array that differed in shape and removed it from x train and the corresponding label from y train. Still didnt fix the problem.
Eventually fixed the problem by splitting using list slicing instead of train test split, dont know why.