设置序列值错误的数组元素

问题描述 投票:0回答:1

为什么只有 x_train 才会出现此错误?在评论 x_train 时,没有出现错误。

---------------------------------------------------------------------------
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.

这是我正在运行的相关代码

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)

此外,我尝试对所有对象使用 dtype = object 。现在重塑过程中会出现不同的错误。 代码:

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)

错误:

---------------------------------------------------------------------------
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)

同样,对于其余部分,不会出现形状所示的问题: 输出:

x train shape: (1374,)
x val shape: (344, 224, 224, 3)
x test shape: (141, 224, 224, 3)
numpy opencv numpy-ndarray normalization array-broadcasting
1个回答
0
投票

发现其中一个元素没有相似的形状,这导致了问题。

我修复了形状不同的单个数组,并将其从 x 列车中删除,并从 y 列车中删除相应的标签。还是没有解决问题。

最终通过使用列表切片而不是训练测试拆分来解决问题,不知道为什么。

© www.soinside.com 2019 - 2024. All rights reserved.