【发布时间】:2018-10-22 19:47:31
【问题描述】:
我有一个预训练的网络,我想使用它来评估我的 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 对象。可接受的提要值包括 Python 标量、字符串、列表、numpy ndarray 或 TensorHandles。
我做错了什么?有没有办法评估来自不同网络(由 Keras 使用 TensorFlow 后端制作)的 TensorFlow 对象上的网络。
提前致谢。
【问题讨论】:
标签: python tensorflow keras deep-learning loss-function