I'm trying to implement the VGG perceptual loss for a model training for video inputs. I implemented the perceptual loss like the recommendation in the question AttributeError: 'Tensor' object has no attribute '_keras_history':
My mainModel looks like the following graph: Graph of mainModel
The input size is (bathsize, frame_num, row, col, channel)
. And I want to get the perceptual loss for the middle frame, that is, frame_num/2
.
So, I implemented the following lossModel:
lossModel = VGG19(weights='imagenet')
lossModel = Model(inputs=lossModel.input,outputs=lossModel.get_layer('block3_conv4').output)
lossOut = lossModel(mainModel.output[:,frame_num/2])
fullModel = Model(mainModel.input,lossOut)
But I faced an error message in the line fullModel = Model(mainModel.input, lossOut)
:
attributeError: 'Tensor' object has no attribute '_keras_history'
BTW, I'm using keras version is '2.0.9'.
Could anyone help me with this?
THANKS a lot!!
This most of the times means that you're doing calculations outside layers.
A keras model needs to be made of keras layers. Operations outside layers are not allowed.
Take all your calculations and put them inside Lambda
layers: https://keras.io/layers/core/#lambda
Here, the mainModel.output[:,frame_num/2]
is an operation outside a layer.
Transfer it to a Lambda layer:
lossModel = VGG19(weights='imagenet')
lossModel = Model(inputs=lossModel.input,outputs=lossModel.get_layer('block3_conv4').output)
#you must connect lossmodel and mainmodel somewhere!!!
output = lossModel(mainModel.output)
output = Lambda(lambda x: x[:,frame_num/2])(output)
fullModel = Model(mainModel.input, output)
来源:https://stackoverflow.com/questions/47887288/attributeerror-tensor-object-has-no-attribute-keras-history-during-implem