[英]How to use a pre-trained TensorFlow net in Keras loss function
我有一个要使用的经过预先训练的网,以便评估我的Keras网中的损失。 预训练网络是使用TensorFlow进行训练的,我只想将其用作损失计算的一部分。
我的自定义损失函数的代码当前为:
def custom_loss_func(y_true, y_pred):
# Get saliency of both true and pred
sal_true = deep_gaze.get_saliency_map(y_true)
sal_pred = deep_gaze.get_saliency_map(y_pred)
return K.mean(K.square(sal_true-sal_pred))
其中deep_gaze是一个对象,用于管理对我正在使用的外部预训练网络的访问。
它是这样定义的:
class DeepGaze(object):
CHECK_POINT = os.path.join(os.path.dirname(__file__), 'DeepGazeII.ckpt') # DeepGaze II
def __init__(self):
print('Loading Deep Gaze II...')
with tf.Graph().as_default() as deep_gaze_graph:
saver = tf.train.import_meta_graph('{}.meta'.format(self.CHECK_POINT))
self.input_tensor = tf.get_collection('input_tensor')[0]
self.log_density_wo_centerbias = tf.get_collection('log_density_wo_centerbias')[0]
self.tf_session = tf.Session(graph=deep_gaze_graph)
saver.restore(self.tf_session, self.CHECK_POINT)
print('Deep Gaze II Loaded')
'''
Returns the saliency map of the input data.
input format is a 4d array [batch_num, height, width, channel]
'''
def get_saliency_map(self, input_data):
log_density_prediction = self.tf_session.run(self.log_density_wo_centerbias,
{self.input_tensor: input_data})
return log_density_prediction
当我运行此错误时:
TypeError:供稿的值不能是tf.Tensor对象。 可接受的feed值包括Python标量,字符串,列表,numpy ndarrays或TensorHandles。
我究竟做错了什么? 有没有一种方法可以评估TensorFlow对象上的网络以获取其他网络(该网络由Keras与TensorFlow后端组成)。
提前致谢。
主要有两个问题:
当您使用input_data=y_true
调用get_saliency_map
,您正在将张量input_data
馈送到另一个张量self.input_tensor
,这是无效的。 此外,这些张量在图创建时不保存值,而是定义最终将产生值的计算。
即使您可以从get_saliency_map
获得输出,您的代码仍然无法正常工作,因为此函数会断开TensorFlow图的连接(它不会返回张量),并且所有逻辑都必须驻留在图中。 必须根据图中的其他可用张量来计算每个张量。
解决此问题的方法是在直接定义张量y_true
和y_pred
作为输入而无需断开图形的情况下,在定义损失函数的图形中定义在模型中生成self.log_density_wo_centerbias
的模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.