-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.py
145 lines (117 loc) · 5.56 KB
/
main.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
OMP_NUM_THREADS=2 python3 -m torch.distributed.launch --nproc_per_node=2 main.py
'''
import argparse
import os
import random
import shutil
from datetime import datetime
from pprint import pprint
import numpy as np
import toml
import torch
from torch.utils.data import DataLoader
import utils
from dataloader.data_generator import DataGenerator
from dataloader.image_file import ImageFileTrain, ImageFileTest
from dataloader.prefetcher import Prefetcher
from trainers.trainer import Trainer
from utils import CONFIG
torch.manual_seed(8282)
torch.cuda.manual_seed(8282)
torch.cuda.manual_seed_all(8282) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(8282)
random.seed(8282)
def copy_script(root_path=None):
if not os.path.exists(root_path):
os.makedirs(root_path)
os.makedirs(CONFIG.log.logging_path)
os.makedirs(CONFIG.log.checkpoint_path)
shutil.copytree('./config', os.path.join(root_path, 'config'), ignore=shutil.ignore_patterns('__pycache__'))
shutil.copytree('./dataloader', os.path.join(root_path, 'dataloader'), ignore=shutil.ignore_patterns('__pycache__'))
shutil.copytree('./networks', os.path.join(root_path, 'networks'), ignore=shutil.ignore_patterns('__pycache__'))
shutil.copytree('./trainers', os.path.join(root_path, 'trainers'), ignore=shutil.ignore_patterns('__pycache__'))
shutil.copytree('./utils', os.path.join(root_path, 'utils'), ignore=shutil.ignore_patterns('__pycache__'))
shutil.copy('./main.py', os.path.join(root_path, 'main.py'))
shutil.copy('./inference.py', os.path.join(root_path, 'inference.py'))
shutil.copy('./evaluation.py', os.path.join(root_path, 'evaluation.py'))
def main():
# Train or Test
if CONFIG.phase.lower() == "train":
# set distributed training
if CONFIG.dist:
CONFIG.gpu = CONFIG.local_rank
torch.cuda.set_device(CONFIG.gpu)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
CONFIG.world_size = torch.distributed.get_world_size()
# Create directories if not exist.
if CONFIG.local_rank == 0:
utils.make_dir(CONFIG.log.logging_path)
utils.make_dir(CONFIG.log.checkpoint_path)
"""=== Set logger ==="""
logger = utils.get_logger(CONFIG.log.logging_path, logging_level=CONFIG.log.logging_level)
"""=== Set data loader ==="""
# [1] Composition-1k dataset
train_image_file = ImageFileTrain(alpha_dir=CONFIG.data.train_alpha,
fg_dir=CONFIG.data.train_fg,
bg_dir=CONFIG.data.train_bg)
test_image_file = ImageFileTest(alpha_dir=CONFIG.data.test_alpha,
merged_dir=CONFIG.data.test_merged,
trimap_dir=CONFIG.data.test_trimap)
train_dataset = DataGenerator(train_image_file, phase='train')
test_dataset = DataGenerator(test_image_file, phase='val')
if CONFIG.dist:
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
test_sampler = torch.utils.data.distributed.DistributedSampler(test_dataset)
else:
train_sampler = None
test_sampler = None
train_dataloader = DataLoader(train_dataset,
batch_size=CONFIG.model.batch_size,
shuffle=(train_sampler is None),
num_workers=CONFIG.data.workers,
pin_memory=True,
sampler=train_sampler,
drop_last=True)
train_dataloader = Prefetcher(train_dataloader)
test_dataloader = DataLoader(test_dataset,
batch_size=1,
shuffle=False,
num_workers=CONFIG.data.workers,
sampler=test_sampler,
drop_last=False)
"""=== Set Trainer ==="""
trainer = Trainer(train_dataloader=train_dataloader,
test_dataloader=test_dataloader,
logger=logger)
"""=== Run Trainer ==="""
trainer.train()
else:
raise NotImplementedError("Unknown Phase: {}".format(CONFIG.phase))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--phase', type=str, default='train')
parser.add_argument('--local_rank', type=int, default=0)
# Composition-1k
parser.add_argument('--config', type=str, default='config/MatteFormer_Composition1k.toml')
# Parse configuration
args = parser.parse_args()
with open(args.config) as f:
utils.load_config(toml.load(f))
# Check if toml config file is loaded
if CONFIG.is_default:
raise ValueError("No .toml config loaded.")
CONFIG.phase = args.phase
# set_experiment path
CONFIG.log.experiment_root = os.path.join(CONFIG.log.experiment_root, datetime.now().strftime("%y%m%d_%H%M%S"))
CONFIG.log.logging_path = os.path.join(CONFIG.log.experiment_root, CONFIG.log.logging_path)
CONFIG.log.checkpoint_path = os.path.join(CONFIG.log.experiment_root, CONFIG.log.checkpoint_path)
if args.local_rank == 0:
print('CONFIG: ')
pprint(CONFIG)
copy_script(root_path=CONFIG.log.experiment_root)
CONFIG.local_rank = args.local_rank
# Train
main()