Skip to content

Commit

Permalink
update AvgPool2D to AdaptiveAvgPool2D
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo committed Sep 24, 2021
1 parent fddf07e commit 8701690
Showing 1 changed file with 90 additions and 38 deletions.
128 changes: 90 additions & 38 deletions python/paddle/vision/models/googlenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from paddle.fluid.param_attr import ParamAttr
from paddle.nn import Conv2D, Linear, Dropout
from paddle.nn import MaxPool2D, AvgPool2D
from paddle.nn import MaxPool2D, AvgPool2D, AdaptiveAvgPool2D
from paddle.nn.initializer import Uniform
from paddle.utils.download import get_weights_path_from_url

Expand All @@ -30,19 +30,26 @@
model_urls = {
"GoogLeNet": (
"https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/GoogLeNet_pretrained.pdparams",
"80c06f038e905c53ab32c40eca6e26ae",
)
"80c06f038e905c53ab32c40eca6e26ae", )
}


def xavier(channels, filter_size, name):
stdv = (3.0 / (filter_size ** 2 * channels)) ** 0.5
param_attr = ParamAttr(initializer=Uniform(-stdv, stdv), name=name + "_weights")
stdv = (3.0 / (filter_size**2 * channels))**0.5
param_attr = ParamAttr(
initializer=Uniform(-stdv, stdv), name=name + "_weights")
return param_attr


class ConvLayer(nn.Layer):
def __init__(self, num_channels, num_filters, filter_size, stride=1, groups=1, act=None, name=None):
def __init__(self,
num_channels,
num_filters,
filter_size,
stride=1,
groups=1,
act=None,
name=None):
super(ConvLayer, self).__init__()

self._conv = Conv2D(
Expand All @@ -53,26 +60,46 @@ def __init__(self, num_channels, num_filters, filter_size, stride=1, groups=1, a
padding=(filter_size - 1) // 2,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
bias_attr=False, )

def forward(self, inputs):
y = self._conv(inputs)
return y


class Inception(nn.Layer):
def __init__(self, input_channels, output_channels, filter1, filter3R, filter3, filter5R, filter5, proj, name=None):
def __init__(self,
input_channels,
output_channels,
filter1,
filter3R,
filter3,
filter5R,
filter5,
proj,
name=None):
super(Inception, self).__init__()

self._conv1 = ConvLayer(input_channels, filter1, 1, name="inception_" + name + "_1x1")
self._conv3r = ConvLayer(input_channels, filter3R, 1, name="inception_" + name + "_3x3_reduce")
self._conv3 = ConvLayer(filter3R, filter3, 3, name="inception_" + name + "_3x3")
self._conv5r = ConvLayer(input_channels, filter5R, 1, name="inception_" + name + "_5x5_reduce")
self._conv5 = ConvLayer(filter5R, filter5, 5, name="inception_" + name + "_5x5")
self._conv1 = ConvLayer(
input_channels, filter1, 1, name="inception_" + name + "_1x1")
self._conv3r = ConvLayer(
input_channels,
filter3R,
1,
name="inception_" + name + "_3x3_reduce")
self._conv3 = ConvLayer(
filter3R, filter3, 3, name="inception_" + name + "_3x3")
self._conv5r = ConvLayer(
input_channels,
filter5R,
1,
name="inception_" + name + "_5x5_reduce")
self._conv5 = ConvLayer(
filter5R, filter5, 5, name="inception_" + name + "_5x5")
self._pool = MaxPool2D(kernel_size=3, stride=1, padding=1)

self._convprj = ConvLayer(input_channels, proj, 1, name="inception_" + name + "_3x3_proj")
self._convprj = ConvLayer(
input_channels, proj, 1, name="inception_" + name + "_3x3_proj")

def forward(self, inputs):
conv1 = self._conv1(inputs)
Expand All @@ -99,38 +126,61 @@ def __init__(self, class_num=1000):
self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1")
self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3")

self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32, name="ince3a")
self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64, name="ince3b")

self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64, name="ince4a")
self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64, name="ince4b")
self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64, name="ince4c")
self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64, name="ince4d")
self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128, name="ince4e")

self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128, name="ince5a")
self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128, name="ince5b")

self._pool_5 = AvgPool2D(kernel_size=7, stride=7)
self._ince3a = Inception(
192, 192, 64, 96, 128, 16, 32, 32, name="ince3a")
self._ince3b = Inception(
256, 256, 128, 128, 192, 32, 96, 64, name="ince3b")

self._ince4a = Inception(
480, 480, 192, 96, 208, 16, 48, 64, name="ince4a")
self._ince4b = Inception(
512, 512, 160, 112, 224, 24, 64, 64, name="ince4b")
self._ince4c = Inception(
512, 512, 128, 128, 256, 24, 64, 64, name="ince4c")
self._ince4d = Inception(
512, 512, 112, 144, 288, 32, 64, 64, name="ince4d")
self._ince4e = Inception(
528, 528, 256, 160, 320, 32, 128, 128, name="ince4e")

self._ince5a = Inception(
832, 832, 256, 160, 320, 32, 128, 128, name="ince5a")
self._ince5b = Inception(
832, 832, 384, 192, 384, 48, 128, 128, name="ince5b")

self._pool_5 = AdaptiveAvgPool2D(1)

self._drop = Dropout(p=0.4, mode="downscale_in_infer")
self._fc_out = Linear(
1024, class_num, weight_attr=xavier(1024, 1, "out"), bias_attr=ParamAttr(name="out_offset")
)
1024,
class_num,
weight_attr=xavier(1024, 1, "out"),
bias_attr=ParamAttr(name="out_offset"))
self._pool_o1 = AvgPool2D(kernel_size=5, stride=3)
self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1")
self._fc_o1 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o1"), bias_attr=ParamAttr(name="fc_o1_offset"))
self._fc_o1 = Linear(
1152,
1024,
weight_attr=xavier(2048, 1, "fc_o1"),
bias_attr=ParamAttr(name="fc_o1_offset"))
self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer")
self._out1 = Linear(
1024, class_num, weight_attr=xavier(1024, 1, "out1"), bias_attr=ParamAttr(name="out1_offset")
)
1024,
class_num,
weight_attr=xavier(1024, 1, "out1"),
bias_attr=ParamAttr(name="out1_offset"))
self._pool_o2 = AvgPool2D(kernel_size=5, stride=3)
self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2")
self._fc_o2 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o2"), bias_attr=ParamAttr(name="fc_o2_offset"))
self._fc_o2 = Linear(
1152,
1024,
weight_attr=xavier(2048, 1, "fc_o2"),
bias_attr=ParamAttr(name="fc_o2_offset"))
self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer")
self._out2 = Linear(
1024, class_num, weight_attr=xavier(1024, 1, "out2"), bias_attr=ParamAttr(name="out2_offset")
)
1024,
class_num,
weight_attr=xavier(1024, 1, "out2"),
bias_attr=ParamAttr(name="out2_offset"))

def forward(self, inputs):
x = self._conv(inputs)
Expand Down Expand Up @@ -181,8 +231,10 @@ def GoogLeNet(pretrained=False, **kwargs):
if pretrained:
assert (
arch in model_urls
), "{} model do not have a pretrained model now, you should set pretrained=False".format(arch)
weight_path = get_weights_path_from_url(model_urls[arch][0], model_urls[arch][1])
), "{} model do not have a pretrained model now, you should set pretrained=False".format(
arch)
weight_path = get_weights_path_from_url(model_urls[arch][0],
model_urls[arch][1])

param = paddle.load(weight_path)
model.set_dict(param)
Expand Down

0 comments on commit 8701690

Please sign in to comment.