简体   繁体   English

如何在Keras损失函数中使用预训练的TensorFlow网络

[英]How to use a pre-trained TensorFlow net in Keras loss function

I have a pre-trained net I want to use in order to evaluate loss in my Keras net. 我有一个要使用的经过预先训练的网,以便评估我的Keras网中的损失。 The pre-trained network was trained using TensorFlow and I just want to use it as part of my loss calculation. 预训练网络是使用TensorFlow进行训练的,我只想将其用作损失计算的一部分。

The code of my custom loss function is currently: 我的自定义损失函数的代码当前为:

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))

Where deep_gaze is an object that is ment to manage the access to the external pre-trained net I am using. 其中deep_gaze是一个对象,用于管理对我正在使用的外部预训练网络的访问。

It is defined this way: 它是这样定义的:

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

When I run this I get the error: 当我运行此错误时:

TypeError: The value of a feed cannot be a tf.Tensor object. TypeError:供稿的值不能是tf.Tensor对象。 Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. 可接受的feed值包括Python标量,字符串,列表,numpy ndarrays或TensorHandles。

What am I doing wrong? 我究竟做错了什么? Is there a way to evaluate a net on a TensorFlow object coming for a different net (that was made by Keras with a TensorFlow backend). 有没有一种方法可以评估TensorFlow对象上的网络以获取其他网络(该网络由Keras与TensorFlow后端组成)。

Thanks in advance. 提前致谢。

There are two main problems: 主要有两个问题:

  • When you call get_saliency_map with input_data=y_true you are feeding a tensor input_data to another tensor self.input_tensor , and this is not valid. 当您使用input_data=y_true调用get_saliency_map ,您正在将张量input_data馈送到另一个张量self.input_tensor ,这是无效的。 Moreover, these tensors do not hold a value at graph creation time, but rather they define a computation that will eventually produce a value. 此外,这些张量在图创建时不保存值,而是定义最终将产生值的计算。

  • Even if you could get an output from get_saliency_map , your code would still not work because this function disconnects your TensorFlow graph (it doesn't return a tensor), and all the logic must reside within the graph. 即使您可以从get_saliency_map获得输出,您的代码仍然无法正常工作,因为此函数会断开TensorFlow图的连接(它不会返回张量),并且所有逻辑都必须驻留在图中。 Each tensor has to be computed based on the other available tensors in the graph. 必须根据图中的其他可用张量来计算每个张量。

The solution to this problem is to define the model producing self.log_density_wo_centerbias within the graph where you define your loss function, using the tensors y_true and y_pred directly as input without disconnecting the graph. 解决此问题的方法是在直接定义张量y_truey_pred作为输入而无需断开图形的情况下,在定义损失函数的图形中定义在模型中生成self.log_density_wo_centerbias的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在TensorFlow中使用预训练模型 - How to use pre-trained model in TensorFlow 如何在Tensorflow中使用不带类的预训练模型? - How to use pre-trained models without classes in Tensorflow? 如何使用预训练的权重在 tensorflow 中训练卷积神经网络? - How to use pre-trained weight for training convolutional NN in tensorflow? 如何在 tensorflow 中使用预训练的 model 进行预测? - how to predict with pre-trained model in tensorflow? 当我尝试加载和使用预训练的 model 时出现 Tensorflow Keras 错误 - Tensorflow Keras error when I tried to load and use a pre-trained model 无法在Tensorflow估算器中训练Keras预训练模型 - Cannot Train Keras Pre-trained Model in Tensorflow Estimator 如何使用 OpenVINO 预训练模型? - How to use OpenVINO pre-trained models? 如何使用现有CNN模型中的预训练权重在Keras中进行迁移学习? - How can I use pre-trained weights from an existing CNN model for transfer learning in Keras? 如何在 tf.data.Dataset.map 中使用预训练的 keras 模型进行推理? - How to use a pre-trained keras model for inference in tf.data.Dataset.map? 如何在Keras中将自己的词嵌入与像word2vec这样的预训练嵌入一起使用 - How to use own word embedding with pre-trained embedding like word2vec in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM