Я пытаюсь использовать лямбда-слой, чтобы обернуть функцию ('get_reconstruction_loss'), которая объединяет два слоя таким образом, чтобы он вычислял MSE для результатов обоих. К сожалению, я не могу создать экземпляр модели из-за ошибки, показанной ниже.
Я благодарен за любые подсказки!
Код основан на работе https://github.com/rajatkb/Deep-Super-Resolution-Research
Код:
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)
Ошибка:
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)