简体   繁体   中英

How to fix ''NoneType' object has no attribute '_inbound_nodes'' ' when working wih Keras layer Lambda

I'm trying to use a Lambda layer to wrap a function ('get_reconstruction_loss') that combines two layers in a way so that it calculates the MSE of the results of both. Unfortunately, I can't instantiate the model due to the error shown below.

I'm thankful for any hints!

The code is based on the work of https://github.com/rajatkb/Deep-Super-Resolution-Research

Code:

import cv2
from keras import Model
from keras import backend as K
from keras.applications.vgg16 import VGG16
from keras.layers import Conv2D, Input, Lambda
import numpy as np

class MyClass:
    # Source: https://github.com/rajatkb/Deep-Super-Resolution-Research
    def __init__(self, img_size, channels, is_train):
        # Var definition
        self.lambda_content = 1
        loss_layer = 'block2_conv2'

        ##############
        ### define Model here ###
        model_inp = Input(shape = (img_size , img_size , channels) , name='input_layer')
        model_output = Conv2D(filters = 64, kernel_size = (9,9),padding ='same', activation ='relu', kernel_initializer= 'RandomNormal' )(model_inp)
        model_output = Conv2D(filters = 32, kernel_size = (1,1),padding ='same', activation ='relu', kernel_initializer= 'RandomNormal' )(model_output)
        model_output = Conv2D(filters = 3, kernel_size = (5,5),padding ='same', activation ='linear', kernel_initializer= 'RandomNormal', name = 'model_output')(model_output)

        self.inference_model = Model(inputs=model_inp, outputs=model_output)
        ##############
        if is_train:

            vgg_inp = Input(shape =(img_size, img_size, channels), name='vgg_net_input')
            vgg = VGG16(input_tensor =vgg_inp, input_shape =(img_size,img_size,channels) , weights='imagenet' , include_top=False)
            for l in vgg.layers: l.trainable =False

            # Layer Output
            loss_layer_output = [vgg.get_layer(loss_layer).output]

            # Define a Model that calculates the feature representation
            vgg_reconstruction_model = Model(inputs =vgg_inp, outputs =loss_layer_output)
            vgg_reconstruction_model.summary()

            # Feature represenation of hr image and prediction image
            hr_vgg = vgg_reconstruction_model(vgg_inp)
            pred_vgg = vgg_reconstruction_model(model_output)

            reconstruction_loss = Lambda(self.get_reconstruction_loss,output_shape=(1,), name='reconstruction_loss')([pred_vgg[0], hr_vgg[0]])


            self.loss_model = Model(inputs=[model_inp, vgg_inp] , outputs = [model_output, reconstruction_loss], name='loss_model')



    def get_reconstruction_loss(self,args):
        new_activation, content_activation = args[0], args[1]
        return K.constant(self.lambda_content) * K.mean(K.square(new_activation - content_activation))


if __name__ == "__main__":
    net = MyClass(500,3,True)

Error:

Exception has occurred: AttributeError
'NoneType' object has no attribute '_inbound_nodes'
File "/home/robousb2/gD_tools/playground/percep_loss_question.py", line 44, in __init__
    self.loss_model = Model(inputs=[model_inp, vgg_inp] , outputs = [model_output, reconstruction_loss], name='loss_model')
File "/home/robousb2/gD_tools/playground/percep_loss_question.py", line 54, in <module>
    net = MyClass(500,3,True)

The problem is here:

reconstruction_loss = Lambda(self.get_reconstruction_loss,output_shape=(1,), name='reconstruction_loss')([pred_vgg[0], hr_vgg[0]])

I am not sure why you are indexing with [0] , if you remove these, it works:

reconstruction_loss = Lambda(self.get_reconstruction_loss,output_shape=(1,), name='reconstruction_loss')([pred_vgg, hr_vgg])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM