-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
81 lines (67 loc) · 2.97 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
import os
import sys
import json
import logging
import torch
from easydict import EasyDict as edict
from config import get_config
from lib.test import test
from lib.train import train
from lib.utils import count_parameters
from torch.utils.data import DataLoader
from lib.datasets import get_dataset_by_name
from models import get_model_by_name
ch = logging.StreamHandler(sys.stdout)
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(
format=os.uname()[1].split('.')[0] + ' %(asctime)s %(message)s',
datefmt='%m/%d %H:%M:%S',
handlers=[ch])
def main():
config = get_config()
if config.resume:
json_config = json.load(open(config.resume + '/config.json', 'r'))
json_config['resume'] = config.resume
config = edict(json_config)
if config.is_cuda and not torch.cuda.is_available():
raise Exception("No GPU found")
device = torch.device('cuda' if config.is_cuda else 'cpu')
logging.info('===> Configurations')
dconfig = vars(config)
for k in dconfig:
logging.info(' {}: {}'.format(k, dconfig[k]))
DatasetClass = get_dataset_by_name(config.dataset)
logging.info('===> Initializing dataloader')
if config.is_train:
train_dataset = DatasetClass(config, phase=config.train_phase)
train_data_loader = DataLoader(dataset=train_dataset,
num_workers=config.num_workers,
batch_size=config.batch_size,
shuffle=True)
val_dataset = DatasetClass(config, phase=config.val_phase)
val_data_loader = DataLoader(dataset=val_dataset,
num_workers=config.num_val_workers,
batch_size=config.val_batch_size,
shuffle=False)
num_in_channel = train_data_loader.dataset.NUM_IN_CHANNEL
num_labels = train_data_loader.dataset.NUM_LABELS
else:
test_dataset = DatasetClass(config, phase=config.test_phase)
test_data_loader = DataLoader(dataset=test_dataset,
num_workers=config.num_val_workers,
batch_size=config.test_batch_size,
shuffle=False)
num_in_channel = test_data_loader.dataset.NUM_IN_CHANNEL
num_labels = test_data_loader.dataset.NUM_LABELS
logging.info('===> Building model')
model = get_model_by_name(config.model, in_channels=num_in_channel, out_channels=num_labels)
logging.info('===> Number of trainable parameters: {}: {}'.format(config.model, count_parameters(model)))
logging.info(model)
logging.info('===> Model is on device: {}'.format(device))
model = model.to(device)
if config.is_train:
train(model, train_data_loader, val_data_loader, config)
else:
test(model, test_data_loader, config)
if __name__ == '__main__':
main()