python - 为功能性 api 模型创建 keras 序列
问题描述
我创建了一个使用 Keras 的功能 API 的模型,该模型需要 2 个输入,因此我使用
video_input = Input(shape=(16, 112, 112, 3))
image_input = Input(shape=(112, 112, 3))
Model(inputs=[video_input, image_input], outputs=merge_model)
如您所见,这意味着模型需要一个数组,其中第一个元素的形状为 (16, 112, 112, 3),第二个元素的形状为 (112, 112, 3)。
我正在使用我创建的一个类,它继承 Keras.util.sequence 类来提供生成的数据批次。当 tensorflow 尝试向模型提供输入时,问题出现在生成批量数据后,输入从 2 的数组更改为数组 1,并且这个 1 元素由 2 组成,例如它应该期望
[array(...), array(...)] instead it receives [array(array[...],array[...])]
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[array([[[[-76.87925 , -81.45539 , -82.91122 ],
[-76.90526 , -81.45103 , -83.00473 ],
[-76.77082 , -81.259674, -82.92529 ],
...,
[-76.17821 , -80.61866 , -8...
我试图将序列生成器中的数据持有者作为 python 数组添加数据,然后将其转换为 numpy 数组,但出现上述错误。keras 以某种方式将其包装到 1 个数组中,然后再将其返回给模型。
这是数据生成方法
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
X = []
y = np.empty((self.batch_size), dtype=int)
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
print(ID)
frame_data = input_data.get_frames_data(
self.work_directory + ID, self.num_of_frames, self.crop_size)
image_index = random.randint(0, len(frame_data) - 1)
im = frame_data[image_index]
X.append([frame_data, im])
# Store class
y[i] = self.labels[ID]
return np.array(X), keras.utils.to_categorical(
y, num_classes=self.n_classes)
有效的编辑功能
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
vX = np.empty((self.batch_size, *self.c3d_dim))
iX = np.empty((self.batch_size, *self.static_dim))
y = np.empty((self.batch_size), dtype=int)
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
print(ID)
frame_data = input_data.get_frames_data(
self.work_directory + ID, self.num_of_frames, self.crop_size)
image_index = random.randint(0, len(frame_data) - 1)
im = frame_data[image_index]
vX[i, ] = frame_data
iX[i, ] = im
# Store class
y[i] = self.labels[ID]
return vX, iX, keras.utils.to_categorical(
y, num_classes=self.n_classes)
解决方案
我记得你应该将每个输入作为独立数组提供。例如,您有 2 个输入图像,您不应该有类型数组,[[image_1, image_2], [image_3, image_4],[image_5, image_6] ..]
而是应该有类似[[image_1, image_3,image_5 ..], [image_2, image_4, image_6 ..]]
的东西,第一个数组是第一个图像的输入,第二个数组是第二个图像的输入。这也适用于您的情况。只需将输入存储在不同的数组中,并在应用 fit 时将它们组合起来。应该是[video_frames, images]
希望它有帮助的东西。
推荐阅读
- python - 当值不尊重模式时,如何将 Pandas 列转换为日期类型?
- firebase - Firestore 事务是否被视为串行队列?
- log4j2 - log4j2 monitorInterval 使用 spring 云配置服务器
- artifactory - Artifactory - 自定义基本 URL 无法正常工作
- sqlite - 我可以在 sqlite 中使用 group 的所有行吗?
- javascript - 我的数字时钟上缺少前导零
- google-calendar-api - 与与会者一起创建日历事件
- python - 如何解决错误“Nonetype object has no attribute 'bind'”?
- node.js - 如何在使用 create-react-app 创建的应用程序中设置环境变量?
- android - 使用 FCM 的 Android 通知