关于“ValueError: Loss should be a callable”

关于“ValueError: Loss should be a callable”


最近在跑一个别人的代码,关于图像块匹配。那个人自定义了Loss。在我电脑上跑时出现错误,“ValueError: Loss should be a callable”。尝试了很多方法都不行。最后。我把他那个Loss的类,改成函数,居然就跑通了。

改之前:

	class position_loss(tf.keras.losses.Loss):
		def __init__(self):
    		super(position_loss, self).__init__()
    		
		def call(self, y_true, y_pred):
			y_pred_index=tf.squeeze(y_pred, axis=3)
    		y_true_index = tf.squeeze(y_true, axis=3)
    		centriod_pred=tf.map_fn(lambda x:self.cal_position(x),elems=y_pred_index)
    		centriod_true=tf.map_fn(lambda y:self.cal_position(y),elems=y_true_index)
    		loss0=tf.keras.losses.binary_crossentropy(y_true, y_pred)
    		loss1=tf.reduce_mean(tf.square(y_true-y_pred))*100
    		loss2=tf.reduce_mean(tf.square(centriod_pred-centriod_true))
		 	tf.print(loss1,loss2)
    		return loss0+loss1+loss2

		def cal_position(self,y_pre):
			x = tf.cast(tf.range(0, tf.shape(y_pre)[0]), dtype=tf.float32)
    		x = tf.reshape(x, (1, tf.shape(y_pre)[0]))
    		x = tf.tile(x, (tf.shape(y_pre)[0], 1))
			y = tf.cast(tf.range(0, tf.shape(y_pre)[0]), dtype=tf.float32)
    		y = tf.reshape(y, (tf.shape(y_pre)[0], 1))
    		y = tf.tile(y, (1, tf.shape(y_pre)[0]))
			M00=tf.reduce_sum(y_pre)
    		M10=tf.reduce_sum(x*y_pre)
    		M01=tf.reduce_sum(y*y_pre)
			position_x=M10/M00
    		position_y=M01/M00
			position_x=tf.reshape(position_x,(1,1))
    		position_y=tf.reshape(position_y,(1,1))
			position=tf.concat((position_x,position_y),axis=1)
			return position

model.complie(…, loss=position_loss,…) 报错:ValueError: Loss should be a callable

把类改成函数之后:

    def cal_position(y_pre):
    	x = tf.cast(tf.range(0, tf.shape(y_pre)[0]), dtype=tf.float32)
    	x = tf.reshape(x, (1, tf.shape(y_pre)[0]))
    	x = tf.tile(x, (tf.shape(y_pre)[0], 1))
		y = tf.cast(tf.range(0, tf.shape(y_pre)[0]), dtype=tf.float32)
    	y = tf.reshape(y, (tf.shape(y_pre)[0], 1))
    	y = tf.tile(y, (1, tf.shape(y_pre)[0]))
		M00=tf.reduce_sum(y_pre)
    	M10=tf.reduce_sum(x*y_pre)
    	M01=tf.reduce_sum(y*y_pre)
		position_x=M10/M00
    	position_y=M01/M00
		position_x=tf.reshape(position_x,(1,1))
    	position_y=tf.reshape(position_y,(1,1))
		position=tf.concat((position_x,position_y),axis=1)
		return position
    def func_loss(y_true, y_pred):
		y_pred_index=tf.squeeze(y_pred, axis=3)
		index_pred = tf.map_fn(lambda x: tf.reduce_mean(tf.cast(tf.where((x > 0.95)), 		dtype=tf.float32), axis=0), elems=y_pred_index,dtype=tf.float32)
		y_true_index = tf.squeeze(y_true, axis=3)
		index_true = tf.map_fn(lambda y: tf.reduce_mean(tf.cast(tf.where((y ==1.0)), 	dtype=tf.float32), axis=0),elems=y_true_index, dtype=tf.float32)
		centriod_pred=tf.map_fn(lambda x:cal_position(x),elems=y_pred_index)
		centriod_true=tf.map_fn(lambda y:cal_position(y),elems=y_true_index)
		loss0=tf.keras.losses.binary_crossentropy(y_true, y_pred)
		loss1=tf.reduce_mean(tf.square(y_true-y_pred))*100
		loss2=tf.reduce_mean(tf.square(centriod_pred-centriod_true))
		return loss0+loss1+loss2

再model.complie(…, loss=func_loss,…)就ok了。

我也不知道为什么。。。。

Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值