Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PaddlePaddle Hackathon] add WideResNet #36952

Merged
merged 11 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/paddle/tests/test_pretrained_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_models(self):
'mobilenet_v1', 'mobilenet_v2', 'resnet18', 'vgg16', 'alexnet',
'resnext50_32x4d', 'inception_v3', 'densenet121', 'squeezenet1_0',
'squeezenet1_1', 'googlenet', 'shufflenet_v2_x0_25',
'shufflenet_v2_swish'
'shufflenet_v2_swish', 'wide_resnet50_2', 'wide_resnet101_2'
]
for arch in arches:
self.infer(arch)
Expand Down
6 changes: 6 additions & 0 deletions python/paddle/tests/test_vision_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def test_resnet101(self):
def test_resnet152(self):
self.models_infer('resnet152')

def test_wide_resnet50_2(self):
self.models_infer('wide_resnet50_2')

def test_wide_resnet101_2(self):
self.models_infer('wide_resnet101_2')

def test_densenet121(self):
self.models_infer('densenet121')

Expand Down
2 changes: 2 additions & 0 deletions python/paddle/vision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from .models import resnet50 # noqa: F401
from .models import resnet101 # noqa: F401
from .models import resnet152 # noqa: F401
from .models import wide_resnet50_2 # noqa: F401
from .models import wide_resnet101_2 # noqa: F401
from .models import MobileNetV1 # noqa: F401
from .models import mobilenet_v1 # noqa: F401
from .models import MobileNetV2 # noqa: F401
Expand Down
4 changes: 4 additions & 0 deletions python/paddle/vision/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from .resnet import resnet50 # noqa: F401
from .resnet import resnet101 # noqa: F401
from .resnet import resnet152 # noqa: F401
from .resnet import wide_resnet50_2 # noqa: F401
from .resnet import wide_resnet101_2 # noqa: F401
from .mobilenetv1 import MobileNetV1 # noqa: F401
from .mobilenetv1 import mobilenet_v1 # noqa: F401
from .mobilenetv2 import MobileNetV2 # noqa: F401
Expand Down Expand Up @@ -66,6 +68,8 @@
'resnet50',
'resnet101',
'resnet152',
'wide_resnet50_2',
'wide_resnet101_2',
'VGG',
'vgg11',
'vgg13',
Expand Down
152 changes: 136 additions & 16 deletions python/paddle/vision/models/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
'02f35f034ca3858e1e54d4036443c92d'),
'resnet152': ('https://paddle-hapi.bj.bcebos.com/models/resnet152.pdparams',
'7ad16a2f1e7333859ff986138630fd7a'),
'wide_resnet50_2':
('https://paddle-hapi.bj.bcebos.com/models/wide_resnet50_2.pdparams',
'0282f804d73debdab289bd9fea3fa6dc'),
'wide_resnet101_2':
('https://paddle-hapi.bj.bcebos.com/models/wide_resnet101_2.pdparams',
'd4360a2d23657f059216f5d5a1a9ac93'),
}


Expand Down Expand Up @@ -153,23 +159,37 @@ class ResNet(nn.Layer):
Args:
Block (BasicBlock|BottleneckBlock): block module of model.
depth (int): layers of resnet, default: 50.
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
width (int): base width of resnet, default: 64.
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
will not be defined. Default: 1000.
with_pool (bool): use pool before the last fc layer or not. Default: True.

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import ResNet
from paddle.vision.models.resnet import BottleneckBlock, BasicBlock

resnet50 = ResNet(BottleneckBlock, 50)

wide_resnet50_2 = ResNet(BottleneckBlock, 50, width=64*2)

resnet18 = ResNet(BasicBlock, 18)

x = paddle.rand([1, 3, 224, 224])
out = resnet18(x)

print(out.shape)

"""

def __init__(self, block, depth, num_classes=1000, with_pool=True):
def __init__(self,
block,
depth=50,
width=64,
num_classes=1000,
with_pool=True):
super(ResNet, self).__init__()
layer_cfg = {
18: [2, 2, 2, 2],
Expand All @@ -179,6 +199,8 @@ def __init__(self, block, depth, num_classes=1000, with_pool=True):
152: [3, 8, 36, 3]
}
layers = layer_cfg[depth]
self.groups = 1
self.base_width = width
self.num_classes = num_classes
self.with_pool = with_pool
self._norm_layer = nn.BatchNorm2D
Expand Down Expand Up @@ -225,11 +247,17 @@ def _make_layer(self, block, planes, blocks, stride=1, dilate=False):

layers = []
layers.append(
block(self.inplanes, planes, stride, downsample, 1, 64,
previous_dilation, norm_layer))
block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, norm_layer=norm_layer))
layers.append(
block(
self.inplanes,
planes,
groups=self.groups,
base_width=self.base_width,
norm_layer=norm_layer))

return nn.Sequential(*layers)

Expand Down Expand Up @@ -268,100 +296,192 @@ def _resnet(arch, Block, depth, pretrained, **kwargs):


def resnet18(pretrained=False, **kwargs):
"""ResNet 18-layer model

"""ResNet 18-layer model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import resnet18

# build model
model = resnet18()

# build model and load imagenet pretrained weight
# model = resnet18(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
return _resnet('resnet18', BasicBlock, 18, pretrained, **kwargs)


def resnet34(pretrained=False, **kwargs):
"""ResNet 34-layer model

"""ResNet 34-layer model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import resnet34

# build model
model = resnet34()

# build model and load imagenet pretrained weight
# model = resnet34(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
return _resnet('resnet34', BasicBlock, 34, pretrained, **kwargs)


def resnet50(pretrained=False, **kwargs):
"""ResNet 50-layer model

"""ResNet 50-layer model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import resnet50

# build model
model = resnet50()

# build model and load imagenet pretrained weight
# model = resnet50(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
return _resnet('resnet50', BottleneckBlock, 50, pretrained, **kwargs)


def resnet101(pretrained=False, **kwargs):
"""ResNet 101-layer model

"""ResNet 101-layer model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import resnet101

# build model
model = resnet101()

# build model and load imagenet pretrained weight
# model = resnet101(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
return _resnet('resnet101', BottleneckBlock, 101, pretrained, **kwargs)


def resnet152(pretrained=False, **kwargs):
"""ResNet 152-layer model

"""ResNet 152-layer model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import resnet152

# build model
model = resnet152()

# build model and load imagenet pretrained weight
# model = resnet152(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
return _resnet('resnet152', BottleneckBlock, 152, pretrained, **kwargs)


def wide_resnet50_2(pretrained=False, **kwargs):
"""Wide ResNet-50-2 model from
`"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_.

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. code-block 与 import 间需要有一个空行,否则会有奇怪的显示bug;上面的修改同理哈 都需要有的~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改~~~


import paddle
from paddle.vision.models import wide_resnet50_2

# build model
model = wide_resnet50_2()

# build model and load imagenet pretrained weight
# model = wide_resnet50_2(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
kwargs['width'] = 64 * 2
return _resnet('wide_resnet50_2', BottleneckBlock, 50, pretrained, **kwargs)


def wide_resnet101_2(pretrained=False, **kwargs):
"""Wide ResNet-101-2 model from
`"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_.

Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet

Examples:
.. code-block:: python

import paddle
from paddle.vision.models import wide_resnet101_2

# build model
model = wide_resnet101_2()

# build model and load imagenet pretrained weight
# model = wide_resnet101_2(pretrained=True)

x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)
"""
kwargs['width'] = 64 * 2
return _resnet('wide_resnet101_2', BottleneckBlock, 101, pretrained,
**kwargs)