AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

狂风中的少年 提交于 2020-01-04 07:47:12

问题


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')

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?


回答1:


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

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)

NOTICE: NOT TEST



来源:https://stackoverflow.com/questions/51510091/attributeerror-nonetype-object-has-no-attribute-inbound-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!