AttributeError:"NoneType"对象没有属性"_inbound_nodes" [英] AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

查看:115
本文介绍了AttributeError:"NoneType"对象没有属性"_inbound_nodes"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施 在此处定义的损失函数. 我使用fcn-VGG16获得地图x,并添加一个激活层.(x是fcn vgg16 net的输出).然后只需执行一些操作即可提取特征.

I want to implement the loss function defined here. I use fcn-VGG16 to obtain a map x, and add a activation layer.(x is the output of the fcn vgg16 net). And then just some operations to get extracted features.

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模型的可接受输出.

The error i get is at this line model = Model(inputs=img_input, outputs= loss ,name='generator') I think is because the way i calculate the loss makes it not an accepted output to keras models.

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)

我如何修改我的亏损功能以使其正常工作?

How can i modify my loss function to make this work?

推荐答案

loss = Lambda(co_attention_loss,name='name')([o_feature,b_feature])

表示您输入的args是一个列表,但您将 args 称为元组

means the args you input is a list, but you call args as a tuple

o_feature,b_feature = args

您可以将丢失代码更改为

you could change the loss code to

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)

注意:不进行测试

这篇关于AttributeError:"NoneType"对象没有属性"_inbound_nodes"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆