I'm trying to create an image denoising ConvNet in Keras and I want to create my own loss function. I want it to take a noisy image as an input and to get the noise as an output. This loss function is pretty much like a MSE loss but which will make my network learn to remove the clean image and not the noise from the input noisy image.
我想在Keras中创建一个图像去噪,我想创建自己的损失函数。我希望它以一个噪声图像作为输入,并将噪声作为输出。这个损失函数非常类似于MSE的损失,但是这将使我的网络学习去除干净的图像,而不是来自输入噪声图像的噪声。
The loss function I want to implement with y the noisy image, x the clean image and R(y) the predicted image:
我要用y来实现的损失函数图像,x图像和R(y)的预测图像:
I've tried to make it by myself but I don't know how to make the loss access to my noisy images since it changes all the time.
我试着自己去做,但我不知道如何让我的噪音消失,因为它一直在变化。
def residual_loss(noisy_img):
def loss(y_true, y_pred):
return np.mean(np.square(y_pred - (noisy_img - y_true), axis=-1)
return loss
Basically, what I need to do is something like this :
基本上,我需要做的是这样的:
input_img = Input(shape=(None,None,3))
c1 = Convolution2D(64, (3, 3))(input_img)
a1 = Activation('relu')(c1)
c2 = Convolution2D(64, (3, 3))(a1)
a2 = Activation('relu')(c2)
c3 = Convolution2D(64, (3, 3))(a2)
a3 = Activation('relu')(c3)
c4 = Convolution2D(64, (3, 3))(a3)
a4 = Activation('relu')(c4)
c5 = Convolution2D(3, (3, 3))(a4)
out = Activation('relu')(c5)
model = Model(input_img, out)
model.compile(optimizer='adam', loss=residual_loss(input_img))
But if I try this, I get :
但如果我这样做,我得到:
IndexError: tuple index out of range
What can I do ?
我能做什么?
1 个解决方案
#1
1
Since it's quite unusual to use the "input" in the loss function (it's not meant for that), I think it's worth saying:
因为在损失函数中使用“输入”是很不寻常的(它不是为了那个),我认为它值得说:
It's not the role of the loss function to separate the noise. The loss function is just a measure of "how far from right you are".
这不是损失函数的作用来分离噪声。损失函数只是衡量“你离正确的距离有多远”。
It's your model that will separate things, and the results you expect from your model are y_true
.
这是您的模型,它将分离事物,您对模型所期望的结果是y_true。
You should use a regular loss, with X_training = noisy images
and Y_training = noises
.
您应该使用常规的损失,使用X_training =嘈杂的图像和Y_training =噪声。
That said...
也就是说……
You can create a tensor for noisy_img
outside the loss function and keep it stored. All operations inside a loss function must be tensor functions, so use the keras backend for that:
您可以在损失函数之外创建一个用于噪声的张量,并保存它。损失函数内的所有操作都必须是张量函数,因此使用keras后端:
import keras.backend as K
noisy_img = K.variable(X_training) #you must do this for each bach
But you must take batch sizes into account, this var being outside the loss function will need you to fit just one batch per epoch.
但是你必须考虑批量大小,这个var在损失函数之外需要你每一个时期只适应一个批次。
def loss(y_true,y_pred):
return K.mean(K.square(y_pred-y_true) - K.square(y_true-noisy_img))
Training one batch per epoch:
每一阶段培训一批:
for batch in range(0,totalSamples,size):
noisy_img = K.variable(X_training[batch:size])
model.fit(X_training[batch:size],Y_training[batch:size], batch_size=size)
For using just a mean squared error, organize your data like this:
为了使用一个平均平方误差,组织你的数据如下:
originalImages = loadYourImages() #without noises
Y_training = createRandomNoises() #without images
X_training = addNoiseToImages(originalImages,Y_training)
Now you just use a "mse", or any other built-in loss.
现在你只需使用“mse”或其他内置的损失。
model.fit(X_training,Y_training,....)
#1
1
Since it's quite unusual to use the "input" in the loss function (it's not meant for that), I think it's worth saying:
因为在损失函数中使用“输入”是很不寻常的(它不是为了那个),我认为它值得说:
It's not the role of the loss function to separate the noise. The loss function is just a measure of "how far from right you are".
这不是损失函数的作用来分离噪声。损失函数只是衡量“你离正确的距离有多远”。
It's your model that will separate things, and the results you expect from your model are y_true
.
这是您的模型,它将分离事物,您对模型所期望的结果是y_true。
You should use a regular loss, with X_training = noisy images
and Y_training = noises
.
您应该使用常规的损失,使用X_training =嘈杂的图像和Y_training =噪声。
That said...
也就是说……
You can create a tensor for noisy_img
outside the loss function and keep it stored. All operations inside a loss function must be tensor functions, so use the keras backend for that:
您可以在损失函数之外创建一个用于噪声的张量,并保存它。损失函数内的所有操作都必须是张量函数,因此使用keras后端:
import keras.backend as K
noisy_img = K.variable(X_training) #you must do this for each bach
But you must take batch sizes into account, this var being outside the loss function will need you to fit just one batch per epoch.
但是你必须考虑批量大小,这个var在损失函数之外需要你每一个时期只适应一个批次。
def loss(y_true,y_pred):
return K.mean(K.square(y_pred-y_true) - K.square(y_true-noisy_img))
Training one batch per epoch:
每一阶段培训一批:
for batch in range(0,totalSamples,size):
noisy_img = K.variable(X_training[batch:size])
model.fit(X_training[batch:size],Y_training[batch:size], batch_size=size)
For using just a mean squared error, organize your data like this:
为了使用一个平均平方误差,组织你的数据如下:
originalImages = loadYourImages() #without noises
Y_training = createRandomNoises() #without images
X_training = addNoiseToImages(originalImages,Y_training)
Now you just use a "mse", or any other built-in loss.
现在你只需使用“mse”或其他内置的损失。
model.fit(X_training,Y_training,....)