深度學習之目標檢測篇——殘差網絡與FPN結合

  • 特征金字塔
  • 多尺度融合
  • 特征金字塔的網絡原理
    在這里插入圖片描述
  • 這里是基于resnet網絡與Fpn做的結合,主要把resnet中的特征層利用FPN的思想一起結合,實現resnet_fpn。增強目標檢測backone的有效性。
  • 代碼實現如下:
import torch
from torch import Tensor
from collections import OrderedDict
import torch.nn.functional as F
from torch import nn
from torch.jit.annotations import Tuple, List, Dictclass Bottleneck(nn.Module):expansion = 4def __init__(self, in_channel, out_channel, stride=1, downsample=None, norm_layer=None):super(Bottleneck, self).__init__()if norm_layer is None:norm_layer = nn.BatchNorm2dself.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,kernel_size=(1,1), stride=(1,1), bias=False)  # squeeze channelsself.bn1 = norm_layer(out_channel)# -----------------------------------------self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,kernel_size=(3,3), stride=(stride,stride), bias=False, padding=(1,1))self.bn2 = norm_layer(out_channel)# -----------------------------------------self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel * self.expansion,kernel_size=(1,1), stride=(1,1), bias=False)  # unsqueeze channelsself.bn3 = norm_layer(out_channel * self.expansion)self.relu = nn.ReLU(inplace=True)self.downsample = downsampledef forward(self, x):identity = xif self.downsample is not None:identity = self.downsample(x)out = self.conv1(x)out = self.bn1(out)out = self.relu(out)out = self.conv2(out)out = self.bn2(out)out = self.relu(out)out = self.conv3(out)out = self.bn3(out)out += identityout = self.relu(out)return outclass ResNet(nn.Module):def  __init__(self, block, blocks_num, num_classes=1000, include_top=True, norm_layer=None):''':param block:塊:param blocks_num:塊數:param num_classes: 分類數:param include_top::param norm_layer: BN'''super(ResNet, self).__init__()if norm_layer is None:norm_layer = nn.BatchNorm2dself._norm_layer = norm_layerself.include_top = include_topself.in_channel = 64self.conv1 = nn.Conv2d(in_channels=3, out_channels=self.in_channel, kernel_size=(7,7), stride=(2,2),padding=(3,3), bias=False)self.bn1 = norm_layer(self.in_channel)self.relu = nn.ReLU(inplace=True)self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.layer1 = self._make_layer(block, 64, blocks_num[0])self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)if self.include_top:self.avgpool = nn.AdaptiveAvgPool2d((1, 1))  # output size = (1, 1)self.fc = nn.Linear(512 * block.expansion, num_classes)'''初始化'''for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')def _make_layer(self, block, channel, block_num, stride=1):norm_layer = self._norm_layerdownsample = Noneif stride != 1 or self.in_channel != channel * block.expansion:downsample = nn.Sequential(nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=(1,1), stride=(stride,stride), bias=False),norm_layer(channel * block.expansion))layers = []layers.append(block(self.in_channel, channel, downsample=downsample,stride=stride, norm_layer=norm_layer))self.in_channel = channel * block.expansionfor _ in range(1, block_num):layers.append(block(self.in_channel, channel, norm_layer=norm_layer))return nn.Sequential(*layers)def forward(self, x):x = self.conv1(x)x = self.bn1(x)x = self.relu(x)x = self.maxpool(x)x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)x = self.layer4(x)if self.include_top:x = self.avgpool(x)x = torch.flatten(x, 1)x = self.fc(x)return xclass IntermediateLayerGetter(nn.ModuleDict):"""Module wrapper that returns intermediate layers from a modelsIt has a strong assumption that the modules have been registeredinto the models in the same order as they are used.This means that one should **not** reuse the same nn.Moduletwice in the forward if you want this to work.Additionally, it is only able to query submodules that are directlyassigned to the models. So if `models` is passed, `models.feature1` canbe returned, but not `models.feature1.layer2`.Arguments:model (nn.Module): models on which we will extract the featuresreturn_layers (Dict[name, new_name]): a dict containing the namesof the modules for which the activations will be returned asthe key of the dict, and the value of the dict is the nameof the returned activation (which the user can specify)."""__annotations__ = {"return_layers": Dict[str, str],}def __init__(self, model, return_layers):if not set(return_layers).issubset([name for name, _ in model.named_children()]):raise ValueError("return_layers are not present in models")# {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}orig_return_layers = return_layersreturn_layers = {k: v for k, v in return_layers.items()}layers = OrderedDict()# 遍歷模型子模塊按順序存入有序字典# 只保存layer4及其之前的結構,舍去之后不用的結構for name, module in model.named_children():layers[name] = moduleif name in return_layers:del return_layers[name]if not return_layers:breaksuper(IntermediateLayerGetter, self).__init__(layers)self.return_layers = orig_return_layersdef forward(self, x):out = OrderedDict()# 依次遍歷模型的所有子模塊,并進行正向傳播,# 收集layer1, layer2, layer3, layer4的輸出for name, module in self.named_children():x = module(x)if name in self.return_layers:out_name = self.return_layers[name]out[out_name] = xreturn outclass FeaturePyramidNetwork(nn.Module):"""Module that adds a FPN from on top of a set of feature maps. This is based on`"Feature Pyramid Network for Object Detection" <https://arxiv.org/abs/1612.03144>`_.The feature maps are currently supposed to be in increasing depthorder.The input to the models is expected to be an OrderedDict[Tensor], containingthe feature maps on top of which the FPN will be added.Arguments:in_channels_list (list[int]): number of channels for each feature map thatis passed to the moduleout_channels (int): number of channels of the FPN representationextra_blocks (ExtraFPNBlock or None): if provided, extra operations willbe performed. It is expected to take the fpn features, the originalfeatures and the names of the original features as input, and returnsa new list of feature maps and their corresponding names"""def __init__(self, in_channels_list, out_channels, extra_blocks=None):super(FeaturePyramidNetwork, self).__init__()# 用來調整resnet特征矩陣(layer1,2,3,4)的channel(kernel_size=1)self.inner_blocks = nn.ModuleList()# 對調整后的特征矩陣使用3x3的卷積核來得到對應的預測特征矩陣self.layer_blocks = nn.ModuleList()for in_channels in in_channels_list:if in_channels == 0:continueinner_block_module = nn.Conv2d(in_channels, out_channels, (1,1))layer_block_module = nn.Conv2d(out_channels, out_channels, (3,3), padding=(1,1))self.inner_blocks.append(inner_block_module)self.layer_blocks.append(layer_block_module)# initialize parameters now to avoid modifying the initialization of top_blocksfor m in self.children():if isinstance(m, nn.Conv2d):nn.init.kaiming_uniform_(m.weight, a=1)nn.init.constant_(m.bias, 0)self.extra_blocks = extra_blocksdef get_result_from_inner_blocks(self, x, idx):# type: (Tensor, int) -> Tensor"""This is equivalent to self.inner_blocks[idx](x),but torchscript doesn't support this yet"""num_blocks = len(self.inner_blocks)if idx < 0:idx += num_blocksi = 0out = xfor module in self.inner_blocks:if i == idx:out = module(x)i += 1return outdef get_result_from_layer_blocks(self, x, idx):# type: (Tensor, int) -> Tensor"""This is equivalent to self.layer_blocks[idx](x),but torchscript doesn't support this yet"""num_blocks = len(self.layer_blocks)if idx < 0:idx += num_blocksi = 0out = xfor module in self.layer_blocks:if i == idx:out = module(x)i += 1return outdef forward(self, x):# type: (Dict[str, Tensor]) -> Dict[str, Tensor]"""Computes the FPN for a set of feature maps.Arguments:x (OrderedDict[Tensor]): feature maps for each feature level.Returns:results (OrderedDict[Tensor]): feature maps after FPN layers.They are ordered from highest resolution first."""# unpack OrderedDict into two lists for easier handlingnames = list(x.keys())x = list(x.values())# 將resnet layer4的channel調整到指定的out_channels# last_inner = self.inner_blocks[-1](x[-1])last_inner = self.get_result_from_inner_blocks(x[-1], -1)# result中保存著每個預測特征層results = []# 將layer4調整channel后的特征矩陣,通過3x3卷積后得到對應的預測特征矩陣# results.append(self.layer_blocks[-1](last_inner))results.append(self.get_result_from_layer_blocks(last_inner, -1))# 倒序遍歷resenet輸出特征層,以及對應inner_block和layer_block# layer3 -> layer2 -> layer1 (layer4已經處理過了)# for feature, inner_block, layer_block in zip(#         x[:-1][::-1], self.inner_blocks[:-1][::-1], self.layer_blocks[:-1][::-1]# ):#     if not inner_block:#         continue#     inner_lateral = inner_block(feature)#     feat_shape = inner_lateral.shape[-2:]#     inner_top_down = F.interpolate(last_inner, size=feat_shape, mode="nearest")#     last_inner = inner_lateral + inner_top_down#     results.insert(0, layer_block(last_inner))for idx in range(len(x) - 2, -1, -1):inner_lateral = self.get_result_from_inner_blocks(x[idx], idx)feat_shape = inner_lateral.shape[-2:]inner_top_down = F.interpolate(last_inner, size=feat_shape, mode="nearest")last_inner = inner_lateral + inner_top_downresults.insert(0, self.get_result_from_layer_blocks(last_inner, idx))# 在layer4對應的預測特征層基礎上生成預測特征矩陣5if self.extra_blocks is not None:results, names = self.extra_blocks(results, names)# make it back an OrderedDictout = OrderedDict([(k, v) for k, v in zip(names, results)])return outclass LastLevelMaxPool(torch.nn.Module):"""Applies a max_pool2d on top of the last feature map"""def forward(self, x, names):# type: (List[Tensor], List[str]) -> Tuple[List[Tensor], List[str]]names.append("pool")x.append(F.max_pool2d(x[-1], 1, 2, 0))return x, namesclass BackboneWithFPN(nn.Module):"""Adds a FPN on top of a models.Internally, it uses torchvision.models._utils.IntermediateLayerGetter toextract a submodel that returns the feature maps specified in return_layers.The same limitations of IntermediatLayerGetter apply here.Arguments:backbone (nn.Module)return_layers (Dict[name, new_name]): a dict containing the namesof the modules for which the activations will be returned asthe key of the dict, and the value of the dict is the nameof the returned activation (which the user can specify).in_channels_list (List[int]): number of channels for each feature mapthat is returned, in the order they are present in the OrderedDictout_channels (int): number of channels in the FPN.Attributes:out_channels (int): the number of channels in the FPN"""def __init__(self, backbone, return_layers, in_channels_list, out_channels):''':param backbone: 特征層:param return_layers: 返回的層數:param in_channels_list: 輸入通道數:param out_channels: 輸出通道數'''super(BackboneWithFPN, self).__init__()'返回有序字典模型'self.body = IntermediateLayerGetter(backbone, return_layers=return_layers)self.fpn = FeaturePyramidNetwork(in_channels_list=in_channels_list,out_channels=out_channels,extra_blocks=LastLevelMaxPool(),)# super(BackboneWithFPN, self).__init__(OrderedDict(#     [("body", body), ("fpn", fpn)]))self.out_channels = out_channelsdef forward(self, x):x = self.body(x)x = self.fpn(x)return xdef resnet50_fpn_backbone():# FrozenBatchNorm2d的功能與BatchNorm2d類似,但參數無法更新# norm_layer=misc.FrozenBatchNorm2dresnet_backbone = ResNet(Bottleneck, [3, 4, 6, 3],include_top=False)# freeze layers# 凍結layer1及其之前的所有底層權重(基礎通用特征)for name, parameter in resnet_backbone.named_parameters():if 'layer2' not in name and 'layer3' not in name and 'layer4' not in name:'''凍結權重,不參與訓練'''parameter.requires_grad_(False)# 字典名字return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}# in_channel 為layer4的輸出特征矩陣channel = 2048in_channels_stage2 = resnet_backbone.in_channel // 8in_channels_list = [in_channels_stage2,  # layer1 out_channel=256in_channels_stage2 * 2,  # layer2 out_channel=512in_channels_stage2 * 4,  # layer3 out_channel=1024in_channels_stage2 * 8,  # layer4 out_channel=2048]out_channels = 256return BackboneWithFPN(resnet_backbone, return_layers, in_channels_list, out_channels)if __name__ == '__main__':net = resnet50_fpn_backbone()x = torch.randn(1,3,224,224)for key,value in net(x).items():print(key,value.shape)
  • 測試結果
    在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/63489.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/63489.shtml
英文地址,請注明出處:http://en.pswp.cn/web/63489.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

游戲AI實現-尋路算法(BFS)

廣度優先搜索算法&#xff08;英語&#xff1a;Breadth-first search&#xff0c;縮寫&#xff1a;BFS&#xff09;&#xff0c;又譯作寬度優先搜索&#xff0c;或橫向優先搜索&#xff0c;是一種圖形搜索算法。 尋路地圖搭建&#xff1a; 游戲AI實現-尋路地圖搭建-CSDN博客 …

CMake的INSTALL FILES和INSTALL DIRECTORY有什么區別

在 CMake 中&#xff0c;install() 命令用于安裝構建的目標文件、頭文件、庫等到指定的目標路徑。install(FILES ...) 和 install(DIRECTORY ...) 都是 install() 命令的具體用法&#xff0c;它們的功能和適用場景不同。 以下是兩者的詳細區別和用法說明&#xff1a; 1. insta…

主流網絡安全產品

目前市場上也出現了品類豐富的安全產品&#xff0c;如“防火墻、抗D、負載均衡、WAF、數據庫審計、漏掃、網頁防篡改、上網行為管理、堡壘機等”這些產品由于功能不同在網絡中部署的位置也有區別。下面來簡單聊一下每類產品的功能和部署位置。 &#xff08;1&#xff09;防火墻…

利用git上傳項目到GitHub

GitHub是基于git實現的代碼托管。git是目前最好用的版本控制系統了&#xff0c;非常受歡迎&#xff0c;比之svn更好。 GitHub可以免費使用&#xff0c;并且快速穩定。 利用GitHub&#xff0c;你可以將項目存檔&#xff0c;與其他人分享交流&#xff0c;并讓其他開發者幫助你一…

《Vue3實戰教程》13:Vue3偵聽器

如果您有疑問&#xff0c;請觀看視頻教程《Vue3實戰教程》 偵聽器? 基本示例? 計算屬性允許我們聲明性地計算衍生值。然而在有些情況下&#xff0c;我們需要在狀態變化時執行一些“副作用”&#xff1a;例如更改 DOM&#xff0c;或是根據異步操作的結果去修改另一處的狀態。…

uboot 打開log 的 方法

uboot 版本 commit f919c3a889f0ec7d63a48b5d0ed064386b0980bd (HEAD -> v2024.10, tag: v2024.10) Author: Tom Rini <trinikonsulko.com> Date: Mon Oct 7 08:54:35 2024 -0600 Prepare v2024.10 Signed-off-by: Tom Rini <trinikonsulko.com> 開啟的選項…

VSCode 搭建Python編程環境 2024新版圖文安裝教程(Python環境搭建+VSCode安裝+運行測試+背景圖設置)

名人說&#xff1a;一點浩然氣&#xff0c;千里快哉風。—— 蘇軾《水調歌頭》 創作者&#xff1a;Code_流蘇(CSDN) 目錄 一、Python環境安裝二、VScode下載及安裝三、VSCode配置Python環境四、運行測試五、背景圖設置 很高興你打開了這篇博客&#xff0c;更多詳細的安裝教程&…

Unity常用面試問題

GC針對的誰 new對象的時候&#xff0c;產生新對象 GC是發生在什么時候 主動調collect接口以及內存分配不足的時候 如何避免gc 別new對象 GC的過程&#xff0c;為什么耗時 每一次GC會經歷以下過程&#xff0c;堆上的對象越多&#xff0c;對象的引用越多&#xff0c;意味著…

在Linux上將 `.sh` 腳本、`.jar` 包或其他腳本文件添加到開機自啟動

在Linux上將 .sh 腳本、.jar 包或其他腳本文件添加到開機自啟動 在Linux環境中&#xff0c;有時需要將一些程序、腳本或應用程序設置為開機時自動啟動。這對于那些需要在系統啟動時啟動的服務或應用非常有用。本文將介紹如何將 .sh 腳本、.jar 包或其他腳本文件添加到Linux系統…

Git使用步驟

Git 是一個分布式版本控制系統&#xff0c;廣泛用于軟件開發和其他需要跟蹤文件變更的項目。以下是 Git 的基本使用方法和一些常用命令的詳細說明。 安裝 Git 在大多數操作系統上&#xff0c;你可以通過包管理器安裝 Git&#xff1a; Windows: 下載并安裝 Git for Windows。…

詳細指南:在Ubuntu 20.04上安裝和配置Orbbec SDK及USB設備權限

詳細指南&#xff1a;在Ubuntu 20.04上安裝和配置Orbbec SDK及USB設備權限 在Ubuntu 20.04上安裝和配置Orbbec SDK以及進行USB設備的權限配置和調整USBFS緩存大小&#xff0c;涉及到一系列系統配置和環境準備步驟。以下是詳細的步驟說明&#xff0c;以確保準確和高效地設置開發…

【GCC】2015: draft-alvestrand-rmcat-congestion-03 機器翻譯

騰訊云的一個分析,明顯是看了這個論文和草案的 : 最新的是應該是這個 A Google Congestion Control Algorithm for Real-Time Communication draft-ietf-rmcat-gcc-02 下面的這個應該過期了: draft-alvestrand-rmcat-congestion-03

計算機網絡技術基礎:5.數據通信系統

一、數據通信的基本概念 1.信息 信息是對客觀事物的運動狀態和存在形式的反映&#xff0c;可以是客觀事實的形態、大小、結構、性能等描述&#xff0c;也可以是客觀事物與外部之間的聯系。信息的載體可以是數字、文字、語音、圖形和圖像等。計算機及其外圍設備產生和交換的信息…

STM32中ADC模數轉換器

一、ADC簡介 ADC模擬-數字轉換器 ADC可以將引腳連續變化的模擬電壓轉換為內存中存儲的數字變量&#xff0c;建立模擬電路到數字電路的橋梁 12位逐次逼近型ADC&#xff0c;1us轉換時間 輸入電壓范圍&#xff1a; 0~3.3V&#xff0c;轉換結果范圍&#xff1a;0~4095 18個輸入…

醫療領域的網絡安全預防:保障患者隱私與醫療數據安全

醫療領域的網絡安全預防&#xff1a;保障患者隱私與醫療數據安全 隨著信息技術的不斷發展和醫療行業的數字化轉型&#xff0c;網絡安全在醫療領域變得愈加重要。醫療行業處理著大量的敏感數據&#xff0c;包括患者的個人信息、醫療記錄、診療方案等&#xff0c;這些數據一旦被…

【數字圖像處理】期末綜合知識點總結 ver1,灰度圖像,圖像增強,平滑濾波,銳化濾波,圖像復原,圖像壓縮

關注作者了解更多 我的其他CSDN專欄 過程控制系統 工程測試技術 虛擬儀器技術 可編程控制器 工業現場總線 數字圖像處理 智能控制 傳感器技術 嵌入式系統 復變函數與積分變換 單片機原理 線性代數 大學物理 熱工與工程流體力學 數字信號處理 光電融合集成電路…

.NET 技術 | 調用系統API創建Windows服務

01閱讀須知 此文所提供的信息只為網絡安全人員對自己所負責的網站、服務器等&#xff08;包括但不限于&#xff09;進行檢測或維護參考&#xff0c;未經授權請勿利用文章中的技術資料對任何計算機系統進行入侵操作。利用此文所提供的信息而造成的直接或間接后果和損失&#xf…

【Qt】QWidget中的常見屬性及其功能(二)

目錄 六、windowOpacity 例子&#xff1a; 七、cursor 例子&#xff1a; 八、font 九、toolTip 例子&#xff1a; 十、focusPolicy 例子&#xff1a; 十一、styleSheet 計算機中的顏色表示 例子&#xff1a; 六、windowOpacity opacity是不透明度的意思。 用于設…

Elasticsearch02-安裝7.x

零、文章目錄 Elasticsearch02-安裝7.x 1、Windows安裝Elasticsearch &#xff08;1&#xff09;JDK安裝 Elasticsearch是基于java開發的&#xff0c;所以需要安裝JDK。我們安裝的Elasticsearch版本是7.15&#xff0c;對應JDK至少1.8版本以上。也可以不安裝jdk&#xff0c;…

php學習資料分享

php學習資料分享&#xff1a;夸克網盤分享