def test_deeplabv3_1p(): start_time = time.time() epoch_size = 100 args_opt = argparse.Namespace(base_size=513, crop_size=513, batch_size=2) args_opt.base_size = config.crop_size args_opt.crop_size = config.crop_size args_opt.batch_size = config.batch_size train_dataset = create_dataset(args_opt, data_url, 1, config.batch_size, usage="eval") dataset_size = train_dataset.get_dataset_size() callback = LossCallBack(dataset_size) net = deeplabv3_resnet50(config.seg_num_classes, [config.batch_size, 3, args_opt.crop_size, args_opt.crop_size], infer_scale_sizes=config.eval_scales, atrous_rates=config.atrous_rates, decoder_output_stride=config.decoder_output_stride, output_stride=config.output_stride, fine_tune_batch_norm=config.fine_tune_batch_norm, image_pyramid=config.image_pyramid) net.set_train() model_fine_tune(net, 'layer') loss = OhemLoss(config.seg_num_classes, config.ignore_label) opt = Momentum(filter(lambda x: 'beta' not in x.name and 'gamma' not in x.name and 'depth' not in x.name and 'bias' not in x.name, net.trainable_params()), learning_rate=config.learning_rate, momentum=config.momentum, weight_decay=config.weight_decay) model = Model(net, loss, opt) model.train(epoch_size, train_dataset, callback) print(time.time() - start_time) print("expect loss: ", callback.loss) print("expect time: ", callback.time) expect_loss = 0.92 expect_time = 40 assert callback.loss.asnumpy() <= expect_loss assert callback.time <= expect_time
def model_fine_tune(flags, train_net, fix_weight_layer): checkpoint_path = flags.checkpoint_url if checkpoint_path is None: return param_dict = load_checkpoint(checkpoint_path) load_param_into_net(train_net, param_dict) for para in train_net.trainable_params(): if fix_weight_layer in para.name: para.requires_grad = False if __name__ == "__main__": start_time = time.time() epoch_size = 3 args_opt.base_size = config.crop_size args_opt.crop_size = config.crop_size train_dataset = create_dataset(args_opt, args_opt.data_url, 1, config.batch_size, usage="train", shuffle=False) dataset_size = train_dataset.get_dataset_size() callback = LossCallBack(dataset_size) net = deeplabv3_resnet50(config.seg_num_classes, [config.batch_size, 3, args_opt.crop_size, args_opt.crop_size], infer_scale_sizes=config.eval_scales, atrous_rates=config.atrous_rates, decoder_output_stride=config.decoder_output_stride, output_stride=config.output_stride, fine_tune_batch_norm=config.fine_tune_batch_norm, image_pyramid=config.image_pyramid) net.set_train() model_fine_tune(args_opt, net, 'layer') loss = OhemLoss(config.seg_num_classes, config.ignore_label) opt = Momentum(filter(lambda x: 'beta' not in x.name and 'gamma' not in x.name and 'depth' not in x.name and 'bias' not in x.name, net.trainable_params()), learning_rate=config.learning_rate, momentum=config.momentum, weight_decay=config.weight_decay) model = Model(net, loss, opt) model.train(epoch_size, train_dataset, callback) print(time.time() - start_time) print("expect loss: ", callback.loss / 3) print("expect time: ", callback.time)
from src.config import config parser = argparse.ArgumentParser(description="Deeplabv3 evaluation") parser.add_argument('--epoch_size', type=int, default=2, help='Epoch size.') parser.add_argument("--device_id", type=int, default=0, help="Device id, default is 0.") parser.add_argument('--batch_size', type=int, default=2, help='Batch size.') parser.add_argument('--data_url', required=True, default=None, help='Evaluation data url') parser.add_argument('--checkpoint_url', default=None, help='Checkpoint path') args_opt = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=args_opt.device_id) print(args_opt) if __name__ == "__main__": args_opt.crop_size = config.crop_size args_opt.base_size = config.crop_size eval_dataset = create_dataset(args_opt, args_opt.data_url, args_opt.epoch_size, args_opt.batch_size, usage="eval") net = deeplabv3_resnet50(config.seg_num_classes, [args_opt.batch_size, 3, args_opt.crop_size, args_opt.crop_size], infer_scale_sizes=config.eval_scales, atrous_rates=config.atrous_rates, decoder_output_stride=config.decoder_output_stride, output_stride=config.output_stride, fine_tune_batch_norm=config.fine_tune_batch_norm, image_pyramid=config.image_pyramid) param_dict = load_checkpoint(args_opt.checkpoint_url) load_param_into_net(net, param_dict) mIou = MiouPrecision(config.seg_num_classes) metrics = {'mIou': mIou} loss = OhemLoss(config.seg_num_classes, config.ignore_label) model = Model(net, loss, metrics=metrics) model.eval(eval_dataset)
load_param_into_net(train_net, param_dict) for para in train_net.trainable_params(): if fix_weight_layer in para.name: para.requires_grad = False if __name__ == "__main__": if args_opt.distribute == "true": context.set_auto_parallel_context( parallel_mode=ParallelMode.DATA_PARALLEL, mirror_mean=True) init() args_opt.base_size = config.crop_size args_opt.crop_size = config.crop_size train_dataset = create_dataset(args_opt, args_opt.data_url, config.epoch_size, config.batch_size, usage="train") dataset_size = train_dataset.get_dataset_size() time_cb = TimeMonitor(data_size=dataset_size) callback = [time_cb, LossCallBack()] if config.enable_save_ckpt: config_ck = CheckpointConfig( save_checkpoint_steps=config.save_checkpoint_steps, keep_checkpoint_max=config.save_checkpoint_num) ckpoint_cb = ModelCheckpoint(prefix='checkpoint_deeplabv3', config=config_ck) callback.append(ckpoint_cb) net = deeplabv3_resnet50( config.seg_num_classes, [config.batch_size, 3, args_opt.crop_size, args_opt.crop_size],
if args_opt.distribute == "true": context.set_auto_parallel_context( parallel_mode=ParallelMode.DATA_PARALLEL, mirror_mean=True) init() args_opt.base_size = config.crop_size args_opt.crop_size = config.crop_size import moxing as mox mox.file.copy_parallel(src_url=args_opt.data_url, dst_url='voc2012/') mox.file.copy_parallel(src_url=args_opt.checkpoint_url, dst_url='checkpoint/') # train train_dataset = create_dataset(args_opt, data_path, config.epoch_size, config.batch_size, usage="train") dataset_size = train_dataset.get_dataset_size() time_cb = TimeMonitor(data_size=dataset_size) callback = [time_cb, LossCallBack()] if config.enable_save_ckpt: config_ck = CheckpointConfig( save_checkpoint_steps=config.save_checkpoint_steps, keep_checkpoint_max=config.save_checkpoint_num) ckpoint_cb = ModelCheckpoint(prefix='checkpoint_deeplabv3', config=config_ck) callback.append(ckpoint_cb) net = deeplabv3_resnet50( config.seg_num_classes, [config.batch_size, 3, args_opt.crop_size, args_opt.crop_size],