-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (62 loc) · 2.18 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
import json
import os
import random
import time
from datetime import timedelta
import torch
from config import get_config
from test import test
from train import train
from valid import valid
from utils.init_util import init_dir, create_valid_data_loader
from model import build_net
def main(args):
print('----------------------------------------------------------------------')
print(args.model_name)
print(args.mode)
if args.mode == 'train':
train(args)
elif args.mode == 'valid':
log = open(os.path.join(args.log, '{}.txt'.format(args.mode)), 'w')
# loader valid data
valid_loader = create_valid_data_loader(args)
net = build_net(args.model_name, args.image_channel)
if args.resume:
if str(args.device) == 'cpu':
resume = torch.load(args.resume, map_location='cpu')
else:
resume = torch.load(args.resume)
state_dict = resume['model']
else:
if str(args.device) == 'cpu':
state_dict = torch.load(args.test_model, map_location='cpu')
else:
state_dict = torch.load(args.test_model)
net.load_state_dict(state_dict)
net = net.to(args.device)
valid(args, net, log, valid_loader)
elif args.mode == 'test':
temp = os.path.normpath(args.test_halftone_path)
temp = temp.split(os.sep)
log = open(os.path.join(args.log, '{}.txt'.format(temp[-2])), 'w')
net = build_net(args.model_name, args.image_channel)
if args.resume:
if str(args.device) == 'cpu':
resume = torch.load(args.resume, map_location='cpu')
else:
resume = torch.load(args.resume)
state_dict = resume['model']
else:
if str(args.device) == 'cpu':
state_dict = torch.load(args.test_model, map_location='cpu')
else:
state_dict = torch.load(args.test_model)
net.load_state_dict(state_dict)
net = net.to(args.device)
test(args, net, log)
if __name__ == '__main__':
args = get_config()
# print(args)
# Init dir
init_dir(args)
main(args)