洞察纵观鸿蒙next版本,如何凭借FinClip加强小程序的跨平台管理,确保企业在数字化转型中的高效运营和数据安全?
895
2022-08-31
PyTorch实现的DenseNet
Densely Connected Convolutional Networks PDF: torchimport torch.nn as nnimport torchvisionprint("PyTorch Version: ",torch.__version__)print("Torchvision Version: ",torchvision.__version__)__all__ = ['DenseNet121', 'DenseNet169','DenseNet201','DenseNet264']def Conv1(in_planes, places, stride=2): return nn.Sequential( nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False), nn.BatchNorm2d(places), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2, padding=1) )class _TransitionLayer(nn.Module): def __init__(self, inplace, plance): super(_TransitionLayer, self).__init__() self.transition_layer = nn.Sequential( nn.BatchNorm2d(inplace), nn.ReLU(inplace=True), nn.Conv2d(in_channels=inplace,out_channels=plance,kernel_size=1,stride=1,padding=0,bias=False), nn.AvgPool2d(kernel_size=2,stride=2), ) def forward(self, x): return self.transition_layer(x)class _DenseLayer(nn.Module): def __init__(self, inplace, growth_rate, bn_size, drop_rate=0): super(_DenseLayer, self).__init__() self.drop_rate = drop_rate self.dense_layer = nn.Sequential( nn.BatchNorm2d(inplace), nn.ReLU(inplace=True), nn.Conv2d(in_channels=inplace, out_channels=bn_size * growth_rate, kernel_size=1, stride=1, padding=0, bias=False), nn.BatchNorm2d(bn_size * growth_rate), nn.ReLU(inplace=True), nn.Conv2d(in_channels=bn_size * growth_rate, out_channels=growth_rate, kernel_size=3, stride=1, padding=1, bias=False), ) self.dropout = nn.Dropout(p=self.drop_rate) def forward(self, x): y = self.dense_layer(x) if self.drop_rate > 0: y = self.dropout(y) return torch.cat([x, y], 1)class DenseBlock(nn.Module): def __init__(self, num_layers, inplances, growth_rate, bn_size , drop_rate=0): super(DenseBlock, self).__init__() layers = [] for i in range(num_layers): layers.append(_DenseLayer(inplances + i * growth_rate, growth_rate, bn_size, drop_rate)) self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x)class DenseNet(nn.Module): def __init__(self, init_channels=64, growth_rate=32, blocks=[6, 12, 24, 16],num_classes=1000): super(DenseNet, self).__init__() bn_size = 4 drop_rate = 0 self.conv1 = Conv1(in_planes=3, places=init_channels) blocks*4 num_features = init_channels self.layer1 = DenseBlock(num_layers=blocks[0], inplances=num_features, growth_rate=growth_rate, bn_size=bn_size, drop_rate=drop_rate) num_features = num_features + blocks[0] * growth_rate self.transition1 = _TransitionLayer(inplace=num_features, plance=num_features // 2) num_features = num_features // 2 self.layer2 = DenseBlock(num_layers=blocks[1], inplances=num_features, growth_rate=growth_rate, bn_size=bn_size, drop_rate=drop_rate) num_features = num_features + blocks[1] * growth_rate self.transition2 = _TransitionLayer(inplace=num_features, plance=num_features // 2) num_features = num_features // 2 self.layer3 = DenseBlock(num_layers=blocks[2], inplances=num_features, growth_rate=growth_rate, bn_size=bn_size, drop_rate=drop_rate) num_features = num_features + blocks[2] * growth_rate self.transition3 = _TransitionLayer(inplace=num_features, plance=num_features // 2) num_features = num_features // 2 self.layer4 = DenseBlock(num_layers=blocks[3], inplances=num_features, growth_rate=growth_rate, bn_size=bn_size, drop_rate=drop_rate) num_features = num_features + blocks[3] * growth_rate self.avgpool = nn.AvgPool2d(7, stride=1) self.fc = nn.Linear(num_features, num_classes) def forward(self, x): x = self.conv1(x) x = self.layer1(x) x = self.transition1(x) x = self.layer2(x) x = self.transition2(x) x = self.layer3(x) x = self.transition3(x) x = self.layer4(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.fc(x) return xdef DenseNet121(): return DenseNet(init_channels=64, growth_rate=32, blocks=[6, 12, 24, 16])def DenseNet169(): return DenseNet(init_channels=64, growth_rate=32, blocks=[6, 12, 32, 32])def DenseNet201(): return DenseNet(init_channels=64, growth_rate=32, blocks=[6, 12, 48, 32])def DenseNet264(): return DenseNet(init_channels=64, growth_rate=32, blocks=[6, 12, 64, 48])if __name__=='__main__': # model = torchvision.models.densenet121() model = DenseNet121() print(model) input = torch.randn(1, 3, 224, 224) out = model(input) print(out.shape)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~