当我希望模型具有多个输出时,从发电机中的DICT映射将产生什么?
我有一个使用功能 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论