squeezenet.py 6.6 KB
Newer Older
C
cuicheng01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

G
gaotingquan 已提交
15 16
# reference: https://arxiv.org/abs/1709.01507

W
WuHaobo 已提交
17
import paddle
18 19 20
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
21 22
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
23

C
cuicheng01 已提交
24 25 26
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url

MODEL_URLS = {
littletomatodonkey's avatar
littletomatodonkey 已提交
27 28 29 30 31
    "SqueezeNet1_0":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_0_pretrained.pdparams",
    "SqueezeNet1_1":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/SqueezeNet1_1_pretrained.pdparams",
}
C
cuicheng01 已提交
32 33

__all__ = list(MODEL_URLS.keys())
34

35 36 37 38 39 40 41 42

class MakeFireConv(nn.Layer):
    def __init__(self,
                 input_channels,
                 output_channels,
                 filter_size,
                 padding=0,
                 name=None):
W
fix  
wqz960 已提交
43
        super(MakeFireConv, self).__init__()
44
        self._conv = Conv2D(
45 46 47 48 49 50 51 52 53 54 55
            input_channels,
            output_channels,
            filter_size,
            padding=padding,
            weight_attr=ParamAttr(name=name + "_weights"),
            bias_attr=ParamAttr(name=name + "_offset"))

    def forward(self, x):
        x = self._conv(x)
        x = F.relu(x)
        return x
56

W
WuHaobo 已提交
57

58
class MakeFire(nn.Layer):
59
    def __init__(self,
60 61 62 63 64
                 input_channels,
                 squeeze_channels,
                 expand1x1_channels,
                 expand3x3_channels,
                 name=None):
W
fix  
wqz960 已提交
65
        super(MakeFire, self).__init__()
66 67 68 69 70 71 72 73 74 75
        self._conv = MakeFireConv(
            input_channels, squeeze_channels, 1, name=name + "_squeeze1x1")
        self._conv_path1 = MakeFireConv(
            squeeze_channels, expand1x1_channels, 1, name=name + "_expand1x1")
        self._conv_path2 = MakeFireConv(
            squeeze_channels,
            expand3x3_channels,
            3,
            padding=1,
            name=name + "_expand3x3")
W
WuHaobo 已提交
76

77 78 79 80
    def forward(self, inputs):
        x = self._conv(inputs)
        x1 = self._conv_path1(x)
        x2 = self._conv_path2(x)
81
        return paddle.concat([x1, x2], axis=1)
W
WuHaobo 已提交
82

83 84

class SqueezeNet(nn.Layer):
littletomatodonkey's avatar
littletomatodonkey 已提交
85
    def __init__(self, version, class_num=1000):
86
        super(SqueezeNet, self).__init__()
W
WuHaobo 已提交
87 88
        self.version = version

89
        if self.version == "1.0":
90
            self._conv = Conv2D(
91 92 93 94 95 96
                3,
                96,
                7,
                stride=2,
                weight_attr=ParamAttr(name="conv1_weights"),
                bias_attr=ParamAttr(name="conv1_offset"))
97
            self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
W
fix  
wqz960 已提交
98 99 100
            self._conv1 = MakeFire(96, 16, 64, 64, name="fire2")
            self._conv2 = MakeFire(128, 16, 64, 64, name="fire3")
            self._conv3 = MakeFire(128, 32, 128, 128, name="fire4")
101

W
fix  
wqz960 已提交
102 103 104 105
            self._conv4 = MakeFire(256, 32, 128, 128, name="fire5")
            self._conv5 = MakeFire(256, 48, 192, 192, name="fire6")
            self._conv6 = MakeFire(384, 48, 192, 192, name="fire7")
            self._conv7 = MakeFire(384, 64, 256, 256, name="fire8")
106

W
fix  
wqz960 已提交
107
            self._conv8 = MakeFire(512, 64, 256, 256, name="fire9")
W
WuHaobo 已提交
108
        else:
109
            self._conv = Conv2D(
110 111 112 113 114 115 116
                3,
                64,
                3,
                stride=2,
                padding=1,
                weight_attr=ParamAttr(name="conv1_weights"),
                bias_attr=ParamAttr(name="conv1_offset"))
117
            self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
W
fix  
wqz960 已提交
118 119
            self._conv1 = MakeFire(64, 16, 64, 64, name="fire2")
            self._conv2 = MakeFire(128, 16, 64, 64, name="fire3")
W
WuHaobo 已提交
120

W
fix  
wqz960 已提交
121 122
            self._conv3 = MakeFire(128, 32, 128, 128, name="fire4")
            self._conv4 = MakeFire(256, 32, 128, 128, name="fire5")
W
WuHaobo 已提交
123

W
fix  
wqz960 已提交
124 125 126 127
            self._conv5 = MakeFire(256, 48, 192, 192, name="fire6")
            self._conv6 = MakeFire(384, 48, 192, 192, name="fire7")
            self._conv7 = MakeFire(384, 64, 256, 256, name="fire8")
            self._conv8 = MakeFire(512, 64, 256, 256, name="fire9")
128

littletomatodonkey's avatar
littletomatodonkey 已提交
129
        self._drop = Dropout(p=0.5, mode="downscale_in_infer")
130
        self._conv9 = Conv2D(
131
            512,
littletomatodonkey's avatar
littletomatodonkey 已提交
132
            class_num,
133 134 135
            1,
            weight_attr=ParamAttr(name="conv10_weights"),
            bias_attr=ParamAttr(name="conv10_offset"))
136
        self._avg_pool = AdaptiveAvgPool2D(1)
W
WuHaobo 已提交
137

138 139
    def forward(self, inputs):
        x = self._conv(inputs)
140
        x = F.relu(x)
141
        x = self._pool(x)
142
        if self.version == "1.0":
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            x = self._conv1(x)
            x = self._conv2(x)
            x = self._conv3(x)
            x = self._pool(x)
            x = self._conv4(x)
            x = self._conv5(x)
            x = self._conv6(x)
            x = self._conv7(x)
            x = self._pool(x)
            x = self._conv8(x)
        else:
            x = self._conv1(x)
            x = self._conv2(x)
            x = self._pool(x)
            x = self._conv3(x)
            x = self._conv4(x)
            x = self._pool(x)
            x = self._conv5(x)
            x = self._conv6(x)
            x = self._conv7(x)
            x = self._conv8(x)
        x = self._drop(x)
        x = self._conv9(x)
166
        x = F.relu(x)
167
        x = self._avg_pool(x)
168
        x = paddle.squeeze(x, axis=[2, 3])
169 170
        return x

littletomatodonkey's avatar
littletomatodonkey 已提交
171

C
cuicheng01 已提交
172 173 174 175 176 177 178 179 180 181 182
def _load_pretrained(pretrained, model, model_url, use_ssld=False):
    if pretrained is False:
        pass
    elif pretrained is True:
        load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
    elif isinstance(pretrained, str):
        load_dygraph_pretrain(model, pretrained)
    else:
        raise RuntimeError(
            "pretrained type is not available. Please use `string` or `boolean` type."
        )
littletomatodonkey's avatar
littletomatodonkey 已提交
183

C
cuicheng01 已提交
184 185 186

def SqueezeNet1_0(pretrained=False, use_ssld=False, **kwargs):
    model = SqueezeNet(version="1.0", **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
187 188
    _load_pretrained(
        pretrained, model, MODEL_URLS["SqueezeNet1_0"], use_ssld=use_ssld)
189 190
    return model

W
WuHaobo 已提交
191

C
cuicheng01 已提交
192 193
def SqueezeNet1_1(pretrained=False, use_ssld=False, **kwargs):
    model = SqueezeNet(version="1.1", **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
194 195
    _load_pretrained(
        pretrained, model, MODEL_URLS["SqueezeNet1_1"], use_ssld=use_ssld)
196
    return model