Spaces:
Sleeping
Sleeping
fix:dk
Browse files- compo-singleone-v2-dev-acc.py +162 -30
compo-singleone-v2-dev-acc.py
CHANGED
@@ -27,6 +27,19 @@ import time
|
|
27 |
import re
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Determining the size of the passes
|
32 |
pass1_image_size = 1328
|
@@ -93,7 +106,9 @@ else:
|
|
93 |
#######################################################
|
94 |
|
95 |
def get_model_simplified_name_from_dirname(dirname):
|
96 |
-
|
|
|
|
|
97 |
|
98 |
def get_padded_checkpoint_no_from_filename(checkpoint_filename):
|
99 |
match = re.search(r'ckpt-(\d+)', checkpoint_filename)
|
@@ -138,7 +153,24 @@ def setup(opts):
|
|
138 |
saver = tf.train.Saver()
|
139 |
# saver2 = tf.train.Saver()
|
140 |
# saver3 = tf.train.Saver()
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
#Getting the model name
|
143 |
model_name = [p for p in os.listdir(path) if os.path.isdir(os.path.join(path, p))][0]
|
144 |
if not os.getenv('MODELNAME'):
|
@@ -170,13 +202,14 @@ def setup(opts):
|
|
170 |
# checkpoint3_dir = os.path.join(path, model3_name, 'checkpoint_long')
|
171 |
print("-----------------------------------------")
|
172 |
print("modelname is : " + model_name)
|
|
|
173 |
found_model=get_model_simplified_name_from_dirname(model_name)
|
174 |
|
175 |
#print("model2name is : " + model2_name)
|
176 |
# print("model3name is : " + model3_name)
|
177 |
print("checkpoint_dir is : " + checkpoint_dir)
|
178 |
|
179 |
-
|
180 |
|
181 |
|
182 |
#print("checkpoint2_dir is : " + checkpoint2_dir)
|
@@ -203,41 +236,131 @@ def setup(opts):
|
|
203 |
# models.m3 = m3
|
204 |
return models
|
205 |
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
global found_model,found_model_checkpoint
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
|
217 |
meta_inputs={'meta':text}
|
218 |
-
meta_outputs={'
|
219 |
|
220 |
@runway.command('meta2', inputs=meta_inputs, outputs=meta_outputs)
|
221 |
def get_geta(models, inp):
|
222 |
global found_model,found_model_checkpoint
|
223 |
|
224 |
-
json_return = _make_meta_as_json(
|
225 |
# "files": "nothing yet"
|
226 |
print(json_return)
|
227 |
return json_return
|
228 |
|
229 |
|
230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
|
233 |
#@STCGoal add number or text to specify resolution of the three pass
|
234 |
-
inputs={'contentImage': runway.image,'x1':number(default=1024,min=24,max=
|
235 |
-
outputs={'stylizedImage': runway.image,'totaltime':number,'x1': number,'c1':number,'model1name':text,'
|
236 |
|
237 |
@runway.command('stylize', inputs=inputs, outputs=outputs)
|
238 |
def stylize(models, inp):
|
|
|
239 |
start = time.time()
|
240 |
-
|
241 |
model = models.m1
|
242 |
#model2 = models.m2
|
243 |
# model3 = models.m3
|
@@ -249,12 +372,9 @@ def stylize(models, inp):
|
|
249 |
# m3name=models.m3.name
|
250 |
|
251 |
#get size from inputs rather than env
|
252 |
-
x1 = inp['x1']
|
253 |
-
|
254 |
-
|
255 |
-
if c1 > 99:
|
256 |
-
ci = abcdefault
|
257 |
-
|
258 |
|
259 |
#
|
260 |
img = inp['contentImage']
|
@@ -266,7 +386,7 @@ def stylize(models, inp):
|
|
266 |
image_size=x1
|
267 |
img_shape = img.shape[:2]
|
268 |
alpha = float(image_size) / float(min(img_shape))
|
269 |
-
dtprint ("DEBUG::content.imgshape:" + str(tuple(img_shape)) + ", alpha:" + str(alpha))
|
270 |
|
271 |
try:
|
272 |
img = scipy.misc.imresize(img, size=alpha)
|
@@ -278,7 +398,7 @@ def stylize(models, inp):
|
|
278 |
#@a INFERENCE PASS 1
|
279 |
dtprint("INFO:Pass1 inference starting")
|
280 |
img = model['sess'].run(model['output_photo'], feed_dict={model['input_photo']: img})
|
281 |
-
|
282 |
#
|
283 |
img = (img + 1.) * 127.5
|
284 |
img = img.astype('uint8')
|
@@ -339,18 +459,30 @@ def stylize(models, inp):
|
|
339 |
# #pass3
|
340 |
|
341 |
#dtprint("INFO:Composing done")
|
342 |
-
|
343 |
-
print(c1)
|
344 |
if c1 != 0 :
|
345 |
-
print('Auto Brightening images...')
|
346 |
img = img, alpha2, beta = automatic_brightness_and_contrast(img,c1)
|
347 |
|
348 |
stop = time.time()
|
349 |
totaltime = stop - start
|
350 |
print("The time of the run:", totaltime)
|
351 |
-
|
352 |
-
|
353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
|
355 |
|
356 |
|
|
|
27 |
import re
|
28 |
|
29 |
|
30 |
+
SRV_TYPE="s1"
|
31 |
+
|
32 |
+
#set env var RW_ if not already set
|
33 |
+
if not os.getenv('RW_PORT'):
|
34 |
+
os.environ["RW_PORT"] = "7860"
|
35 |
+
|
36 |
+
if not os.getenv('RW_DEBUG'):
|
37 |
+
os.environ["RW_DEBUG"] = "0"
|
38 |
+
if not os.getenv('RW_HOST'):
|
39 |
+
os.environ["RW_HOST"] = "0.0.0.0"
|
40 |
+
#RW_MODEL_OPTIONS
|
41 |
+
if not os.getenv('RW_MODEL_OPTIONS'):
|
42 |
+
os.environ["RW_MODEL_OPTIONS"]='{"styleCheckpoint":"/data/styleCheckpoint"}'
|
43 |
|
44 |
# Determining the size of the passes
|
45 |
pass1_image_size = 1328
|
|
|
106 |
#######################################################
|
107 |
|
108 |
def get_model_simplified_name_from_dirname(dirname):
|
109 |
+
result_simple_name = dirname.replace("model_","").replace("_864x","").replace("_864","").replace("_new","").replace("-864","")
|
110 |
+
print(" result_simple_name:" + result_simple_name)
|
111 |
+
return result_simple_name
|
112 |
|
113 |
def get_padded_checkpoint_no_from_filename(checkpoint_filename):
|
114 |
match = re.search(r'ckpt-(\d+)', checkpoint_filename)
|
|
|
153 |
saver = tf.train.Saver()
|
154 |
# saver2 = tf.train.Saver()
|
155 |
# saver3 = tf.train.Saver()
|
156 |
+
print("-------------====PATH---------------------->>>>--")
|
157 |
+
path_default = '/data/styleCheckpoint'
|
158 |
+
print("opts:")
|
159 |
+
print(opts)
|
160 |
+
print("----------------------------------------")
|
161 |
+
if opts is None:
|
162 |
+
print("ERROR:opts is None")
|
163 |
+
path = path_default
|
164 |
+
try:
|
165 |
+
path = opts['styleCheckpoint']
|
166 |
+
except:
|
167 |
+
opts= {'styleCheckpoint': u'/data/styleCheckpoint'}
|
168 |
+
path = opts['styleCheckpoint']
|
169 |
+
if not os.path.exists(path):
|
170 |
+
print("ERROR:Path does not exist:" + path)
|
171 |
+
path = path_default
|
172 |
+
print(path)
|
173 |
+
print("----------------PATH=======---------------<<<<--")
|
174 |
#Getting the model name
|
175 |
model_name = [p for p in os.listdir(path) if os.path.isdir(os.path.join(path, p))][0]
|
176 |
if not os.getenv('MODELNAME'):
|
|
|
202 |
# checkpoint3_dir = os.path.join(path, model3_name, 'checkpoint_long')
|
203 |
print("-----------------------------------------")
|
204 |
print("modelname is : " + model_name)
|
205 |
+
|
206 |
found_model=get_model_simplified_name_from_dirname(model_name)
|
207 |
|
208 |
#print("model2name is : " + model2_name)
|
209 |
# print("model3name is : " + model3_name)
|
210 |
print("checkpoint_dir is : " + checkpoint_dir)
|
211 |
|
212 |
+
|
213 |
|
214 |
|
215 |
#print("checkpoint2_dir is : " + checkpoint2_dir)
|
|
|
236 |
# models.m3 = m3
|
237 |
return models
|
238 |
|
239 |
+
|
240 |
+
|
241 |
+
def make_target_output_filename( mname,checkpoint, fn='',res1=0,abc=0, ext='.jpg',svrtype="s1", modelid='', suffix='', xtra_model_id='',verbose=False):
|
242 |
+
fn_base=fn.replace(ext,"")
|
243 |
+
fn_base=fn_base.replace(".jpg","")
|
244 |
+
fn_base=fn_base.replace(".jpeg","")
|
245 |
+
fn_base=fn_base.replace(".JPG","")
|
246 |
+
fn_base=fn_base.replace(".JPEG","")
|
247 |
+
fn_base=fn_base.replace(".png","")
|
248 |
+
fn_base=fn_base.replace(".PNG","")
|
249 |
+
|
250 |
+
#pad res1 and res2 to 4 digits
|
251 |
+
res1_pad=str(res1).zfill(4)
|
252 |
+
|
253 |
+
abc_pad=str(abc).zfill(2)
|
254 |
+
if res1_pad=="0000":
|
255 |
+
res1_pad=""
|
256 |
+
|
257 |
+
|
258 |
+
#pad checkpoint to 3 digits
|
259 |
+
checkpoint=checkpoint.zfill(3)
|
260 |
+
|
261 |
+
if fn_base=="none":
|
262 |
+
fn_base=""
|
263 |
+
|
264 |
+
if '/' in fn_base:
|
265 |
+
fn_base=fn_base.split('/')[-1]
|
266 |
+
# Print out all input info:
|
267 |
+
if verbose :
|
268 |
+
|
269 |
+
print("-----------------------------")
|
270 |
+
print("fn_base: ",fn_base)
|
271 |
+
print("mname: ",mname)
|
272 |
+
print("suffix: ",suffix)
|
273 |
+
print("res1: ",res1_pad)
|
274 |
+
print("abc: ",abc_pad)
|
275 |
+
print("ext: ",ext)
|
276 |
+
print("svrtype: ",svrtype)
|
277 |
+
print("modelid: ",modelid)
|
278 |
+
print("xtra_model_id: ",xtra_model_id)
|
279 |
+
print("checkpoint: ",checkpoint)
|
280 |
+
print("fn: ",fn)
|
281 |
+
|
282 |
+
mtag = "{}__{}__{}x{}__{}__{}k".format(mname,suffix,res1_pad,abc_pad, svrtype, checkpoint).replace("_0x" + str(abc_pad), "")
|
283 |
+
if verbose:
|
284 |
+
print(mtag)
|
285 |
+
target_output = "{}__{}__{}{}{}".format(fn_base, modelid, mtag, xtra_model_id, ext).replace("_"+str(abc_pad)+"x"+str(abc_pad)+"_","").replace("_0x0_", "").replace("_0_", "").replace("_-", "_").replace("____", "__").replace("___", "__").replace("___", "__").replace("..",".").replace("model_","").replace("_x"+str(abc_pad)+"_","").replace("gia-ds-","")
|
286 |
+
target_output = replace_values_from_csv(target_output)
|
287 |
+
|
288 |
+
return target_output
|
289 |
+
|
290 |
+
def replace_values_from_csv(target_output):
|
291 |
+
# Implement the logic to replace values from CSV
|
292 |
+
#load replacer.csv and replace the values (src,dst)
|
293 |
+
src_dest_file = 'replacer.csv'
|
294 |
+
if os.path.exists(src_dest_file):
|
295 |
+
with open(src_dest_file, 'r') as file:
|
296 |
+
lines = file.readlines()
|
297 |
+
for line in lines:
|
298 |
+
src, dst = line.split(',')
|
299 |
+
target_output = target_output.replace(src, dst)
|
300 |
+
return target_output.replace("\n", "").replace("\r", "").replace(" ", "_")
|
301 |
+
|
302 |
+
|
303 |
+
def _make_meta_as_json(x1=0,c1=0,inp=None,result_dict=None):
|
304 |
global found_model,found_model_checkpoint
|
305 |
+
fn='none'
|
306 |
+
if inp['fn'] != 'none':
|
307 |
+
fn=inp['fn']
|
308 |
+
ext='.jpg'
|
309 |
+
if inp['ext'] != '.jpg':
|
310 |
+
ext=inp['ext']
|
311 |
+
|
312 |
+
filename=make_target_output_filename(found_model,found_model_checkpoint,fn,x1,c1,ext,SRV_TYPE)
|
313 |
+
|
314 |
+
if result_dict is None:
|
315 |
+
json_return = {
|
316 |
+
"model": str(found_model),
|
317 |
+
"checkpoint": str(found_model_checkpoint),
|
318 |
+
"filename": str(filename)
|
319 |
+
}
|
320 |
+
return json_return
|
321 |
+
else: #support adding to the existing dict the data directly
|
322 |
+
result_dict['model']=str(found_model)
|
323 |
+
result_dict['checkpoint']=str(found_model_checkpoint)
|
324 |
+
result_dict['filename']=str(filename)
|
325 |
+
return result_dict
|
326 |
+
|
327 |
|
328 |
|
329 |
meta_inputs={'meta':text}
|
330 |
+
meta_outputs={'model':text,'filename':text,'checkpoint':text}
|
331 |
|
332 |
@runway.command('meta2', inputs=meta_inputs, outputs=meta_outputs)
|
333 |
def get_geta(models, inp):
|
334 |
global found_model,found_model_checkpoint
|
335 |
|
336 |
+
json_return = _make_meta_as_json()
|
337 |
# "files": "nothing yet"
|
338 |
print(json_return)
|
339 |
return json_return
|
340 |
|
341 |
|
342 |
|
343 |
+
@runway.command('meta', inputs=meta_inputs, outputs=meta_outputs)
|
344 |
+
def get_geta(models, inp):
|
345 |
+
global found_model,found_model_checkpoint
|
346 |
+
|
347 |
+
json_return = _make_meta_as_json(inp)
|
348 |
+
# "files": "nothing yet"
|
349 |
+
print(json_return)
|
350 |
+
return json_return
|
351 |
+
|
352 |
+
|
353 |
|
354 |
|
355 |
#@STCGoal add number or text to specify resolution of the three pass
|
356 |
+
inputs={'contentImage': runway.image,'x1':number(default=1024,min=24,max=18000),'c1':number(default=0,min=-99,max=99),'fn':text(default='none'),'ext':text(default='.jpg')}
|
357 |
+
outputs={'stylizedImage': runway.image,'totaltime':number,'x1': number,'c1':number,'model1name':text,'checkpoint':text,'filename':text,'model':text}
|
358 |
|
359 |
@runway.command('stylize', inputs=inputs, outputs=outputs)
|
360 |
def stylize(models, inp):
|
361 |
+
global found_model,found_model_checkpoint,model1name
|
362 |
start = time.time()
|
363 |
+
|
364 |
model = models.m1
|
365 |
#model2 = models.m2
|
366 |
# model3 = models.m3
|
|
|
372 |
# m3name=models.m3.name
|
373 |
|
374 |
#get size from inputs rather than env
|
375 |
+
x1 = int(inp['x1'])
|
376 |
+
|
377 |
+
c1 = int(inp['c1'])
|
|
|
|
|
|
|
378 |
|
379 |
#
|
380 |
img = inp['contentImage']
|
|
|
386 |
image_size=x1
|
387 |
img_shape = img.shape[:2]
|
388 |
alpha = float(image_size) / float(min(img_shape))
|
389 |
+
#dtprint ("DEBUG::content.imgshape:" + str(tuple(img_shape)) + ", alpha:" + str(alpha))
|
390 |
|
391 |
try:
|
392 |
img = scipy.misc.imresize(img, size=alpha)
|
|
|
398 |
#@a INFERENCE PASS 1
|
399 |
dtprint("INFO:Pass1 inference starting")
|
400 |
img = model['sess'].run(model['output_photo'], feed_dict={model['input_photo']: img})
|
401 |
+
|
402 |
#
|
403 |
img = (img + 1.) * 127.5
|
404 |
img = img.astype('uint8')
|
|
|
459 |
# #pass3
|
460 |
|
461 |
#dtprint("INFO:Composing done")
|
462 |
+
|
|
|
463 |
if c1 != 0 :
|
464 |
+
print('Auto Brightening images...' + str(c1))
|
465 |
img = img, alpha2, beta = automatic_brightness_and_contrast(img,c1)
|
466 |
|
467 |
stop = time.time()
|
468 |
totaltime = stop - start
|
469 |
print("The time of the run:", totaltime)
|
470 |
+
|
471 |
+
#if model1name UNNAMED, use found_model
|
472 |
+
if model1name == "UNNAMED":
|
473 |
+
model1name=found_model
|
474 |
+
|
475 |
+
include_meta_directly_in_result=True
|
476 |
+
|
477 |
+
|
478 |
+
if include_meta_directly_in_result:
|
479 |
+
result_dict = dict(stylizedImage=img,totaltime=totaltime,x1=x1,model1name=model1name,c1=c1)
|
480 |
+
result_dict = _make_meta_as_json(x1,c1,inp,result_dict)
|
481 |
+
else:
|
482 |
+
meta_data = _make_meta_as_json(x1,c1,inp)
|
483 |
+
result_dict = dict(stylizedImage=img,totaltime=totaltime,x1=x1,model1name=model1name,c1=c1,meta=meta_data)
|
484 |
+
|
485 |
+
return result_dict
|
486 |
|
487 |
|
488 |
|