'attributeError:'Tensor'对象在使用keras通过预训练的VGG实现感知损失的过程中没有属性'_keras_history' [英] 'attributeError: 'Tensor' object has no attribute '_keras_history' during implementing perceptual loss with pretrained VGG using keras
问题描述
我正在尝试为视频输入的模型训练实现VGG感知损失. 我在问题 AttributeError:'Tensor'中实施了建议中的感知损失对象没有属性"_keras_history" :
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':
我的mainModel如下图所示: mainModel图
My mainModel looks like the following graph: Graph of mainModel
输入大小为(bathsize, frame_num, row, col, channel)
.我想获得中间帧的感知损失,即frame_num/2
.
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
.
因此,我实现了以下lossModel:
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)
但是我在fullModel = Model(mainModel.input, lossOut)
行遇到错误消息:
But I faced an error message in the line fullModel = Model(mainModel.input, lossOut)
:
attributeError:张量"对象没有属性"_keras_history"
attributeError: 'Tensor' object has no attribute '_keras_history'
顺便说一句,我使用的是keras版本是'2.0.9'.
BTW, I'm using keras version is '2.0.9'.
有人可以帮我吗?
非常感谢!
推荐答案
大多数情况下,这意味着您要在图层外部进行计算.
This most of the times means that you're doing calculations outside layers.
一个keras模型需要由keras图层组成.不允许在层外进行操作.
A keras model needs to be made of keras layers. Operations outside layers are not allowed.
进行所有计算并将其放在Lambda
层中: https://keras.io /layers/core/#lambda
Take all your calculations and put them inside Lambda
layers: https://keras.io/layers/core/#lambda
在这里,mainModel.output[:,frame_num/2]
是层外的操作.
Here, the mainModel.output[:,frame_num/2]
is an operation outside a layer.
将其传输到Lambda层:
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)
这篇关于'attributeError:'Tensor'对象在使用keras通过预训练的VGG实现感知损失的过程中没有属性'_keras_history'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!