我正在使用功能模型API,我想在Keras中创建以下自定义丢失功能:
我正在实现一个无监督的CNN,值Ix,Iy,It和epsilon是常量,u和v是我想要学习的参数。
到目前为止,我的尝试失败了,当我尝试训练时,我的模型返回一个ValueError,说“没有值不受支持”。搜索谷歌后,我发现这是由于我的损失不可分辨。
有人可以告诉我如何以Keras可以区分的方式正确创建这种损失函数。我的代码如下,任何帮助将不胜感激。
def charbonnier(I_x, I_y, I_t, U, V, e):
def charb(y_true, y_pred):
loss = K.sqrt(K.pow((multiply([U, I_x]) + multiply([V, I_y]) + I_t), 2) + e)
return K.mean(loss, axis=-1)
return charb
# Return the tf session used by the backend (optional)
tf_session = K.get_session()
def cnn(frame1, frame2):
# Concatenate the two images into one tensor.
input_ = np.dstack((frame1, frame2))
input_ = np.expand_dims(input_, axis=0)
print(input_.shape)
# Compute derivatives
I_x, I_y, I_t = compute_derivatives(input_)
#input_ = K.variable(input_)
inp_shape = (1, frame1.shape[0], frame1.shape[1], 2)
inp = Input(batch_shape=inp_shape)
conv1 = Conv2D(256, (7, 7), activation='relu', padding='same')(inp)
conv2 = Conv2D(256, (5, 5), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(512, (5, 5), activation='relu', padding='same')(pool1)
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv3)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool2)
conv6 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv6)
conv7 = Conv2D(1024, (3, 3), activation='relu', padding='same')(pool3)
conv8 = Conv2D(512, (1, 1), activation='relu', padding='same')(conv7)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv8)
conv9 = Conv2D(256, (5, 5), activation='relu', padding='same')(pool4)
upsample1 = UpSampling2D(size=(2, 2))(conv9)
conv10 = Conv2D(128, (5, 5), activation='relu', padding='same')(upsample1)
upsample2 = UpSampling2D(size=(2, 2))(conv10)
conv11 = Conv2D(64, (5, 5), activation='relu', padding='same')(upsample2)
upsample3 = UpSampling2D(size=(2, 2))(conv11)
conv12 = Conv2D(2, (15, 13), padding='same')(upsample3)
leaky_relu = LeakyReLU(alpha=0.3)(conv12)
upsample4 = UpSampling2D(size=(2, 2), name='flow')(leaky_relu)
model = Model(inputs=inp, outputs=upsample4)
U = K.variable(model.output[0, :, :, 0])
V = K.variable(model.output[0, :, :, 1])
e = K.constant(0.1)
c_loss = charbonnier(I_x, I_y, I_t, U, V, e)
model.compile(loss=c_loss, optimizer='adam')
model.fit(input_, input_, batch_size=1, epochs=10)
model.evaluate()
答案 0 :(得分:1)
解决。
变量U和V应按如下方式创建:
U = model.output[0, :, :, 0]
V = model.output[0, :, :, 1]