本文整理汇总了Python中torchvision.models.resnet.BasicBlock方法的典型用法代码示例。如果您正苦于以下问题:Python resnet.BasicBlock方法的具体用法?Python resnet.BasicBlock怎么用?Python resnet.BasicBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models.resnet
的用法示例。
在下文中一共展示了resnet.BasicBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def __init__(self, features):
super().__init__()
self.stride = 128
self.features = features
if isinstance(features, ResNet):
is_light = features.bottleneck == vrn.BasicBlock
channels = [128, 256, 512] if is_light else [512, 1024, 2048]
elif isinstance(features, MobileNet):
channels = [32, 96, 320]
self.lateral3 = nn.Conv2d(channels[0], 256, 1)
self.lateral4 = nn.Conv2d(channels[1], 256, 1)
self.lateral5 = nn.Conv2d(channels[2], 256, 1)
self.pyramid6 = nn.Conv2d(channels[2], 256, 3, stride=2, padding=1)
self.pyramid7 = nn.Conv2d(256, 256, 3, stride=2, padding=1)
self.smooth3 = nn.Conv2d(256, 256, 3, padding=1)
self.smooth4 = nn.Conv2d(256, 256, 3, padding=1)
self.smooth5 = nn.Conv2d(256, 256, 3, padding=1)
示例2: resnet18_ids
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def resnet18_ids(num_attributes, ids_embedding_size, pretrained=False, **kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
classifier = ResNetClassifier(BasicBlock, num_classes=num_attributes, **kwargs)
classifier_ids = ResNetClassifier(BasicBlock, num_classes=ids_embedding_size, **kwargs)
if pretrained:
state_dict = model_zoo.load_url(model_urls['resnet18'])
model.load_state_dict(
{k: v for k, v in state_dict.items() if k in model.state_dict()}
)
return model, classifier, classifier_ids
示例3: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def __init__(self, *args, **kw):
self.inplanes = -1
super(ResNetRPN, self).__init__(*args, **kw)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(
m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# if zero_init_residual:
for m in self.modules():
if isinstance(m, resnet.Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, resnet.BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
示例4: _make_layer
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def _make_layer(self, inplanes, planes, num_blocks, stride=1):
if self.inplanes == -1:
self.inplanes = self._num_input_features
block = resnet.BasicBlock
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(1, num_blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers), self.inplanes
示例5: resnet18_vggmconv1
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def resnet18_vggmconv1(output_layers=None, path=None, **kwargs):
"""Constructs a ResNet-18 model with first-layer VGGm features.
"""
if output_layers is None:
output_layers = ['default']
else:
for l in output_layers:
if l not in ['vggconv1', 'conv1', 'layer1', 'layer2', 'layer3', 'layer4', 'fc']:
raise ValueError('Unknown layer: {}'.format(l))
model = ResNetVGGm1(BasicBlock, [2, 2, 2, 2], output_layers, **kwargs)
if path is not None:
model.load_state_dict(torch.load(path), strict=False)
return model
示例6: residual_basic_block
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def residual_basic_block(feature_dim=256, num_blocks=1, l2norm=True, final_conv=False, norm_scale=1.0, out_dim=None,
interp_cat=False, final_relu=False, init_pool=False):
"""Construct a network block based on the BasicBlock used in ResNet 18 and 34."""
if out_dim is None:
out_dim = feature_dim
feat_layers = []
if interp_cat:
feat_layers.append(InterpCat())
if init_pool:
feat_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
for i in range(num_blocks):
odim = feature_dim if i < num_blocks - 1 + int(final_conv) else out_dim
feat_layers.append(BasicBlock(feature_dim, odim))
if final_conv:
feat_layers.append(nn.Conv2d(feature_dim, out_dim, kernel_size=3, padding=1, bias=False))
if final_relu:
feat_layers.append(nn.ReLU(inplace=True))
if l2norm:
feat_layers.append(InstanceL2Norm(scale=norm_scale))
return nn.Sequential(*feat_layers)
示例7: residual_basic_block_pool
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def residual_basic_block_pool(feature_dim=256, num_blocks=1, l2norm=True, final_conv=False, norm_scale=1.0, out_dim=None,
pool=True):
"""Construct a network block based on the BasicBlock used in ResNet."""
if out_dim is None:
out_dim = feature_dim
feat_layers = []
for i in range(num_blocks):
odim = feature_dim if i < num_blocks - 1 + int(final_conv) else out_dim
feat_layers.append(BasicBlock(feature_dim, odim))
if final_conv:
feat_layers.append(nn.Conv2d(feature_dim, out_dim, kernel_size=3, padding=1, bias=False))
if pool:
feat_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
if l2norm:
feat_layers.append(InstanceL2Norm(scale=norm_scale))
return nn.Sequential(*feat_layers)
示例8: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def __init__(self):
super().__init__(BasicBlock, [3, 4, 6, 3], 1000)
示例9: resnet18
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def resnet18(**kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
return model
示例10: ResNet18FPN
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def ResNet18FPN():
return FPN(ResNet(layers=[2, 2, 2, 2], bottleneck=vrn.BasicBlock, outputs=[3, 4, 5], url=vrn.model_urls['resnet18']))
示例11: ResNet34FPN
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def ResNet34FPN():
return FPN(ResNet(layers=[3, 4, 6, 3], bottleneck=vrn.BasicBlock, outputs=[3, 4, 5], url=vrn.model_urls['resnet34']))
示例12: ResNet18C4
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def ResNet18C4():
return ResNet(layers=[2, 2, 2, 2], bottleneck=vrn.BasicBlock, outputs=[4], url=vrn.model_urls['resnet18'])
示例13: ResNet34C4
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def ResNet34C4():
return ResNet(layers=[3, 4, 6, 3], bottleneck=vrn.BasicBlock, outputs=[4], url=vrn.model_urls['resnet34'])
示例14: resnet18
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def resnet18(pretrained=True, **kwargs):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']), strict=False)
return model
示例15: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import BasicBlock [as 别名]
def __init__(self, resnet_type):
resnet_spec = {18: (BasicBlock, [2, 2, 2, 2], [64, 64, 128, 256, 512], 'resnet18'),
34: (BasicBlock, [3, 4, 6, 3], [64, 64, 128, 256, 512], 'resnet34'),
50: (Bottleneck, [3, 4, 6, 3], [64, 256, 512, 1024, 2048], 'resnet50'),
101: (Bottleneck, [3, 4, 23, 3], [64, 256, 512, 1024, 2048], 'resnet101'),
152: (Bottleneck, [3, 8, 36, 3], [64, 256, 512, 1024, 2048], 'resnet152')}
block, layers, channels, name = resnet_spec[resnet_type]
self.name = name
self.inplanes = 64
super(ResNetBackbone, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
for m in self.modules():
if isinstance(m, nn.Conv2d):
# nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
nn.init.normal_(m.weight, mean=0, std=0.001)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)