Skip to content
Snippets Groups Projects
Unverified Commit 69a645c7 authored by i-robot's avatar i-robot Committed by Gitee
Browse files

!2332 mobilenet_v3 usability rectification

Merge pull request !2332 from 张毅辉/mobilenet_v3
parents 8e8e68a4 8cd715fb
No related branches found
No related tags found
No related merge requests found
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 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.
......@@ -16,10 +16,8 @@
eval.
"""
import argparse
from mindspore import context
import mindspore as ms
from mindspore import nn
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from src.dataset import create_dataset
from src.dataset import create_dataset_cifar
from src.config import config_gpu
......@@ -38,7 +36,7 @@ if __name__ == '__main__':
config = None
if args_opt.device_target == "GPU":
config = config_gpu
context.set_context(mode=context.GRAPH_MODE,
ms.set_context(mode=ms.GRAPH_MODE,
device_target="GPU", save_graphs=False)
dataset = create_dataset(dataset_path=args_opt.dataset_path,
do_train=False,
......@@ -47,7 +45,7 @@ if __name__ == '__main__':
batch_size=config.batch_size)
elif args_opt.device_target == "CPU":
config = config_cpu
context.set_context(mode=context.GRAPH_MODE,
ms.set_context(mode=ms.GRAPH_MODE,
device_target="CPU", save_graphs=False)
dataset = create_dataset_cifar(dataset_path=args_opt.dataset_path,
do_train=False,
......@@ -61,10 +59,10 @@ if __name__ == '__main__':
step_size = dataset.get_dataset_size()
if args_opt.checkpoint_path:
param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(args_opt.checkpoint_path)
ms.load_param_into_net(net, param_dict)
net.set_train(False)
model = Model(net, loss_fn=loss, metrics={'acc'})
model = ms.Model(net, loss_fn=loss, metrics={'acc'})
res = model.eval(dataset)
print("result:", res, "ckpt=", args_opt.checkpoint_path)
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 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.
......@@ -17,7 +17,7 @@ mobilenetv3 export mindir.
"""
import argparse
import numpy as np
from mindspore import context, Tensor, load_checkpoint, load_param_into_net, export
import mindspore as ms
from src.config import config_gpu
from src.config import config_cpu
from src.config import config_ascend
......@@ -35,20 +35,20 @@ if __name__ == '__main__':
cfg = None
if args_opt.device_target == "GPU":
cfg = config_gpu
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
ms.set_context(mode=ms.GRAPH_MODE, device_target="GPU")
elif args_opt.device_target == "CPU":
cfg = config_cpu
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
ms.set_context(mode=ms.GRAPH_MODE, device_target="CPU")
elif args_opt.device_target == "Ascend":
cfg = config_ascend
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
ms.set_context(mode=ms.GRAPH_MODE, device_target="Ascend")
else:
raise ValueError("Unsupported device_target.")
net = mobilenet_v3_large(num_classes=cfg.num_classes, activation="Softmax")
param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(args_opt.checkpoint_path)
ms.load_param_into_net(net, param_dict)
input_shp = [1, 3, cfg.image_height, cfg.image_width]
input_array = Tensor(np.random.uniform(-1.0, 1.0, size=input_shp).astype(np.float32))
export(net, input_array, file_name=args_opt.file_name, file_format=args_opt.file_format)
input_array = ms.Tensor(np.random.uniform(-1.0, 1.0, size=input_shp).astype(np.float32))
ms.export(net, input_array, file_name=args_opt.file_name, file_format=args_opt.file_format)
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 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.
......@@ -15,20 +15,17 @@
"""
create train or eval dataset.
"""
import mindspore.common.dtype as mstype
import mindspore as ms
import mindspore.dataset as ds
import mindspore.dataset.vision.c_transforms as C
import mindspore.dataset.transforms.c_transforms as C2
def create_dataset(dataset_path, do_train, config, device_target, repeat_num=1, batch_size=32, run_distribute=False):
def create_dataset(dataset_path, do_train, config, device_target, batch_size=32, run_distribute=False):
"""
create a train or eval dataset
Args:
dataset_path(string): the path of dataset.
do_train(bool): whether dataset is used for train or eval.
repeat_num(int): the repeat times of dataset. Default: 1
batch_size(int): the batch size of dataset. Default: 32
Returns:
......@@ -52,23 +49,24 @@ def create_dataset(dataset_path, do_train, config, device_target, repeat_num=1,
buffer_size = 1000
# define map operations
decode_op = C.Decode()
resize_crop_op = C.RandomCropDecodeResize(resize_height, scale=(0.08, 1.0), ratio=(0.75, 1.333))
horizontal_flip_op = C.RandomHorizontalFlip(prob=0.5)
resize_op = C.Resize(256)
center_crop = C.CenterCrop(resize_width)
rescale_op = C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4)
normalize_op = C.Normalize(mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
decode_op = ds.vision.c_transforms.Decode()
resize_crop_op = ds.vision.c_transforms.RandomCropDecodeResize(resize_height,
scale=(0.08, 1.0), ratio=(0.75, 1.333))
horizontal_flip_op = ds.vision.c_transforms.RandomHorizontalFlip(prob=0.5)
resize_op = ds.vision.c_transforms.Resize(256)
center_crop = ds.vision.c_transforms.CenterCrop(resize_width)
rescale_op = ds.vision.c_transforms.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4)
normalize_op = ds.vision.c_transforms.Normalize(mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
std=[0.229 * 255, 0.224 * 255, 0.225 * 255])
change_swap_op = C.HWC2CHW()
change_swap_op = ds.vision.c_transforms.HWC2CHW()
if do_train:
trans = [resize_crop_op, horizontal_flip_op, rescale_op, normalize_op, change_swap_op]
else:
trans = [decode_op, resize_op, center_crop, normalize_op, change_swap_op]
type_cast_op = C2.TypeCast(mstype.int32)
type_cast_op = ds.transforms.c_transforms.TypeCast(ms.int32)
data_set = data_set.map(operations=trans, input_columns="image", num_parallel_workers=8)
data_set = data_set.map(operations=type_cast_op, input_columns="label", num_parallel_workers=8)
......@@ -79,24 +77,18 @@ def create_dataset(dataset_path, do_train, config, device_target, repeat_num=1,
# apply batch operations
data_set = data_set.batch(batch_size, drop_remainder=True)
# apply dataset repeat operation
data_set = data_set.repeat(repeat_num)
return data_set
def create_dataset_cifar(dataset_path,
do_train,
repeat_num=1,
batch_size=32,
target="CPU"):
batch_size=32):
"""
create a train or evaluate cifar10 dataset
Args:
dataset_path(string): the path of dataset.
do_train(bool): whether dataset is used for train or eval.
repeat_num(int): the repeat times of dataset. Default: 1
batch_size(int): the batch size of dataset. Default: 32
target(str): the device target. Default: Ascend
Returns:
dataset
......@@ -107,24 +99,24 @@ def create_dataset_cifar(dataset_path,
# define map operations
if do_train:
trans = [
C.RandomCrop((32, 32), (4, 4, 4, 4)),
C.RandomHorizontalFlip(prob=0.5),
C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4),
C.Resize((224, 224)),
C.Rescale(1.0 / 255.0, 0.0),
C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
C.CutOut(112),
C.HWC2CHW()
ds.vision.c_transforms.RandomCrop((32, 32), (4, 4, 4, 4)),
ds.vision.c_transforms.RandomHorizontalFlip(prob=0.5),
ds.vision.c_transforms.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4),
ds.vision.c_transforms.Resize((224, 224)),
ds.vision.c_transforms.Rescale(1.0 / 255.0, 0.0),
ds.vision.c_transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
ds.vision.c_transforms.CutOut(112),
ds.vision.c_transforms.HWC2CHW()
]
else:
trans = [
C.Resize((224, 224)),
C.Rescale(1.0 / 255.0, 0.0),
C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
C.HWC2CHW()
ds.vision.c_transforms.Resize((224, 224)),
ds.vision.c_transforms.Rescale(1.0 / 255.0, 0.0),
ds.vision.c_transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
ds.vision.c_transforms.HWC2CHW()
]
type_cast_op = C2.TypeCast(mstype.int32)
type_cast_op = ds.transforms.c_transforms.TypeCast(ms.int32)
data_set = data_set.map(operations=type_cast_op,
input_columns="label",
......@@ -136,7 +128,4 @@ def create_dataset_cifar(dataset_path,
# apply batch operations
data_set = data_set.batch(batch_size, drop_remainder=True)
# apply dataset repeat operation
data_set = data_set.repeat(repeat_num)
return data_set
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 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.
......@@ -15,9 +15,9 @@
"""MobileNetV3 model define"""
from functools import partial
import numpy as np
import mindspore as ms
import mindspore.nn as nn
from mindspore.ops import operations as P
from mindspore import Tensor
import mindspore.ops as ops
__all__ = ['mobilenet_v3_large',
......@@ -71,7 +71,7 @@ class GlobalAvgPooling(nn.Cell):
def __init__(self, keep_dims=False):
super(GlobalAvgPooling, self).__init__()
self.mean = P.ReduceMean(keep_dims=keep_dims)
self.mean = ops.ReduceMean(keep_dims=keep_dims)
def construct(self, x):
x = self.mean(x, (2, 3))
......@@ -103,7 +103,7 @@ class SE(nn.Cell):
self.conv2 = nn.Conv2d(in_channels=num_mid, out_channels=num_out,
kernel_size=1, has_bias=True, pad_mode='pad')
self.act2 = Activation('hsigmoid')
self.mul = P.Mul()
self.mul = ops.Mul()
def construct(self, x):
out = self.pool(x)
......@@ -197,7 +197,7 @@ class ResUnit(nn.Cell):
padding=0, act_type=act_type, use_act=False)
if num_in != num_out or stride != 1:
self.use_short_cut_conv = False
self.add = P.Add() if self.use_short_cut_conv else None
self.add = ops.Add() if self.use_short_cut_conv else None
def construct(self, x):
"""construct"""
......@@ -292,13 +292,13 @@ class MobileNetV3(nn.Cell):
self.output = nn.Conv2d(in_channels=model_cfgs['cls_ch_expand'],
out_channels=num_classes,
kernel_size=1, has_bias=True, pad_mode='pad')
self.squeeze = P.Squeeze(axis=(2, 3))
self.squeeze = ops.Squeeze(axis=(2, 3))
if activation != "None":
self.need_activation = True
if activation == "Sigmoid":
self.activation = P.Sigmoid()
self.activation = ops.Sigmoid()
elif activation == "Softmax":
self.activation = P.Softmax()
self.activation = ops.Softmax()
else:
raise NotImplementedError(f"The activation {activation} not in [Sigmoid, Softmax].")
......@@ -339,22 +339,21 @@ class MobileNetV3(nn.Cell):
for _, m in self.cells_and_names():
if isinstance(m, (nn.Conv2d)):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_data(Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.set_data(ms.Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32")))
if m.bias is not None:
m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
ms.numpy.zeros(m.bias.data.shape, dtype="float32"))
elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_data(
Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
ms.Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_data(
Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
ms.numpy.zeros(m.beta.data.shape, dtype="float32"))
elif isinstance(m, nn.Dense):
m.weight.set_data(Tensor(np.random.normal(
m.weight.set_data(ms.Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None:
m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
m.bias.set_data(ms.numpy.zeros(m.bias.data.shape, dtype="float32"))
def mobilenet_v3(model_name, **kwargs):
......
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 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.
......@@ -19,21 +19,10 @@ import argparse
import ast
import numpy as np
from mindspore import context
from mindspore import Tensor
from mindspore import nn
from mindspore.nn.optim.momentum import Momentum
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
from mindspore.nn.loss.loss import LossBase
from mindspore.ops import operations as P
from mindspore.ops import functional as F
from mindspore.common import dtype as mstype
from mindspore.train.model import Model
from mindspore.context import ParallelMode
import mindspore as ms
import mindspore.nn as nn
import mindspore.ops as ops
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, Callback
from mindspore.train.loss_scale_manager import FixedLossScaleManager
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.common import set_seed
from mindspore.communication.management import init, get_group_size, get_rank
from src.dataset import create_dataset
......@@ -43,7 +32,7 @@ from src.config import config_gpu
from src.config import config_cpu
from src.mobilenetV3 import mobilenet_v3_large
set_seed(1)
ms.set_seed(1)
parser = argparse.ArgumentParser(description='Image classification')
parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
......@@ -53,23 +42,23 @@ parser.add_argument('--run_distribute', type=ast.literal_eval, default=False, he
args_opt = parser.parse_args()
if args_opt.device_target == "GPU":
context.set_context(mode=context.GRAPH_MODE,
ms.set_context(mode=ms.GRAPH_MODE,
device_target="GPU",
save_graphs=False)
if args_opt.run_distribute:
init()
context.set_auto_parallel_context(device_num=get_group_size(),
parallel_mode=ParallelMode.DATA_PARALLEL,
ms.set_auto_parallel_context(device_num=get_group_size(),
parallel_mode=ms.ParallelMode.DATA_PARALLEL,
gradients_mean=True)
elif args_opt.device_target == "CPU":
context.set_context(mode=context.GRAPH_MODE,
ms.set_context(mode=ms.GRAPH_MODE,
device_target="CPU",
save_graphs=False)
else:
raise ValueError("Unsupported device_target.")
class CrossEntropyWithLabelSmooth(LossBase):
class CrossEntropyWithLabelSmooth(nn.LossBase):
"""
CrossEntropyWith LabelSmooth.
......@@ -86,16 +75,15 @@ class CrossEntropyWithLabelSmooth(LossBase):
def __init__(self, smooth_factor=0., num_classes=1000):
super(CrossEntropyWithLabelSmooth, self).__init__()
self.onehot = P.OneHot()
self.on_value = Tensor(1.0 - smooth_factor, mstype.float32)
self.off_value = Tensor(1.0 * smooth_factor /
(num_classes - 1), mstype.float32)
self.onehot = ops.OneHot()
self.on_value = ms.Tensor(1.0 - smooth_factor, ms.float32)
self.off_value = ms.Tensor(1.0 * smooth_factor / (num_classes - 1), ms.float32)
self.ce = nn.SoftmaxCrossEntropyWithLogits()
self.mean = P.ReduceMean(False)
self.cast = P.Cast()
self.mean = ops.ReduceMean(False)
self.cast = ops.Cast()
def construct(self, logit, label):
one_hot_label = self.onehot(self.cast(label, mstype.int32), F.shape(logit)[1],
one_hot_label = self.onehot(self.cast(label, ms.int32), ops.shape(logit)[1],
self.on_value, self.off_value)
out_loss = self.ce(logit, one_hot_label)
out_loss = self.mean(out_loss, 0)
......@@ -113,7 +101,7 @@ class Monitor(Callback):
None
Examples:
>>> Monitor(100,lr_init=Tensor([0.05]*100).asnumpy())
>>> Monitor(100,lr_init=ms.Tensor([0.05]*100).asnumpy())
"""
def __init__(self, lr_init=None):
......@@ -142,9 +130,9 @@ class Monitor(Callback):
step_mseconds = (time.time() - self.step_time) * 1000
step_loss = cb_params.net_outputs
if isinstance(step_loss, (tuple, list)) and isinstance(step_loss[0], Tensor):
if isinstance(step_loss, (tuple, list)) and isinstance(step_loss[0], ms.Tensor):
step_loss = step_loss[0]
if isinstance(step_loss, Tensor):
if isinstance(step_loss, ms.Tensor):
step_loss = np.mean(step_loss.asnumpy())
self.losses.append(step_loss)
......@@ -160,7 +148,7 @@ if __name__ == '__main__':
config_ = None
if args_opt.device_target == "GPU":
config_ = config_gpu
context.set_context(enable_graph_kernel=True)
ms.set_context(enable_graph_kernel=True)
elif args_opt.device_target == "CPU":
config_ = config_cpu
else:
......@@ -176,7 +164,7 @@ if __name__ == '__main__':
loss = CrossEntropyWithLabelSmooth(
smooth_factor=config_.label_smooth, num_classes=config_.num_classes)
else:
loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
# define dataset
epoch_size = config_.epoch_size
if args_opt.device_target == "GPU":
......@@ -184,7 +172,6 @@ if __name__ == '__main__':
do_train=True,
config=config_,
device_target=args_opt.device_target,
repeat_num=1,
batch_size=config_.batch_size,
run_distribute=args_opt.run_distribute)
elif args_opt.device_target == "CPU":
......@@ -196,23 +183,22 @@ if __name__ == '__main__':
step_size = dataset.get_dataset_size()
# resume
if args_opt.pre_trained:
param_dict = load_checkpoint(args_opt.pre_trained)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(args_opt.pre_trained)
ms.load_param_into_net(net, param_dict)
# define optimizer
loss_scale = FixedLossScaleManager(
loss_scale = ms.FixedLossScaleManager(
config_.loss_scale, drop_overflow_update=False)
lr = Tensor(get_lr(global_step=0,
lr = ms.Tensor(get_lr(global_step=0,
lr_init=0,
lr_end=0,
lr_max=config_.lr,
warmup_epochs=config_.warmup_epochs,
total_epochs=epoch_size,
steps_per_epoch=step_size))
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, config_.momentum,
opt = nn.Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, config_.momentum,
config_.weight_decay, config_.loss_scale)
# define model
model = Model(net, loss_fn=loss, optimizer=opt,
loss_scale_manager=loss_scale)
model = ms.Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale)
cb = [Monitor(lr_init=lr.asnumpy())]
if args_opt.run_distribute and args_opt.device_target != "CPU":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment