2 Star 0 Fork 0

testorigind/ssd_resnet34

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
train.py 12.69 KB
一键复制 编辑 原始数据 按行查看 历史
嘎嘎皮5.0 提交于 2021-08-27 10:01 . updata scripts
# 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 SSD and get checkpoint files."""
import argparse
import ast
import os
import mindspore.nn as nn
from mindspore import context, Tensor, DatasetHelper
from mindspore.communication.management import init, get_rank
from mindspore.train.callback import CheckpointConfig, ModelCheckpoint, LossMonitor, TimeMonitor
from mindspore.train import Model
from mindspore.context import ParallelMode
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.common import set_seed, dtype
from src.ssd import SSD300, SSDWithLossCell, TrainingWrapper, ssd_mobilenet_v2, ssd_mobilenet_v1_fpn, ssd_resnet50_fpn, ssd_vgg16, ssd_resnet34
from src.config import config
from src.dataset import create_ssd_dataset, create_mindrecord
from src.lr_schedule import get_lr
from src.init_params import init_net_param, filter_checkpoint_parameter_by_list
from src.resnet34 import resnet34
# from src.ssd_resnet34 import SSD_ResNet34
set_seed(1)
def get_args():
parser = argparse.ArgumentParser(description="SSD_ResNet34 training")
parser.add_argument("--data_url", type=str)
parser.add_argument("--train_url", type=str)
parser.add_argument("--mindrecord_url", type=str)
parser.add_argument("--run_online", type=ast.literal_eval,default=False)
parser.add_argument("--eval_callback", type=ast.literal_eval, default=False)
parser.add_argument("--run_platform", type=str, default="Ascend", choices=("Ascend", "GPU", "CPU"),
help="run platform, support Ascend, GPU and CPU.")
parser.add_argument("--only_create_dataset", type=ast.literal_eval, default=False,
help="If set it true, only create Mindrecord, default is False.")
parser.add_argument("--distribute", type=ast.literal_eval, default=False,
help="Run distribute, default is False.")
parser.add_argument("--device_id", type=int, default=1, help="Device id, default is 0.")
parser.add_argument("--device_num", type=int, default=1, help="Use device nums, default is 1.")
parser.add_argument("--lr", type=float, default=0.05, help="Learning rate, default is 0.05.")
parser.add_argument("--mode", type=str, default="sink", help="Run sink mode or not, default is sink.")
parser.add_argument("--dataset", type=str, default="coco", help="Dataset, default is coco.")
parser.add_argument("--epoch_size", type=int, default=500, help="Epoch size, default is 500.")
parser.add_argument("--batch_size", type=int, default=32, help="Batch size, default is 32.")
parser.add_argument("--pre_trained", type=str, default=None, help="Pretrained Checkpoint file path.")
parser.add_argument("--pre_trained_epoch_size", type=int, default=0, help="Pretrained epoch size.")
parser.add_argument("--save_checkpoint_epochs", type=int, default=10, help="Save checkpoint epochs, default is 10.")
parser.add_argument("--loss_scale", type=int, default=1024, help="Loss scale, default is 1024.")
parser.add_argument("--filter_weight", type=ast.literal_eval, default=False,
help="Filter head weight parameters, default is False.")
parser.add_argument('--freeze_layer', type=str, default="none", choices=["none", "backbone"],
help="freeze the weights of network, support freeze the backbone's weights, "
"default is not freezing.")
args_opt = parser.parse_args()
return args_opt
def ssd_model_build(args_opt):
if config.model == "ssd300":
backbone = ssd_resnet34()
ssd = SSD300(backbone=backbone, config=config)
init_net_param(ssd)
if args_opt.freeze_layer == "backbone":
for param in backbone.feature_1.trainable_params():
param.requires_grad = False
elif config.model == "ssd_mobilenet_v1_fpn":
ssd = ssd_mobilenet_v1_fpn(config=config)
init_net_param(ssd)
if config.feature_extractor_base_param != "":
param_dict = load_checkpoint(config.feature_extractor_base_param)
for x in list(param_dict.keys()):
param_dict["network.feature_extractor.mobilenet_v1." + x] = param_dict[x]
del param_dict[x]
load_param_into_net(ssd.feature_extractor.mobilenet_v1.network, param_dict)
elif config.model == "ssd_resnet50_fpn":
ssd = ssd_resnet50_fpn(config=config)
init_net_param(ssd)
if config.feature_extractor_base_param != "":
param_dict = load_checkpoint(config.feature_extractor_base_param)
for x in list(param_dict.keys()):
param_dict["network.feature_extractor.resnet." + x] = param_dict[x]
del param_dict[x]
load_param_into_net(ssd.feature_extractor.resnet, param_dict)
elif config.model == "ssd_vgg16":
ssd = ssd_vgg16(config=config)
init_net_param(ssd)
if config.feature_extractor_base_param != "":
param_dict = load_checkpoint(config.feature_extractor_base_param)
from src.vgg16 import ssd_vgg_key_mapper
for k in ssd_vgg_key_mapper:
v = ssd_vgg_key_mapper[k]
param_dict["network.backbone." + v + ".weight"] = param_dict[k + ".weight"]
del param_dict[k + ".weight"]
load_param_into_net(ssd.backbone, param_dict)
elif config.model == "ssd_resnet34":
ssd = ssd_resnet34(config=config)
init_net_param(ssd)
if config.feature_extractor_base_param != "":
param_dict = load_checkpoint(config.feature_extractor_base_param)
for x in list(param_dict.keys()):
param_dict["network.feature_extractor.resnet." + x] = param_dict[x]
del param_dict[x]
load_param_into_net(ssd.feature_extractor.resnet, param_dict)
else:
raise ValueError(f'config.model: {config.model} is not supported')
return ssd
'''def organize_configuration(config, args):
args_dict = vars(args)
for item in args_dict.items():
config[item[0]] = item[1]'''
def main():
args_opt = get_args()
#organize_configuration(args_opt)
rank = 0
device_num = 1
config.coco_root = args_opt.data_url
config.mindrecord_dir = args_opt.mindrecord_url
local_train_url = args_opt.train_url
mindrecord_exist = False # Whether mindrecord files exist.
mindrecord_files = []
if args_opt.run_platform == "CPU":
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
else:
context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.run_platform)
if args_opt.run_online:
import moxing as mox
config.coco_root = "/cache/data_ori_url"
config.mindrecord_dir = "/cache/mindrecord_url"
mox.file.copy_parallel(args_opt.mindrecord_url, config.mindrecord_dir)
mox.file.copy_parallel(args_opt.data_url, config.coco_root)
# check whether there is a mindrecord file.
file_list = os.listdir(config.mindrecord_dir)
if len(file_list) != 0:
print("Mindrecord files exist.")
mindrecord_exist = True
mindrecord_files = [config.mindrecord_dir+"/ssd.mindrecord{}".format(i) for i in range(8)]
for file in mindrecord_files:
if file.split("/")[-1] in file_list:
print(file)
else:
mox.file.copy_parallel(args_opt.data_url, config.coco_root)
local_train_url = "/cache/train_out"
if args_opt.distribute:
init()
device_num = int(os.getenv("RANK_SIZE"))
context.reset_auto_parallel_context()
context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, gradients_mean=True,
device_num=device_num)
if config.model == "ssd_resnet50_fpn":
context.set_auto_parallel_context(all_reduce_fusion_config=[90, 183, 279])
if config.model == "ssd_vgg16":
context.set_auto_parallel_context(all_reduce_fusion_config=[20, 41, 62])
else:
context.set_auto_parallel_context(all_reduce_fusion_config=[29, 58, 89])
rank = get_rank()
if not mindrecord_exist:
mindrecord_file = create_mindrecord(args_opt.dataset, "ssd.mindrecord", True)
mindrecord_files = [mindrecord_file.strip('0')+'{}'.format(i) for i in range(8)]
if args_opt.only_create_dataset:
return
loss_scale = float(args_opt.loss_scale)
if args_opt.run_platform == "CPU":
loss_scale = 1.0
# When create MindDataset, using the fitst mindrecord file, such as ssd.mindrecord0.
use_multiprocessing = (args_opt.run_platform != "CPU")
dataset = create_ssd_dataset(mindrecord_files, repeat_num=1, batch_size=args_opt.batch_size,
device_num=device_num, rank=rank, use_multiprocessing=use_multiprocessing)
dataset_size = dataset.get_dataset_size()
print(f"Create dataset done! dataset size is {dataset_size}")
ssd = ssd_model_build(args_opt)
if ("use_float16" in config and config.use_float16) or args_opt.run_platform == "GPU":
ssd.to_float(dtype.float16)
net = SSDWithLossCell(ssd, config)
# checkpoint
ckpt_config = CheckpointConfig(save_checkpoint_steps=dataset_size * args_opt.save_checkpoint_epochs ,keep_checkpoint_max=20)
ckpoint_cb = ModelCheckpoint(prefix="ssd", directory=local_train_url+"/card{}".format(rank), config=ckpt_config)
if args_opt.pre_trained:
local_pre_train = ""
if args_opt.run_online:
import moxing as mox
local_pre_train = "/cache/pre_checkpoint_path/.ckpt"
mox.file.copy_parallel(args_opt.pre_trained,local_pre_train)
else:
local_pre_train = args_opt.pre_trained
param_dict = load_checkpoint(local_pre_train)
if args_opt.filter_weight:
filter_checkpoint_parameter_by_list(param_dict, config.checkpoint_filter_list)
load_param_into_net(net, param_dict, True)
lr = Tensor(get_lr(global_step=args_opt.pre_trained_epoch_size * dataset_size,
lr_init=config.lr_init, lr_end=config.lr_end_rate * args_opt.lr, lr_max=args_opt.lr,
warmup_epochs=config.warmup_epochs,
total_epochs=args_opt.epoch_size,
steps_per_epoch=dataset_size))
if "use_global_norm" in config and config.use_global_norm:
opt = nn.Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr,
config.momentum, config.weight_decay, 1.0)
net = TrainingWrapper(net, opt, loss_scale, True)
else:
opt = nn.Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr,
config.momentum, config.weight_decay, loss_scale)
net = TrainingWrapper(net, opt, loss_scale)
callback = [TimeMonitor(data_size=dataset_size), LossMonitor(), ckpoint_cb]
model = Model(net)
if args_opt.dataset == "coco":
json_path = os.path.join(config.coco_root, config.instances_set.format(config.val_data_type))
#train_and_eval
if args_opt.eval_callback:
local_eval_data = ""
if args_opt.run_online:
local_eval_data = "/cache/data_eval"
import moxing as mox
mox.file.copy_parallel(args_opt.mindrecord_url, local_eval_data)
else:
local_eval_data = args_opt.mindrecord_url
from src.callback import eval_callback
eval_cb = eval_callback(local_eval_data, ssd, json_path, local_train_url, eval_per_epoch=10)
callback.append(eval_cb)
dataset_sink_mode = False
if args_opt.mode == "sink" and args_opt.run_platform != "CPU":
print("In sink mode, one epoch return a loss.")
dataset_sink_mode = True
print("Start train SSD, the first epoch will be slower because of the graph compilation.")
model.train(args_opt.epoch_size, dataset, callbacks=callback, dataset_sink_mode=dataset_sink_mode)
if args_opt.run_online:
import moxing as mox
mox.file.copy_parallel(local_train_url, args_opt.train_url)
if __name__ == '__main__':
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/testorigind/ssd_resnet34.git
git@gitee.com:testorigind/ssd_resnet34.git
testorigind
ssd_resnet34
ssd_resnet34
master

搜索帮助