您好,登錄后才能下訂單哦!
小編這次要給大家分享的是Keras如何自定義IOU,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
我就廢話不多說了,大家還是直接看代碼吧!
def iou(y_true, y_pred, label: int): """ Return the Intersection over Union (IoU) for a given label. Args: y_true: the expected y values as a one-hot y_pred: the predicted y values as a one-hot or softmax output label: the label to return the IoU for Returns: the IoU for the given label """ # extract the label values using the argmax operator then # calculate equality of the predictions and truths to the label y_true = K.cast(K.equal(K.argmax(y_true), label), K.floatx()) y_pred = K.cast(K.equal(K.argmax(y_pred), label), K.floatx()) # calculate the |intersection| (AND) of the labels intersection = K.sum(y_true * y_pred) # calculate the |union| (OR) of the labels union = K.sum(y_true) + K.sum(y_pred) - intersection # avoid divide by zero - if the union is zero, return 1 # otherwise, return the intersection over union return K.switch(K.equal(union, 0), 1.0, intersection / union) def mean_iou(y_true, y_pred): """ Return the Intersection over Union (IoU) score. Args: y_true: the expected y values as a one-hot y_pred: the predicted y values as a one-hot or softmax output Returns: the scalar IoU value (mean over all labels) """ # get number of labels to calculate IoU for num_labels = K.int_shape(y_pred)[-1] - 1 # initialize a variable to store total IoU in mean_iou = K.variable(0) # iterate over labels to calculate IoU for for label in range(num_labels): mean_iou = mean_iou + iou(y_true, y_pred, label) # divide total IoU by number of labels to get mean IoU return mean_iou / num_labels
補充知識:keras 自定義評估函數(shù)和損失函數(shù)loss訓(xùn)練模型后加載模型出現(xiàn)ValueError: Unknown metric function:fbeta_score
keras自定義評估函數(shù)
有時候訓(xùn)練模型,現(xiàn)有的評估函數(shù)并不足以科學(xué)的評估模型的好壞,這時候就需要自定義一些評估函數(shù),比如樣本分布不均衡是準確率accuracy評估無法判定一個模型的好壞,這時候需要引入精確度和召回率作為評估標準,不幸的是keras沒有這些評估函數(shù)。
以下是參考別的文章摘取的兩個自定義評估函數(shù)
召回率:
def recall(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall = true_positives / (possible_positives + K.epsilon()) return recall
精確度:
def precision(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) return precision
自定義了評估函數(shù),一般在編譯模型階段加入即可:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', precision, recall])
自定義了損失函數(shù)focal_loss一般也在編譯階段加入:
model.compile(optimizer=Adam(lr=0.0001), loss=[focal_loss],
metrics=['accuracy',fbeta_score], )
其他的沒有特別要注意的點,直接按照原來的思路訓(xùn)練一版模型出來就好了,關(guān)鍵的地方在于加載模型這里,自定義的函數(shù)需要特殊的加載方式,不然會出現(xiàn)加載沒有自定義函數(shù)的問題:ValueError: Unknown loss function:focal_loss
解決方案:
model_name = 'test_calssification_model.h6' model_dfcw = load_model(model_name, custom_objects={'focal_loss': focal_loss,'fbeta_score':fbeta_score})
注意點:將自定義的損失函數(shù)和評估函數(shù)都加入到custom_objects里,以上就是在自定義一個損失函數(shù)從編譯模型階段到加載模型階段出現(xiàn)的所有的問題。
看完這篇關(guān)于Keras如何自定義IOU的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。