【发布时间】: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