forked from clovaai/ext_portrait_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_model.py
180 lines (145 loc) · 7.24 KB
/
Test_model.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'''
ExtPortraitSeg
Copyright (c) 2019-present NAVER Corp.
MIT license
'''
import os
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
import time
import json
import argparse
import numpy as np
import cv2
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
from etc.Tensor_logger import Logger
from data.dataloader import get_dataloader
import models
from etc.help_function import *
from etc.utils import *
from etc.Visualize_video import ExportVideo
from etc.flops_counter import add_flops_counting_methods, flops_to_string, get_model_parameters_number
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-c', '--config', type=str, default='./setting/Test_SINet.json', help='JSON file for configuration')
parser.add_argument('-n', '--use_nsml', type=bool, default=False, help='Play with NSML!')
parser.add_argument('-d', '--decoder_only', type=bool, default=False, help='Decoder only training')
parser.add_argument('-o', '--optim', type=str, default="Adam", help='Adam , SGD, RMS')
parser.add_argument('-s', '--lrsch', type=str, default="multistep", help='step, poly, multistep, warmpoly')
parser.add_argument('-t', '--wd_tfmode', type=bool, default=True, help='Play with NSML!')
parser.add_argument('-w', '--weight_decay', type=float, default=2e-4, help='value for weight decay')
parser.add_argument('-v', '--visualize', type=bool, default=False, help='visualize result image')
args = parser.parse_args()
others= args.weight_decay*0.05
############### setting framework ##########################################
with open(args.config) as fin:
config = json.load(fin)
test_config = config['test_config']
data_config = config['data_config']
args.optim = test_config["optim"]
args.lrsch = test_config["lrsch"]
args.wd_tfmode = test_config["wd_tfmode"]
args.weight_decay = test_config["weight_decay"]
others = args.weight_decay * 0.05
if test_config["loss"] == "Lovasz":
test_config["num_classes"] = 1
print("Use Lovasz loss ")
Lovasz = True
else:
print("Use Cross Entropy loss ")
Lovasz = False
if not os.path.isdir(test_config['save_dir']):
os.mkdir(test_config['save_dir'])
print("Run : " + test_config["Model"])
D_ratio = []
if test_config["Model"].startswith('Stage1'):
model = models.__dict__[test_config["Model"]](
p=test_config["p"], q=test_config["q"], classes=test_config["num_classes"])
elif test_config["Model"].startswith('Stage2'):
model = models.__dict__[test_config["Model"]](classes=test_config["num_classes"],
p=test_config["p"], q=test_config["q"])
elif test_config["Model"].startswith('ExtremeC3Net_small'):
model = models.__dict__[test_config["Model"]](classes=test_config["num_classes"],
p=test_config["p"], q=test_config["q"])
elif test_config["Model"].startswith('Dnc_SINet'):
model = models.__dict__[test_config["Model"]](
classes=test_config["num_classes"], p=test_config["p"], q=test_config["q"],
chnn=test_config["chnn"])
model_name = test_config["Model"]
print(test_config["num_classes"])
batch = torch.FloatTensor(1, 3, data_config["w"], data_config["h"])
model_eval = add_flops_counting_methods(model)
model_eval.eval().start_flops_count()
out = model_eval(batch)
N_flop = model.compute_average_flops_cost()
total_paramters = netParams(model)
color_transform = Colorize(test_config["num_classes"])
#################### common model setting and opt setting #######################################
if args.use_nsml:
from nsml import DATASET_PATH
data_config['data_dir'] = os.path.join(DATASET_PATH, 'train')
Max_name = test_config["weight_name"]
if torch.cuda.device_count() > 0:
model.load_state_dict(torch.load(Max_name))
else:
model.load_state_dict(torch.load(Max_name, "cpu"))
use_cuda = torch.cuda.is_available()
num_gpu = torch.cuda.device_count()
if use_cuda:
print("Use gpu : %d" % torch.cuda.device_count())
if num_gpu > 1:
model = torch.nn.DataParallel(model)
print("make DataParallel")
model = model.cuda()
print("Done")
###################################stage Enc setting ##############################################
if (not args.decoder_only):
logger, this_savedir = info_setting(test_config['save_dir'], test_config["Model"], total_paramters, N_flop)
logger.flush()
logdir = this_savedir.split(test_config['save_dir'])[1]
nsml_logger = Logger(8097, './logs/' + logdir, args.use_nsml)
trainLoader, valLoader, data = get_dataloader(data_config)
print(data['mean'])
print(data['std'])
weight = torch.from_numpy(data['classWeights']) # convert the numpy array to torch
print(weight)
if test_config["loss"] == "Lovasz":
from etc.lovasz_losses import lovasz_hinge
criteria = lovasz_hinge(ignore=data_config["ignore_idx"])
else:
from etc.Criteria import CrossEntropyLoss2d
criteria = CrossEntropyLoss2d(weight,ignore=data_config["ignore_idx"]) # weight
if num_gpu > 0:
weight = weight.cuda()
criteria = criteria.cuda()
print("init_lr: " + str(test_config["learning_rate"]) + " batch_size : " + str(data_config["batch_size"]) +
args.lrsch + " sch use weight and class " + str(test_config["num_classes"]))
print("logs saved in " + logdir + "\tlr sch: " + args.lrsch + "\toptim method: " + args.optim +
"\ttf style : " + str(args.wd_tfmode) + "\tbn-weight : " + str(others))
print('Flops: {}'.format(flops_to_string(N_flop)))
print('Params: ' + get_model_parameters_number(model))
print('Output shape: {}'.format(list(out.shape)))
print(total_paramters)
################################ start Enc train ##########################################
if args.visualize:
lossVal, ElossVal, mIOU_val, save_input, save_est, save_gt = \
val_edge(num_gpu, valLoader, model, criteria, Lovasz, args.visualize)
else:
lossVal, ElossVal, mIOU_val = val_edge(num_gpu, valLoader, model, criteria, Lovasz)
print("mIOU(val) = %.4f" %mIOU_val)
print("========== TRAINING FINISHED ===========")
mean = data['mean']
std = data['std']
print(mean)
print(std)
if data_config["dataset_name"] =="pilportrait":
isPILlodear=True
else:
isPILlodear=False
# ExportVideo(model, Max_name, "./video", logdir, "video2.mp4", data_config["h"], data_config["w"], mean, std, Lovasz,
# pil=isPILlodear)
# ExportVideo(model, Max_name, "./video", logdir, "video1.mp4", data_config["h"], data_config["w"], mean, std, Lovasz,
# pil=isPILlodear)