日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 目标检测 >内容正文

目标检测

keras优化算法_目标检测算法 - CenterNet - 代码分析

發布時間:2024/9/27 目标检测 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 keras优化算法_目标检测算法 - CenterNet - 代码分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼出處

吃水不忘打井人,分析github上的基于keras的實現:

xuannianz/keras-CenterNet?github.com代碼主體結構

模型訓練的主函數流程如下所示,該流程也是使用keras的較為標準的流程。其中代碼篇幅較大的是數據準備的部分,通常的代碼也亦如此。下面按照不同的部分分別進行說明。

create_generators 數據集準備

該代碼支持Pascal VOC格式、COCO格式以及CSV格式。keras中有三個函數可以用來進行模型的訓練:分別是fit,fit_generator和train_on_batch。

fit(train_x, train_y, batchsize, epochs)

在使用fit進行模型訓練時,通常假設整個訓練集都可以放入RAM,并且沒有數據增強(即不需要keras生成器)。常用于簡單小型的數據集訓練。

fit_generator;常常使用的模型訓練函數

fit_generator適用于大數據集無法直接全部放入內存中,以及標注數據較少需要使用數據增強來增加訓練模型的泛化能力。fit_generator需要傳入一個數據生成器,數據生成器可以每次動態的生成一個batchsize的訓練數據,通常我們也將數據增強放入數據生成器中,這樣便可以動態的生成增強后的數據。在使用fit_generator時,需要傳入steps_per_epoch的值,而fit函數則不需要,這是因為fit函數的steps_per_epoch默認等于總的訓練數據/batchsize,而對于fit_generator來說,如果采用了數據增強,則可以產生無限的batchsize訓練數據,因此需要指定該參數。

By the way,數據生成器可以使用keras的API或者直接自己手碼python的代碼,因為其本質上也就是python的函數。

train_on_batch(batchX, batchY)

train_on_batch用于需要對訓練迭代進行精細控制,給其傳入一批數據即可(數據大小任意),不需要提供batchsize的大小。通常很少使用該函數進行模型訓練。

  • 本算法的實現過程就是采用的fit_generator進行的模型訓練。因此需要為其構建數據生成器。common.py文件:class Generator(keras.utils.Sequence)構建數據生成器的基類,咱們先說道說道keras.utils.Sequence這個類。
keras.utils.Sequence:這個基類通常應用于數據集生成一個數據序列。使用時需構建一個python類繼承自該 基類,并必須實現__len__和__getitem__兩個函數,如果要在每個epoch間修改數據集則需要實現on_epoch_end 方法。 NOTE:特別注意,__getitem__要返回一個完整的batchsize數據,__len__統計的也是有多少個batch

Generator類可以當成一個抽象基類,其中主要實現的是batch的劃分、數據增強的處理、以及標注數據的轉換(將bounding box的標注形式轉換成高斯分布的標注)。而真正使用的數據集的生成器如下所示。主要按照不同的數據集生成的類,并均都繼承于Generator抽象類,這里區分不同的數據集主要為了能方便區分其不同的數據標注格式,使用起來更為方便。主要是load_annotations()和load_image()函數的實現。至此數據生成器便構建完成了。

class PascalVocGenerator(Generator) class CocoGenerator(Generator)centernet網絡構建

算法實現采用的Resnet50作為網絡的backbone,采用下述引用網絡。網絡構建這里相對就比較簡單了,取出Resnet的C5,先添加了一層dropout,然后進行了上采樣,然后分別構建網絡head,主要有三支:中心點預測、中心點偏移值預測以及bouding box的size預測。

from keras.applications.resnet50 import ResNet50

最后構建model,使用keras的Lambda層構建loss,作為model的output

loss_ = Lambda(loss, name='centernet_loss')([y1, y2, y3, hm_input, wh_input, reg_input, reg_mask_input, index_input]) model = Model(inputs=[image_input, hm_input, wh_input, reg_input, reg_mask_input, index_input], outputs=[loss_])預訓練模型權重加載

keras的模型加載可以使用load_weights來實現,其模型加載可以按照模型結構加載,此時by_name需設置為False。否則將按照網絡層的名字來加載,此時通常將skip_mismatch也設置成True,即僅加載名字相同的層,其他名字不同的層直接跳過。因此可以利用這個特性,對已訓練好的網絡局部進行修改,然后再加載之前訓練好的模型,方便進行模型的調優。

model.load_weights(args.snapshot, by_name=True, skip_mismatch=True)模型配置

其中loss參數的傳遞有幾種形式。

  • 目標函數/損失函數的字符串,比如keras內置的一些損失函數
  • 目標函數/損失函數,通常為自定義的損失函數
  • 將目標函數/損失函數定義成model的一個層,類似本代碼的實現。本代碼實現時,因為直接把loss作為model的輸出,因此輸入y_true和y_pred,實際使用y_pred即輸出loss,對其進行優化。
model.compile(optimizer=Adam(lr=1e-3), loss={'centernet_loss': lambda y_true, y_pred: y_pred})def compile(self, optimizer,loss=None,metrics=None,loss_weights=None,sample_weight_mode=None,weighted_metrics=None,target_tensors=None,**kwargs):"""Configures the model for training.# Argumentsoptimizer: String (name of optimizer) or optimizer instance.See [optimizers](/optimizers).loss: String (name of objective function) or objective function.See [losses](/losses).If the model has multiple outputs, you can use a different losson each output by passing a dictionary or a list of losses.The loss value that will be minimized by the modelwill then be the sum of all individual losses.metrics: List of metrics to be evaluated by the modelduring training and testing.Typically you will use `metrics=['accuracy']`.To specify different metrics for different outputs of amulti-output model, you could also pass a dictionary,such as `metrics={'output_a': 'accuracy'}`.loss_weights: Optional list or dictionary specifying scalarcoefficients (Python floats) to weight the loss contributionsof different model outputs.The loss value that will be minimized by the modelwill then be the *weighted sum* of all individual losses,weighted by the `loss_weights` coefficients.If a list, it is expected to have a 1:1 mappingto the model's outputs. If a dict, it is expected to mapoutput names (strings) to scalar coefficients.sample_weight_mode: If you need to do timestep-wisesample weighting (2D weights), set this to `"temporal"`.`None` defaults to sample-wise weights (1D).If the model has multiple outputs, you can use a different`sample_weight_mode` on each output by passing adictionary or a list of modes.weighted_metrics: List of metrics to be evaluated and weightedby sample_weight or class_weight during training and testing.target_tensors: By default, Keras will create placeholders for themodel's target, which will be fed with the target data duringtraining. If instead you would like to use your owntarget tensors (in turn, Keras will not expect externalNumpy data for these targets at training time), youcan specify them via the `target_tensors` argument. It can bea single tensor (for a single-output model), a list of tensors,or a dict mapping output names to target tensors.**kwargs: When using the Theano/CNTK backends, these argumentsare passed into `K.function`.When using the TensorFlow backend,these arguments are passed into `tf.Session.run`.

總結

以上是生活随笔為你收集整理的keras优化算法_目标检测算法 - CenterNet - 代码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。