Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIB: Add mindspore backend #169

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions examples/lib-samples/backend/mindspore/ResNet50/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Resnet Example with Mindspore Backend
This document describes how to use the mindspore backend to train Resnet-50 network with the cifar-10 dataset.

## Script Description

### Script and Sample Code
```shell
└──ResNet50
├── README.md
├── scripts
├── run_eval.sh # launch ascend evaluation
├── run_eval_cpu.sh # launch cpu evaluation
├── run_infer.sh # launch cpu inference
├── run_standalone_train.sh # launch ascend standalone training
├── run_standalone_train_cpu.sh # launch cpu training
├── src
├── config.py # parameter configuration
├── dataset.py # data preprocessing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move the way of data preprocessing for midnspore to sedna lib, instead of in examples? the other people may be want to use it when they develop a appliation based on mindspore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you think is the most appropriate ? sedna.datasource or sedna.backend.mindspore ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we develop preprocessing methods for each model, or develop a general preprocessing method?
In my assumption, for a certain scene (such as image classification), we can predefine several fixed preprocessing methods (such as normalize, resize), and the user only needs to pass some parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest not to integrate data preprocessing functions into Sedna Lib, for the reason:

  • There are various preprocessing functions, and they cannot be covered by Sedna.
  • The data preprocessing functions are always already defined in developer's training code or AI framework.

├── CrossEntropySmooth.py # loss definition for ImageNet2012 dataset
├── lr_generator.py # generate learning rate for each step
├── resnet.py # resnet backbone, including resnet50 and resnet101 and se-resnet50
├── inference.py # Entrance to inference
├── interface.py # Implements class "Estimator"
├── eval.py # Entrance to evaluation
├── train.py # Entrance to training
```

## Script Parameters

Parameters for both training and evaluation can be set in `config.py`.


```bash
"class_num": 10, # dataset class num
"batch_size": 32, # batch size of input tensor
"loss_scale": 1024, # loss scale
"momentum": 0.9, # momentum
"weight_decay": 1e-4, # weight decay
"epoch_size": 90, # only valid for taining, which is always 1 for inference
"pretrain_epoch_size": 0, # epoch size that model has been trained before loading pretrained checkpoint, actual training epoch size is equal to epoch_size minus pretrain_epoch_size
"save_checkpoint": True, # whether save checkpoint or not
"save_checkpoint_epochs": 5, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
"keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
"warmup_epochs": 5, # number of warmup epoch
"lr_decay_mode": "poly" # decay mode can be selected in steps, ploy and default
"lr_init": 0.01, # initial learning rate
"lr_end": 0.00001, # final learning rate
"lr_max": 0.1, # maximum learning rate
```

## Preparatory Stage
### Prepare Dataset
In this example, we need to prepare the cifar10 dataset in advance, and put it into `/home/sedna/examples/backend/mindspore/resnet/`.
```bash
cd /home/sedna/examples/lib-samples/backend/mindspore/ResNet50
wget http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
tar -zxvf cifar-10-binary.tar.gz
```
### Parameters
you can change the parameters of the model in `src/config.py`.

## Modeling Stage
This example support CPU and NPU, you can follow these steps for training, testing and inference.

### Training
* #### Running on CPU
```bash
bash scripts/run_standalone_train_cpu.sh [DATASET_PATH] [MODEL_SAVE_PATH]
# model_save_path must be ABSOLUTE PATH
# The log message would be showed in the terminal
# The ckpt file would be saved in [MODEL_SAVE_PATH]
```
* #### Runing on NPU
```bash
bash scripts/run_standalone_train.sh [DATASET_PATH] [MODEL_SAVE_PATH]
# [MODEL_SAVE_PATH] must be ABSOLUTE PATH
# The log message would be saved to scripts/train/log
# The ckpt file would be saved in [MODEL_SAVE_PATH]
```

### Evaluation
* #### Running on CPU
```bash
bash scripts/run_eval_cpu.sh [DATASET_PATH] [CHECKPOINT_PATH]
# [CHECKPOINT_PATH] must be ABSOLUTE PATH
# The log message would be saved to scripts/test/log
```
* #### Running on NPU
```bash
bash scripts/run_eval.sh [DATASET_PATH] [CHECKPOINT_PATH]
# [CHECKPOINT_PATH] must be ABSOLUTE PATH
# The log message would be saved to scripts/test/log
```

### Inference
```bash
bash scripts/run_infer.sh [IMAGE_PATH] [CHECKPOINT_PATH]
# [CHECKPOINT_PATH] must be ABSOLUTE PATH
# The log message would be saved to scripts/infer/log
```
37 changes: 37 additions & 0 deletions examples/lib-samples/backend/mindspore/ResNet50/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 resnet."""
import argparse
from mindspore.common import set_seed
from sedna.backend import set_backend
from interface import Estimator

parser = argparse.ArgumentParser(description='Image classification')
parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
parser.add_argument('--device_target', type=str, default='Ascend', choices=("Ascend", "GPU", "CPU"),
help="Device target, support Ascend, GPU and CPU.")
set_seed(1)


def main():
args_opt = parser.parse_args()
valid_data_path = args_opt.dataset_path
instance = set_backend(estimator=Estimator)
return instance.evaluate(valid_data_path, args_opt=args_opt)


if __name__ == '__main__':
main()
47 changes: 47 additions & 0 deletions examples/lib-samples/backend/mindspore/ResNet50/inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import argparse
import mindspore as ms
from mindspore import Tensor
import mindspore.dataset.vision.c_transforms as C
import numpy as np
import cv2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import cv2 before sedna

from sedna.backend import set_backend
from interface import Estimator

parser = argparse.ArgumentParser(description="resnet50 infer")
parser.add_argument('--image_path', type=str, default="")
parser.add_argument(
'--device_target',
type=str,
default="Ascend",
choices=(
"Ascend",
"CPU"),
help="Device target, support Ascend, CPU")
parser.add_argument('--checkpoint_path', type=str)


def preprocess():
resize = C.Resize((224, 224))
rescale = C.Rescale(1.0 / 255.0, 0.0)
normalize = C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
transpose = C.HWC2CHW()
return [resize, rescale, normalize, transpose]


def main():
args = parser.parse_args()

# read image and preprocess
img = cv2.imread(args.image_path)
data_preprocess = preprocess()
for method in data_preprocess:
img = method(img)
img = np.expand_dims(img, 0)
data = Tensor(img, ms.float32)

model = set_backend(estimator=Estimator)
return model.predict(data)


if __name__ == '__main__':
main()
Loading