当使用Keras/Tensorflow 2使用UNET架构时,图像重建的损失函数应该是什么?
我正在为一项任务构建和测试一种新的图像重建方法。我用了类似的东西。但不是比较黑、白、灰三类像素;我想重建完整的图像,因为它是这样,我的模型可以学到一些东西。我使用数据生成器生成黑白图像,如下所示:当使用Keras/Tensorflow 2使用UNET架构时,图像重建的损失函数应该是什么?,tensorflow,keras,deep-learning,computer-vision,conv-neural-network,Tensorflow,Keras,Deep Learning,Computer Vision,Conv Neural Network,我正在为一项任务构建和测试一种新的图像重建方法。我用了类似的东西。但不是比较黑、白、灰三类像素;我想重建完整的图像,因为它是这样,我的模型可以学到一些东西。我使用数据生成器生成黑白图像,如下所示: train = gen.flow_from_directory(dir,color_mode='grayscale',class_mode='input',batch_size=batch_size,target_size=(w,h),seed=SEED,subset='training') 我使用
train = gen.flow_from_directory(dir,color_mode='grayscale',class_mode='input',batch_size=batch_size,target_size=(w,h),seed=SEED,subset='training')
我使用代码的最后两行作为:
outputs = layers.Conv2D(1, 1, activation = 'sigmoid')(x) # Last Layer is 1x1 Conv
# Sigmoid will give 0-1 values per pixel and we'll calculate RMSE
model = Model(inputs, outputs)
model.compile(optimizer='adam',loss='mse',metrics=[keras.metrics.RootMeanSquaredError()])
对于不同的模型方法,我尝试了,RMSE
,并将r_平方
作为一个度量:
def rmse(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true)))
def r_squared(y_true, y_pred):
SS_res = K.sum(K.square( y_true-y_pred ))
SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
return ( 1 - SS_res/(SS_tot + K.epsilon()) )
model =model.compile('adam',loss=rmse, metrics=[r_squared])
我想知道Keras或Tensorflow对于此类问题是否给出了任何其他度量和/或损失。我正在使用
灰度
图像,试图迫使潜在空间了解图像的一些有意义的信息。据我所知,您正在执行一项自动编码任务,试图压缩然后重建输入
根据我的经验,使用的损失通常是以下两种情况之一:
的优点是,模型的最佳解决方案是预测潜在值的平均值。例如,如果模型认为某个像素的值为0,概率为0.3,1的概率为0.7,那么模型的最佳解决方案是预测MSE
0.7=0.3*0+0.7*1
,平均绝对误差,也称为L1。MAE
的优点是,模型的最佳解决方案是预测中值。在先例中,中位数为1。有时MAE
比MEA
产生更好的示例MSE
一个更极端的例子,如果你正在驾驶一辆自驾车车,而汽车必须决定向左或向右行驶,<代码> MSE 将进入中间,而<代码> MAE<代码>将选择一方。
您可以通过以下方式计算MAE
:
mae = tf.reduce_mean(tf.abs(prediction - taget))
RMSE
应产生与MSE
类似的结果。平方根可能不是很有用。这似乎是正确的,因为自动编码器上的Tensorflow使用“mae”
和“mse”