-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.py
294 lines (250 loc) · 14.8 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: main.py
# Created Date: 2020.4.26
# Author: Chen Xuanhong
# Email: [email protected]
# Last Modified: Monday, 27th April 2020 11:19:48 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2019 Shanghai Jiao Tong University
#############################################################
import platform
import os
import json
import shutil
from utilities.reporter import Reporter
from utilities.json_config import *
from utilities.yaml_config import getConfigYaml
from utilities.sshupload import fileUploaderClass
from torch.backends import cudnn
import argparse
def str2bool(v):
return v.lower() in ('true')
def getParameters():
parser = argparse.ArgumentParser()
# general
parser.add_argument('--mode', type=str, default="train", choices=['train', 'finetune','test','debug'])
parser.add_argument('--cuda', type=int, default=0)
parser.add_argument('--dataloader_workers', type=int, default=6)
parser.add_argument('--checkpoint', type=int, default=278000)
# training
parser.add_argument('--version', type=str, default='condition2')
parser.add_argument('--experimentDescription', type=str, default="")
parser.add_argument('--trainYaml', type=str, default="train_SN_FC_256_conditional_multiscale.yaml")
# test
parser.add_argument('--testScriptsName', type=str, default='common')
parser.add_argument('--nodeName', type=str, default='4card',choices=['localhost', '4card', '8card','lyh','loc','localhost'])
parser.add_argument('--testBatchSize', type=int, default=1)
parser.add_argument('--totalImg', type=int, default=20)
parser.add_argument('--saveTestImg', type=str2bool, default=True)
parser.add_argument('--testImgRoot', type=str, default="D:\\PatchFace\\PleaseWork\\Benchmark\\styletransfer")
parser.add_argument('--specify_sytle', type=int, default=-1, help= 'When the value is -1, 11 painters styles are used for image(s) respectively for style transfer. '\
'The values corresponding to each painters style are as follows' \
'[0: Berthe Moriso, 1: Edvard Munch, 2: Ernst Ludwig Kirchner, 3: Jackson Pollock, 4: Wassily Kandinsky, \
5: Oscar-Claude Monet, 6: Nicholas Roerich, 7: Paul Cézanne, 8: Pablo Picasso, 9 : Samuel Colman, \
10: Vincent Willem van Gogh]')
parser.add_argument('--useSpecifiedImg', type=str2bool, default=False)
parser.add_argument('--specifiedTestImages', nargs='+', help='selected images for validation',
# '000121.jpg','000124.jpg','000129.jpg','000132.jpg','000135.jpg','001210.jpg','001316.jpg',
default=[183947])
parser.add_argument('--testClasses', type=int, default=3)
return parser.parse_args()
def create_dirs(sys_state):
# the base dir
if not os.path.exists(sys_state["logRootPath"]):
os.makedirs(sys_state["logRootPath"])
# create dirs
sys_state["projectRoot"] = os.path.join(sys_state["logRootPath"], sys_state["version"])
if not os.path.exists(sys_state["projectRoot"]):
os.makedirs(sys_state["projectRoot"])
sys_state["projectSummary"] = os.path.join(sys_state["projectRoot"], "summary")
if not os.path.exists(sys_state["projectSummary"]):
os.makedirs(sys_state["projectSummary"])
sys_state["projectCheckpoints"] = os.path.join(sys_state["projectRoot"], "checkpoints")
if not os.path.exists(sys_state["projectCheckpoints"]):
os.makedirs(sys_state["projectCheckpoints"])
sys_state["projectSamples"] = os.path.join(sys_state["projectRoot"], "samples")
if not os.path.exists(sys_state["projectSamples"]):
os.makedirs(sys_state["projectSamples"])
sys_state["projectScripts"] = os.path.join(sys_state["projectRoot"], "scripts")
if not os.path.exists(sys_state["projectScripts"]):
os.makedirs(sys_state["projectScripts"])
sys_state["reporterPath"] = os.path.join(sys_state["projectRoot"],sys_state["version"]+"_report")
if __name__ == '__main__':
logo_str = """
███╗ ██╗██████╗ ███████╗██╗ ██████╗ ███████╗ ██╗████████╗██╗ ██╗
████╗ ██║██╔══██╗██╔════╝██║██╔════╝ ██╔════╝ ██║╚══██╔══╝██║ ██║
██╔██╗ ██║██████╔╝███████╗██║██║ ███╗ ███████╗ ██║ ██║ ██║ ██║
██║╚██╗██║██╔══██╗╚════██║██║██║ ██║ ╚════██║██ ██║ ██║ ██║ ██║
██║ ╚████║██║ ██║███████║██║╚██████╔╝ ███████║╚█████╔╝ ██║ ╚██████╔╝
╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚══════╝ ╚════╝ ╚═╝ ╚═════╝
Neural Rendering Special Interesting Group of SJTU
"""
print(logo_str)
# import warnings
# warnings.filterwarnings('ignore')
config = getParameters()
# speed up the program
cudnn.benchmark = True
ignoreKey = [
"dataloader_workers","logRootPath",
"projectRoot","projectSummary","projectCheckpoints",
"projectSamples","projectScripts","reporterPath",
"useSpecifiedImg","dataset_path", "cuda"
]
sys_state = {}
sys_state["dataloader_workers"] = config.dataloader_workers
if config.cuda >= 0:
os.environ["CUDA_VISIBLE_DEVICES"] = str(config.cuda)
# For fast training
# read system environment path
env_config = read_config('env/config.json')
env_config = env_config["path"]
sys_state["cuda"] = config.cuda
# Train mode
if config.mode == "train":
sys_state["version"] = config.version
sys_state["experimentDescription"] = config.experimentDescription
sys_state["mode"] = config.mode
# read training configurations
ymal_config = getConfigYaml(os.path.join(env_config["trainConfigPath"], config.trainYaml))
for item in ymal_config.items():
sys_state[item[0]] = item[1]
# create dirs
sys_state["logRootPath"] = env_config["trainLogRoot"]
create_dirs(sys_state)
# create reporter file
reporter = Reporter(sys_state["reporterPath"])
# save the config json
config_json = os.path.join(sys_state["projectRoot"], env_config["configJsonName"])
write_config(config_json, sys_state)
# save the scripts
# copy the scripts to the project dir
file1 = os.path.join(env_config["trainScriptsPath"], "trainer_%s.py"%sys_state["trainScriptName"])
tgtfile1 = os.path.join(sys_state["projectScripts"], "trainer_%s.py"%sys_state["trainScriptName"])
shutil.copyfile(file1,tgtfile1)
file2 = os.path.join("./components", "%s.py"%sys_state["gScriptName"])
tgtfile2 = os.path.join(sys_state["projectScripts"], "%s.py"%sys_state["gScriptName"])
shutil.copyfile(file2,tgtfile2)
file3 = os.path.join("./components", "%s.py"%sys_state["dScriptName"])
tgtfile3 = os.path.join(sys_state["projectScripts"], "%s.py"%sys_state["dScriptName"])
shutil.copyfile(file3,tgtfile3)
elif config.mode == "finetune":
sys_state["logRootPath"] = env_config["trainLogRoot"]
sys_state["version"] = config.version
sys_state["projectRoot"] = os.path.join(sys_state["logRootPath"], sys_state["version"])
sys_state["checkpointStep"] = config.checkpoint
config_json = os.path.join(sys_state["projectRoot"], env_config["configJsonName"])
train_config = read_config(config_json)
for item in train_config.items():
if item[0] in ignoreKey:
pass
else:
sys_state[item[0]] = item[1]
sys_state["mode"] = config.mode
create_dirs(sys_state)
reporter = Reporter(sys_state["reporterPath"])
sys_state["com_base"] = "train_logs.%s.scripts."%sys_state["version"]
elif config.mode == "test":
sys_state["version"] = config.version
sys_state["logRootPath"] = env_config["trainLogRoot"]
sys_state["nodeName"] = config.nodeName
sys_state["totalImg"] = config.totalImg
sys_state["useSpecifiedImg"]= config.useSpecifiedImg
sys_state["checkpointStep"] = config.checkpoint
sys_state["testImgRoot"] = config.testImgRoot
sys_state["specify_sytle"] = config.specify_sytle
sys_state["testSamples"] = os.path.join(env_config["testLogRoot"], sys_state["version"] , "samples")
if not os.path.exists(sys_state["testSamples"]):
os.makedirs(sys_state["testSamples"])
if config.useSpecifiedImg:
sys_state["useSpecifiedImg"] = config.useSpecifiedImg
# Create dirs
create_dirs(sys_state)
config_json = os.path.join(sys_state["projectRoot"], env_config["configJsonName"])
# Read model_config.json from remote machine
if sys_state["nodeName"]!="localhost":
print("ready to fetch the %s from the server!"%config_json)
nodeinf = read_config(env_config["remoteNodeInfo"])
nodeinf = nodeinf[sys_state["nodeName"]]
uploader = fileUploaderClass(nodeinf["ip"],nodeinf["user"],nodeinf["passwd"])
remotebase = os.path.join(nodeinf['basePath'],"train_logs",sys_state["version"]).replace('\\','/')
# Get the config.json
print("ready to get the config.json...")
remoteFile = os.path.join(remotebase, env_config["configJsonName"]).replace('\\','/')
localFile = config_json
state = uploader.sshScpGet(remoteFile,localFile)
if not state:
raise Exception(print("Get file %s failed! Program exists!"%remoteFile))
print("success get the config file from server %s"%nodeinf['ip'])
# Read model_config.json
json_obj = read_config(config_json)
for item in json_obj.items():
if item[0] in ignoreKey:
pass
else:
sys_state[item[0]] = item[1]
# get the dataset path
sys_state["content"]= env_config["datasetPath"]["Place365_big"]
sys_state["style"] = env_config["datasetPath"]["WikiArt"]
# Read scripts from remote machine
if sys_state["nodeName"]!="localhost":
# Get scripts
remoteFile = os.path.join(remotebase, "scripts", sys_state["gScriptName"]+".py").replace('\\','/')
localFile = os.path.join(sys_state["projectScripts"], sys_state["gScriptName"]+".py")
state = uploader.sshScpGet(remoteFile, localFile)
if not state:
raise Exception(print("Get file %s failed! Program exists!"%remoteFile))
print("Get the scripts:%s.py successfully"%sys_state["gScriptName"])
# Get checkpoint of generator
localFile = os.path.join(sys_state["projectCheckpoints"], "%d_Generator.pth"%sys_state["checkpointStep"])
if not os.path.exists(localFile):
remoteFile = os.path.join(remotebase, "checkpoints", "%d_Generator.pth"%sys_state["checkpointStep"]).replace('\\','/')
state = uploader.sshScpGet(remoteFile, localFile, True)
if not state:
raise Exception(print("Get file %s failed! Program exists!"%remoteFile))
print("Get the %s file successfully"%("%d_Generator.pth"%sys_state["checkpointStep"]))
else:
print("%s file exists"%("%d_Generator.pth"%sys_state["checkpointStep"]))
sys_state["ckp_name"] = os.path.join(sys_state["projectCheckpoints"], "%d_Generator.pth"%sys_state["checkpointStep"])
# Get the test configurations
sys_state["testScriptsName"]= config.testScriptsName
sys_state["batchSize"] = config.testBatchSize
sys_state["totalImg"] = config.totalImg
sys_state["saveTestImg"] = config.saveTestImg
sys_state["com_base"] = "train_logs.%s.scripts."%sys_state["version"]
reporter = Reporter(sys_state["reporterPath"])
# Display the test information
moduleName = "test_scripts.tester_" + sys_state["testScriptsName"]
print("Start to run test script: {}".format(moduleName))
print("Test version: %s"%sys_state["version"])
print("Test Script Name: %s"%sys_state["testScriptsName"])
print("Generator Script Name: %s"%sys_state["gScriptName"])
package = __import__(moduleName, fromlist=True)
testerClass = getattr(package, 'Tester')
tester = testerClass(sys_state,reporter)
tester.test()
if config.mode == "train" or config.mode == "finetune":
# get the dataset path
sys_state["content"]= env_config["datasetPath"]["Place365_big"]
sys_state["style"] = env_config["datasetPath"]["WikiArt"]
# display the training information
moduleName = "train_scripts.trainer_" + sys_state["trainScriptName"]
if config.mode == "finetune":
moduleName = sys_state["com_base"] + "trainer_" + sys_state["trainScriptName"]
# print some important information
print("Start to run training script: {}".format(moduleName))
print("Traning version: %s"%sys_state["version"])
print("Training Script Name: %s"%sys_state["trainScriptName"])
print("Generator Script Name: %s"%sys_state["gScriptName"])
print("Discriminator Script Name: %s"%sys_state["dScriptName"])
print("D : G = %d : %d"%(sys_state["dStep"],sys_state["gStep"]))
print("Resblock number %d"%(sys_state["resNum"]))
# Load the training script and start to train
reporter.writeConfig(sys_state)
package = __import__(moduleName, fromlist=True)
trainerClass= getattr(package, 'Trainer')
trainer = trainerClass(sys_state,reporter)
# trainer = Trainer(sys_state,reporter)
trainer.train()