我想实施 loss function在这里定义。 我使用fcn-VGG16获得地图x,并添加一个激活层。(x是fcn vgg16 net的输出)。然后只需执行一些操作即可提取特征。
co_map = Activation('sigmoid')(x)
#add mean values
img = Lambda(AddMean, name = 'addmean')(img_input)
#img map multiply
img_o = Lambda(HighLight, name='highlightlayer1')([img, co_map])
img_b = Lambda(HighLight, name='highlightlayer2')([img, 1-co_map])
extractor = ResNet50(weights = 'imagenet', include_top = False, pooling = 'avg')
extractor.trainable = False
extractor.summary()
o_feature = extractor(img_o)
b_feature = extractor(img_b)
loss = Lambda(co_attention_loss,name='name')([o_feature,b_feature])
model = Model(inputs=img_input, outputs= loss ,name='generator')
我得到的错误是在这一行model = Model(inputs=img_input, outputs= loss ,name='generator')
我认为是因为我计算损失的方式使它不能被keras模型接受。
def co_attention_loss(args):
loss = []
o_feature,b_feature = args
c = 2048
for i in range(5):
for j in range(i,5):
if i!=j:
print("feature shape : "+str(o_feature.shape))
d1 = K.sum(K.pow(o_feature[i] - o_feature[j],2))/c
d2 = K.sum(K.pow(o_feature[i] - b_feature[i],2))
d3 = K.sum(K.pow(o_feature[j] - b_feature[j],2))
d4 = d2 + d3/(2*c)
p = K.exp(-d1)/K.sum([K.exp(-d1),K.exp(-d4)])
loss.append(-K.log(p))
return K.sum(loss)
我如何修改我的亏损功能以使其正常工作?
答案 0 :(得分:1)
loss = Lambda(co_attention_loss,name='name')([o_feature,b_feature])
表示您输入的args是一个列表,但是您将 args 称为元组
o_feature,b_feature = args
您可以将丢失代码更改为
def co_attention_loss(args):
loss = []
o_feature = args[0]
b_feature = args[1]
c = 2048
for i in range(5):
for j in range(i,5):
if i!=j:
print("feature shape : "+str(o_feature.shape))
d1 = K.sum(K.pow(o_feature[i] - o_feature[j],2))/c
d2 = K.sum(K.pow(o_feature[i] - b_feature[i],2))
d3 = K.sum(K.pow(o_feature[j] - b_feature[j],2))
d4 = d2 + d3/(2*c)
p = K.exp(-d1)/K.sum([K.exp(-d1),K.exp(-d4)])
loss.append(-K.log(p))
return K.sum(loss)
注意:不进行测试