train_demo.py 4.6 KB
Newer Older
W
wuyefeilin 已提交
1
# coding: utf8
2
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
W
wuyefeilin 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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.

16 17 18 19
import os.path as osp
import argparse
import transforms.transforms as T
from readers.reader import Reader
20
from models import UNet, HRNet
21 22 23 24


def parse_args():
    parser = argparse.ArgumentParser(description='RemoteSensing training')
25 26 27 28 29 30
    parser.add_argument(
        '--model_type',
        dest='model_type',
        help="Model type for traing, which is one of ('unet', 'hrnet')",
        type=str,
        default='hrnet')
31 32 33 34 35 36 37 38 39 40 41 42
    parser.add_argument(
        '--data_dir',
        dest='data_dir',
        help='dataset directory',
        default=None,
        type=str)
    parser.add_argument(
        '--save_dir',
        dest='save_dir',
        help='model save directory',
        default=None,
        type=str)
43 44 45 46 47 48
    parser.add_argument(
        '--num_classes',
        dest='num_classes',
        help='Number of classes',
        default=None,
        type=int)
49 50 51 52 53 54
    parser.add_argument(
        '--channel',
        dest='channel',
        help='number of data channel',
        default=3,
        type=int)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    parser.add_argument(
        '--clip_min_value',
        dest='clip_min_value',
        help='Min values for clipping data',
        nargs='+',
        default=None,
        type=int)
    parser.add_argument(
        '--clip_max_value',
        dest='clip_max_value',
        help='Max values for clipping data',
        nargs='+',
        default=None,
        type=int)
    parser.add_argument(
        '--mean',
        dest='mean',
        help='Data means',
        nargs='+',
        default=None,
        type=float)
    parser.add_argument(
        '--std',
        dest='std',
        help='Data standard deviation',
        nargs='+',
        default=None,
        type=float)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    parser.add_argument(
        '--num_epochs',
        dest='num_epochs',
        help='number of traing epochs',
        default=100,
        type=int)
    parser.add_argument(
        '--train_batch_size',
        dest='train_batch_size',
        help='training batch size',
        default=4,
        type=int)
    parser.add_argument(
        '--lr', dest='lr', help='learning rate', default=0.01, type=float)
    return parser.parse_args()


args = parse_args()
data_dir = args.data_dir
save_dir = args.save_dir
103
num_classes = args.num_classes
104
channel = args.channel
105 106 107 108
clip_min_value = args.clip_min_value
clip_max_value = args.clip_max_value
mean = args.mean
std = args.std
109 110 111 112 113
num_epochs = args.num_epochs
train_batch_size = args.train_batch_size
lr = args.lr

# 定义训练和验证时的transforms
114 115 116 117 118 119 120 121 122
train_transforms = T.Compose([
    T.RandomVerticalFlip(0.5),
    T.RandomHorizontalFlip(0.5),
    T.ResizeStepScaling(0.5, 2.0, 0.25),
    T.RandomPaddingCrop(1000),
    T.Clip(min_val=clip_min_value, max_val=clip_max_value),
    T.Normalize(
        min_val=clip_min_value, max_val=clip_max_value, mean=mean, std=std),
])
123

124 125 126 127 128
eval_transforms = T.Compose([
    T.Clip(min_val=clip_min_value, max_val=clip_max_value),
    T.Normalize(
        min_val=clip_min_value, max_val=clip_max_value, mean=mean, std=std),
])
129 130 131 132 133 134 135 136 137 138 139

train_list = osp.join(data_dir, 'train.txt')
val_list = osp.join(data_dir, 'val.txt')
label_list = osp.join(data_dir, 'labels.txt')

# 定义数据读取器
train_reader = Reader(
    data_dir=data_dir,
    file_list=train_list,
    label_list=label_list,
    transforms=train_transforms,
140
    shuffle=True)
141 142 143 144 145

eval_reader = Reader(
    data_dir=data_dir,
    file_list=val_list,
    label_list=label_list,
146
    transforms=eval_transforms)
147

148
if args.model_type == 'unet':
149
    model = UNet(num_classes=num_classes, input_channel=channel)
150
elif args.model_type == 'hrnet':
151
    model = HRNet(num_classes=num_classes, input_channel=channel)
152 153 154 155
else:
    raise ValueError(
        "--model_type: {} is set wrong, it shold be one of ('unet', "
        "'hrnet')".format(args.model_type))
156 157 158 159 160 161

model.train(
    num_epochs=num_epochs,
    train_reader=train_reader,
    train_batch_size=train_batch_size,
    eval_reader=eval_reader,
162
    eval_best_metric='miou',
163 164 165 166
    save_interval_epochs=5,
    log_interval_steps=10,
    save_dir=save_dir,
    learning_rate=lr,
L
LutaoChu 已提交
167
    use_vdl=True)