train.py 4.6 KB
Newer Older
W
wangjun260 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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 vgg16 example on cifar10########################
python train.py --data_path=$DATA_HOME --device_id=$DEVICE_ID
"""
import argparse
C
caojian05 已提交
20
import os
W
wangjun260 已提交
21
import random
22

W
wangjun260 已提交
23
import numpy as np
24

W
wangjun260 已提交
25 26
import mindspore.nn as nn
from mindspore import Tensor
27
from mindspore import context
C
caojian05 已提交
28
from mindspore.communication.management import init
W
wangjun260 已提交
29
from mindspore.nn.optim.momentum import Momentum
C
caojian05 已提交
30
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
31
from mindspore.train.model import Model, ParallelMode
32
from mindspore.train.serialization import load_param_into_net, load_checkpoint
33 34 35 36
from src.config import cifar_cfg as cfg
from src.dataset import vgg_create_dataset
from src.vgg import vgg16

W
wangjun260 已提交
37 38 39
random.seed(1)
np.random.seed(1)

40

C
caojian05 已提交
41
def lr_steps(global_step, lr_init, lr_max, warmup_epochs, total_epochs, steps_per_epoch):
W
wangjun260 已提交
42 43 44
    """Set learning rate."""
    lr_each_step = []
    total_steps = steps_per_epoch * total_epochs
C
caojian05 已提交
45 46 47 48 49
    warmup_steps = steps_per_epoch * warmup_epochs
    if warmup_steps != 0:
        inc_each_step = (float(lr_max) - float(lr_init)) / float(warmup_steps)
    else:
        inc_each_step = 0
W
wangjun260 已提交
50
    for i in range(total_steps):
C
caojian05 已提交
51 52
        if i < warmup_steps:
            lr_value = float(lr_init) + inc_each_step * float(i)
W
wangjun260 已提交
53
        else:
C
caojian05 已提交
54 55 56 57 58 59
            base = (1.0 - (float(i) - float(warmup_steps)) / (float(total_steps) - float(warmup_steps)))
            lr_value = float(lr_max) * base * base
            if lr_value < 0.0:
                lr_value = 0.0
        lr_each_step.append(lr_value)

W
wangjun260 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
    current_step = global_step
    lr_each_step = np.array(lr_each_step).astype(np.float32)
    learning_rate = lr_each_step[current_step:]

    return learning_rate


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Cifar10 classification')
    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('--data_path', type=str, default='./cifar', help='path where the dataset is saved')
    parser.add_argument('--device_id', type=int, default=None, help='device id of GPU or Ascend. (Default: None)')
73
    parser.add_argument('--pre_trained', type=str, default=None, help='the pretrained checkpoint file path.')
W
wangjun260 已提交
74 75 76 77 78
    args_opt = parser.parse_args()

    context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target)
    context.set_context(device_id=args_opt.device_id)

C
caojian05 已提交
79 80 81 82 83 84 85
    device_num = int(os.environ.get("DEVICE_NUM", 1))
    if device_num > 1:
        context.reset_auto_parallel_context()
        context.set_auto_parallel_context(device_num=device_num, parallel_mode=ParallelMode.DATA_PARALLEL,
                                          mirror_mean=True)
        init()

86
    dataset = vgg_create_dataset(args_opt.data_path, 1)
C
caojian05 已提交
87 88
    batch_num = dataset.get_dataset_size()

89
    net = vgg16(num_classes=cfg.num_classes)
90 91 92 93
    # pre_trained
    if args_opt.pre_trained:
        load_param_into_net(net, load_checkpoint(args_opt.pre_trained))

C
caojian05 已提交
94 95
    lr = lr_steps(0, lr_init=cfg.lr_init, lr_max=cfg.lr_max, warmup_epochs=cfg.warmup_epochs,
                  total_epochs=cfg.epoch_size, steps_per_epoch=batch_num)
96 97
    opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), Tensor(lr), cfg.momentum,
                   weight_decay=cfg.weight_decay)
W
wangjun260 已提交
98
    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
99 100
    model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
                  amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
W
wangjun260 已提交
101 102

    config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5, keep_checkpoint_max=cfg.keep_checkpoint_max)
C
caojian05 已提交
103
    time_cb = TimeMonitor(data_size=batch_num)
W
wangjun260 已提交
104 105
    ckpoint_cb = ModelCheckpoint(prefix="train_vgg_cifar10", directory="./", config=config_ck)
    loss_cb = LossMonitor()
C
caojian05 已提交
106 107
    model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
    print("train success")