亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2

瀏覽:2日期:2022-06-20 14:57:37
一、model.py1.1 Channel Shuffle

Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2

def channel_shuffle(x: Tensor, groups: int) -> Tensor: batch_size, num_channels, height, width = x.size() channels_per_group = num_channels // groups # reshape # [batch_size, num_channels, height, width] -> [batch_size, groups, channels_per_group, height, width] x = x.view(batch_size, groups, channels_per_group, height, width) x = torch.transpose(x, 1, 2).contiguous() # flatten x = x.view(batch_size, -1, height, width) return x1.2 block

Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2

class InvertedResidual(nn.Module): def __init__(self, input_c: int, output_c: int, stride: int):super(InvertedResidual, self).__init__()if stride not in [1, 2]: raise ValueError('illegal stride value.')self.stride = strideassert output_c % 2 == 0branch_features = output_c // 2# 當(dāng)stride為1時(shí),input_channel應(yīng)該是branch_features的兩倍# python中 ’<<’ 是位運(yùn)算,可理解為計(jì)算×2的快速方法assert (self.stride != 1) or (input_c == branch_features << 1)if self.stride == 2: self.branch1 = nn.Sequential(self.depthwise_conv(input_c, input_c, kernel_s=3, stride=self.stride, padding=1),nn.BatchNorm2d(input_c),nn.Conv2d(input_c, branch_features, kernel_size=1, stride=1, padding=0, bias=False),nn.BatchNorm2d(branch_features),nn.ReLU(inplace=True) )else: self.branch1 = nn.Sequential()self.branch2 = nn.Sequential( nn.Conv2d(input_c if self.stride > 1 else branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False), nn.BatchNorm2d(branch_features), nn.ReLU(inplace=True), self.depthwise_conv(branch_features, branch_features, kernel_s=3, stride=self.stride, padding=1), nn.BatchNorm2d(branch_features), nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False), nn.BatchNorm2d(branch_features), nn.ReLU(inplace=True)) @staticmethod def depthwise_conv(input_c: int, output_c: int, kernel_s: int, stride: int = 1, padding: int = 0, bias: bool = False) -> nn.Conv2d:return nn.Conv2d(in_channels=input_c, out_channels=output_c, kernel_size=kernel_s, stride=stride, padding=padding, bias=bias, groups=input_c) def forward(self, x: Tensor) -> Tensor:if self.stride == 1: x1, x2 = x.chunk(2, dim=1) out = torch.cat((x1, self.branch2(x2)), dim=1)else: out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)out = channel_shuffle(out, 2)return out1.3 shufflenet v2

Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2

class ShuffleNetV2(nn.Module): def __init__(self, stages_repeats: List[int], stages_out_channels: List[int], num_classes: int = 1000, inverted_residual: Callable[..., nn.Module] = InvertedResidual):super(ShuffleNetV2, self).__init__()if len(stages_repeats) != 3: raise ValueError('expected stages_repeats as list of 3 positive ints')if len(stages_out_channels) != 5: raise ValueError('expected stages_out_channels as list of 5 positive ints')self._stage_out_channels = stages_out_channels# input RGB imageinput_channels = 3output_channels = self._stage_out_channels[0]self.conv1 = nn.Sequential( nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(output_channels), nn.ReLU(inplace=True))input_channels = output_channelsself.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)# Static annotations for mypyself.stage2: nn.Sequentialself.stage3: nn.Sequentialself.stage4: nn.Sequentialstage_names = ['stage{}'.format(i) for i in [2, 3, 4]]for name, repeats, output_channels in zip(stage_names, stages_repeats, self._stage_out_channels[1:]): seq = [inverted_residual(input_channels, output_channels, 2)] for i in range(repeats - 1):seq.append(inverted_residual(output_channels, output_channels, 1)) setattr(self, name, nn.Sequential(*seq)) input_channels = output_channelsoutput_channels = self._stage_out_channels[-1]self.conv5 = nn.Sequential( nn.Conv2d(input_channels, output_channels, kernel_size=1, stride=1, padding=0, bias=False), nn.BatchNorm2d(output_channels), nn.ReLU(inplace=True))self.fc = nn.Linear(output_channels, num_classes) def _forward_impl(self, x: Tensor) -> Tensor:# See note [TorchScript super()]x = self.conv1(x)x = self.maxpool(x)x = self.stage2(x)x = self.stage3(x)x = self.stage4(x)x = self.conv5(x)x = x.mean([2, 3]) # global poolx = self.fc(x)return x def forward(self, x: Tensor) -> Tensor:return self._forward_impl(x)二、train.py

Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2

到此這篇關(guān)于Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2的文章就介紹到這了,更多相關(guān)Python用Pytorch搭建ShuffleNetv2內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久精品毛片 | 欧美大尺度交性视频 | 婷婷六月综合 | www.亚洲综合 | 综合久久久久综合 | 久久免费精彩视频 | 性欧美高清come | 丝袜美女被出水视频一区 | 1024免费观看 | 网站在线观看 | 草逼免费看 | 91香蕉视频破解版 | 伊人精品综合 | 91久久亚洲最新一本 | 中文字幕日本亚洲欧美不卡 | 尤物视频在线看 | 日韩欧美黄色大片 | 1024成线在人免费视频 | 爱操成人网 | 停停五月天 | 自拍偷拍视频网站 | 精品久久久久久国产 | 亚洲日本色图 | 欧美中文字幕一区二区三区 | www.黄色片| 农村寡妇一级毛片免费播放 | 国模午夜写真福利视频在线 | 国产精品无码永久免费888 | 爱爱免费网站 | 亚洲精品区在线播放一区二区 | 一区二区三区福利 | a级毛片在线免费看 | 欧美激情精品久久久久久大尺度 | 欧美午夜理伦三级在线观看 | 在线欧美69v免费观看视频 | 日韩亚洲欧美在线爱色 | 亚洲综合在线播放 | 成人免费大片a毛片 | 黄色网址免费大全 | 日本黄色性生活片 | 国产精品视频第一区二区 |