python - 如何使用图像中的特定像素作为损失函数keras?
问题描述
所以我有 100x100x3 的图像,它有 3 个类别的分类问题。
所以我的CNN架构如下:
visible = Input(shape=(100, 100, 3))
conv_1 = Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), padding='same')(visible)
flatten_1 = Flatten()(conv_1)
dense_1 = Dense(100)(flatten_1)
dense_2 = Dense(3)(dense_1)
act_6 = Activation('softmax')(dense_2)
model = Model(inputs=visible, outputs=act_6)
现在我定义我的自定义损失函数如下:
def custom_loss(visible):
def loss_fun(y_true, y_pred):
return sparse_categorical_crossentropy(y_true, y_pred) + visible[?, 0, 0, 0]
return loss_fun
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss=custom_loss(visible), optimizer=sgd)
当然,上面的代码会因为“?”而报错。
如您所见,我想将每个图像的 (0, 0, 0) 处的图像像素值添加到损失函数中。
事实上,我希望能够根据特定图像的 y_pred 将 (0, 0, 0) 更改为一些 (x, y, z)。
我怎样才能做到这一点?
解决方案
您可以执行以下操作,
def custom_loss(visible):
def loss_fun(y_true, y_pred):
# Getting the classes
classes = tf.argmax(y_pred, axis=1)
# The different x,y,z combinations for each class (None, 3) sized array
# We are doing a gather operation to select correct combination of x,y,z for a given class
inds = tf.gather(tf.constant([[0,1,2],[1,3,1],[7,8,1]], dtype=tf.int32), classes)
# We concatenate the batch dimnsion to the front [0, 1, 2, ....]
final_inds = tf.concat([tf.cumsum(tf.ones_like(classes, dtype=tf.int32))[:,tf.newaxis], inds], axis=1)
# Get the related ids from visible
return sparse_categorical_crossentropy(y_true, y_pred) + tf.reduce_mean(tf.gather_nd(visible,final_inds))
return loss_fun
PS:请记住,损失的第二部分(来自visible
)是不可微的(因为它只是一个输入并且没有任何参数)。
推荐阅读
- keras - 将密集输出添加到卷积输出
- docker - Docker - CMD npm start 先于复制所有
- jmeter - 用于测试仪表板向下钻取报告的 Locust 或 Jmeter
- mysql - 无法将表名传递给 MySQL 函数
- oauth - 从共享点设计器工作流调用 https://login.microsoftonline.com/{tanent ID}/oauth2/token
- javascript - 事件处理程序中的 React SetState 与模拟数据和来自 API 的数据的行为不同
- node.js - 将 MS 转换为 DD:HH:MM:SS
- rust - 实现返回 HashMap::IntoIter 的 IntoIterator 时出现“类型参数数量错误”
- python - 带有输入和输出的 Python 自定义构建
- java - 在套接字 java 上读取和写入对象