我就廢話不多說了,大家還是直接看代碼吧!
def iou(y_true, y_pred, label: int): """ Return the Intersection overunion(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 labelsunion= K.sum(y_true) + K.sum(y_pred) - intersection # avoid pide by zero - if theunionis zero, return 1 # otherwise, return the intersection overunionreturn K.switch(K.equal(union, 0), 1.0, intersection / union) def mean_iou(y_true, y_pred): """ Return the Intersection overunion(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) # pide total IoU by number of labels to get mean IoU return mean_iou / num_labels
補充知識:keras 自定義評估函數和損失函數loss訓練模型后加載模型出現ValueError: Unknown metric function:fbeta_score
keras自定義評估函數
有時候訓練模型,現有的評估函數并不足以科學的評估模型的好壞,這時候就需要自定義一些評估函數,比如樣本分布不均衡是準確率accuracy評估無法判定一個模型的好壞,這時候需要引入精確度和召回率作為評估標準,不幸的是keras沒有這些評估函數。
以下是參考別的文章摘取的兩個自定義評估函數
召回率:
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
自定義了評估函數,一般在編譯模型階段加入即可:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', precision, recall])
自定義了損失函數focal_loss一般也在編譯階段加入:
model.compile(optimizer=Adam(lr=0.0001), loss=[focal_loss],
metrics=['accuracy',fbeta_score], )
其他的沒有特別要注意的點,直接按照原來的思路訓練一版模型出來就好了,關鍵的地方在于加載模型這里,自定義的函數需要特殊的加載方式,不然會出現加載沒有自定義函數的問題: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})
注意點:將自定義的損失函數和評估函數都加入到custom_objects里,以上就是在自定義一個損失函數從編譯模型階段到加載模型階段出現的所有的問題。
看完這篇關于Keras如何自定義IOU的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
(資料圖片僅供參考)提起掃描后的文件怎么才能編輯大家在熟悉不過了,被越來越多的人所熟知,那你知道掃描后的文件怎么才能編輯嗎?快和小編一起去了解一下吧!掃描后的文件的編輯方法是可以直接在圖片編輯軟件如PHOTOSHOP中處理并編輯;如果打描的是打印的文字稿,可用OCR軟件識別后用OFFICE軟件處理。用掃描儀掃描后生成的電子圖片。便于攜帶和保存,是各企、事業單位進行材料保。用于某些特殊申請中的資料。...
2022年5月31日,工信部等四部門聯合發布了《關于開展2022新能源汽車下鄉活動的通知》,通知中包含了2022年汽車下鄉補貼車型目錄,以及汽車下鄉活動時間、地區,那么,汽車下鄉補貼車型目錄有哪些?2022年汽車下鄉補貼最新政策是什么?一起來看看吧!汽車下鄉補貼車型目錄有哪些?比亞迪:S2、E1、E2、E3、全新元EV360、元EV535、全新秦EV。五菱:宏光MINIEV、寶駿E100、寶駿E2...
提起電腦桌面快捷方式看不見了怎么辦大家在熟悉不過了,被越來越多的人所熟知,那你知道電腦桌面快捷方式看不見了怎么辦嗎?快和小編一起去了解一下吧!桌面快捷方式找回方法:(相關資料圖)一、回收站里恢復?;謴筒襟E:1、打開回收站,查看是否有丟失的快捷方式。2、若有,選中快捷方式。3、點擊鼠標右鍵,選擇還原即可。二、顯示圖標恢復?;謴筒襟E:1、所有快捷方式都不見了。找回步驟:桌面上右擊鼠標,彈出灰色的列表后...