1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| # encoding: utf-8
def train(cfg):
model=build_fcn_model(cfg)
#4.非常重要,要将model从cpu转换到cuda,否则会报错
model=model.cuda()
# 1.在model下面添加model.pth ,其中cfg.MODEL.CONTINUE.MODEL为模型保存的位置
model.load_state_dict(torch.load(cfg.MODEL.CONTINUE.MODEL))
optimizer=make_optimizer(cfg, model)
# 2.在optimizer 下面添加optimizer .pth ,其中cfg.MODEL.CONTINUE.optimizer 为模型保存的位置
optimizer.load_state_dict(torch.load(cfg.MODEL.CONTINUE.OPTIMIZER))
arguments={}
data_loader=make_data_loader(cfg, is_train=True)
val_loader=make_data_loader(cfg, is_train=False)
do_train(
cfg,
model,
data_loader,
val_loader,
optimizer,
cross_entropy2d,
)
def main():
parser=argparse.ArgumentParser(description="PyTorch Training")
parser.add_argument(
"--config_file", default="", help="path to config file", type=str
)
parser.add_argument("opts", help="Modify config options using the command-line", default=None,
nargs=argparse.REMAINDER)
args=parser.parse_args()
num_gpus=int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1
if args.config_file !="":
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
output_dir=cfg.OUTPUT_DIR
if output_dir and not os.path.exists(output_dir):
mkdir(output_dir)
logger=setup_logger("Model", output_dir, 0)
logger.info("Using {} GPUS".format(num_gpus))
logger.info(args)
if args.config_file !="":
logger.info("Loaded configuration file {}".format(args.config_file))
with open(args.config_file, 'r') as cf:
config_str="
" + cf.read()
logger.info(config_str)
logger.info("Running with config:
{}".format(cfg))
train(cfg)
if __name__=='__main__':
main() |