You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.5 KiB
87 lines
2.5 KiB
3 years ago
|
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||
|
#
|
||
|
# 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.
|
||
|
|
||
|
import paddle.nn as nn
|
||
|
import paddle.nn.functional as F
|
||
|
|
||
|
|
||
|
def normal_init(param, *args, **kwargs):
|
||
|
"""
|
||
|
Initialize parameters with a normal distribution.
|
||
|
|
||
|
Args:
|
||
|
param (Tensor): The tensor that needs to be initialized.
|
||
|
|
||
|
Returns:
|
||
|
The initialized parameters.
|
||
|
"""
|
||
3 years ago
|
|
||
3 years ago
|
return nn.initializer.Normal(*args, **kwargs)(param)
|
||
|
|
||
|
|
||
|
def kaiming_normal_init(param, *args, **kwargs):
|
||
|
"""
|
||
|
Initialize parameters with the Kaiming normal distribution.
|
||
|
|
||
|
For more information about the Kaiming initialization method, please refer to
|
||
3 years ago
|
https://arxiv.org/abs/1502.01852
|
||
3 years ago
|
|
||
|
Args:
|
||
|
param (Tensor): The tensor that needs to be initialized.
|
||
|
|
||
|
Returns:
|
||
|
The initialized parameters.
|
||
|
"""
|
||
3 years ago
|
|
||
3 years ago
|
return nn.initializer.KaimingNormal(*args, **kwargs)(param)
|
||
|
|
||
|
|
||
|
def constant_init(param, *args, **kwargs):
|
||
|
"""
|
||
|
Initialize parameters with constants.
|
||
|
|
||
|
Args:
|
||
|
param (Tensor): The tensor that needs to be initialized.
|
||
|
|
||
|
Returns:
|
||
|
The initialized parameters.
|
||
|
"""
|
||
3 years ago
|
|
||
3 years ago
|
return nn.initializer.Constant(*args, **kwargs)(param)
|
||
|
|
||
|
|
||
|
class KaimingInitMixin:
|
||
|
"""
|
||
|
A mix-in that provides the Kaiming initialization functionality.
|
||
|
|
||
|
Examples:
|
||
|
|
||
2 years ago
|
from paddlers.rs_models.cd.models.param_init import KaimingInitMixin
|
||
3 years ago
|
|
||
|
class CustomNet(nn.Layer, KaimingInitMixin):
|
||
|
def __init__(self, num_channels, num_classes):
|
||
|
super().__init__()
|
||
|
self.conv = nn.Conv2D(num_channels, num_classes, 3, 1, 0, bias_attr=False)
|
||
|
self.bn = nn.BatchNorm2D(num_classes)
|
||
|
self.init_weight()
|
||
|
"""
|
||
3 years ago
|
|
||
3 years ago
|
def init_weight(self):
|
||
|
for layer in self.sublayers():
|
||
|
if isinstance(layer, nn.Conv2D):
|
||
|
kaiming_normal_init(layer.weight)
|
||
|
elif isinstance(layer, (nn.BatchNorm, nn.SyncBatchNorm)):
|
||
|
constant_init(layer.weight, value=1)
|
||
3 years ago
|
constant_init(layer.bias, value=0)
|