一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 關于keras多任務多loss回傳的思考

關于keras多任務多loss回傳的思考

2021-11-12 12:21chestnut-- Python

這篇文章主要介紹了關于keras多任務多loss回傳的思考,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

如果有一個多任務多loss的網絡,那么在訓練時,loss是如何工作的呢?

比如下面:

model = Model(inputs = input, outputs = [y1, y2])
l1 = 0.5
l2 = 0.3
model.compile(loss = [loss1, loss2], loss_weights=[l1, l2], ...)

其實我們最終得到的loss為

final_loss = l1 * loss1 + l2 * loss2

我們最終的優化效果是最小化final_loss。

問題來了,在訓練過程中,是否loss2只更新得到y2的網絡通路,還是loss2會更新所有的網絡層呢?

此問題的關鍵在梯度回傳上,即反向傳播算法。

關于keras多任務多loss回傳的思考

關于keras多任務多loss回傳的思考

所以loss1只對x1和x2有影響,而loss2只對x1和x3有影響。

補充:keras 多個LOSS總和定義

關于keras多任務多loss回傳的思考

用字典形式,名字是模型中輸出那一層的名字,這里的loss可以是自己定義的,也可是自帶的

補充:keras實戰-多類別分割loss實現

本文樣例均為3d數據的onehot標簽形式,即y_true(batch_size,x,y,z,class_num)

1、dice loss

def dice_coef_fun(smooth=1):
    def dice_coef(y_true, y_pred):
        #求得每個sample的每個類的dice
        intersection = K.sum(y_true * y_pred, axis=(1,2,3))
        union = K.sum(y_true, axis=(1,2,3)) + K.sum(y_pred, axis=(1,2,3))
        sample_dices=(2. * intersection + smooth) / (union + smooth) #一維數組 為各個類別的dice
        #求得每個類的dice
        dices=K.mean(sample_dices,axis=0)
        return K.mean(dices) #所有類別dice求平均的dice
    return dice_coef
 
def dice_coef_loss_fun(smooth=0):
    def dice_coef_loss(y_true,y_pred):
        return 1-1-dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
    return dice_coef_loss

2、generalized dice loss

def generalized_dice_coef_fun(smooth=0):
    def generalized_dice(y_true, y_pred):
        # Compute weights: "the contribution of each label is corrected by the inverse of its volume"
        w = K.sum(y_true, axis=(0, 1, 2, 3))
        w = 1 / (w ** 2 + 0.00001)
        # w為各個類別的權重,占比越大,權重越小
        # Compute gen dice coef:
        numerator = y_true * y_pred
        numerator = w * K.sum(numerator, axis=(0, 1, 2, 3))
        numerator = K.sum(numerator)
 
        denominator = y_true + y_pred
        denominator = w * K.sum(denominator, axis=(0, 1, 2, 3))
        denominator = K.sum(denominator)
 
        gen_dice_coef = numerator / denominator
 
        return  2 * gen_dice_coef
    return generalized_dice
 
def generalized_dice_loss_fun(smooth=0):
    def generalized_dice_loss(y_true,y_pred):
        return 1 - generalized_dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
    return generalized_dice_loss

3、tversky coefficient loss

# Ref: salehi17, "Twersky loss function for image segmentation using 3D FCDN"
# -> the score is computed for each class separately and then summed
# alpha=beta=0.5 : dice coefficient
# alpha=beta=1   : tanimoto coefficient (also known as jaccard)
# alpha+beta=1   : produces set of F*-scores
# implemented by E. Moebel, 06/04/18
def tversky_coef_fun(alpha,beta):
    def tversky_coef(y_true, y_pred):
        p0 = y_pred  # proba that voxels are class i
        p1 = 1 - y_pred  # proba that voxels are not class i
        g0 = y_true
        g1 = 1 - y_true
 
        # 求得每個sample的每個類的dice
        num = K.sum(p0 * g0, axis=( 1, 2, 3))
        den = num + alpha * K.sum(p0 * g1,axis= ( 1, 2, 3)) + beta * K.sum(p1 * g0, axis=( 1, 2, 3))
        T = num / den  #[batch_size,class_num]
        
        # 求得每個類的dice
        dices=K.mean(T,axis=0) #[class_num]
        
        return K.mean(dices)
    return tversky_coef
 
def tversky_coef_loss_fun(alpha,beta):
    def tversky_coef_loss(y_true,y_pred):
        return 1-tversky_coef_fun(alpha=alpha,beta=beta)(y_true=y_true,y_pred=y_pred)
    return tversky_coef_loss

4、IoU loss

def IoU_fun(eps=1e-6):
    def IoU(y_true, y_pred):
        # if np.max(y_true) == 0.0:
        #     return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
        intersection = K.sum(y_true * y_pred, axis=[1,2,3])
        union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
        #
        ious=K.mean((intersection + eps) / (union + eps),axis=0)
        return K.mean(ious)
    return IoU
 
def IoU_loss_fun(eps=1e-6):
    def IoU_loss(y_true,y_pred):
        return 1-IoU_fun(eps=eps)(y_true=y_true,y_pred=y_pred)
    return IoU_loss

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/m0_37477175/article/details/85163362

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品一区久久精品 | 欧美激情精品久久久久久不卡 | 国产日韩欧美成人 | 国产免费又粗又猛又爽视频国产 | 被教官揉了一晚上的奶小说 | 污樱桃视频| 高h禁伦奶水女 | 99精品国产自在现线观看 | 成人午夜爽爽爽免费视频 | 国产最新进精品视频 | 美女主播免费观看 | 全色黄大色黄大片爽一次 | 精品在线免费观看视频 | 嫩模被黑人粗大挺进 | 99精品久久精品一区二区 | 秋霞一级 | 99热这里只有精品国产免费 | 羞羞色男人的天堂伊人久久 | 美女脱了内裤让男生玩屁股 | 日韩国产成人 | 男人资源站 | 亚洲品质水蜜桃 | 91桃花 | 欧美高清乌克兰精品另类 | 亚洲mm色国产网站 | 99re5在线精品视频热线 | 亚洲福利一区二区精品秒拍 | 久久91精品国产91久久户 | 思思99热久久精品在2019线 | 韩国理论三级在线观看视频 | 欧美一区二区三区久久久 | www.男人的天堂 | 久久影院中文字幕 | 亚洲 激情 | 本站只有精品 | 冰漪丰满大乳人体图片欣赏 | 欧美精品综合一区二区三区 | 国产亚洲玖玖玖在线观看 | fistingvideos头交尿眼 | 成人性生交大片免费看软件 | 青山葵在线 |