python - 如何处理 keras 中的批量输入?
问题描述
我有一个接受形状输入(None,128,128,1)
(灰度图像)训练的 CNN。我想在另一个模型中使用这个模型权重作为基础来提取特征(不训练第一个模型),[(None,128,128,10), (None,10,8)]
这里接受两个形状输入10
是单个样本中的图像数量。基本上它接受10
shape 的图像(128,128)
。我将每个图像视为通道,因为 keras 中的 Conv 层接受 4D 输入。
我的模型看起来像这样
def create_model():
trajectory_input = Input(shape=(10, 8), name='trajectory_input')
image_input = Input(shape=(128, 128, 10), name='image_input')
x_aware = (Conv2D(32, kernel_size=(3, 3), weights=model1_weights, activation='relu'))(image_input)
x = concatenate([trajectory_input , x_aware])
x_reg = (Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)
model = Model(inputs=[trajectory_input, image_input], outputs=[x_reg])
x_aware
keras 中是否有任何方法可以在从模型 1(此处为层)提取特征时将此单个样本视为一批图像而不是单个样本?
解决方案
假设你最后想要一个[None, 126, 126, 32, 10]
大小的张量,你需要为此定义一个自定义层。
class CustomLayer(tf.keras.layers.Layer):
def __init__(self, conv_layer):
super(CustomLayer, self).__init__()
self.conv_layer = conv_layer
def build(self, input_shape):
pass
def call(self, x):
return tf.stack([self.conv_layer(tf.expand_dims(t, axis=-1)) for t in tf.unstack(x, axis=-1)], axis=-1)
并将其称为,
x_aware = CustomLayer(conv_layer)(image_input)
推荐阅读
- python - 如何在 Csv 文件中更新/附加模型名称列,以参考另一个值为 0 的列(Epoch)
- typescript - 用于从父类的静态方法获取类属性的打字稿类型
- docker - 尝试删除旧 docker 映像时,我在 nexus 中遇到与磁盘空间相关的问题
- javascript - react-aad-msal 清除本地存储。传递状态
- android - 使用 RecyclerView、ListAdapter 和 ViewModel 保存检查状态的最佳实践
- dolphindb - 在 DolphinDB 中显示 dfsdatabase
- c# - 如果少于 1 秒,WPF 表单不透明度淡入淡出动画未完成
- mysql - 如何在特殊条件下每月划分数据
- c# - 如何通过 Autofac 5 的配置文件(运行时配置)使用外部插件
- java - 使用 FirebaseRecyclerAdapter 遍历 Firebase 中对象中的对象