最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Keras自定義IOU方式

 更新時(shí)間:2020年06月10日 14:51:32   作者:Nick Blog  
這篇文章主要介紹了Keras自定義IOU方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧!

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

補(bǔ)充知識:keras 自定義評估函數(shù)和損失函數(shù)loss訓(xùn)練模型后加載模型出現(xiàn)ValueError: Unknown metric function:fbeta_score

keras自定義評估函數(shù)

有時(shí)候訓(xùn)練模型,現(xiàn)有的評估函數(shù)并不足以科學(xué)的評估模型的好壞,這時(shí)候就需要自定義一些評估函數(shù),比如樣本分布不均衡是準(zhǔn)確率accuracy評估無法判定一個(gè)模型的好壞,這時(shí)候需要引入精確度和召回率作為評估標(biāo)準(zhǔn),不幸的是keras沒有這些評估函數(shù)。

以下是參考別的文章摘取的兩個(gè)自定義評估函數(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], )

其他的沒有特別要注意的點(diǎn),直接按照原來的思路訓(xùn)練一版模型出來就好了,關(guān)鍵的地方在于加載模型這里,自定義的函數(shù)需要特殊的加載方式,不然會出現(xiàn)加載沒有自定義函數(shù)的問題:ValueError: Unknown loss function:focal_loss

解決方案:

model_name = 'test_calssification_model.h5'
model_dfcw = load_model(model_name,
            custom_objects={'focal_loss': focal_loss,'fbeta_score':fbeta_score})

注意點(diǎn):將自定義的損失函數(shù)和評估函數(shù)都加入到custom_objects里,以上就是在自定義一個(gè)損失函數(shù)從編譯模型階段到加載模型階段出現(xiàn)的所有的問題。

以上這篇Keras自定義IOU方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中的map、reduce和filter淺析

    Python中的map、reduce和filter淺析

    這篇文章主要介紹了Python中的map、reduce和filter,用實(shí)例來理解這3個(gè)函數(shù),需要的朋友可以參考下
    2014-04-04
  • Pyhton自動化測試持續(xù)集成和Jenkins

    Pyhton自動化測試持續(xù)集成和Jenkins

    這篇文章介紹了Pyhton自動化測試持續(xù)集成和Jenkins,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Python??序列化反序列化和異常處理的問題小結(jié)

    Python??序列化反序列化和異常處理的問題小結(jié)

    這篇文章主要介紹了Python?序列化反序列化和異常處理,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • opencv轉(zhuǎn)換顏色空間更改圖片背景

    opencv轉(zhuǎn)換顏色空間更改圖片背景

    這篇文章主要為大家詳細(xì)介紹了opencv轉(zhuǎn)換顏色空間更改圖片背景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別

    詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別

    這篇文章主要介紹了詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)

    Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)

    本文主要介紹了Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 深入了解Python枚舉類型的相關(guān)知識

    深入了解Python枚舉類型的相關(guān)知識

    這篇文章主要介紹了深入了解Python枚舉類型的相關(guān)知識,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python psutil 模塊概述及使用示例

    python psutil 模塊概述及使用示例

    psutil是一個(gè)跨平臺的Python庫,用于系統(tǒng)監(jiān)控、性能分析和進(jìn)程管理,它提供了豐富的API,可用于獲取系統(tǒng)的CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等資源的使用情況,以及進(jìn)行進(jìn)程管理,psutil支持Linux、Windows、macOS等主流操作系統(tǒng)
    2024-11-11
  • 解決python中導(dǎo)入win32com.client出錯(cuò)的問題

    解決python中導(dǎo)入win32com.client出錯(cuò)的問題

    今天小編就為大家分享一篇解決python中導(dǎo)入win32com.client出錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Django框架中render_to_response()函數(shù)的使用方法

    Django框架中render_to_response()函數(shù)的使用方法

    這篇文章主要介紹了Django框架中render_to_response()函數(shù)的使用方法,注意范例中該方法的參數(shù)的使用,需要的朋友可以參考下
    2015-07-07

最新評論

英吉沙县| 介休市| 高唐县| 长汀县| 皋兰县| 玛沁县| 毕节市| 孝昌县| 高青县| 珲春市| 安塞县| 尚志市| 阳信县| 郴州市| 满城县| 铜鼓县| 织金县| 芦山县| 拉萨市| 新巴尔虎右旗| 华亭县| 鄂托克旗| 临武县| 嫩江县| 泰宁县| 商水县| 元阳县| 柳州市| 兴安县| 隆尧县| 天祝| 奎屯市| 棋牌| 巴楚县| 尉犁县| 开封市| 元氏县| 和平县| 五河县| 内丘县| 顺昌县|