numpy 设置数组元素时出现序列值错误

lrpiutwd  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(47)

为什么我只在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)

wecizke3

wecizke31#

发现其中一个元素没有类似的形状,这是造成问题。
我修复了形状不同的单个数组,并从x train中删除了它,从y train中删除了相应的标签。
最终通过使用列表切片而不是训练测试拆分来解决这个问题,不知道为什么。

相关问题