当我希望模型具有多个输出时,从发电机中的DICT映射将产生什么?

发布于 2025-01-18 04:22:33 字数 1545 浏览 0 评论 0原文

我有一个使用功能 API 创建的 keras 模型,它几乎是 MobileNet V2,直到它被分成两个头,用于分类和边界框回归:

## Class prediction
x_class = keras.layers.Conv2D(NUM_CLASSES, (1, 1), padding='same')(x)
x_class = keras.layers.Activation('softmax')(x_class)
x_class = keras.layers.Reshape((NUM_CLASSES, ),  name='softmax')(x_class)

## Bounding box prediction
x_flatten = keras.layers.Flatten()(x)
x_bbox = keras.layers.Dense(128, activation='relu')(x_flatten)
x_bbox = keras.layers.Dense(64, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(32, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(4, kernel_regularizer='l2')(x_bbox)
x_bbox = keras.layers.Activation('linear', name='linear')(x_bbox)

model = keras.models.Model(inputs, [x_class, x_bbox])

losses = {'softmax': 'sparse_categorical_crossentropy',
          'linear': 'mse'}
model.compile(loss=losses, optimizer='adam', metrics=['accuracy'])

因为我有 [x_class, x_bbox] 作为输出,所以我确保训练数据生成器产生以下字典映射:

yield {'input_1': np.expand_dims(image, 0), 'softmax': np.array(label), 'linear': np.array(bbox)}

但是,我收到以下警告和错误:

UserWarning:输入字典包含与任何模型输入不匹配的键 ['softmax','线性']。它们将被模型忽略。
类型错误:目标数据丢失。您的模型具有 loss:{'softmax': 'sparse_categorical_crossentropy', 'linear': 'mse'},因此期望在 fit() 中传递目标数据。

因此,“softmax”和“线性”显然不是字典映射的正确键。我还尝试了一些键,例如“output”、“output_1”和“outputs”,但它也不接受这些键。

我正在寻找的密钥的名称是什么,或者如何设置它?

编辑说明:我希望images作为输入(即“X”),标签、bboxes作为“y”。当我只有一个输出时,我能够将输入和输出作为一个生成器传递,这也是我在这里想要做的。

I have a keras model created with the functional API, that is pretty much MobileNet V2 until it is split into two heads, for classification and bounding box regression:

## Class prediction
x_class = keras.layers.Conv2D(NUM_CLASSES, (1, 1), padding='same')(x)
x_class = keras.layers.Activation('softmax')(x_class)
x_class = keras.layers.Reshape((NUM_CLASSES, ),  name='softmax')(x_class)

## Bounding box prediction
x_flatten = keras.layers.Flatten()(x)
x_bbox = keras.layers.Dense(128, activation='relu')(x_flatten)
x_bbox = keras.layers.Dense(64, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(32, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(4, kernel_regularizer='l2')(x_bbox)
x_bbox = keras.layers.Activation('linear', name='linear')(x_bbox)

model = keras.models.Model(inputs, [x_class, x_bbox])

losses = {'softmax': 'sparse_categorical_crossentropy',
          'linear': 'mse'}
model.compile(loss=losses, optimizer='adam', metrics=['accuracy'])

Since I have [x_class, x_bbox] as outputs, I made sure the train data generator yields the following dict mapping:

yield {'input_1': np.expand_dims(image, 0), 'softmax': np.array(label), 'linear': np.array(bbox)}

However, I get the following warning and error:

UserWarning: Input dict contained keys ['softmax', 'linear'] which did not match any model input. They will be ignored by the model.
TypeError: Target data is missing. Your model has loss: {'softmax': 'sparse_categorical_crossentropy', 'linear': 'mse'}, and therefore expects target data to be passed in fit().

So 'softmax' and 'linear' are clearly not the correct keys for the dict mapping. I also tried some keys like 'output', 'output_1' and 'outputs', but it doesn't accept those keys either.

What's the name of the key I'm looking for, or how can I set it up?

Edit Clarification: I want images to be the input (i.e. "X") and labels, bboxes to be the "y". When I only have a single output, I'm able to pass both the input and output as one generator, which is also what I want to do here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文