In my model, the output of the hidden layer, namely 'encoded', has two channels (eg. shape: [none, 128, 128, 2]). I hope to add SSIM between these two channels in loss function:
loss = ssim(input, output) + theta*ssim(encoded(channel1), encoded(channel2)).
How could I implement this? The following is the architecture of my model.
def structural_similarity_index(y_true, y_pred):
loss = 1 - tf.image.ssim(y_true, y_pred, max_val=1.0)
return loss
def mymodel():
input_img = Input(shape=(256, 256, 1))
# encoder
x = Conv2D(4, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
encoded = Conv2D(2, (3, 3), activation='relu', padding='same', name='encoder')(x)
# decoder
x = Conv2D(4, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer = 'adadelta', loss = structural_similarity_index)
autoencoder.summary()
return autoencoder
I tried to define a 'loss_warper' function as shown below, but it didn't work. This is how I added this loss function:
autoencoder.add_loss(loss_wrapper(encoded[:,:,:,0],encoded[:,:,:,1])(input_img, decoded))
the 'loss_warper' function:
def loss_wrapper(CH1, CH2):
def structural_similarity_index(y_true, y_pred):
regweight = 0.01
loss = 1 - tf.image.ssim(y_true, y_pred, max_val=1.0)
loss = loss + regweight*(1-tf.image.ssim(CH1, CH2, max_val=1.0))
return loss
return structural_similarity_index
The error message:
File "E:/Autoencoder.py", line 160, in trainprocess
validation_data= (x_validate, x_validate))
...
ValueError: ('Error when checking model target: expected no data, but got:', array([...]...[...]))
Does anyone know how to implement this? Any help is highly appreciated!