base_network.py 20.4 KB
Newer Older
L
lvmengsi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#copyright (c) 2019 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.

from __future__ import division
import paddle.fluid as fluid
import numpy as np
L
lvmengsi 已提交
18
import math
L
lvmengsi 已提交
19
import os
L
lvmengsi 已提交
20
import warnings
L
lvmengsi 已提交
21 22 23 24 25 26

use_cudnn = True
if 'ce_mode' in os.environ:
    use_cudnn = False


L
lvmengsi 已提交
27 28 29 30 31 32 33 34 35 36
def cal_padding(img_size, stride, filter_size, dilation=1):
    """Calculate padding size."""
    valid_filter_size = dilation * (filter_size - 1) + 1
    if img_size % stride == 0:
        out_size = max(filter_size - stride, 0)
    else:
        out_size = max(filter_size - (img_size % stride), 0)
    return out_size // 2, out_size - out_size // 2


L
lvmengsi 已提交
37 38 39 40 41
def norm_layer(input,
               norm_type='batch_norm',
               name=None,
               is_test=False,
               affine=True):
L
lvmengsi 已提交
42
    if norm_type == 'batch_norm':
Z
zhumanyu 已提交
43 44 45 46
        if affine == True:
            param_attr = fluid.ParamAttr(
                name=name + '_w', initializer=fluid.initializer.Constant(1.0))
            bias_attr = fluid.ParamAttr(
L
lvmengsi 已提交
47 48
                name=name + '_b',
                initializer=fluid.initializer.Constant(value=0.0))
Z
zhumanyu 已提交
49 50
        else:
            param_attr = fluid.ParamAttr(
L
lvmengsi 已提交
51 52 53
                name=name + '_w',
                initializer=fluid.initializer.Constant(1.0),
                trainable=False)
Z
zhumanyu 已提交
54
            bias_attr = fluid.ParamAttr(
L
lvmengsi 已提交
55 56 57
                name=name + '_b',
                initializer=fluid.initializer.Constant(value=0.0),
                trainable=False)
L
lvmengsi 已提交
58 59 60 61
        return fluid.layers.batch_norm(
            input,
            param_attr=param_attr,
            bias_attr=bias_attr,
L
lvmengsi 已提交
62
            is_test=is_test,
L
lvmengsi 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
            moving_mean_name=name + '_mean',
            moving_variance_name=name + '_var')

    elif norm_type == 'instance_norm':
        helper = fluid.layer_helper.LayerHelper("instance_norm", **locals())
        dtype = helper.input_dtype()
        epsilon = 1e-5
        mean = fluid.layers.reduce_mean(input, dim=[2, 3], keep_dim=True)
        var = fluid.layers.reduce_mean(
            fluid.layers.square(input - mean), dim=[2, 3], keep_dim=True)
        if name is not None:
            scale_name = name + "_scale"
            offset_name = name + "_offset"
Z
zhumanyu 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        if affine:
            scale_param = fluid.ParamAttr(
                name=scale_name,
                initializer=fluid.initializer.Constant(1.0),
                trainable=True)
            offset_param = fluid.ParamAttr(
                name=offset_name,
                initializer=fluid.initializer.Constant(0.0),
                trainable=True)
        else:
            scale_param = fluid.ParamAttr(
                name=scale_name,
                initializer=fluid.initializer.Constant(1.0),
                trainable=False)
            offset_param = fluid.ParamAttr(
                name=offset_name,
                initializer=fluid.initializer.Constant(0.0),
                trainable=False)
L
lvmengsi 已提交
94 95 96 97 98 99 100 101 102 103
        scale = helper.create_parameter(
            attr=scale_param, shape=input.shape[1:2], dtype=dtype)
        offset = helper.create_parameter(
            attr=offset_param, shape=input.shape[1:2], dtype=dtype)

        tmp = fluid.layers.elementwise_mul(x=(input - mean), y=scale, axis=1)
        tmp = tmp / fluid.layers.sqrt(var + epsilon)
        tmp = fluid.layers.elementwise_add(tmp, offset, axis=1)
        return tmp
    else:
L
lvmengsi 已提交
104
        raise NotImplementedError("norm type: [%s] is not support" % norm_type)
L
lvmengsi 已提交
105 106


L
lvmengsi 已提交
107
def initial_type(name,
L
lvmengsi 已提交
108 109 110
                 input,
                 op_type,
                 fan_out,
L
lvmengsi 已提交
111 112 113 114 115
                 init="normal",
                 use_bias=False,
                 filter_size=0,
                 stddev=0.02):
    if init == "kaiming":
L
lvmengsi 已提交
116 117 118 119 120 121 122 123 124
        if op_type == 'conv':
            fan_in = input.shape[1] * filter_size * filter_size
        elif op_type == 'deconv':
            fan_in = fan_out * filter_size * filter_size
        else:
            if len(input.shape) > 2:
                fan_in = input.shape[1] * input.shape[2] * input.shape[3]
            else:
                fan_in = input.shape[1]
L
lvmengsi 已提交
125 126 127
        bound = 1 / math.sqrt(fan_in)
        param_attr = fluid.ParamAttr(
            name=name + "_w",
L
lvmengsi 已提交
128 129
            initializer=fluid.initializer.Uniform(
                low=-bound, high=bound))
L
lvmengsi 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        if use_bias == True:
            bias_attr = fluid.ParamAttr(
                name=name + '_b',
                initializer=fluid.initializer.Uniform(
                    low=-bound, high=bound))
        else:
            bias_attr = False
    else:
        param_attr = fluid.ParamAttr(
            name=name + "_w",
            initializer=fluid.initializer.NormalInitializer(
                loc=0.0, scale=stddev))
        if use_bias == True:
            bias_attr = fluid.ParamAttr(
                name=name + "_b", initializer=fluid.initializer.Constant(0.0))
        else:
            bias_attr = False
    return param_attr, bias_attr


L
lvmengsi 已提交
150 151 152 153 154 155 156 157 158
def conv2d(input,
           num_filters=64,
           filter_size=7,
           stride=1,
           stddev=0.02,
           padding=0,
           name="conv2d",
           norm=None,
           activation_fn=None,
L
lvmengsi 已提交
159
           relufactor=0.2,
L
lvmengsi 已提交
160 161
           use_bias=False,
           padding_type=None,
L
lvmengsi 已提交
162 163
           initial="normal",
           is_test=False):
L
lvmengsi 已提交
164 165 166 167 168 169 170 171

    if padding != 0 and padding_type != None:
        warnings.warn(
            'padding value and padding type are set in the same time, and the final padding width and padding height are computed by padding_type'
        )

    param_attr, bias_attr = initial_type(
        name=name,
L
lvmengsi 已提交
172 173 174
        input=input,
        op_type='conv',
        fan_out=num_filters,
L
lvmengsi 已提交
175 176 177 178 179 180 181 182 183
        init=initial,
        use_bias=use_bias,
        filter_size=filter_size,
        stddev=stddev)

    need_crop = False
    if padding_type == "SAME":
        top_padding, bottom_padding = cal_padding(input.shape[2], stride,
                                                  filter_size)
L
lvmengsi 已提交
184
        left_padding, right_padding = cal_padding(input.shape[3], stride,
L
lvmengsi 已提交
185 186 187 188 189 190 191 192 193 194 195 196
                                                  filter_size)
        height_padding = bottom_padding
        width_padding = right_padding
        if top_padding != bottom_padding or left_padding != right_padding:
            height_padding = top_padding + stride
            width_padding = left_padding + stride
            need_crop = True
        padding = [height_padding, width_padding]
    elif padding_type == "VALID":
        height_padding = 0
        width_padding = 0
        padding = [height_padding, width_padding]
L
lvmengsi 已提交
197
    else:
L
lvmengsi 已提交
198
        padding = padding
L
lvmengsi 已提交
199 200 201 202 203 204 205 206 207 208 209

    conv = fluid.layers.conv2d(
        input,
        num_filters,
        filter_size,
        name=name,
        stride=stride,
        padding=padding,
        use_cudnn=use_cudnn,
        param_attr=param_attr,
        bias_attr=bias_attr)
L
lvmengsi 已提交
210 211 212 213 214
    if need_crop:
        conv = fluid.layers.crop(
            conv,
            shape=(-1, conv.shape[1], conv.shape[2] - 1, conv.shape[3] - 1),
            offsets=(0, 0, 1, 1))
L
lvmengsi 已提交
215
    if norm is not None:
L
lvmengsi 已提交
216 217
        conv = norm_layer(
            input=conv, norm_type=norm, name=name + "_norm", is_test=is_test)
L
lvmengsi 已提交
218 219 220
    if activation_fn == 'relu':
        conv = fluid.layers.relu(conv, name=name + '_relu')
    elif activation_fn == 'leaky_relu':
L
lvmengsi 已提交
221 222 223
        if relufactor == 0.0:
            raise Warning(
                "the activation is leaky_relu, but the relufactor is 0")
L
lvmengsi 已提交
224 225 226 227
        conv = fluid.layers.leaky_relu(
            conv, alpha=relufactor, name=name + '_leaky_relu')
    elif activation_fn == 'tanh':
        conv = fluid.layers.tanh(conv, name=name + '_tanh')
L
lvmengsi 已提交
228 229
    elif activation_fn == 'sigmoid':
        conv = fluid.layers.sigmoid(conv, name=name + '_sigmoid')
L
lvmengsi 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    elif activation_fn == None:
        conv = conv
    else:
        raise NotImplementedError("activation: [%s] is not support" %
                                  activation_fn)

    return conv


def deconv2d(input,
             num_filters=64,
             filter_size=7,
             stride=1,
             stddev=0.02,
L
lvmengsi 已提交
244
             padding=0,
L
lvmengsi 已提交
245 246 247 248
             outpadding=[0, 0, 0, 0],
             name="deconv2d",
             norm=None,
             activation_fn=None,
L
lvmengsi 已提交
249
             relufactor=0.2,
L
lvmengsi 已提交
250
             use_bias=False,
L
lvmengsi 已提交
251 252
             padding_type=None,
             output_size=None,
L
lvmengsi 已提交
253 254
             initial="normal",
             is_test=False):
L
lvmengsi 已提交
255 256 257 258 259 260 261 262

    if padding != 0 and padding_type != None:
        warnings.warn(
            'padding value and padding type are set in the same time, and the final padding width and padding height are computed by padding_type'
        )

    param_attr, bias_attr = initial_type(
        name=name,
L
lvmengsi 已提交
263 264 265
        input=input,
        op_type='deconv',
        fan_out=num_filters,
L
lvmengsi 已提交
266 267 268 269 270 271 272 273 274
        init=initial,
        use_bias=use_bias,
        filter_size=filter_size,
        stddev=stddev)

    need_crop = False
    if padding_type == "SAME":
        top_padding, bottom_padding = cal_padding(input.shape[2], stride,
                                                  filter_size)
L
lvmengsi 已提交
275
        left_padding, right_padding = cal_padding(input.shape[3], stride,
L
lvmengsi 已提交
276 277 278 279 280 281 282 283 284 285 286 287
                                                  filter_size)
        height_padding = bottom_padding
        width_padding = right_padding
        if top_padding != bottom_padding or left_padding != right_padding:
            height_padding = top_padding + stride
            width_padding = left_padding + stride
            need_crop = True
        padding = [height_padding, width_padding]
    elif padding_type == "VALID":
        height_padding = 0
        width_padding = 0
        padding = [height_padding, width_padding]
L
lvmengsi 已提交
288
    else:
L
lvmengsi 已提交
289
        padding = padding
L
lvmengsi 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302

    conv = fluid.layers.conv2d_transpose(
        input,
        num_filters,
        output_size=output_size,
        name=name,
        filter_size=filter_size,
        stride=stride,
        padding=padding,
        use_cudnn=use_cudnn,
        param_attr=param_attr,
        bias_attr=bias_attr)

L
lvmengsi 已提交
303
    if np.mean(outpadding) != 0 and padding_type == None:
L
lvmengsi 已提交
304 305
        conv = fluid.layers.pad2d(
            conv, paddings=outpadding, mode='constant', pad_value=0.0)
L
lvmengsi 已提交
306 307

    if norm is not None:
L
lvmengsi 已提交
308 309
        conv = norm_layer(
            input=conv, norm_type=norm, name=name + "_norm", is_test=is_test)
L
lvmengsi 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    if activation_fn == 'relu':
        conv = fluid.layers.relu(conv, name=name + '_relu')
    elif activation_fn == 'leaky_relu':
        if relufactor == 0.0:
            raise Warning(
                "the activation is leaky_relu, but the relufactor is 0")
        conv = fluid.layers.leaky_relu(
            conv, alpha=relufactor, name=name + '_leaky_relu')
    elif activation_fn == 'tanh':
        conv = fluid.layers.tanh(conv, name=name + '_tanh')
    elif activation_fn == 'sigmoid':
        conv = fluid.layers.sigmoid(conv, name=name + '_sigmoid')
    elif activation_fn == None:
        conv = conv
    else:
        raise NotImplementedError("activation: [%s] is not support" %
                                  activation_fn)

    return conv


def linear(input,
           output_size,
           norm=None,
           stddev=0.02,
           activation_fn=None,
           relufactor=0.2,
L
lvmengsi 已提交
337
           name="linear",
L
lvmengsi 已提交
338 339
           initial="normal",
           is_test=False):
L
lvmengsi 已提交
340 341 342

    param_attr, bias_attr = initial_type(
        name=name,
L
lvmengsi 已提交
343 344 345
        input=input,
        op_type='linear',
        fan_out=output_size,
L
lvmengsi 已提交
346 347 348 349 350
        init=initial,
        use_bias=True,
        filter_size=1,
        stddev=stddev)

L
lvmengsi 已提交
351 352 353 354 355 356 357
    linear = fluid.layers.fc(input,
                             output_size,
                             param_attr=param_attr,
                             bias_attr=bias_attr,
                             name=name)

    if norm is not None:
L
lvmengsi 已提交
358 359
        linear = norm_layer(
            input=linear, norm_type=norm, name=name + '_norm', is_test=is_test)
L
lvmengsi 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    if activation_fn == 'relu':
        linear = fluid.layers.relu(linear, name=name + '_relu')
    elif activation_fn == 'leaky_relu':
        if relufactor == 0.0:
            raise Warning(
                "the activation is leaky_relu, but the relufactor is 0")
        linear = fluid.layers.leaky_relu(
            linear, alpha=relufactor, name=name + '_leaky_relu')
    elif activation_fn == 'tanh':
        linear = fluid.layers.tanh(linear, name=name + '_tanh')
    elif activation_fn == 'sigmoid':
        linear = fluid.layers.sigmoid(linear, name=name + '_sigmoid')
    elif activation_fn == None:
        linear = linear
    else:
        raise NotImplementedError("activation: [%s] is not support" %
                                  activation_fn)

    return linear


def conv_cond_concat(x, y):
    ones = fluid.layers.fill_constant_batch_size_like(
        x, [-1, y.shape[1], x.shape[2], x.shape[3]], "float32", 1.0)
    out = fluid.layers.concat([x, ones * y], 1)
    return out


def conv_and_pool(x, num_filters, name, stddev=0.02, act=None):
    param_attr = fluid.ParamAttr(
        name=name + '_w',
        initializer=fluid.initializer.NormalInitializer(
            loc=0.0, scale=stddev))
    bias_attr = fluid.ParamAttr(
        name=name + "_b", initializer=fluid.initializer.Constant(0.0))

    out = fluid.nets.simple_img_conv_pool(
        input=x,
        filter_size=5,
        num_filters=num_filters,
        pool_size=2,
        pool_stride=2,
        param_attr=param_attr,
        bias_attr=bias_attr,
        act=act)
    return out
L
lvmengsi 已提交
406 407


Z
zhumanyu 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420
def conv2d_spectral_norm(input,
                         num_filters=64,
                         filter_size=7,
                         stride=1,
                         stddev=0.02,
                         padding=0,
                         name="conv2d_spectral_norm",
                         norm=None,
                         activation_fn=None,
                         relufactor=0.0,
                         use_bias=False,
                         padding_type=None,
                         initial="normal",
L
lvmengsi 已提交
421 422
                         is_test=False,
                         norm_affine=True):
Z
zhumanyu 已提交
423 424 425 426 427 428
    b, c, h, w = input.shape
    height = num_filters
    width = c * filter_size * filter_size
    helper = fluid.layer_helper.LayerHelper("conv2d_spectral_norm", **locals())
    dtype = helper.input_dtype()
    weight_param = fluid.ParamAttr(
L
lvmengsi 已提交
429
        name=name + ".weight_orig",
L
lvmengsi 已提交
430 431
        initializer=fluid.initializer.Normal(
            loc=0.0, scale=1.0),
Z
zhumanyu 已提交
432 433
        trainable=True)
    weight = helper.create_parameter(
L
lvmengsi 已提交
434 435 436 437 438
        attr=weight_param,
        shape=(num_filters, c, filter_size, filter_size),
        dtype=dtype)
    weight_spectral_norm = fluid.layers.spectral_norm(
        weight, dim=0, name=name + ".spectral_norm")
Z
zhumanyu 已提交
439 440 441
    weight = weight_spectral_norm
    if use_bias:
        bias_attr = fluid.ParamAttr(
L
lvmengsi 已提交
442 443 444
            name=name + "_b",
            initializer=fluid.initializer.Normal(
                loc=0.0, scale=1.0))
Z
zhumanyu 已提交
445 446
    else:
        bias_attr = False
L
lvmengsi 已提交
447 448
    conv = conv2d_with_filter(
        input, weight, stride, padding, bias_attr=bias_attr, name=name)
Z
zhumanyu 已提交
449 450
    if norm is not None:
        conv = norm_layer(
L
lvmengsi 已提交
451 452 453 454 455
            input=conv,
            norm_type=norm,
            name=name + "_norm",
            is_test=is_test,
            affine=norm_affine)
Z
zhumanyu 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    if activation_fn == 'relu':
        conv = fluid.layers.relu(conv, name=name + '_relu')
    elif activation_fn == 'leaky_relu':
        conv = fluid.layers.leaky_relu(
            conv, alpha=relufactor, name=name + '_leaky_relu')
    elif activation_fn == 'tanh':
        conv = fluid.layers.tanh(conv, name=name + '_tanh')
    elif activation_fn == 'sigmoid':
        conv = fluid.layers.sigmoid(conv, name=name + '_sigmoid')
    elif activation_fn == None:
        conv = conv
    else:
        raise NotImplementedError("activation: [%s] is not support" %
                                  activation_fn)
    return conv


def conv2d_with_filter(input,
                       filter,
                       stride=1,
                       padding=0,
                       dilation=1,
                       groups=None,
                       bias_attr=None,
                       use_cudnn=True,
                       act=None,
                       name=None):
    """ 
    Similar with conv2d, this is a convolution2D layers. Difference
    is filter can be token as input directly instead of setting filter size
    and number of fliters. Filter is a  4-D tensor with shape 
    [num_filter, num_channel, filter_size_h, filter_size_w].
     Args:
        input (Variable): The input image with [N, C, H, W] format.
        filter(Variable): The input filter with [N, C, H, W] format.
        stride (int|tuple): The stride size. If stride is a tuple, it must
            contain two integers, (stride_H, stride_W). Otherwise, the
            stride_H = stride_W = stride. Default: stride = 1.
        padding (int|tuple): The padding size. If padding is a tuple, it must
            contain two integers, (padding_H, padding_W). Otherwise, the
            padding_H = padding_W = padding. Default: padding = 0.
        dilation (int|tuple): The dilation size. If dilation is a tuple, it must
            contain two integers, (dilation_H, dilation_W). Otherwise, the
            dilation_H = dilation_W = dilation. Default: dilation = 1.
        bias_attr (ParamAttr|bool|None): The parameter attribute for the bias of conv2d.
            If it is set to False, no bias will be added to the output units.
            If it is set to None or one attribute of ParamAttr, conv2d
            will create ParamAttr as bias_attr. If the Initializer of the bias_attr
            is not set, the bias is initialized zero. Default: None.
        use_cudnn (bool): Use cudnn kernel or not, it is valid only when the cudnn
            library is installed. Default: True
        act (str): Activation type, if it is set to None, activation is not appended.
            Default: None
        name (str|None): A name for this layer(optional). If set None, the layer
            will be named automatically. Default: None
    Returns:
        Variable: The tensor variable storing the convolution and \
                  non-linearity activation result.
    Raises:
        ValueError: If the shapes of input, filter_size, stride, padding and
                    groups mismatch.
    Examples:
        .. code-block:: python
          data = fluid.layers.data(name='data', shape=[3, 32, 32], \
                                  dtype='float32')
          filter = fluid.layers.data(name='filter',shape=[10,3,3,3], \
                                    dtype='float32',append_batch_size=False)
          conv2d = fluid.layers.conv2d(input=data, 
                                       filter=filter,
                                       act="relu") 
    """
    helper = fluid.layer_helper.LayerHelper("conv2d_with_filter", **locals())
    num_channels = input.shape[1]
    num_filters = filter.shape[0]
    num_filter_channels = filter.shape[1]
    l_type = 'conv2d'
    if (num_channels == groups and num_filters % num_channels == 0 and
            not use_cudnn):
        l_type = 'depthwise_conv2d'
    if groups is None:
        assert num_filter_channels == num_channels
    else:
        if num_channels % groups != 0:
            raise ValueError("num_channels must be divisible by groups.")
        if num_channels // groups != num_filter_channels:
            raise ValueError("num_filter_channels must equal to num_channels\
                              divided by groups.")
    stride = fluid.layers.utils.convert_to_list(stride, 2, 'stride')
    padding = fluid.layers.utils.convert_to_list(padding, 2, 'padding')
    dilation = fluid.layers.utils.convert_to_list(dilation, 2, 'dilation')
    if not isinstance(use_cudnn, bool):
        raise ValueError("use_cudnn should be True or False")
    pre_bias = helper.create_variable_for_type_inference(dtype=input.dtype)
    helper.append_op(
        type=l_type,
        inputs={
            'Input': input,
            'Filter': filter,
        },
        outputs={"Output": pre_bias},
        attrs={
            'strides': stride,
            'paddings': padding,
            'dilations': dilation,
            'groups': groups,
            'use_cudnn': use_cudnn,
            'use_mkldnn': False
        })
    pre_act = helper.append_bias_op(pre_bias, dim_start=1, dim_end=2)
    return helper.append_activation(pre_act)