added densenet100

pull/13519/head
huangbo77 4 years ago
parent d7e7df3a68
commit 5765337238

@ -15,6 +15,7 @@ In order to facilitate developers to enjoy the benefits of MindSpore framework,
- [Official](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official)
- [Computer Vision](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv)
- [Image Classification](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv)
- [DenseNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet/README.md)
- [GoogleNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet/README.md)
- [ResNet50[benchmark]](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/resnet/README.md)
- [ResNet50_Quant](https://gitee.com/mindspore/mindspore/blob/master/model_zoo/official/cv/resnet50_quant/README.md)

@ -15,6 +15,7 @@
- [官方](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official)
- [计算机视觉](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv)
- [图像分类](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv)
- [DenseNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet/README.md)
- [GoogleNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet/README.md)
- [ResNet-50[基准]](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/resnet/README.md)
- [ResNet50_Quant](https://gitee.com/mindspore/mindspore/blob/master/model_zoo/official/cv/resnet50_quant/README.md)

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,7 +15,7 @@
"""
##############test densenet example#################
python eval.py --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/CHECKPOINT
python eval.py --net densenet121 --dataset imagenet --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/CHECKPOINT
"""
import os
@ -34,10 +34,6 @@ from mindspore.ops import functional as F
from mindspore.common import dtype as mstype
from src.utils.logging import get_logger
from src.datasets import classification_dataset
from src.network import DenseNet121
from src.config import config
class ParameterReduce(nn.Cell):
"""
@ -61,10 +57,13 @@ def parse_args(cloud_args=None):
"""
parser = argparse.ArgumentParser('mindspore classification test')
# network and dataset choices
parser.add_argument('--net', type=str, default='', help='Densenet Model, densenet100 or densenet121')
parser.add_argument('--dataset', type=str, default='', help='Dataset, either cifar10 or imagenet')
# dataset related
parser.add_argument('--data_dir', type=str, default='', help='eval data dir')
parser.add_argument('--num_classes', type=int, default=1000, help='num of classes in dataset')
parser.add_argument('--image_size', type=str, default='224,224', help='image size of the dataset')
# network related
parser.add_argument('--backbone', default='resnet50', help='backbone')
parser.add_argument('--pretrained', default='', type=str, help='fully path of pretrained model to load.'
@ -80,12 +79,21 @@ def parse_args(cloud_args=None):
parser.add_argument('--train_url', type=str, default="", help='train url')
# platform
parser.add_argument('--device_target', type=str, default='Ascend', choices=('Ascend', 'GPU'), help='device target')
parser.add_argument('--device_target', type=str, default='Ascend', choices=('Ascend', 'GPU', 'CPU'),
help='device target')
args, _ = parser.parse_known_args()
args = merge_args(args, cloud_args)
if args.net == "densenet100":
from src.config import config_100 as config
else:
from src.config import config_121 as config
args.per_batch_size = config.per_batch_size
args.image_size = config.image_size
args.num_classes = config.num_classes
args.image_size = list(map(int, args.image_size.split(',')))
return args
@ -151,7 +159,8 @@ def generate_results(model, rank, group_size, top1_correct, top5_correct, img_to
def test(cloud_args=None):
"""
network eval function. Get top1 and top5 ACC from classification.
network eval function. Get top1 and top5 ACC from classification for imagenet,
and top1 ACC for cifar10.
The result will be save at [./outputs] by default.
"""
args = parse_args(cloud_args)
@ -185,13 +194,23 @@ def test(cloud_args=None):
else:
args.models = [args.pretrained,]
if args.net == "densenet100":
from src.network.densenet import DenseNet100 as DenseNet
else:
from src.network.densenet import DenseNet121 as DenseNet
if args.dataset == "cifar10":
from src.datasets import classification_dataset_cifar10 as classification_dataset
else:
from src.datasets import classification_dataset_imagenet as classification_dataset
for model in args.models:
de_dataset = classification_dataset(args.data_dir, image_size=args.image_size,
per_batch_size=args.per_batch_size,
max_epoch=1, rank=args.rank, group_size=args.group_size,
mode='eval')
eval_dataloader = de_dataset.create_tuple_iterator()
network = DenseNet121(args.num_classes)
network = DenseNet(args.num_classes)
param_dict = load_checkpoint(model)
param_dict_new = {}
@ -240,15 +259,13 @@ def test(cloud_args=None):
img_tot = results[2, 0]
acc1 = 100.0 * top1_correct / img_tot
acc5 = 100.0 * top5_correct / img_tot
args.logger.info('after allreduce eval: top1_correct={}, tot={}, acc={:.2f}%'.format(top1_correct,
img_tot,
args.logger.info('after allreduce eval: top1_correct={}, tot={}, acc={:.2f}%'.format(top1_correct, img_tot,
acc1))
args.logger.info('after allreduce eval: top5_correct={}, tot={}, acc={:.2f}%'.format(top5_correct,
img_tot,
acc5))
if args.dataset == 'imagenet':
args.logger.info('after allreduce eval: top5_correct={}, tot={}, acc={:.2f}%'.format(top5_correct, img_tot,
acc5))
if args.is_distributed:
release()
if __name__ == "__main__":
test()

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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.
@ -20,14 +20,13 @@ from mindspore.common import dtype as mstype
from mindspore import context, Tensor
from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
from src.network import DenseNet121
from src.config import config
parser = argparse.ArgumentParser(description="densenet export")
parser = argparse.ArgumentParser(description="densenet121 export")
parser.add_argument("--net", type=str, default='', help="Densenet Model, densenet100 or densenet121")
parser.add_argument("--device_id", type=int, default=0, help="Device id")
parser.add_argument("--batch_size", type=int, default=32, help="batch size")
parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.")
parser.add_argument("--file_name", type=str, default="densenet121", help="output file name.")
parser.add_argument("--file_name", type=str, default="densenet", help="output file name.")
parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format")
parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend",
help="device target")
@ -37,8 +36,15 @@ context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
if args.device_target == "Ascend":
context.set_context(device_id=args.device_id)
if args.net == "densenet100":
from src.config import config_100 as config
from src.network.densenet import DenseNet100 as DenseNet
else:
from src.config import config_121 as config
from src.network.densenet import DenseNet121 as DenseNet
if __name__ == "__main__":
network = DenseNet121(config.num_classes)
network = DenseNet(config.num_classes)
param_dict = load_checkpoint(args.ckpt_file)

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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.
@ -13,9 +13,11 @@
# limitations under the License.
# ============================================================================
"""hub config."""
from src.network import DenseNet121
from src.network import DenseNet121, DenseNet100
def create_network(name, *args, **kwargs):
if name == 'densenet121':
return DenseNet121(*args, **kwargs)
if name == 'densenet100':
return DenseNet100(*args, **kwargs)
raise NotImplementedError(f"{name} is not implemented in the repo")

@ -1,5 +1,5 @@
#!/bin/bash
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,8 +16,8 @@
echo "=============================================================================================================="
echo "Please run the script as: "
echo "sh scripts/run_distribute_eval.sh DEVICE_NUM RANK_TABLE_FILE DATASET CKPT_PATH"
echo "for example: sh scripts/run_distribute_train.sh 8 /data/hccl.json /path/to/dataset /path/to/ckpt"
echo "sh scripts/run_distribute_eval.sh DEVICE_NUM RANK_TABLE_FILE NET_NAME DATASET_NAME DATASET CKPT_PATH"
echo "for example: sh scripts/run_distribute_train.sh 8 /data/hccl.json densenet121 imagenet /path/to/dataset /path/to/ckpt"
echo "It is better to use absolute path."
echo "================================================================================================================="
@ -25,8 +25,10 @@ echo "After running the script, the network runs in the background. The log will
export RANK_SIZE=$1
export RANK_TABLE_FILE=$2
DATASET=$3
CKPT_PATH=$4
NET_NAME=$3
DATASET_NAME=$4
DATASET=$5
CKPT_PATH=$6
for((i=0;i<RANK_SIZE;i++))
do
@ -40,9 +42,10 @@ do
echo "start inferring for rank $i, device $DEVICE_ID"
env > env.log
python eval.py \
--net=$NET_NAME \
--dataset=$DATASET_NAME \
--data_dir=$DATASET \
--pretrained=$CKPT_PATH > log.txt 2>&1 &
cd ../
done

@ -14,26 +14,26 @@
# limitations under the License.
# ============================================================================
if [ $# -lt 4 ]
if [ $# -lt 6 ]
then
echo "Usage: sh run_distribute_eval_gpu.sh [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] [CHECKPOINT_PATH]"
exit 1
echo "Usage: sh run_distribute_eval_gpu.sh [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [NET_NAME] [DATASET_NAME] [DATASET_PATH] [CHECKPOINT_PATH]"
exit 1
fi
if [ $1 -lt 1 ] && [ $1 -gt 8 ]
then
echo "error: DEVICE_NUM=$1 is not in (1-8)"
exit 1
exit 1
fi
export DEVICE_NUM=$1
export RANK_SIZE=$1
# check checkpoint file
if [ ! -f $4 ]
if [ ! -f $6 ]
then
echo "error: CHECKPOINT_PATH=$4 is not a file"
exit 1
echo "error: CHECKPOINT_PATH=$6 is not a file"
exit 1
fi
BASEPATH=$(cd "`dirname $0`" || exit; pwd)
@ -51,13 +51,17 @@ export CUDA_VISIBLE_DEVICES="$2"
if [ $1 -gt 1 ]
then
mpirun -n $1 --allow-run-as-root python3 ${BASEPATH}/../eval.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--device_target='GPU' \
--pretrained=$4 > eval.log 2>&1 &
--pretrained=$6 > eval.log 2>&1 &
else
python3 ${BASEPATH}/../eval.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--device_target='GPU' \
--pretrained=$4 > eval.log 2>&1 &
--pretrained=$6 > eval.log 2>&1 &
fi

@ -1,5 +1,5 @@
#!/bin/bash
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,8 +16,8 @@
echo "=============================================================================================================="
echo "Please run the script as: "
echo "sh scripts/run_distribute_train.sh DEVICE_NUM RANK_TABLE_FILE DATASET CKPT_FILE"
echo "for example: sh scripts/run_distribute_train.sh 8 /data/hccl.json /path/to/dataset ckpt_file"
echo "sh scripts/run_distribute_train.sh DEVICE_NUM RANK_TABLE_FILE NET_NAME DATASET_NAME DATASET CKPT_FILE"
echo "for example: sh scripts/run_distribute_train.sh 8 /data/hccl.json densenet121 imagenet /path/to/dataset ckpt_file"
echo "It is better to use absolute path."
echo "================================================================================================================="
@ -25,8 +25,10 @@ echo "After running the script, the network runs in the background. The log will
export RANK_SIZE=$1
export RANK_TABLE_FILE=$2
DATASET=$3
CKPT_FILE=$4
NET_NAME=$3
DATASET_NAME=$4
DATASET=$5
CKPT_FILE=$6
for((i=0;i<RANK_SIZE;i++))
do
@ -41,9 +43,9 @@ do
env > env.log
if [ -f $CKPT_FILE ]
then
python train.py --data_dir=$DATASET --pretrained=$CKPT_FILE > log.txt 2>&1 &
python train.py --net=$NET_NAME --dataset=$DATASET_NAME --data_dir=$DATASET --pretrained=$CKPT_FILE > log.txt 2>&1 &
else
python train.py --data_dir=$DATASET > log.txt 2>&1 &
python train.py --net=$NET_NAME --dataset=$DATASET_NAME --data_dir=$DATASET > log.txt 2>&1 &
fi
cd ../

@ -14,16 +14,16 @@
# limitations under the License.
# ============================================================================
if [ $# -lt 3 ]
if [ $# -lt 5 ]
then
echo "Usage: sh run_distribute_train_gpu.sh [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] [PRE_TRAINED](optional)"
exit 1
echo "Usage: sh run_distribute_train_gpu.sh [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [NET_NAME] [DATASET_NAME] [DATASET_PATH] [PRE_TRAINED](optional)"
exit 1
fi
if [ $1 -lt 1 ] && [ $1 -gt 8 ]
then
echo "error: DEVICE_NUM=$1 is not in (1-8)"
exit 1
exit 1
fi
export DEVICE_NUM=$1
@ -40,30 +40,38 @@ cd ../train || exit
export CUDA_VISIBLE_DEVICES="$2"
if [ -f $4 ] # pretrained ckpt
then
if [ -f $6 ] # pretrained ckpt
then
if [ $1 -gt 1 ]
then
mpirun -n $1 --allow-run-as-root python3 ${BASEPATH}/../train.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--device_target='GPU' \
--pretrained=$4 > train.log 2>&1 &
--pretrained=$6 > train.log 2>&1 &
else
python3 ${BASEPATH}/../train.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--is_distributed=0 \
--device_target='GPU' \
--pretrained=$4 > train.log 2>&1 &
--pretrained=$6 > train.log 2>&1 &
fi
else
if [ $1 -gt 1 ]
then
mpirun -n $1 --allow-run-as-root python3 ${BASEPATH}/../train.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--device_target='GPU' > train.log 2>&1 &
else
python3 ${BASEPATH}/../train.py \
--data_dir=$3 \
--net=$3 \
--dataset=$4 \
--data_dir=$5 \
--is_distributed=0 \
--device_target='GPU' > train.log 2>&1 &
fi

@ -0,0 +1,46 @@
#!/bin/bash
# Copyright 2021 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.
# ============================================================================
if [ $# -lt 4 ]
then
echo "Usage: sh run_eval_cpu.sh [NET_NAME] [DATASET_NAME] [DATASET_PATH] [CHECKPOINT_PATH]"
exit 1
fi
# check checkpoint file
if [ ! -f $4 ]
then
echo "error: CHECKPOINT_PATH=$4 is not a file"
exit 1
fi
BASEPATH=$(cd "`dirname $0`" || exit; pwd)
export PYTHONPATH=${BASEPATH}:$PYTHONPATH
if [ -d "../eval" ];
then
rm -rf ../eval
fi
mkdir ../eval
cd ../eval || exit
python ${BASEPATH}/../eval.py \
--net=$1 \
--dataset=$2 \
--data_dir=$3 \
--device_target='CPU' \
--is_distributed=0 \
--pretrained=$4 > eval.log 2>&1 &

@ -0,0 +1,49 @@
#!/bin/bash
# Copyright 2021 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.
# ============================================================================
if [ $# -lt 3 ]
then
echo "Usage: sh run_train_cpu.sh [NET_NAME] [DATASET_NAME] [DATASET_PATH] [PRE_TRAINED](optional)"
exit 1
fi
BASEPATH=$(cd "`dirname $0`" || exit; pwd)
export PYTHONPATH=${BASEPATH}:$PYTHONPATH
if [ -d "../train" ];
then
rm -rf ../train
fi
mkdir ../train
cd ../train || exit
if [ -f $4 ] # pretrained ckpt
then
python ${BASEPATH}/../train.py \
--net=$1 \
--dataset=$2 \
--data_dir=$3 \
--is_distributed=0 \
--device_target='CPU' \
--pretrained=$4 > train.log 2>&1 &
else
python ${BASEPATH}/../train.py \
--net=$1 \
--dataset=$2 \
--data_dir=$3 \
--is_distributed=0 \
--device_target='CPU' > train.log 2>&1 &
fi

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,7 +15,39 @@
"""config"""
from easydict import EasyDict as ed
config = ed({
#config for densenet100 and cifar10
config_100 = ed({
"image_size": '32, 32',
"num_classes": 10,
"lr": 0.1,
"lr_scheduler": 'exponential',
"lr_epochs": '150, 225, 300',
"lr_gamma": 0.1,
"eta_min": 0,
"T_max": 120,
"max_epoch": 300,
"per_batch_size": 64,
"warmup_epochs": 0,
"weight_decay": 0.0001,
"momentum": 0.9,
"is_dynamic_loss_scale": 0,
"loss_scale": 1024,
"label_smooth": 0,
"label_smooth_factor": 0.1,
"log_interval": 100,
"ckpt_interval": 3124,
"ckpt_path": 'outputs_cifar10/',
"is_save_on_master": 1,
"rank": 0,
"group_size": 1
})
# config for densenet121 and imagenet
config_121 = ed({
"image_size": '224,224',
"num_classes": 1000,
@ -38,7 +70,7 @@ config = ed({
"log_interval": 100,
"ckpt_interval": 50000,
"ckpt_path": 'outputs/',
"ckpt_path": 'outputs_imagenet/',
"is_save_on_master": 1,
"rank": 0,

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,6 +17,6 @@
read dataset for classification
"""
from .classification import classification_dataset
from .classification import classification_dataset_cifar10, classification_dataset_imagenet
__all__ = ["classification_dataset"]
__all__ = ["classification_dataset_cifar10", "classification_dataset_imagenet"]

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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.
@ -50,17 +50,10 @@ class TxtDataset():
return len(self.imgs)
def classification_dataset(data_dir, image_size, per_batch_size, max_epoch, rank, group_size,
mode='train',
input_mode='folder',
root='',
num_parallel_workers=None,
shuffle=None,
sampler=None,
class_indexing=None,
drop_remainder=True,
transform=None,
target_transform=None):
def classification_dataset_imagenet(data_dir, image_size, per_batch_size, max_epoch, rank, group_size, mode='train',
input_mode='folder', root='', num_parallel_workers=None, shuffle=None,
sampler=None, class_indexing=None, drop_remainder=True, transform=None,
target_transform=None):
"""
A function that returns a dataset for classification. The mode of input dataset could be "folder" or "txt".
If it is "folder", all images within one folder have the same label. If it is "txt", all paths of images
@ -88,7 +81,7 @@ def classification_dataset(data_dir, image_size, per_batch_size, max_epoch, rank
unique index starting from 0).
Examples:
>>> from src.datasets.classification import classification_dataset
>>> from src.datasets.classification import classification_dataset_imagenet
>>> # path to imagefolder directory. This directory needs to contain sub-directories which contain the images
>>> data_dir = "/path/to/imagefolder_directory"
>>> de_dataset = classification_dataset(data_dir, image_size=[224, 244],
@ -152,3 +145,77 @@ def classification_dataset(data_dir, image_size, per_batch_size, max_epoch, rank
de_dataset = de_dataset.repeat(1)
return de_dataset
def classification_dataset_cifar10(data_dir, image_size, per_batch_size, max_epoch, rank, group_size, mode='train',
num_parallel_workers=None, shuffle=None, sampler=None, drop_remainder=True,
transform=None, target_transform=None):
"""
A function that returns cifar10 dataset for classification.
Args:
data_dir (str): Path to the root directory that contains the dataset's bin files.
image_size (Union(int, sequence)): Size of the input images.
per_batch_size (int): the batch size of evey step during training.
max_epoch (int): the number of epochs.
rank (int): The shard ID within num_shards (default=None).
group_size (int): Number of shards that the dataset should be divided
into (default=None).
mode (str): "train" or others. Default: " train".
input_mode (str): The form of the input dataset. "folder" or "txt". Default: "folder".
root (str): the images path for "input_mode="txt"". Default: " ".
num_parallel_workers (int): Number of workers to read the data. Default: None.
shuffle (bool): Whether or not to perform shuffle on the dataset
(default=None, performs shuffle).
sampler (Sampler): Object used to choose samples from the dataset. Default: None.
Examples:
>>> from src.datasets.classification import classification_dataset_cifar10
>>> # path to imagefolder directory. This directory needs to contain bin files of data.
>>> data_dir = "/path/to/datafolder_directory"
>>> de_dataset = classification_dataset_cifar10(data_dir, image_size=[32, 32],
>>> per_batch_size=64, max_epoch=100,
>>> rank=0, group_size=1)
"""
mean = [0.5 * 255, 0.5 * 255, 0.5 * 255]
std = [0.5 * 255, 0.5 * 255, 0.5 * 255]
if transform is None:
if mode == 'train':
transform_img = [
vision_C.RandomCrop(image_size, padding=4),
vision_C.RandomHorizontalFlip(prob=0.5),
vision_C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4),
vision_C.Normalize(mean=mean, std=std),
vision_C.HWC2CHW()
]
else:
transform_img = [
vision_C.Normalize(mean=mean, std=std),
vision_C.HWC2CHW()
]
else:
transform_img = transform
if target_transform is None:
transform_label = [
normal_C.TypeCast(mstype.int32)
]
else:
transform_label = target_transform
de_dataset = de.Cifar10Dataset(data_dir, num_parallel_workers=num_parallel_workers, shuffle=shuffle,
sampler=sampler, num_shards=group_size,
shard_id=rank)
de_dataset = de_dataset.map(input_columns="image", num_parallel_workers=8, operations=transform_img)
de_dataset = de_dataset.map(input_columns="label", num_parallel_workers=8, operations=transform_label)
columns_to_project = ["image", "label"]
de_dataset = de_dataset.project(columns=columns_to_project)
de_dataset = de_dataset.batch(per_batch_size, drop_remainder=drop_remainder)
de_dataset = de_dataset.repeat(1)
return de_dataset

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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,4 +15,4 @@
"""
densenet network
"""
from .densenet import DenseNet121
from .densenet import DenseNet121, DenseNet100

@ -1,4 +1,4 @@
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2021 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.
@ -25,7 +25,7 @@ from mindspore.ops import operations as P
from mindspore.common import initializer as init
from src.utils.var_init import default_recurisive_init, KaimingNormal
__all__ = ["DenseNet121"]
__all__ = ["DenseNet121", "DenseNet100"]
class GlobalAvgPooling(nn.Cell):
"""
@ -123,13 +123,17 @@ class _Transition(nn.Cell):
"""
the transition layer
"""
def __init__(self, num_input_features, num_output_features):
def __init__(self, num_input_features, num_output_features, avgpool=False):
super(_Transition, self).__init__()
if avgpool:
poollayer = nn.AvgPool2d(kernel_size=2, stride=2)
else:
poollayer = nn.MaxPool2d(kernel_size=2, stride=2)
self.features = nn.SequentialCell(OrderedDict([
('norm', nn.BatchNorm2d(num_input_features)),
('relu', nn.ReLU()),
('conv', conv1x1(num_input_features, num_output_features)),
('pool', nn.MaxPool2d(kernel_size=2, stride=2))
('pool', poollayer)
]))
def construct(self, x):
@ -142,17 +146,23 @@ class Densenet(nn.Cell):
"""
__constants__ = ['features']
def __init__(self, growth_rate, block_config, num_init_features, bn_size=4, drop_rate=0):
def __init__(self, growth_rate, block_config, num_init_features=None, bn_size=4, drop_rate=0):
super(Densenet, self).__init__()
layers = OrderedDict()
layers['conv0'] = conv7x7(3, num_init_features, stride=2, padding=3)
layers['norm0'] = nn.BatchNorm2d(num_init_features)
layers['relu0'] = nn.ReLU()
layers['pool0'] = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='same')
if num_init_features:
layers['conv0'] = conv7x7(3, num_init_features, stride=2, padding=3)
layers['norm0'] = nn.BatchNorm2d(num_init_features)
layers['relu0'] = nn.ReLU()
layers['pool0'] = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='same')
num_features = num_init_features
else:
layers['conv0'] = conv3x3(3, growth_rate*2, stride=1, padding=1)
layers['norm0'] = nn.BatchNorm2d(growth_rate*2)
layers['relu0'] = nn.ReLU()
num_features = growth_rate * 2
# Each denseblock
num_features = num_init_features
for i, num_layers in enumerate(block_config):
block = _DenseBlock(
num_layers=num_layers,
@ -165,8 +175,12 @@ class Densenet(nn.Cell):
num_features = num_features + num_layers*growth_rate
if i != len(block_config)-1:
trans = _Transition(num_input_features=num_features,
num_output_features=num_features // 2)
if num_init_features:
trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2,
avgpool=False)
else:
trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2,
avgpool=True)
layers['transition%d'%(i+1)] = trans
num_features = num_features // 2
@ -184,6 +198,11 @@ class Densenet(nn.Cell):
def get_out_channels(self):
return self.out_channels
def _densenet100(**kwargs):
return Densenet(growth_rate=12, block_config=(16, 16, 16), **kwargs)
def _densenet121(**kwargs):
return Densenet(growth_rate=32, block_config=(6, 12, 24, 16), num_init_features=64, **kwargs)
@ -200,6 +219,38 @@ def _densenet201(**kwargs):
return Densenet(growth_rate=32, block_config=(6, 12, 48, 32), num_init_features=64, **kwargs)
class DenseNet100(nn.Cell):
"""
the densenet100 architecture
"""
def __init__(self, num_classes, include_top=True):
super(DenseNet100, self).__init__()
self.backbone = _densenet100()
out_channels = self.backbone.get_out_channels()
self.include_top = include_top
if self.include_top:
self.head = CommonHead(num_classes, out_channels)
default_recurisive_init(self)
for _, cell in self.cells_and_names():
if isinstance(cell, nn.Conv2d):
cell.weight.set_data(init.initializer(KaimingNormal(a=math.sqrt(5), mode='fan_out',
nonlinearity='relu'),
cell.weight.shape,
cell.weight.dtype))
elif isinstance(cell, nn.BatchNorm2d):
cell.gamma.set_data(init.initializer('ones', cell.gamma.shape))
cell.beta.set_data(init.initializer('zeros', cell.beta.shape))
elif isinstance(cell, nn.Dense):
cell.bias.set_data(init.initializer('zeros', cell.bias.shape))
def construct(self, x):
x = self.backbone(x)
if not self.include_top:
return x
x = self.head(x)
return x
class DenseNet121(nn.Cell):
"""

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save