diff --git a/example/googlenet_cifar10/README.md b/example/googlenet_cifar10/README.md new file mode 100755 index 0000000000000000000000000000000000000000..fbadab141d54a959b3dc67f8c5296bc4386691ec --- /dev/null +++ b/example/googlenet_cifar10/README.md @@ -0,0 +1,106 @@ +# Googlenet Example + +## Description + +This example is for Googlenet model training and evaluation. + +## Requirements + +- Install [MindSpore](https://www.mindspore.cn/install/en). + +- Download the dataset [CIFAR-10](http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz). + +> Unzip the CIFAR-10 dataset to any path you want and the folder structure should be as follows: +> ``` +> . +> ├── cifar-10-batches-bin # train dataset +> └── cifar-10-verify-bin # infer dataset +> ``` + +## Running the Example + +### Training + +``` +python train.py --data_path=your_data_path --device_id=6 > out.train.log 2>&1 & +``` +The python command above will run in the background, you can view the results through the file `out.train.log`. + +After training, you'll get some checkpoint files under the script folder by default. + +You will get the loss value as following: +``` +# grep "loss is " out.train.log +epoch: 1 step: 390, loss is 1.4842823 +epcoh: 2 step: 390, loss is 1.0897788 +... +``` + +### Evaluation + +``` +python eval.py --data_path=your_data_path --device_id=6 --checkpoint_path=./train_googlenet_cifar10-125-390.ckpt > out.eval.log 2>&1 & +``` +The above python command will run in the background, you can view the results through the file `out.eval.log`. + +You will get the accuracy as following: +``` +# grep "result: " out.eval.log +result: {'acc': 0.934} +``` + +### Distribute Training +``` +sh run_distribute_train.sh rank_table.json your_data_path +``` +The above shell script will run distribute training in the background, you can view the results through the file `train_parallel[X]/log`. + +You will get the loss value as following: +``` +# grep "result: " train_parallel*/log +train_parallel0/log:epoch: 1 step: 48, loss is 1.4302931 +train_parallel0/log:epcoh: 2 step: 48, loss is 1.4023874 +... +train_parallel1/log:epoch: 1 step: 48, loss is 1.3458025 +train_parallel1/log:epcoh: 2 step: 48, loss is 1.3729336 +... +... +``` +> About rank_table.json, you can refer to the [distributed training tutorial](https://www.mindspore.cn/tutorial/en/master/advanced_use/distributed_training.html). + +## Usage: + +### Training +``` +usage: train.py [--device_target TARGET][--data_path DATA_PATH] + [--device_id DEVICE_ID] + +parameters/options: + --device_target the training backend type, default is Ascend. + --data_path the storage path of dataset + --device_id the device which used to train model. + +``` + +### Evaluation + +``` +usage: eval.py [--device_target TARGET][--data_path DATA_PATH] + [--device_id DEVICE_ID][--checkpoint_path CKPT_PATH] + +parameters/options: + --device_target the evaluation backend type, default is Ascend. + --data_path the storage path of datasetd + --device_id the device which used to evaluate model. + --checkpoint_path the checkpoint file path used to evaluate model. +``` + +### Distribute Training + +``` +Usage: sh run_distribute_train.sh [MINDSPORE_HCCL_CONFIG_PATH] [DATA_PATH] + +parameters/options: + MINDSPORE_HCCL_CONFIG_PATH HCCL configuration file path. + DATA_PATH the storage path of dataset. +``` diff --git a/example/googlenet_cifar10/config.py b/example/googlenet_cifar10/config.py new file mode 100644 index 0000000000000000000000000000000000000000..4b134f68da3e7f548c36b359673bd29ab679f48d --- /dev/null +++ b/example/googlenet_cifar10/config.py @@ -0,0 +1,31 @@ +# 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. +# ============================================================================ +""" +network config setting, will be used in main.py +""" +from easydict import EasyDict as edict + +cifar_cfg = edict({ + 'num_classes': 10, + 'lr_init': 0.1, + 'batch_size': 128, + 'epoch_size': 125, + 'momentum': 0.9, + 'weight_decay': 5e-4, + 'buffer_size': 10, + 'image_height': 224, + 'image_width': 224, + 'keep_checkpoint_max': 10 +}) diff --git a/example/googlenet_cifar10/dataset.py b/example/googlenet_cifar10/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..e7b6abfb563c98d8e64454657fc5e9733d1274a7 --- /dev/null +++ b/example/googlenet_cifar10/dataset.py @@ -0,0 +1,67 @@ +# 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. +# ============================================================================ +""" +Data operations, will be used in train.py and eval.py +""" +import os + +import mindspore.common.dtype as mstype +import mindspore.dataset as ds +import mindspore.dataset.transforms.c_transforms as C +import mindspore.dataset.transforms.vision.c_transforms as vision +from config import cifar_cfg as cfg + + +def create_dataset(data_home, repeat_num=1, training=True): + """Data operations.""" + ds.config.set_seed(1) + data_dir = os.path.join(data_home, "cifar-10-batches-bin") + if not training: + data_dir = os.path.join(data_home, "cifar-10-verify-bin") + + rank_size = int(os.environ.get("RANK_SIZE")) if os.environ.get("RANK_SIZE") else None + rank_id = int(os.environ.get("RANK_ID")) if os.environ.get("RANK_ID") else None + data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id) + + resize_height = cfg.image_height + resize_width = cfg.image_width + + # define map operations + random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT + random_horizontal_op = vision.RandomHorizontalFlip() + resize_op = vision.Resize((resize_height, resize_width)) # interpolation default BILINEAR + normalize_op = vision.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) + changeswap_op = vision.HWC2CHW() + type_cast_op = C.TypeCast(mstype.int32) + + c_trans = [] + if training: + c_trans = [random_crop_op, random_horizontal_op] + c_trans += [resize_op, normalize_op, changeswap_op] + + # apply map operations on images + data_set = data_set.map(input_columns="label", operations=type_cast_op) + data_set = data_set.map(input_columns="image", operations=c_trans) + + # apply repeat operations + data_set = data_set.repeat(repeat_num) + + # apply shuffle operations + data_set = data_set.shuffle(buffer_size=10) + + # apply batch operations + data_set = data_set.batch(batch_size=cfg.batch_size, drop_remainder=True) + + return data_set diff --git a/example/googlenet_cifar10/eval.py b/example/googlenet_cifar10/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..8674e97e1e9c7903d3b283a8530df3b1246af3c7 --- /dev/null +++ b/example/googlenet_cifar10/eval.py @@ -0,0 +1,56 @@ +# 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. +# ============================================================================ +""" +##############test googlenet example on cifar10################# +python eval.py --data_path=$DATA_HOME --device_id=$DEVICE_ID +""" +import argparse + +import mindspore.nn as nn +from mindspore import context +from mindspore.model_zoo.googlenet import GooGLeNet +from mindspore.nn.optim.momentum import Momentum +from mindspore.train.model import Model +from mindspore.train.serialization import load_checkpoint, load_param_into_net + +import dataset +from config import cifar_cfg as cfg + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Cifar10 classification') + parser.add_argument('--device_target', type=str, default='Ascend', choices=['Ascend', 'GPU'], + help='device where the code will be implemented. (Default: Ascend)') + parser.add_argument('--data_path', type=str, default='./cifar', help='path where the dataset is saved') + parser.add_argument('--checkpoint_path', type=str, default=None, help='checkpoint file path.') + parser.add_argument('--device_id', type=int, default=None, help='device id of GPU or Ascend. (Default: None)') + args_opt = parser.parse_args() + + context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target) + context.set_context(device_id=args_opt.device_id) + context.set_context(enable_mem_reuse=True) + + 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) + loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False) + model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'}) + + param_dict = load_checkpoint(args_opt.checkpoint_path) + load_param_into_net(net, param_dict) + net.set_train(False) + dataset = dataset.create_dataset(args_opt.data_path, 1, False) + res = model.eval(dataset) + print("result: ", res) diff --git a/example/googlenet_cifar10/run_distribute_train.sh b/example/googlenet_cifar10/run_distribute_train.sh new file mode 100755 index 0000000000000000000000000000000000000000..c9b8dfc48f98cea333f7912942355c43e6bf6abf --- /dev/null +++ b/example/googlenet_cifar10/run_distribute_train.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# 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. +# ============================================================================ + +if [ $# != 2 ] +then + echo "Usage: sh run_distribute_train.sh [MINDSPORE_HCCL_CONFIG_PATH] [DATA_PATH]" +exit 1 +fi + +if [ ! -f $1 ] +then + echo "error: MINDSPORE_HCCL_CONFIG_PATH=$1 is not a file" +exit 1 +fi + +if [ ! -d $2 ] +then + echo "error: DATA_PATH=$2 is not a directory" +exit 1 +fi + +ulimit -u unlimited +export DEVICE_NUM=8 +export RANK_SIZE=8 +export MINDSPORE_HCCL_CONFIG_PATH=$1 + +for((i=0; i<${DEVICE_NUM}; i++)) +do + export DEVICE_ID=$i + export RANK_ID=$i + rm -rf ./train_parallel$i + mkdir ./train_parallel$i + cp *.py ./train_parallel$i + cp *.sh ./train_parallel$i + cd ./train_parallel$i || exit + echo "start training for rank $RANK_ID, device $DEVICE_ID" + env > env.log + python train.py --data_path=$2 --device_id=$i &> log & + cd .. +done diff --git a/example/googlenet_cifar10/train.py b/example/googlenet_cifar10/train.py new file mode 100644 index 0000000000000000000000000000000000000000..442a2320e69a38c27289996e2956efc9d50bfdea --- /dev/null +++ b/example/googlenet_cifar10/train.py @@ -0,0 +1,100 @@ +# 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. +# ============================================================================ +""" +#################train googlent example on cifar10######################## +python train.py --data_path=$DATA_HOME --device_id=$DEVICE_ID +""" +import argparse +import os +import random + +import numpy as np + +import mindspore.nn as nn +from mindspore import Tensor +from mindspore import context +from mindspore.communication.management import init +from mindspore.model_zoo.googlenet import GooGLeNet +from mindspore.nn.optim.momentum import Momentum +from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor +from mindspore.train.model import Model, ParallelMode + + +from dataset import create_dataset +from config import cifar_cfg as cfg + +random.seed(1) +np.random.seed(1) + + +def lr_steps(global_step, lr_max=None, total_epochs=None, steps_per_epoch=None): + """Set learning rate.""" + lr_each_step = [] + total_steps = steps_per_epoch * total_epochs + decay_epoch_index = [0.3 * total_steps, 0.6 * total_steps, 0.8 * total_steps] + for i in range(total_steps): + if i < decay_epoch_index[0]: + lr_each_step.append(lr_max) + elif i < decay_epoch_index[1]: + lr_each_step.append(lr_max * 0.1) + elif i < decay_epoch_index[2]: + lr_each_step.append(lr_max * 0.01) + else: + lr_each_step.append(lr_max * 0.001) + current_step = global_step + lr_each_step = np.array(lr_each_step).astype(np.float32) + learning_rate = lr_each_step[current_step:] + + return learning_rate + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Cifar10 classification') + parser.add_argument('--device_target', type=str, default='Ascend', choices=['Ascend', 'GPU'], + help='device where the code will be implemented. (Default: Ascend)') + parser.add_argument('--data_path', type=str, default='./cifar', help='path where the dataset is saved') + parser.add_argument('--device_id', type=int, default=None, help='device id of GPU or Ascend. (Default: None)') + args_opt = parser.parse_args() + + context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target) + context.set_context(device_id=args_opt.device_id) + context.set_context(enable_task_sink=True) + context.set_context(enable_loop_sink=True) + context.set_context(enable_mem_reuse=True) + + device_num = int(os.environ.get("DEVICE_NUM", 1)) + if device_num > 1: + context.reset_auto_parallel_context() + context.set_auto_parallel_context(device_num=device_num, parallel_mode=ParallelMode.DATA_PARALLEL, + mirror_mean=True) + init() + + dataset = create_dataset(args_opt.data_path, cfg.epoch_size) + batch_num = dataset.get_dataset_size() + + net = GooGLeNet(num_classes=cfg.num_classes) + lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size, steps_per_epoch=batch_num) + opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), Tensor(lr), cfg.momentum, + weight_decay=cfg.weight_decay) + loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False) + model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'}, + amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None) + + config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5, keep_checkpoint_max=cfg.keep_checkpoint_max) + time_cb = TimeMonitor(data_size=batch_num) + ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./", config=config_ck) + loss_cb = LossMonitor() + model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb]) + print("train success") diff --git a/mindspore/model_zoo/googlenet.py b/mindspore/model_zoo/googlenet.py new file mode 100644 index 0000000000000000000000000000000000000000..de1d582eb57802a3a6ad7798d5ee64a6f31c61ff --- /dev/null +++ b/mindspore/model_zoo/googlenet.py @@ -0,0 +1,143 @@ +# 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. +# ============================================================================ +"""GoogleNet""" +import mindspore.nn as nn +from mindspore.common.initializer import TruncatedNormal +from mindspore.ops import operations as P + + +def weight_variable(): + """Weight variable.""" + return TruncatedNormal(0.02) + + +class Conv2dBlock(nn.Cell): + """ + Basic convolutional block + Args: + in_channles (int): Input channel. + out_channels (int): Output channel. + kernel_size (int): Input kernel size. Default: 1 + stride (int): Stride size for the first convolutional layer. Default: 1. + padding (int): Implicit paddings on both sides of the input. Default: 0. + pad_mode (int): Padding mode. Optional values are "same", "valid", "pad". Default: "same". + Returns: + Tensor, output tensor. + """ + + def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, pad_mode="same"): + super(Conv2dBlock, self).__init__() + self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, + padding=padding, pad_mode=pad_mode, weight_init=weight_variable(), + bias_init=False) + self.bn = nn.BatchNorm2d(out_channels, eps=0.001) + self.relu = nn.ReLU() + + def construct(self, x): + x = self.conv(x) + x = self.bn(x) + x = self.relu(x) + return x + + +class Inception(nn.Cell): + """ + Inception Block + """ + + def __init__(self, in_channels, n1x1, n3x3red, n3x3, n5x5red, n5x5, pool_planes): + super(Inception, self).__init__() + self.b1 = Conv2dBlock(in_channels, n1x1, kernel_size=1) + self.b2 = nn.SequentialCell([Conv2dBlock(in_channels, n3x3red, kernel_size=1), + Conv2dBlock(n3x3red, n3x3, kernel_size=3, padding=0)]) + self.b3 = nn.SequentialCell([Conv2dBlock(in_channels, n5x5red, kernel_size=1), + Conv2dBlock(n5x5red, n5x5, kernel_size=3, padding=0)]) + self.maxpool = P.MaxPoolWithArgmax(ksize=3, strides=1, padding="same") + self.b4 = Conv2dBlock(in_channels, pool_planes, kernel_size=1) + self.concat = P.Concat(axis=1) + + def construct(self, x): + branch1 = self.b1(x) + branch2 = self.b2(x) + branch3 = self.b3(x) + cell, argmax = self.maxpool(x) + branch4 = self.b4(cell) + _ = argmax + return self.concat((branch1, branch2, branch3, branch4)) + + +class GooGLeNet(nn.Cell): + """ + Googlenet architecture + """ + + def __init__(self, num_classes): + super(GooGLeNet, self).__init__() + self.conv1 = Conv2dBlock(3, 64, kernel_size=7, stride=2, padding=0) + self.maxpool1 = P.MaxPoolWithArgmax(ksize=3, strides=2, padding="same") + + self.conv2 = Conv2dBlock(64, 64, kernel_size=1) + self.conv3 = Conv2dBlock(64, 192, kernel_size=3, padding=0) + self.maxpool2 = P.MaxPoolWithArgmax(ksize=3, strides=2, padding="same") + + self.block3a = Inception(192, 64, 96, 128, 16, 32, 32) + self.block3b = Inception(256, 128, 128, 192, 32, 96, 64) + self.maxpool3 = P.MaxPoolWithArgmax(ksize=3, strides=2, padding="same") + + self.block4a = Inception(480, 192, 96, 208, 16, 48, 64) + self.block4b = Inception(512, 160, 112, 224, 24, 64, 64) + self.block4c = Inception(512, 128, 128, 256, 24, 64, 64) + self.block4d = Inception(512, 112, 144, 288, 32, 64, 64) + self.block4e = Inception(528, 256, 160, 320, 32, 128, 128) + self.maxpool4 = P.MaxPoolWithArgmax(ksize=2, strides=2, padding="same") + + self.block5a = Inception(832, 256, 160, 320, 32, 128, 128) + self.block5b = Inception(832, 384, 192, 384, 48, 128, 128) + + self.mean = P.ReduceMean(keep_dims=True) + self.dropout = nn.Dropout(keep_prob=0.8) + self.flatten = nn.Flatten() + self.classifier = nn.Dense(1024, num_classes, weight_init=weight_variable(), + bias_init=weight_variable()) + + + def construct(self, x): + x = self.conv1(x) + x, argmax = self.maxpool1(x) + + x = self.conv2(x) + x = self.conv3(x) + x, argmax = self.maxpool2(x) + + x = self.block3a(x) + x = self.block3b(x) + x, argmax = self.maxpool3(x) + + x = self.block4a(x) + x = self.block4b(x) + x = self.block4c(x) + x = self.block4d(x) + x = self.block4e(x) + x, argmax = self.maxpool4(x) + + x = self.block5a(x) + x = self.block5b(x) + + x = self.mean(x, (2, 3)) + x = self.flatten(x) + x = self.classifier(x) + + _ = argmax + return x