Browse Source

!10394 Modify resnet50_adv_pruning net

From: @zhanghuiyao
Reviewed-by: @linqingke,@oacjiewen
Signed-off-by: @linqingke
tags/v1.1.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
de63ffc952
5 changed files with 47 additions and 64 deletions
  1. +24
    -23
      model_zoo/research/cv/resnet50_adv_pruning/Readme.md
  2. +9
    -10
      model_zoo/research/cv/resnet50_adv_pruning/eval.py
  3. +1
    -19
      model_zoo/research/cv/resnet50_adv_pruning/src/config.py
  4. +6
    -5
      model_zoo/research/cv/resnet50_adv_pruning/src/pet_dataset.py
  5. +7
    -7
      model_zoo/research/cv/resnet50_adv_pruning/src/resnet_imgnet.py

+ 24
- 23
model_zoo/research/cv/resnet50_adv_pruning/Readme.md View File

@@ -4,14 +4,14 @@
- [Dataset](#dataset) - [Dataset](#dataset)
- [Environment Requirements](#environment-requirements) - [Environment Requirements](#environment-requirements)
- [Script Description](#script-description) - [Script Description](#script-description)
- [Script and Sample Code](#script-and-sample-code)
- [Training Process](#training-process)
- [Evaluation Process](#evaluation-process)
- [Evaluation](#evaluation)
- [Script and Sample Code](#script-and-sample-code)
- [Training Process](#training-process)
- [Evaluation Process](#evaluation-process)
- [Evaluation](#evaluation)
- [Model Description](#model-description) - [Model Description](#model-description)
- [Performance](#performance)
- [Training Performance](#evaluation-performance)
- [Inference Performance](#evaluation-performance)
- [Performance](#performance)
- [Training Performance](#evaluation-performance)
- [Inference Performance](#evaluation-performance)
- [Description of Random Situation](#description-of-random-situation) - [Description of Random Situation](#description-of-random-situation)
- [ModelZoo Homepage](#modelzoo-homepage) - [ModelZoo Homepage](#modelzoo-homepage)


@@ -26,20 +26,20 @@ The Adversarial Pruning method is a reliable neural network pruning algorithm by
Dataset used: [Oxford-IIIT Pet](https://www.robots.ox.ac.uk/~vgg/data/pets/) Dataset used: [Oxford-IIIT Pet](https://www.robots.ox.ac.uk/~vgg/data/pets/)


- Dataset size: 7049 colorful images in 1000 classes - Dataset size: 7049 colorful images in 1000 classes
- Train: 3680 images
- Test: 3369 images
- Train: 3680 images
- Test: 3369 images
- Data format: RGB images. - Data format: RGB images.
- Note: Data will be processed in src/dataset.py
- Note: Data will be processed in src/dataset.py


# [Environment Requirements](#contents) # [Environment Requirements](#contents)


- Hardware(Ascend/GPU) - Hardware(Ascend/GPU)
- Prepare hardware environment with Ascend or GPU processor. If you want to try Ascend, please send the [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) to ascend@huawei.com. Once approved, you can get the resources.
- Prepare hardware environment with Ascend or GPU processor. If you want to try Ascend, please send the [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) to ascend@huawei.com. Once approved, you can get the resources.
- Framework - Framework
- [MindSpore](https://www.mindspore.cn/install/en)
- [MindSpore](https://www.mindspore.cn/install/en)
- For more information, please check the resources below: - For more information, please check the resources below:
- [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
- [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
- [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
- [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)


# [Script description](#contents) # [Script description](#contents)


@@ -58,6 +58,7 @@ Dataset used: [Oxford-IIIT Pet](https://www.robots.ox.ac.uk/~vgg/data/pets/)
``` ```


## [Training process](#contents) ## [Training process](#contents)

To Be Done To Be Done


## [Eval process](#contents) ## [Eval process](#contents)
@@ -66,12 +67,11 @@ To Be Done


After installing MindSpore via the official website, you can start evaluation as follows: After installing MindSpore via the official website, you can start evaluation as follows:



### Launch ### Launch


```
# infer example
```bash
# infer example
Ascend: python eval.py --dataset_path ~/Pets/test.mindrecord --platform Ascend --checkpoint_path [CHECKPOINT_PATH] Ascend: python eval.py --dataset_path ~/Pets/test.mindrecord --platform Ascend --checkpoint_path [CHECKPOINT_PATH]
GPU: python eval.py --dataset_path ~/Pets/test.mindrecord --platform GPU --checkpoint_path [CHECKPOINT_PATH] GPU: python eval.py --dataset_path ~/Pets/test.mindrecord --platform GPU --checkpoint_path [CHECKPOINT_PATH]
``` ```
@@ -80,18 +80,18 @@ After installing MindSpore via the official website, you can start evaluation as


### Result ### Result


```
```python
result: {'acc': 0.8023984736985554} ckpt= ./resnet50-imgnet-0.65x-80.24.ckpt result: {'acc': 0.8023984736985554} ckpt= ./resnet50-imgnet-0.65x-80.24.ckpt

``` ```


# [Model Description](#contents) # [Model Description](#contents)


## [Performance](#contents) ## [Performance](#contents)


#### Evaluation Performance
### Evaluation Performance

#### ResNet50-0.65x on ImageNet2012


###### ResNet50-0.65x on ImageNet2012
| Parameters | | | Parameters | |
| -------------------------- | -------------------------------------- | | -------------------------- | -------------------------------------- |
| Model Version | ResNet50-0.65x | | Model Version | ResNet50-0.65x |
@@ -102,7 +102,8 @@ result: {'acc': 0.8023984736985554} ckpt= ./resnet50-imgnet-0.65x-80.24.ckpt
| FLOPs (G) | 2.1 | | FLOPs (G) | 2.1 |
| Accuracy (Top1) | 75.80 | | Accuracy (Top1) | 75.80 |


###### ResNet50-0.65x on Oxford-IIIT Pet
#### ResNet50-0.65x on Oxford-IIIT Pet

| Parameters | | | Parameters | |
| -------------------------- | -------------------------------------- | | -------------------------- | -------------------------------------- |
| Model Version | ResNet50-0.65x | | Model Version | ResNet50-0.65x |


+ 9
- 10
model_zoo/research/cv/resnet50_adv_pruning/eval.py View File

@@ -26,7 +26,7 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.common import dtype as mstype from mindspore.common import dtype as mstype


from src.pet_dataset import create_dataset from src.pet_dataset import create_dataset
from src.config import config_ascend, config_gpu
from src.config import cfg
from src.resnet_imgnet import resnet50 from src.resnet_imgnet import resnet50




@@ -40,14 +40,12 @@ args_opt = parser.parse_args()




if __name__ == '__main__': if __name__ == '__main__':
config_platform = None
config_platform = cfg
if args_opt.platform == "Ascend": if args_opt.platform == "Ascend":
config_platform = config_ascend
device_id = int(os.getenv('DEVICE_ID')) device_id = int(os.getenv('DEVICE_ID'))
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
device_id=device_id, save_graphs=False) device_id=device_id, save_graphs=False)
elif args_opt.platform == "GPU": elif args_opt.platform == "GPU":
config_platform = config_gpu
context.set_context(mode=context.GRAPH_MODE, context.set_context(mode=context.GRAPH_MODE,
device_target="GPU", save_graphs=False) device_target="GPU", save_graphs=False)
else: else:
@@ -55,12 +53,6 @@ if __name__ == '__main__':


loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')


if args_opt.platform == "Ascend":
net.to_float(mstype.float16)
for _, cell in net.cells_and_names():
if isinstance(cell, nn.Dense):
cell.to_float(mstype.float32)

dataset = create_dataset(dataset_path=args_opt.dataset_path, dataset = create_dataset(dataset_path=args_opt.dataset_path,
do_train=False, do_train=False,
config=config_platform, config=config_platform,
@@ -76,6 +68,13 @@ if __name__ == '__main__':


net = resnet50( net = resnet50(
rate=0.65, class_num=config_platform.num_classes, index=index) rate=0.65, class_num=config_platform.num_classes, index=index)

if args_opt.platform == "Ascend":
net.to_float(mstype.float16)
for _, cell in net.cells_and_names():
if isinstance(cell, nn.Dense):
cell.to_float(mstype.float32)

if args_opt.checkpoint_path: if args_opt.checkpoint_path:
param_dict = load_checkpoint(args_opt.checkpoint_path) param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict) load_param_into_net(net, param_dict)


+ 1
- 19
model_zoo/research/cv/resnet50_adv_pruning/src/config.py View File

@@ -17,25 +17,7 @@ network config setting, will be used in train.py and eval.py
""" """
from easydict import EasyDict as ed from easydict import EasyDict as ed


config_ascend = ed({
"num_classes": 438,
"image_height": 224,
"image_width": 224,
"batch_size": 256,
"epoch_size": 200,
"warmup_epochs": 1,
"lr": 0.02,
"momentum": 0.9,
"weight_decay": 4e-5,
"label_smooth": 0.1,
"loss_scale": 1024,
"save_checkpoint": True,
"save_checkpoint_epochs": 5,
"keep_checkpoint_max": 200,
"save_checkpoint_path": "./checkpoint",
})

config_gpu = ed({
cfg = ed({
"num_classes": 37, "num_classes": 37,
"image_height": 224, "image_height": 224,
"image_width": 224, "image_width": 224,


+ 6
- 5
model_zoo/research/cv/resnet50_adv_pruning/src/pet_dataset.py View File

@@ -18,10 +18,11 @@ create train or eval dataset.
import os import os
import mindspore.common.dtype as mstype import mindspore.common.dtype as mstype
import mindspore.dataset.engine as de import mindspore.dataset.engine as de
import mindspore.dataset.transforms.vision.c_transforms as C
import mindspore.dataset.transforms.vision.py_transforms as P
import mindspore.dataset.vision.c_transforms as C
import mindspore.dataset.vision.py_transforms as P
import mindspore.dataset.transforms.c_transforms as C2 import mindspore.dataset.transforms.c_transforms as C2
from mindspore.dataset.transforms.vision import Inter
import mindspore.dataset.transforms.py_transforms as P2
from mindspore.dataset.vision import Inter




def create_dataset(dataset_path, do_train, config, platform, repeat_num=1, batch_size=100): def create_dataset(dataset_path, do_train, config, platform, repeat_num=1, batch_size=100):
@@ -78,13 +79,13 @@ def create_dataset(dataset_path, do_train, config, platform, repeat_num=1, batch
center_crop_p = P.CenterCrop(224) center_crop_p = P.CenterCrop(224)
totensor = P.ToTensor() totensor = P.ToTensor()
normalize_p = P.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) normalize_p = P.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
composeop = P.ComposeOp(
composeop = P2.Compose(
[decode_p, resize_p, center_crop_p, totensor, normalize_p]) [decode_p, resize_p, center_crop_p, totensor, normalize_p])
if do_train: if do_train:
trans = [resize_crop_op, horizontal_flip_op, color_op, trans = [resize_crop_op, horizontal_flip_op, color_op,
rescale_op, normalize_op, change_swap_op] rescale_op, normalize_op, change_swap_op]
else: else:
trans = composeop()
trans = composeop
type_cast_op = C2.TypeCast(mstype.int32) type_cast_op = C2.TypeCast(mstype.int32)


ds = ds.map(input_columns="image", operations=trans, ds = ds.map(input_columns="image", operations=trans,


+ 7
- 7
model_zoo/research/cv/resnet50_adv_pruning/src/resnet_imgnet.py View File

@@ -327,21 +327,21 @@ class ResNet(nn.Cell):
for _, m in self.cells_and_names(): for _, m in self.cells_and_names():
if isinstance(m, (nn.Conv2d)): if isinstance(m, (nn.Conv2d)):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_parameter_data(Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32")))
m.weight.set_data(Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(
m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data(
m.gamma.set_data(
Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data(
m.beta.set_data(
Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
elif isinstance(m, nn.Dense): elif isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal(
m.weight.set_data(Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32"))) 0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(
m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))






Loading…
Cancel
Save