-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemCode.py
297 lines (249 loc) · 7.65 KB
/
SystemCode.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
295
296
297
import os
import sys
try:
from delphifuncts import *
print("Delphi OK")
except Exception as e:
# Most likely running from command line
have_delphi = False
sys.path.append('pysrc')
from delphifuncts import *
print("Delphi Missing")
import time
import logging
import torch
import json
from mlfuncts import *
have_psutils = True
try:
import psutil
except Exception as e:
have_psutils = False
def get_sys_info():
if gpu_supported:
d = torch.cuda.get_device_name(0)
t = torch.cuda.get_device_properties(0).total_memory
r = torch.cuda.memory_reserved(0)
a = torch.cuda.memory_allocated(0)
f = r-a # free inside reserved
gpu = TJsonLog(
device = d,
free = f,
reserved = r,
allocated = a,
total = t)
if have_psutils:
m = psutil.virtual_memory()
mem = TJsonLog(
total = m.total,
available = m.available,
percent = m.percent,
used = m.used,
free = m.free)
if have_psutils and gpu_supported:
stats = TJsonLog(gpu = gpu, mem = mem)
elif have_psutils and not gpu_supported:
stats = TJsonLog(gpu = False, mem = mem)
elif not have_psutils and gpu_supported:
stats = TJsonLog(gpu = gpu, mem = False)
else:
stats = TJsonLog(gpu = False, mem = False)
return(stats)
def check_gpu():
gpu_supported = False
try:
torch.cuda.init()
if(torch.cuda.is_available()):
gpu_supported = True
except:
pass
return gpu_supported
def show_elapsed(from_time):
elapsed = time.time() - from_time
print("Elapsed time = %f secs" % (elapsed))
hour = elapsed // 3600
elapsed %= 3600
minutes = elapsed // 60
elapsed %= 60
seconds = elapsed
print("Elapsed time = %d hours %d mins %d secs" % (hour, minutes, seconds))
def do_train(opts = None):
is_gpu_available = check_gpu()
if opts == None:
opts = TTrain(dataset="/train/unsplash/256",
style_image="style-images/gig.jpg",
model_name="gig-256",
model_dir="models",
checkpoint_model_dir="cache",
model_ext = ".pth",
net="vgg19",
vgg16_path=None,
vgg19_path=None,
logfile="",
epochs=2,
limit=0,
batch_size=8,
image_size=256,
seed=42,
content_weight=1e5,
style_weight=1e10,
lr=1e-3,
style_scale=1.0,
channels=32,
force_size=True,
ignore_gpu=False,
log_event_api=False)
# check_paths(args)
trial_batch = opts.batch_size
start = time.time()
while(1):
oom = False
try:
print("Trying batch of ", trial_batch)
if opts.ignore_gpu:
train(opts, False, trial_batch)
else:
train(opts, is_gpu_available, trial_batch)
except RuntimeError as e:
print("Hit exception handler")
if trial_batch > 0:
oom = True
else:
print(e)
return(1)
else:
break
if oom:
trial_batch -= 1
if is_gpu_available and not opts.ignore_gpu:
torch.cuda.empty_cache()
if trial_batch == 0:
print("No batch size found to run current training session (style image too large)")
return(1)
show_elapsed(start)
def do_stylize(opts = None):
is_gpu_available = check_gpu()
if opts == None:
opts = TStylize( content_image = "input-images/haywain.jpg",
content_image_raw = "",
output_image = "output-images/command-test.jpg",
model = "dae_mosaic_1-200",
model_dir = "models",
model_ext = ".pth",
logfile = "",
content_scale = 1,
ignore_gpu = True,
export_onnx = False,
add_model_ext = True,
log_event_api = False)
start = time.time()
if opts.ignore_gpu:
stylize(opts, False)
else:
stylize(opts, is_gpu_available)
show_elapsed(start)
def do_test(opts = None):
# is_gpu_available = check_gpu()
if opts == None:
opts = TStylize( content_image = "input-images\\haywain.jpg",
content_image_raw = "",
output_image = "output-images\\test-dae-sketch1-512.jpg",
model = "test-dae-sketch1-512",
# model = "mosaic-vgg16-1010-512",
model_dir = "models",
model_ext = ".pth",
logfile = "",
content_scale = 1,
ignore_gpu = False,
export_onnx = False,
add_model_ext = True,
log_event_api = False
)
for k, v in opts.items():
print(k, '=', v)
def delphi_train():
is_gpu_available = check_gpu()
trainopts = TDelphiTrain()
for i in ptrain.GetPropertyList():
print(i, '=', ptrain.GetProperty(i))
rval = None
trial_batch = trainopts.batch_size
start = time.time()
while(1):
oom = False
try:
print("Trying batch of ", trial_batch)
if trainopts.ignore_gpu:
rval = train(trainopts, False, trial_batch)
else:
rval = train(trainopts, is_gpu_available, trial_batch)
except RuntimeError as e:
print("Hit exception handler")
if trial_batch > 0:
oom = True
else:
print(e)
return("Unrecoverable Error")
else:
break
if oom:
trial_batch -= 1
if is_gpu_available and not trainopts.ignore_gpu:
torch.cuda.empty_cache()
if trial_batch == 0:
return("No batch size found to run current training session (style image too large)")
show_elapsed(start)
return (rval)
def delphi_style():
is_gpu_available = check_gpu()
styleopts = TDelphiStylize()
for i in pstyle.GetPropertyList():
print(i, '=', pstyle.GetProperty(i))
start = time.time()
if styleopts.ignore_gpu:
rval = stylize(styleopts, False)
else:
rval = stylize(styleopts, is_gpu_available)
show_elapsed(start)
return (rval)
class TStylize(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
class TTrain(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
class TProperties:
def __getattr__(Self, Key):
return props.GetProperty(Key)
def __setattr__(Self, Key, Value):
props.SetProperty(Key, Value)
def __repr__(Self):
tmp = ""
for i in props.GetPropertyList():
if tmp:
tmp = tmp + ", "
tmp = tmp + i + " = " + str(getattr(Self,i))
return tmp
def do_main():
print("Running from command line");
do_stylize()
try:
if not __embedded_python__:
do_main()
except NameError:
# Only run main if called explicitly
if __name__ == "__main__":
do_main()
else:
pass
# gpu_supported = check_gpu()
# print("Using Embedded Environment")
# print(json.dumps(get_sys_info()))
# styleopts = TDelphiStylize()
# for i in pstyle.GetPropertyList():
# print(i, '=', pstyle.GetProperty(i))
# trainopts = TDelphiTrain()
# for i in ptrain.GetPropertyList():
# print(i, '=', ptrain.GetProperty(i))