From 9bdd4195a4a7c3fed800de975cd83afc1104a81c Mon Sep 17 00:00:00 2001 From: CaoJian Date: Fri, 11 Sep 2020 15:02:39 +0800 Subject: [PATCH] SoftmaxCrossEntropyWithLogic api adapt --- model_zoo/official/cv/googlenet/eval.py | 7 ++-- .../cv/googlenet/src/CrossEntropySmooth.py | 38 +++++++++++++++++++ model_zoo/official/cv/googlenet/train.py | 7 ++-- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 model_zoo/official/cv/googlenet/src/CrossEntropySmooth.py diff --git a/model_zoo/official/cv/googlenet/eval.py b/model_zoo/official/cv/googlenet/eval.py index 79b8debcbe..c774d17e48 100644 --- a/model_zoo/official/cv/googlenet/eval.py +++ b/model_zoo/official/cv/googlenet/eval.py @@ -29,6 +29,7 @@ from src.config import cifar_cfg, imagenet_cfg from src.dataset import create_dataset_cifar10, create_dataset_imagenet from src.googlenet import GoogleNet +from src.CrossEntropySmooth import CrossEntropySmooth set_seed(1) @@ -43,7 +44,7 @@ if __name__ == '__main__': if args_opt.dataset_name == 'cifar10': cfg = cifar_cfg dataset = create_dataset_cifar10(cfg.data_path, 1, False) - loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False) + loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') net = GoogleNet(num_classes=cfg.num_classes) opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, cfg.momentum, weight_decay=cfg.weight_decay) @@ -54,8 +55,8 @@ if __name__ == '__main__': dataset = create_dataset_imagenet(cfg.val_data_path, 1, False) if not cfg.use_label_smooth: cfg.label_smooth_factor = 0.0 - loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean", - smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes) + loss = CrossEntropySmooth(sparse=True, reduction="mean", + smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes) net = GoogleNet(num_classes=cfg.num_classes) model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'}) diff --git a/model_zoo/official/cv/googlenet/src/CrossEntropySmooth.py b/model_zoo/official/cv/googlenet/src/CrossEntropySmooth.py new file mode 100644 index 0000000000..bf38c6e77b --- /dev/null +++ b/model_zoo/official/cv/googlenet/src/CrossEntropySmooth.py @@ -0,0 +1,38 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""define loss function for network""" +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.common import dtype as mstype +from mindspore.nn.loss.loss import _Loss +from mindspore.ops import functional as F +from mindspore.ops import operations as P + + +class CrossEntropySmooth(_Loss): + """CrossEntropy""" + def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000): + super(CrossEntropySmooth, self).__init__() + self.onehot = P.OneHot() + self.sparse = sparse + self.on_value = Tensor(1.0 - smooth_factor, mstype.float32) + self.off_value = Tensor(1.0 * smooth_factor / (num_classes - 1), mstype.float32) + self.ce = nn.SoftmaxCrossEntropyWithLogits(reduction=reduction) + + def construct(self, logit, label): + if self.sparse: + label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value) + loss = self.ce(logit, label) + return loss diff --git a/model_zoo/official/cv/googlenet/train.py b/model_zoo/official/cv/googlenet/train.py index b370177530..1c87698b8c 100644 --- a/model_zoo/official/cv/googlenet/train.py +++ b/model_zoo/official/cv/googlenet/train.py @@ -36,6 +36,7 @@ from mindspore.common import set_seed from src.config import cifar_cfg, imagenet_cfg from src.dataset import create_dataset_cifar10, create_dataset_imagenet from src.googlenet import GoogleNet +from src.CrossEntropySmooth import CrossEntropySmooth set_seed(1) @@ -148,7 +149,7 @@ if __name__ == '__main__': learning_rate=Tensor(lr), momentum=cfg.momentum, weight_decay=cfg.weight_decay) - loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False) + loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') elif args_opt.dataset_name == 'imagenet': lr = lr_steps_imagenet(cfg, batch_num) @@ -188,8 +189,8 @@ if __name__ == '__main__': loss_scale=cfg.loss_scale) if not cfg.use_label_smooth: cfg.label_smooth_factor = 0.0 - loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean", - smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes) + loss = CrossEntropySmooth(sparse=True, reduction="mean", + smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes) if cfg.is_dynamic_loss_scale == 1: loss_scale_manager = DynamicLossScaleManager(init_loss_scale=65536, scale_factor=2, scale_window=2000)