Skip to content

Commit d554a35

Browse files
hanxiaojina-bot
andauthored
fix: less verbose on gif and progress output (#127)
* fix: less verbose on gif and progress output * style: fix overload and cli autocomplete Co-authored-by: Jina Dev Bot <dev-bot@jina.ai>
1 parent 31a690e commit d554a35

6 files changed

Lines changed: 33 additions & 33 deletions

File tree

discoart/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
) as ymlfile:
2828
cut_schedules = yaml.load(ymlfile, Loader=Loader)
2929

30-
_legacy_args = {'clip_sequential_evaluation', 'fuzzy_prompt'}
30+
_legacy_args = {'clip_sequential_evaluation', 'fuzzy_prompt', 'display_rate'}
3131

3232

3333
def load_config(

discoart/create.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def create(
3939
diffusion_model: Optional[str] = '512x512_diffusion_uncond_finetune_008100',
4040
diffusion_model_config: Optional[Dict[str, Any]] = None,
4141
diffusion_sampling_mode: Optional[str] = 'ddim',
42-
display_rate: Optional[int] = None,
4342
eta: Optional[float] = 0.8,
4443
gif_fps: Optional[int] = 20,
4544
gif_size_ratio: Optional[float] = 0.5,
@@ -113,9 +112,8 @@ def create(**kwargs) -> Optional['DocumentArray']:
113112
:param diffusion_model: Diffusion_model of choice. Note that you don't have to write the full name of the diffusion model, e.g. any prefix is enough.To use a listed all diffusion models, you can do:```pythonfrom discoart import createcreate(diffusion_model='portrait_generator', ...)```
114113
:param diffusion_model_config: [DiscoArt] The customized diffusion model config as a dictionary, if specified will override the values with the same name in the default model config.
115114
:param diffusion_sampling_mode: Two alternate diffusion denoising algorithms. ddim has been around longer, and is more established and tested. plms is a newly added alternate method that promises good diffusion results in fewer steps, but has not been as fully tested and may have side effects. This new plms mode is actively being researched in the #settings-and-techniques channel in the DD Discord.
116-
:param display_rate: [DiscoArt] Display rate is deprecated in DiscoArt as it is always 1, meaning display is always in real-time. There is no need to worry on the speed as the rendering happens in another thread. To control the save rate, use the `save_rate` parameter.Setting this will override the `save_rate` parameter.
117115
:param eta: eta (greek letter η) is a diffusion model variable that mixes in a random amount of scaled noise into each timestep. 0 is no noise, 1.0 is more noise. As with most DD parameters, you can go below zero for eta, but it may give you unpredictable results. The steps parameter has a close relationship with the eta parameter. If you set eta to 0, then you can get decent output with only 50-75 steps. Setting eta to 1.0 favors higher step counts, ideally around 250 and up. eta has a subtle, unpredictable effect on image, so you’ll need to experiment to see how this affects your projects.
118-
:param gif_fps: [DiscoArt] The frame rate of the generated GIF.
116+
:param gif_fps: [DiscoArt] The frame rate of the generated GIF. Set it to -1 for not saving GIF.
119117
:param gif_size_ratio: [DiscoArt] The relative size vs. the original image, small size ratio gives smaller file size.
120118
:param init_document: [DiscoArt] Use a Document object as the initial state for DD: its ``.tags`` will be used as parameters, ``.uri`` (if present) will be used as init image.
121119
:param init_image: Recall that in the image sequence above, the first image shown is just noise. If an init_image is provided, diffusion will replace the noise with the init_image as its starting state. To use an init_image, upload the image to the Colab instance or your Google Drive, and enter the full image path here. If using an init_image, you may need to increase skip_steps to ~ 50% of total steps to retain the character of the init. See skip_steps above for further discussion.
@@ -129,7 +127,7 @@ def create(**kwargs) -> Optional['DocumentArray']:
129127
:param randomize_class: Controls whether the imagenet class is randomly changed each iteration
130128
:param range_scale: Optional, set to zero to turn off. Used for adjustment of color contrast. Lower range_scale will increase contrast. Very low numbers create a reduced color palette, resulting in more vibrant or poster-like images. Higher range_scale will reduce contrast, for more muted images.[DiscoArt] Can be scheduled via syntax `[val1]*400+[val2]*600`.
131129
:param sat_scale: Saturation scale. Optional, set to zero to turn off. If used, sat_scale will help mitigate oversaturation. If your image is too saturated, increase sat_scale to reduce the saturation.[DiscoArt] Can be scheduled via syntax `[val1]*400+[val2]*600`.
132-
:param save_rate: [DiscoArt] The number of steps to save intermediate results. It is a replacement to original `display_rate` parameter.
130+
:param save_rate: [DiscoArt] The number of steps to save intermediate results. It is a replacement to original `display_rate` parameter. Set it to -1 for not saving any intermediate result.
133131
:param seed: Deep in the diffusion code, there is a random number ‘seed’ which is used as the basis for determining the initial state of the diffusion. By default, this is random, but you can also specify your own seed. This is useful if you like a particular result and would like to run more iterations that will be similar. After each run, the actual seed value used will be reported in the parameters report, and can be reused if desired by entering seed # here. If a specific numerical seed is used repeatedly, the resulting images will be quite similar but not identical.
134132
:param skip_augs: Controls whether to skip torchvision augmentations.[DiscoArt] Can be scheduled via syntax `[val1]*400+[val2]*600`.
135133
:param skip_event: [DiscoArt] A multiprocessing/asyncio/threading.Event that once set, will skip the current run and move to the next run as defined in `n_batches`.

discoart/persist.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def _sample(
2929
output_dir,
3030
is_sampling_done,
3131
is_save_step,
32+
is_save_gif,
3233
):
3334
with threading.Lock():
3435
is_sampling_done.clear()
@@ -57,11 +58,13 @@ def _sample(
5758
)
5859

5960
da[k].chunks.append(c)
60-
# root doc always update with the latest progress
61-
da[k].uri = c.uri
62-
else:
61+
62+
if is_save_gif:
6363
da_gif[k].chunks.append(c)
6464

65+
# root doc always update with the latest progress
66+
da[k].uri = c.uri
67+
6568
da[k].tags['_status'] = {
6669
'completed': cur_t == -1,
6770
'cur_t': cur_t,

discoart/resources/default.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ cut_innercut: "[4]*400+[12]*600"
3636
cut_icgray_p: "[0.2]*400+[0]*600"
3737
cut_ic_pow: 1.
3838

39-
display_rate:
4039
save_rate: 20
4140
gif_fps: 20
4241
gif_size_ratio: 0.5

discoart/resources/docstrings.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,8 @@ cut_innercut: |
121121
cut_icgray_p: |
122122
In addition to the overall cut schedule, a portion of the cuts can be set to be grayscale instead of color. This may help with improved definition of shapes and edges, especially in the early diffusion steps where the image structure is being defined.
123123
124-
display_rate: |
125-
[DiscoArt] Display rate is deprecated in DiscoArt as it is always 1, meaning display is always in real-time.
126-
127-
There is no need to worry on the speed as the rendering happens in another thread. To control the save rate, use the `save_rate` parameter.
128-
Setting this will override the `save_rate` parameter.
129124
save_rate: |
130-
[DiscoArt] The number of steps to save intermediate results. It is a replacement to original `display_rate` parameter.
125+
[DiscoArt] The number of steps to save intermediate results. It is a replacement to original `display_rate` parameter. Set it to -1 for not saving any intermediate result.
131126
132127
n_batches: |
133128
This variable sets the number of still images you want DD to create. If you are using an animation mode (see below for details) DD will ignore n_batches and create a single set of animated frames based on the animation settings.
@@ -220,7 +215,7 @@ text_clip_on_cpu: |
220215
[DiscoArt] Place text transformers of CLIP models on CPU. This saves more VRAM and will not hurt the speed at all on T4, P100, 3090; however, there are few community members report issue on V100 when it is `False`.
221216
222217
gif_fps: |
223-
[DiscoArt] The frame rate of the generated GIF.
218+
[DiscoArt] The frame rate of the generated GIF. Set it to -1 for not saving GIF.
224219
225220
gif_size_ratio: |
226221
[DiscoArt] The relative size vs. the original image, small size ratio gives smaller file size.

discoart/runner.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import os.path
33
import random
4+
import tempfile
45
import threading
56

67
import clip
@@ -301,9 +302,9 @@ def cond_fn(x, t, **kwargs):
301302
x_is_NaN = True
302303
grad = torch.zeros_like(x)
303304
logger.warning(
304-
f'NaN detected in grad at the diffusion inner-step {num_step}, '
305-
f'if this message continues to show up, '
306-
f'then your image is not updated and further steps are unnecessary.'
305+
f'NaN detected in grad at the diffusion inner-step {num_step}, no panic. '
306+
f'However, if this message continues to show up *in a row*, '
307+
f'then your generation is ill-conditioned and image will not updated, further steps are unnecessary.'
307308
)
308309

309310
r_grad = grad
@@ -379,7 +380,6 @@ def cond_fn(x, t, **kwargs):
379380
_handlers,
380381
_redraw_fn,
381382
args,
382-
output_dir,
383383
_nb,
384384
)
385385
free_memory()
@@ -443,9 +443,9 @@ def cond_fn(x, t, **kwargs):
443443

444444
cur_t -= 1
445445

446-
is_save_step = (
447-
j % (args.display_rate or args.save_rate) == 0 or cur_t == -1
448-
)
446+
is_save_step = args.save_rate > 0 and j % args.save_rate == 0
447+
is_complete = cur_t == -1
448+
449449
threads.append(
450450
_sample_thread(
451451
sample,
@@ -459,6 +459,7 @@ def cond_fn(x, t, **kwargs):
459459
output_dir,
460460
is_busy_evs[0],
461461
is_save_step,
462+
args.gif_fps > 0,
462463
)
463464
)
464465

@@ -473,13 +474,15 @@ def cond_fn(x, t, **kwargs):
473474
args.gif_size_ratio,
474475
)
475476
)
477+
478+
if is_complete or is_save_step:
476479
threads.extend(
477480
_persist_thread(
478481
da_batches,
479482
args.name_docarray,
480483
is_busy_evs[1:],
481484
is_busy_evs[0],
482-
is_completed=cur_t == -1,
485+
is_completed=is_complete,
483486
)
484487
)
485488

@@ -497,19 +500,21 @@ def cond_fn(x, t, **kwargs):
497500
return da_batches
498501

499502

500-
def redraw_widget(_handlers, _redraw_fn, args, output_dir, _nb):
503+
def redraw_widget(_handlers, _redraw_fn, args, _nb):
501504
_handlers.progress.max = args.n_batches
502505
_handlers.progress.value = _nb + 1
503506
_handlers.progress.description = f'Baking {_nb + 1}/{args.n_batches}: '
504507

505-
svg0 = os.path.join(output_dir, 'config.svg')
506-
save_config_svg(args, svg0, only_non_default=True)
507-
d = Document(uri=svg0).convert_uri_to_datauri()
508-
_handlers.config.value = f'<img src="{d.uri}" alt="non-default config">'
509-
svg1 = os.path.join(output_dir, 'all-config.svg')
510-
save_config_svg(args, svg1)
511-
d = Document(uri=svg1).convert_uri_to_datauri()
512-
_handlers.all_config.value = f'<img src="{d.uri}" alt="all config">'
508+
with tempfile.NamedTemporaryFile(mode='wt', suffix='.svg') as fp:
509+
save_config_svg(args, fp.name, only_non_default=True)
510+
d = Document(uri=fp.name).convert_uri_to_datauri()
511+
_handlers.config.value = f'<img src="{d.uri}" alt="non-default config">'
512+
513+
with tempfile.NamedTemporaryFile(mode='wt', suffix='.svg') as fp:
514+
save_config_svg(args, fp.name)
515+
d = Document(uri=fp.name).convert_uri_to_datauri()
516+
_handlers.all_config.value = f'<img src="{d.uri}" alt="all config">'
517+
513518
_handlers.code.value = export_python(args)
514519
_redraw_fn()
515520

0 commit comments

Comments
 (0)