【问题标题】:reshape a numpy 1D array to a 3D array将 numpy 1D 数组重塑为 3D 数组
【发布时间】:2020-04-22 01:44:13
【问题描述】:

我在将 numpy 数组从 1D 重塑为 3D 时遇到问题。

我正在阅读一个视频文件, 然后我从 x 帧中提取人脸,并将人脸及其标签存储在一个 numpy 数组中。

fps = 3
time_of_video = 10
x = 0
face_size = 128

images = []
labels = []

for original_name, filename, label in tqdm(zip(train_sample_metadata['original'], train_sample_metadata['filename'], train_sample_metadata['label'])):

...

        video_1 = read_video(f'{base_path}/{filename}', fps*time_of_video)
        video_2 = read_video(f'{base_path}/{original_name}', fps*time_of_video)
        face_annotations = get_annotations(real_video)
        faces_1 = crop_faces(video_1, face_annotations, face_size)
        faces_2 = crop_faces(video_2, face_annotations, face_size)

        x = faces_1

        for ff, rf in zip(faces_1, faces_2):
            if np.array_equal(ff, rf):
                y.append(0)
            else:
                y.append(1)

        y = to_categorical(np.array(y), 2)

    images.append(x)
    labels.append(y)


images = np.array(images)
labels = np.array(labels)

images.shape, labels.shape
((400,), (400,))

images = images.reshape((images.shape[0] * images.shape[1], 128, 128, 3))
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-38-af9e927f8a1c> in <module>
----> 1 images = images.reshape((images.shape[0] * images.shape[1], 299, 299, 3))

IndexError: tuple index out of range

【问题讨论】:

  • 鉴于images 是一维的,您期望images.shape[1] 是什么?
  • 在将faces_1 存储到x 之前,您是否检查过它包含的内容?我的猜测是它不是一个 numpy 数组。
  • 我要制作的形状是 images.shape[1], 128, 128, 3) ``` images.shape, labels.shape ((400,) , (400))图像 = images.reshape((images.shape[0] * images.shape[1], 128, 128, 3)) ```
  • @OscarRangel 我看到了。因此我的问题。 images 是一维的,但您可以访问第一维和第二维的大小(images.shape[0]images.shape[1])。

标签: python numpy tensorflow keras


【解决方案1】:

您可以使用以下方法将单通道图像转换为三通道图像

image3Channel=np.stack((image1Channel,)*3, -1)

【讨论】:

  • 谢谢,但 images=np.stack((images,)*3, -1) 不起作用.... images.shape (400, 3) images = images.reshape((images. shape[0] * images.shape[1], 128, 128, 3)) ValueError: cannot reshape array of size 1200 into shape (1200,128,128,3)
  • 你可以很容易地使用 opencv 来解决这个问题cv2.resize(image3Channnel, (128,128), interpolation = cv2.INTER_AREA)
  • TypeError: Expected Ptr<:umat> for argument '%s'
  • 你可以改成cv2.resize(np.float32(image3Channnel), (128,128), interpolation = cv2.INTER_AREA)
  • image = cv2.resize(np.float32(images), (128,128), interpolation = cv2.INTER_AREA) ValueError: setting an array element with a sequence.
【解决方案2】:

您的 images 数组的形状可能为 (400,),因为原始列表不包含所有相等的形状。假设您的图片/视频在 5D 中正确表示,如果所有附加项目的大小相同,它将转换为 numpy 数组。但它没有。试试:

for i in images:
    print(np.array(i.shape))

当你运行这行代码时,你可能会发现为什么你的列表被转换成一个列表数组:

images = np.array(images)

reshape 函数仅重新排列当前数组。想象一个总共有 8 个单元的 2x2x2 立方体。一个数组只能使用这 8 个单元来重构。

正如我所说,如果您发现并非所有图片数组的形状都相同,那么您将无法将它们重塑为相同的形状。您需要裁剪或调整大小。您可以使用 PIL 及其 Image 模块来做到这一点。

from PIL import Image

pic = Image.fromarray(pic).resize((128, 128, 3))

【讨论】:

  • 谢谢你的帖子,我明白你的意思了,所以初始对象是一个列表,然后我创建了 np 列表,但是我如何将它变成一个 128、128、3 数组,所以我可以训练模型。
  • 你确定你所有的图片都可以重新整形为(128, 128, 3)吗?还是您要调整大小?因为您习惯了将列表转换为numpy 数组的正确方法,但我怀疑有些形状不匹配。如果形状不匹配,您必须调整大小,而不是重塑。
  • 对不起,我还在学习....你会如何调整大小?如果我调整大小,我不必重塑?另外它告诉我索引超出范围。
  • 非常感谢您的帖子,所以我必须为每张图片都这样做,然后将所有图像存储在 np 数组中,我不需要重塑,对吧?
  • 没错。但是,如果所有图片都具有相同的形状并且可以重新调整为所需的大小,则首选重新调整形状。我希望你能理解其中的细微差别。
猜你喜欢
  • 2018-03-18
  • 2021-02-25
  • 1970-01-01
  • 2017-09-18
  • 2021-10-25
  • 2016-02-10
  • 2018-03-17
  • 2016-05-22
  • 2016-02-10
相关资源
最近更新 更多