diff --git a/research/cv/resnetv2/README_CN.md b/research/cv/resnetv2/README_CN.md index a71be274aa9b61416ffc25f90ab582051e26b51a..581ed07ff0704d70b8bd3b6b55c94207fcfa32b8 100644 --- a/research/cv/resnetv2/README_CN.md +++ b/research/cv/resnetv2/README_CN.md @@ -5,7 +5,7 @@ ResNet系列模型是在2015年提出的,该网络创新性的提出了残差结构,通过堆叠多个残差结构从而构建了ResNet网络。ResNet一定程度上解决了传统的卷积网络或全连接网络或多或少存在信息丢失的问题。通过将输入信息传递给输出,确保信息完整性,使得网络深度得以不断加深的同时避免了梯度消失或爆炸的影响。ResNetv2是何凯明团队在ResNet发表后,又进一步对其网络结构进行了改进和优化,通过推导证明了前向参数和反向梯度如果直接从Residual Block传递到下一个Residual Block而不用经过ReLU等操作,效果会更好。因此调整了激活层和BN层与卷积层的运算先后顺序,并经过实验验证在深度网络中ResNetv2会有更好的收敛效果。 -如下为MindSpore使用Cifar10/ImageNet2012数据集对ResNetv2_50/ResNetv2_101/ResNetv2_152进行训练的示例。 +如下为MindSpore使用Cifar10/Cifar100数据集对ResNetv2_50/ResNetv2_101/ResNetv2_152进行训练的示例。 ## 论文 @@ -17,7 +17,7 @@ ResNet系列模型是在2015年提出的,该网络创新性的提出了残差 # 数据集 -使用的数据集:[Cifar10](https://www.cs.toronto.edu/~kriz/cifar.html) +使用的数据集:[cifar10|cifar100](https://www.cs.toronto.edu/~kriz/cifar.html) - 数据集大小:共10个类、60,000个32*32彩色图像 - 训练集:50,000个图像 @@ -32,21 +32,6 @@ ResNet系列模型是在2015年提出的,该网络创新性的提出了残差 └─cifar-10-verify-bin ``` -使用的数据集:[ImageNet2012](http://www.image-net.org/) - -- 数据集大小:共1000个类、224*224彩色图像 - - 训练集:共1,281,167张图像 - - 测试集:共50,000张图像 -- 数据格式:JPEG - - 注:数据在dataset.py中处理。 -- 下载数据集,目录结构如下: - -```text -└─dataset - ├─ilsvrc # 训练数据集 - └─validation_preprocess # 评估数据集 -``` - # 环境要求 - 硬件 @@ -66,15 +51,15 @@ ResNet系列模型是在2015年提出的,该网络创新性的提出了残差 ```Shell # 分布式训练 用法: -bash scripts/run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [RANK_TABLE_FILE] [DATASET_PATH] +bash scripts/run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [RANK_TABLE_FILE] [DATASET_PATH] # 单机训练 用法: -bash scripts/run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] +bash scripts/run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] # 运行评估示例 用法: -bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] [CHECKPOINT_PATH] +bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] [CHECKPOINT_PATH] ``` - GPU处理器环境运行 @@ -82,15 +67,15 @@ bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagen ```shell # 分布式训练 用法: -bash scripts/run_distribute_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [RANK_TABLE_FILE] [DATASET_PATH] +bash scripts/run_distribute_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [RANK_TABLE_FILE] [DATASET_PATH] # 单机训练 用法: -bash scripts/run_standalone_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] +bash scripts/run_standalone_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] # 运行评估示例 用法: -bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] [CHECKPOINT_PATH] +bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] [CHECKPOINT_PATH] ``` # 脚本说明 @@ -110,7 +95,6 @@ bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|im ├── src ├── config.py # 参数配置 ├── dataset.py # 数据预处理 - ├── CrossEntropySmooth.py # ImageNet2012数据集的损失定义 ├── lr_generator.py # 生成每个步骤的学习率 └── resnetv2.py # ResNet骨干网络 ├── eval.py # 评估网络 @@ -143,29 +127,6 @@ bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|im "lr_max":0.1, # 最大学习率 ``` -- 配置ResNetv2_50和imagenet2012数据集。 - -```python -"class_num":1001, # 数据集类数 -"batch_size":64, # 输入张量的批次大小 -"loss_scale":1024, # 损失等级 -"momentum":0.9, # 动量优化器 -"weight_decay":1e-4, # 权重衰减 -"epoch_size":100, # 训练周期大小 -"save_checkpoint":True, # 是否保存检查点 -"save_checkpoint_epochs":5, # 两个检查点之间的周期间隔;默认情况下,最后一个检查点将在最后一个周期完成后保存 -"keep_checkpoint_max":10, # 只保存最后一个keep_checkpoint_max检查点 -"save_checkpoint_path":"./checkpoint", # 检查点相对于执行路径的保存路径 -"low_memory": True, # 显存不足时可设置为Ture,默认为False -"warmup_epochs":5, # 热身周期数 -"use_label_smooth":True, # 标签平滑 -"label_smooth_factor":0.1, # 标签平滑因子 -"lr_decay_mode":"cosine", # 用于生成学习率的衰减模式 -"lr_init":0.05, # 基础学习率 -"lr_end":0.0000001, # 最终学习率 -"lr_max":0.05, # 最大学习率 -``` - # 训练过程 ## 用法 @@ -175,11 +136,11 @@ bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|im ```Shell # 分布式训练 用法: -bash scripts/run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [RANK_TABLE_FILE] [DATASET_PATH] +bash scripts/run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [RANK_TABLE_FILE] [DATASET_PATH] # 单机训练 用法: -bash scripts/run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] +bash scripts/run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] ``` 分布式训练需要提前创建JSON格式的HCCL配置文件。 @@ -191,11 +152,11 @@ bash scripts/run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [ci ```shell # 分布式训练 用法: -bash scripts/run_distribute_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [RANK_TABLE_FILE] [DATASET_PATH] +bash scripts/run_distribute_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [RANK_TABLE_FILE] [DATASET_PATH] # 单机训练 用法: -bash scripts/run_standalone_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] +bash scripts/run_standalone_train_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] ``` ## 结果 @@ -213,19 +174,6 @@ epoch time: 4737.401 ms, per step time: 24.166 ms ... ``` -- 使用imagenet2012数据集训练ResNetv2_50 - -```text -# Ascend分布式训练结果 (8P) -epoch: 61 step: 2502, loss is 2.4235027 -epoch time: 813367.327 ms, per step time: 325.087 ms -epoch: 62 step: 2502, loss is 2.0396166 -epoch time: 813387.109 ms, per step time: 325.095 ms -epoch: 63 step: 2502, loss is 1.7643375 -epoch time: 813347.102 ms, per step time: 325.075 ms -... -``` - # 评估过程 ## 用法 @@ -235,7 +183,7 @@ epoch time: 813347.102 ms, per step time: 325.075 ms ```Shell # 评估 用法: -bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] [CHECKPOINT_PATH] +bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] [CHECKPOINT_PATH] ``` ### GPU处理器环境运行 @@ -243,7 +191,7 @@ bash scripts/run_eval.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagen ```shell # 运行评估示例 用法: -bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH] [CHECKPOINT_PATH] +bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH] [CHECKPOINT_PATH] ``` ## 结果 @@ -256,18 +204,12 @@ bash scripts/run_eval_gpu.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|im result: {'top_5_accuracy': 0.9988982371794872, 'top_1_accuracy': 0.9502283653846154} ``` -- 使用imagenet2012数据集评估ResNetv2_50 - -```text -result: {'top_1_accuracy': 0.7606515786082474, 'top_5_accuracy': 0.9271504510309279} -``` - ## 推理过程 ### [导出MindIR](#contents) ```shell -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] --dataset [cifar10|cifar100] ``` 参数ckpt_file为必填项, @@ -315,25 +257,6 @@ bash scripts/run_infer_310.sh [MINDIR_PATH] [DATASET] [DATA_PATH] [DEVICE_ID] | 微调检查点 | 188.36M(.ckpt文件) | | 脚本 | [链接](https://gitee.com/mindspore/models/tree/master/research/cv/resnetv2) | -#### ImageNet2012上的Resnetv2_50 - -| 参数 | Ascend 910 | -| ------------- | ------------------------------------------------------------ | -| 模型版本 | ResNetv2_50 | -| 资源 | Ascend 910;CPU:2.60GHz,192核;内存:755G | -| 上传日期 | 2021-05-6 ; | -| MindSpore版本 | 1.2.0 | -| 数据集 | ImageNet2012 | -| 训练参数 | epoch=90, steps per epoch=2502, batch_size=64 | -| 优化器 | Momentum | -| 损失函数 | Softmax交叉熵 | -| 输出 | 概率 | -| 损失 | 1.8290355 | -| 速度 | 325毫秒/步(8卡) | -| 总时长 | 20.3小时 | -| 微调检查点 | 195.9M(.ckpt文件) | -| 脚本 | [链接](https://gitee.com/mindspore/models/tree/master/research/cv/resnetv2) | - # 随机情况说明 dataset.py中设置了“create_dataset”函数内的种子,同时还使用了train.py中的随机种子。 diff --git a/research/cv/resnetv2/ascend310_infer/src/main.cc b/research/cv/resnetv2/ascend310_infer/src/main.cc index 00ef7813fdbc5841e12b2631c9ec2241d1246039..597764e5a22e8ccc84de6702712b3b8264039fa9 100644 --- a/research/cv/resnetv2/ascend310_infer/src/main.cc +++ b/research/cv/resnetv2/ascend310_infer/src/main.cc @@ -52,7 +52,7 @@ using mindspore::dataset::Execute; DEFINE_string(mindir_path, "", "mindir path"); DEFINE_string(dataset_path, ".", "dataset path"); -DEFINE_string(dataset, "imagenet2012", "dataset"); +DEFINE_string(dataset, "cifar10", "dataset"); DEFINE_int32(device_id, 0, "device id"); int main(int argc, char **argv) { @@ -102,9 +102,6 @@ int main(int argc, char **argv) { std::cout << "Start predict input files:" << all_files[i] <<std::endl; MSTensor image = ReadFileToTensor(all_files[i]); - if (FLAGS_dataset == "imagenet2012") { - SingleOp(image, &image); - } inputs.emplace_back(modelInputs[0].Name(), modelInputs[0].DataType(), modelInputs[0].Shape(), image.Data().get(), image.DataSize()); diff --git a/research/cv/resnetv2/create_imagenet2012_label.py b/research/cv/resnetv2/create_imagenet2012_label.py deleted file mode 100644 index d29a6719fd623343bc3065aa21d463bab116a7e8..0000000000000000000000000000000000000000 --- a/research/cv/resnetv2/create_imagenet2012_label.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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. -# ============================================================================ -"""create_imagenet2012_label""" -import os -import json -import argparse - -parser = argparse.ArgumentParser(description="resnet imagenet2012 label") -parser.add_argument("--img_path", type=str, required=True, help="imagenet2012 file path.") -args = parser.parse_args() - - -def create_label(file_path): - '''create imagenet2012 label''' - print("[WARNING] Create imagenet label. Currently only use for Imagenet2012!") - dirs = os.listdir(file_path) - file_list = [] - for file in dirs: - file_list.append(file) - file_list = sorted(file_list) - - total = 0 - img_label = {} - for i, file_dir in enumerate(file_list): - files = os.listdir(os.path.join(file_path, file_dir)) - for f in files: - img_label[f] = i - total += len(files) - - with open("imagenet_label.json", "w+") as label: - json.dump(img_label, label) - - print("[INFO] Completed! Total {} data.".format(total)) - - -if __name__ == '__main__': - create_label(args.img_path) diff --git a/research/cv/resnetv2/eval.py b/research/cv/resnetv2/eval.py index 426b121157349e95a7540d9397cd0e19ce51efbf..ab422f6261e0fda3ccab715e8b9486269d3480c8 100644 --- a/research/cv/resnetv2/eval.py +++ b/research/cv/resnetv2/eval.py @@ -20,12 +20,11 @@ from mindspore.common import set_seed from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits from mindspore.train.model import Model from mindspore.train.serialization import load_checkpoint, load_param_into_net -from src.CrossEntropySmooth import CrossEntropySmooth parser = argparse.ArgumentParser(description='Image classification') parser.add_argument('--net', type=str, default='resnetv2_50', help='Resnetv2 Model, resnetv2_50, resnetv2_101, resnetv2_152') -parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10, imagenet2012') +parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10, cifar100') 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('--device_num', type=int, default=1, help='Device num.') @@ -48,8 +47,6 @@ if args_opt.dataset == "cifar10": from src.dataset import create_dataset1 as create_dataset elif args_opt.dataset == "cifar100": from src.dataset import create_dataset2 as create_dataset -elif args_opt.dataset == 'imagenet2012': - from src.dataset import create_dataset3 as create_dataset # import config if args_opt.net == "resnetv2_50" or args_opt.net == "resnetv2_101" or args_opt.net == "resnetv2_152": @@ -57,8 +54,6 @@ if args_opt.net == "resnetv2_50" or args_opt.net == "resnetv2_101" or args_opt.n from src.config import config1 as config elif args_opt.dataset == 'cifar100': from src.config import config2 as config - elif args_opt.dataset == 'imagenet2012': - from src.config import config3 as config set_seed(1) @@ -89,13 +84,7 @@ if __name__ == '__main__': net.set_train(False) # define loss, model - if args_opt.dataset == "imagenet2012": - if not config.use_label_smooth: - config.label_smooth_factor = 0.0 - loss = CrossEntropySmooth(sparse=True, reduction='mean', - smooth_factor=config.label_smooth_factor, num_classes=config.class_num) - else: - loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') + loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') # define model model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'}) diff --git a/research/cv/resnetv2/export.py b/research/cv/resnetv2/export.py index 8aad4317432b8d3434aa952a982baf77773dfafa..22245a6c28879b2c03ff0a751e5414d2cd8ff5f1 100644 --- a/research/cv/resnetv2/export.py +++ b/research/cv/resnetv2/export.py @@ -21,9 +21,9 @@ from mindspore import Tensor, load_checkpoint, load_param_into_net, export, cont parser = argparse.ArgumentParser(description='resnet export') parser.add_argument('--net', type=str, default='resnetv2_50', help='Resnetv2 Model, resnetv2_50, resnetv2_101, resnetv2_152') -parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10, cifar100, imagenet2012') +parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10, cifar100') parser.add_argument("--device_id", type=int, default=0, help="Device id") -parser.add_argument("--batch_size", type=int, default=64, help="batch size") +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="resnetv2", help="output file name.") parser.add_argument('--width', type=int, default=32, help='input width') @@ -54,8 +54,6 @@ if __name__ == '__main__': from src.config import config1 as config elif args.dataset == 'cifar100': from src.config import config2 as config - elif args.dataset == 'imagenet2012': - raise ValueError("ImageNet2012 dataset not yet supported") else: raise ValueError("dataset is not support.") else: diff --git a/research/cv/resnetv2/postprocess.py b/research/cv/resnetv2/postprocess.py index 863774c95b28b062d1596f079abc8fa6527db92b..89b2adc64260353f682b9186c453ccded3051263 100644 --- a/research/cv/resnetv2/postprocess.py +++ b/research/cv/resnetv2/postprocess.py @@ -14,7 +14,6 @@ # ============================================================================ """postprocess""" import os -import json import argparse import numpy as np from mindspore.nn import Top1CategoricalAccuracy, Top5CategoricalAccuracy @@ -29,8 +28,6 @@ if args_opt.dataset == "cifar10": from src.config import config1 as config elif args_opt.dataset == "cifar100": from src.config import config2 as config -elif args_opt.dataset == 'imagenet2012': - from src.config import config3 as config else: raise ValueError("dataset is not support.") @@ -54,31 +51,6 @@ def cal_acc_cifar(result_path, label_path): print("top1 acc: ", top1_acc.eval()) print("top5 acc: ", top5_acc.eval()) -def cal_acc_imagenet(result_path, label_path): - '''calculate imagenet2012 accuracy''' - batch_size = 1 - files = os.listdir(result_path) - with open(label_path, "r") as label: - labels = json.load(label) - - top1 = 0 - top5 = 0 - total_data = len(files) - for file in files: - img_ids_name = file.split('_0.')[0] - data_path = os.path.join(result_path, img_ids_name + "_0.bin") - result = np.fromfile(data_path, dtype=np.float32).reshape(batch_size, config.class_num) - for batch in range(batch_size): - predict = np.argsort(-result[batch], axis=-1) - if labels[img_ids_name+".JPEG"] == predict[0]: - top1 += 1 - if labels[img_ids_name+".JPEG"] in predict[:5]: - top5 += 1 - print(f"Total data: {total_data}, top1 accuracy: {top1/total_data}, top5 accuracy: {top5/total_data}.") - if __name__ == '__main__': - if args_opt.dataset.lower() == "cifar10" or args_opt.dataset.lower() == "cifar100": - cal_acc_cifar(args_opt.result_path, args_opt.label_path) - else: - cal_acc_imagenet(args_opt.result_path, args_opt.label_path) + cal_acc_cifar(args_opt.result_path, args_opt.label_path) diff --git a/research/cv/resnetv2/preprocess.py b/research/cv/resnetv2/preprocess.py index 6ddbaa9b5764832be37c64cc80c1907b4c89a920..b0f2e14f3785249bd8b1bafb8cf20f2270c913e7 100644 --- a/research/cv/resnetv2/preprocess.py +++ b/research/cv/resnetv2/preprocess.py @@ -18,7 +18,7 @@ import argparse import numpy as np parser = argparse.ArgumentParser(description='preprocess') -parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10, imagenet2012') +parser.add_argument('--dataset', type=str, default='cifar10', help='Dataset, cifar10') parser.add_argument('--dataset_path', type=str, default="../cifar-10/cifar-10-verify-bin", help='Dataset path.') parser.add_argument('--output_path', type=str, default="./preprocess_Result", diff --git a/research/cv/resnetv2/scripts/run_distribute_train.sh b/research/cv/resnetv2/scripts/run_distribute_train.sh index 60b36498c7b802fcd23793c2dcae781b2ca3564a..0b07614b4d81b05a36a3b531743b7ba5b3eb0a34 100644 --- a/research/cv/resnetv2/scripts/run_distribute_train.sh +++ b/research/cv/resnetv2/scripts/run_distribute_train.sh @@ -16,7 +16,7 @@ if [ $# != 4 ] then - echo "Usage: bash run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [RANK_TABLE_FILE] [DATASET_PATH]" + echo "Usage: bash run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [RANK_TABLE_FILE] [DATASET_PATH]" exit 1 fi @@ -26,9 +26,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/scripts/run_distribute_train_gpu.sh b/research/cv/resnetv2/scripts/run_distribute_train_gpu.sh index 1e84deea82bb6ea8f753eceee21c166a45358264..dbe2a45352ef2d952fff6a362369588ee0520fde 100644 --- a/research/cv/resnetv2/scripts/run_distribute_train_gpu.sh +++ b/research/cv/resnetv2/scripts/run_distribute_train_gpu.sh @@ -16,7 +16,7 @@ if [ $# != 3 ] then - echo "Usage: bash run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH]" + echo "Usage: bash run_distribute_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH]" exit 1 fi @@ -26,9 +26,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/scripts/run_eval.sh b/research/cv/resnetv2/scripts/run_eval.sh index 84a62a58cd978d5959e856da5374f23d14fff802..fa1a8c9867958b91e66e6d2f38fb963df3c2e709 100644 --- a/research/cv/resnetv2/scripts/run_eval.sh +++ b/research/cv/resnetv2/scripts/run_eval.sh @@ -19,9 +19,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/scripts/run_eval_gpu.sh b/research/cv/resnetv2/scripts/run_eval_gpu.sh index 9766e77bb95c11ad13c7754d340cc397c75ae7ed..27bc6ced6f563bd16fff956d9692503fdb024ffd 100644 --- a/research/cv/resnetv2/scripts/run_eval_gpu.sh +++ b/research/cv/resnetv2/scripts/run_eval_gpu.sh @@ -19,9 +19,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/scripts/run_infer_310.sh b/research/cv/resnetv2/scripts/run_infer_310.sh index 74a97fea3a0fea16cadb1552b47d961967e0836a..f4f560c274d33e2e5efd21c2e5ef5fdb3df0e98b 100644 --- a/research/cv/resnetv2/scripts/run_infer_310.sh +++ b/research/cv/resnetv2/scripts/run_infer_310.sh @@ -28,10 +28,10 @@ get_real_path(){ fi } model=$(get_real_path $1) -if [ ${2,,} == 'cifar10' ] || [ ${2,,} == 'cifar100' ] || [ ${2,,} == 'imagenet2012' ]; then +if [ ${2,,} == 'cifar10' ] || [ ${2,,} == 'cifar100' ]; then dataset=$2 else - echo "dataset must choose from [cifar10, cifar100, imagenet2012]" + echo "dataset must choose from [cifar10, cifar100]" exit 1 fi @@ -95,9 +95,6 @@ function cal_acc() { if [ "${dataset}" == "cifar10" ] || [ "${dataset}" == "cifar100" ]; then python ../postprocess.py --dataset=$dataset --label_path=./preprocess_Result/label.npy --result_path=result_Files &> acc.log - else - python ../create_imagenet2012_label.py --img_path=$data_path - python ../postprocess.py --dataset=$dataset --result_path=./result_Files --label_path=./imagenet_label.json &> acc.log fi } diff --git a/research/cv/resnetv2/scripts/run_standalone_train.sh b/research/cv/resnetv2/scripts/run_standalone_train.sh index b8edbd456f6d64f3f044ae17f1903abd89d00bbe..50fc65f49ef2b2592bec8411e0ef988b4c982343 100644 --- a/research/cv/resnetv2/scripts/run_standalone_train.sh +++ b/research/cv/resnetv2/scripts/run_standalone_train.sh @@ -16,7 +16,7 @@ if [ $# != 3 ] then - echo "Usage: bash run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH]" + echo "Usage: bash run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH]" exit 1 fi @@ -26,9 +26,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/scripts/run_standalone_train_gpu.sh b/research/cv/resnetv2/scripts/run_standalone_train_gpu.sh index 92abf2be7673529542bd6a9ac5e408a426698ab4..a88ffcca081381e7802eb08506386191c4fb8f2e 100644 --- a/research/cv/resnetv2/scripts/run_standalone_train_gpu.sh +++ b/research/cv/resnetv2/scripts/run_standalone_train_gpu.sh @@ -16,7 +16,7 @@ if [ $# != 3 ] then - echo "Usage: bash run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|imagenet2012] [DATASET_PATH]" + echo "Usage: bash run_standalone_train.sh [resnetv2_50|resnetv2_101|resnetv2_152] [cifar10|cifar100] [DATASET_PATH]" exit 1 fi @@ -26,9 +26,9 @@ then exit 1 fi -if [ $2 != "cifar10" ] && [ $2 != "imagenet2012" ] +if [ $2 != "cifar10" ] && [ $2 != "cifar100" ] then - echo "error: the selected dataset is neither cifar10 nor imagenet2012" + echo "error: the selected dataset is neither cifar10 nor cifar100" exit 1 fi diff --git a/research/cv/resnetv2/src/CrossEntropySmooth.py b/research/cv/resnetv2/src/CrossEntropySmooth.py deleted file mode 100644 index 0e45c41c51656611dbf5473d59161bf97a4d1fb0..0000000000000000000000000000000000000000 --- a/research/cv/resnetv2/src/CrossEntropySmooth.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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. -# ============================================================================ -""" CrossEntropySmooth.py """ -import mindspore.nn as nn -from mindspore import Tensor -from mindspore.common import dtype as mstype -from mindspore.nn.loss.loss import LossBase -from mindspore.ops import functional as F -from mindspore.ops import operations as P - - -class CrossEntropySmooth(LossBase): - """CrossEntropy""" - def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000): - super(CrossEntropySmooth, self).__init__() - self.onehot = P.OneHot() - self.sparse = sparse - self.on_value = Tensor(1.0 - smooth_factor, mstype.float32) - self.off_value = Tensor(1.0 * smooth_factor / (num_classes - 1), mstype.float32) - self.ce = nn.SoftmaxCrossEntropyWithLogits(reduction=reduction) - - def construct(self, logit, label): - if self.sparse: - label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value) - loss = self.ce(logit, label) - return loss diff --git a/research/cv/resnetv2/src/config.py b/research/cv/resnetv2/src/config.py index 25a080ceab7752873d6027e4db657b1e9ed36148..d66d521216a865e4ba1a65748fc7d47458edcaf7 100644 --- a/research/cv/resnetv2/src/config.py +++ b/research/cv/resnetv2/src/config.py @@ -56,26 +56,3 @@ config2 = ed({ "lr_end": 0.0000000005, "lr_max": 0.1, }) - -# config for ResNetv2, imagenet2012 -config3 = ed({ - "class_num": 1001, - "batch_size": 64, - "loss_scale": 1024, - "momentum": 0.9, - "weight_decay": 1e-4, - "epoch_size": 90, - "pretrain_epoch_size": 0, - "save_checkpoint": True, - "save_checkpoint_epochs": 5, - "keep_checkpoint_max": 10, - "save_checkpoint_path": "./checkpoint", - "low_memory": True, - "warmup_epochs": 0, - "use_label_smooth": True, - "label_smooth_factor": 0.1, - "lr_decay_mode": "cosine", - "lr_init": 0.05, - "lr_end": 0.0000001, - "lr_max": 0.05, -}) diff --git a/research/cv/resnetv2/src/dataset.py b/research/cv/resnetv2/src/dataset.py index 080aefd4b129a32bcda9a8598ea63466070f9451..784e564e904db05ac81a47410b13aeacfdbe36b7 100644 --- a/research/cv/resnetv2/src/dataset.py +++ b/research/cv/resnetv2/src/dataset.py @@ -131,72 +131,6 @@ def create_dataset2(dataset_path, do_train=True, repeat_num=1, batch_size=32, ta return data_set -def create_dataset3(dataset_path, do_train=True, repeat_num=1, batch_size=32, target="Ascend", distribute=False): - """ - create a train or eval imagenet2012 dataset for PreActResnet - - 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 - distribute(bool): data for distribute or not. Default: False - - Returns: - dataset - """ - if target == "Ascend": - device_num, rank_id = _get_rank_info() - else: - if distribute: - init() - rank_id = get_rank() - device_num = get_group_size() - else: - device_num = 1 - - if device_num == 1: - data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True) - else: - data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True, - num_shards=device_num, shard_id=rank_id) - - image_size = 224 - mean = [0.485 * 255, 0.456 * 255, 0.406 * 255] - std = [0.229 * 255, 0.224 * 255, 0.225 * 255] - - # define map operations - if do_train: - trans = [ - C.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)), - C.RandomHorizontalFlip(prob=0.5), - C.Normalize(mean=mean, std=std), - C.HWC2CHW() - ] - else: - trans = [ - C.Decode(), - C.Resize(256), - C.CenterCrop(image_size), - C.Normalize(mean=mean, std=std), - C.HWC2CHW() - ] - - type_cast_op = C2.TypeCast(mstype.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) - - # 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 _get_rank_info(): """ get rank size and rank id diff --git a/research/cv/resnetv2/train.py b/research/cv/resnetv2/train.py index e4b8931ca5dd1e230eae3cd9ed7247f0adafa0c3..e14254c76ffabeb48a4eeff8a7718cdc5981ae8b 100644 --- a/research/cv/resnetv2/train.py +++ b/research/cv/resnetv2/train.py @@ -27,14 +27,13 @@ from mindspore.common.tensor import Tensor from mindspore.train.loss_scale_manager import FixedLossScaleManager from src.lr_generator import get_lr -from src.CrossEntropySmooth import CrossEntropySmooth parser = argparse.ArgumentParser(description='Image classification.') parser.add_argument('--net', type=str, default='resnetv2_50', help='Resnetv2 Model, resnetv2_50, resnetv2_101, resnetv2_152') parser.add_argument('--dataset', type=str, default='cifar10', - help='Dataset, cifar10, imagenet2012') + help='Dataset, cifar10, cifar100') 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('--device_num', type=int, default=1, help='Device num.') @@ -57,8 +56,6 @@ if args_opt.dataset == "cifar10": from src.dataset import create_dataset1 as create_dataset elif args_opt.dataset == "cifar100": from src.dataset import create_dataset2 as create_dataset -elif args_opt.dataset == 'imagenet2012': - from src.dataset import create_dataset3 as create_dataset # import config if args_opt.net == "resnetv2_50" or args_opt.net == "resnetv2_101" or args_opt.net == "resnetv2_152": @@ -66,8 +63,6 @@ if args_opt.net == "resnetv2_50" or args_opt.net == "resnetv2_101" or args_opt.n from src.config import config1 as config elif args_opt.dataset == 'cifar100': from src.config import config2 as config - elif args_opt.dataset == 'imagenet2012': - from src.config import config3 as config set_seed(1) @@ -117,13 +112,7 @@ if __name__ == '__main__': lr = Tensor(lr) # define loss, opt, model - if args_opt.dataset == "imagenet2012": - if not config.use_label_smooth: - config.label_smooth_factor = 0.0 - loss = CrossEntropySmooth(sparse=True, reduction="mean", - smooth_factor=config.label_smooth_factor, num_classes=config.class_num) - else: - loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") + loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") loss_scale = FixedLossScaleManager(config.loss_scale, drop_overflow_update=False) opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, config.momentum, config.weight_decay, config.loss_scale)