msg="PaddlePaddle only support FP16 evaluation when training with AMP O2 now. "
logger.warning(msg)
self.config["AMP"]["use_fp16_test"]=True
self.amp_eval=True
# TODO(gaotingquan): to compatible with different versions of Paddle
paddle_version=paddle.__version__[:3]
# paddle version < 2.3.0 and not develop
ifpaddle_versionnotin["2.3","0.0"]:
ifself.mode=="train":
self.model,self.optimizer=paddle.amp.decorate(
models=self.model,
optimizers=self.optimizer,
level=amp_level,
level=self.amp_level,
save_dtype='float32')
iflen(self.train_loss_func.parameters())>0:
elifself.amp_eval:
ifself.amp_level=="O2":
msg="The PaddlePaddle that installed not support FP16 evaluation in AMP O2. Please use PaddlePaddle version >= 2.3.0. Use FP32 evaluation instead and please notice the Eval Dataset output_fp16 should be 'False'."
msg=f"The training strategy provided by PaddleClas is based on {std_gpu_num} gpus. But the number of gpu is {world_size} in current training. Please modify the stategy (learning rate, batch size and so on) if use this config to train."
# Copyright (c) 2021 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.
from__future__importabsolute_import
from__future__importdivision
from__future__importprint_function
fromtypingimportDict
importpaddle
importpaddle.nnasnn
importpaddle.nn.functionalasF
classCenterLoss(nn.Layer):
def__init__(self,num_classes=5013,feat_dim=2048):
"""Center loss
paper : [A Discriminative Feature Learning Approach for Deep Face Recognition](https://link.springer.com/content/pdf/10.1007%2F978-3-319-46478-7_31.pdf)
warmup_epoch(int): The epoch numbers for LinearWarmup. Default: 0.
warmup_start_lr(float): Initial learning rate of warm up. Default: 0.0.
by_epoch(bool): Whether lr decay by epoch. Default: False.
last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
"""
...
...
@@ -198,6 +216,7 @@ class Piecewise(object):
epochs,
warmup_epoch=0,
warmup_start_lr=0.0,
by_epoch=False,
last_epoch=-1,
**kwargs):
super().__init__()
...
...
@@ -205,15 +224,31 @@ class Piecewise(object):
msg=f"When using warm up, the value of \"Global.epochs\" must be greater than value of \"Optimizer.lr.warmup_epoch\". The value of \"Optimizer.lr.warmup_epoch\" has been set to {epochs}."