-
Notifications
You must be signed in to change notification settings - Fork 246
/
helper.py
525 lines (419 loc) · 16 KB
/
helper.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import gc
import hashlib
import logging
import os
import subprocess
import sys
import urllib.parse
import urllib.request
import warnings
from os.path import expanduser
from pathlib import Path
from typing import Dict, Any, List, Tuple
import regex as re
import torch
import yaml
from open_clip import SimpleTokenizer
from open_clip.tokenizer import whitespace_clean, basic_clean
from spellchecker import SpellChecker
from tqdm import tqdm
cache_dir = f'{expanduser("~")}/.cache/{__package__}'
from yaml import Loader
from . import __resources_path__
with open(
os.environ.get('DISCOART_MODELS_YAML', f'{__resources_path__}/models.yml')
) as ymlfile:
models_list = yaml.load(ymlfile, Loader=Loader)
def get_device():
# check if GPU is available
if torch.cuda.is_available():
device = torch.device('cuda:0')
else:
device = torch.device('cpu')
warnings.warn(
'''
!!!!CUDA is not available. DiscoArt is running on CPU. `create()` will be unbearably slow on CPU!!!!
Please switch to a GPU device. If you are using Google Colab, then free tier would just work.
'''
)
return device
def is_jupyter() -> bool: # pragma: no cover
"""
Check if we're running in a Jupyter notebook, using magic command `get_ipython` that only available in Jupyter.
:return: True if run in a Jupyter notebook else False.
"""
if 'DISCOART_DISABLE_IPYTHON' in os.environ:
return False
try:
get_ipython # noqa: F821
except NameError:
return False
shell = get_ipython().__class__.__name__ # noqa: F821
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'Shell':
return True # Google colab
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
def is_google_colab() -> bool: # pragma: no cover
if 'DISCOART_DISABLE_IPYTHON' in os.environ:
return False
try:
get_ipython # noqa: F821
except NameError:
return False
shell = get_ipython().__class__.__name__ # noqa: F821
return shell == 'Shell'
def get_ipython_funcs():
class NOP:
def __call__(self, *args, **kwargs):
return NOP()
__getattr__ = __enter__ = __exit__ = __call__
if is_jupyter():
from IPython import display as dp1
from IPython.display import FileLink as fl
from ipywidgets import Output
return dp1, fl, Output
else:
return NOP(), NOP(), NOP()
def _get_logger():
logger = logging.getLogger(__package__)
_log_level = os.environ.get('DISCOART_LOG_LEVEL', 'INFO')
logger.setLevel(_log_level)
ch = logging.StreamHandler()
ch.setLevel(_log_level)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
ch.setFormatter(formatter)
logger.handlers.clear()
logger.addHandler(ch)
logger.propagate = False
return logger
logger = _get_logger()
if not os.path.exists(cache_dir):
Path(cache_dir).mkdir(parents=True, exist_ok=True)
logger.debug(f'`.cache` dir is set to: {cache_dir}')
check_model_SHA = False
def _clone_repo_install(repo_url, repo_dir, commit_hash):
if os.path.exists(repo_dir):
res = subprocess.run(
['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, cwd=repo_dir
).stdout.decode('utf-8')
logger.debug(f'commit hash: {res}')
if res.strip() == commit_hash:
logger.debug(f'{repo_dir} is already cloned and up to date')
sys.path.append(repo_dir)
return
import shutil
shutil.rmtree(repo_dir)
res = subprocess.run(
['git', 'clone', '--depth', '1', repo_url, repo_dir], stdout=subprocess.PIPE
).stdout.decode('utf-8')
logger.debug(f'cloned {repo_url} to {repo_dir}: {res}')
sys.path.append(repo_dir)
def install_from_repos():
_clone_repo_install(
'https://github.com/kostarion/guided-diffusion',
f'{cache_dir}/guided_diffusion',
commit_hash='99afa5eb238f32aadaad38ae7107318ec4d987d3',
)
_clone_repo_install(
'https://github.com/assafshocher/ResizeRight',
f'{cache_dir}/resize_right',
commit_hash='510d4d5b67dccf4efdee9f311ed42609a71f17c5',
)
def _wget(url, outputdir):
logger.debug(f'downloading from {url}...')
try:
basename = os.path.basename(url)
with urllib.request.urlopen(url) as source, open(
os.path.join(outputdir, basename), 'wb'
) as output:
with tqdm(
total=int(source.info().get("Content-Length")),
ncols=80,
unit='iB',
unit_scale=True,
) as loop:
while True:
buffer = source.read(8192)
if not buffer:
break
output.write(buffer)
loop.update(len(buffer))
logger.debug(f'write to {outputdir}')
except:
logger.error(f'failed to download {url}')
def load_clip_models(device, enabled: List[str], clip_models: Dict[str, Any] = {}):
logger.debug('loading clip models...')
import open_clip
# load enabled models
for k in enabled:
if k not in clip_models:
if '::' in k:
# use open_clip loader
k1, k2 = k.split('::')
clip_models[k] = (
open_clip.create_model_and_transforms(k1, pretrained=k2)[0]
.eval()
.requires_grad_(False)
.to(device)
)
else:
raise ValueError(
f'''
Since v0.1, DiscoArt depends on `open-clip` which supports more CLIP variants and pretrained weights.
The new names is now a string in the format of `<model_name>::<pretrained_weights_name>`, e.g.
`ViT-B-32::openai` or `ViT-B-32::laion2b_e16`. The full list of supported models and weights can be found here:
https://github.com/mlfoundations/open_clip#pretrained-model-interface
'''
)
# disable not enabled models to save memory
for k in list(clip_models.keys()):
if k not in enabled:
clip_models.pop(k)
return clip_models
def _get_sha(path):
with open(path, 'rb') as f:
return hashlib.sha256(f.read()).hexdigest()
def download_model(model_name: str):
if os.path.isfile(model_name):
logger.debug('use customized local model')
return
if model_name not in models_list:
raise ValueError(
f'{model_name} is not supported, must be one of {models_list.keys()}'
)
model_filename = os.path.basename(models_list[model_name]['sources'][0])
model_local_path = os.path.join(cache_dir, model_filename)
if (
os.path.exists(model_local_path)
and _get_sha(model_local_path) == models_list[model_name]['sha']
):
logger.debug(f'{model_filename} is already downloaded with correct SHA')
else:
for url in models_list[model_name]['sources']:
_wget(url, cache_dir)
if _get_sha(model_local_path) == models_list[model_name]['sha']:
logger.debug(f'{model_filename} is downloaded with correct SHA')
break
def get_diffusion_config(user_args, device=torch.device('cuda:0')) -> Dict[str, Any]:
diffusion_model = user_args.diffusion_model
steps = user_args.steps
diffusion_config = user_args.diffusion_model_config
from guided_diffusion.script_util import (
model_and_diffusion_defaults,
)
model_config = model_and_diffusion_defaults()
if diffusion_model in models_list and models_list[diffusion_model].get(
'config', None
):
model_config.update(models_list[diffusion_model]['config'])
else:
logger.info(
'''
looks like you are using a custom diffusion model,
to override default diffusion model config, you can specify `create(diffusion_model_config={...}, ...)` as well,
'''
)
model_config.update(
{
'attention_resolutions': '16',
'class_cond': False,
'diffusion_steps': 1000,
'rescale_timesteps': True,
'timestep_respacing': 'ddim100',
'image_size': 256,
'learn_sigma': True,
'noise_schedule': 'linear',
'num_channels': 128,
'num_heads': 1,
'num_res_blocks': 2,
'use_checkpoint': True,
'use_scale_shift_norm': False,
}
)
timestep_respacing = f'ddim{steps}'
diffusion_steps = (1000 // steps) * steps if steps < 1000 else steps
model_config.update(
{
'use_fp16': device.type != 'cpu',
'timestep_respacing': timestep_respacing,
'diffusion_steps': diffusion_steps,
}
)
if diffusion_config and isinstance(diffusion_config, dict):
model_config.update(diffusion_config)
return model_config
def load_secondary_model(user_args, device=torch.device('cuda:0')):
if not user_args.use_secondary_model:
return
download_model('secondary')
from discoart.nn.sec_diff import SecondaryDiffusionImageNet2
secondary_model = SecondaryDiffusionImageNet2()
secondary_model.load_state_dict(
torch.load(f'{cache_dir}/secondary_model_imagenet_2.pth', map_location='cpu')
)
secondary_model.eval().requires_grad_(False).to(device)
return secondary_model
def load_diffusion_model(user_args, device):
diffusion_model = user_args.diffusion_model
if diffusion_model in models_list:
rec_size = models_list[diffusion_model].get('recommended_size', None)
if rec_size and user_args.width_height != rec_size:
logger.warning(
f'{diffusion_model} is recommended to have width_height {rec_size}, but you are using {user_args.width_height}. This may lead to suboptimal results.'
)
download_model(diffusion_model)
install_from_repos()
model_config = get_diffusion_config(user_args, device=device)
logger.debug('loading diffusion model...')
from guided_diffusion.script_util import (
create_model_and_diffusion,
)
model, diffusion = create_model_and_diffusion(**model_config)
if os.path.isfile(diffusion_model):
logger.debug(f'loading customized diffusion model from {diffusion_model}')
_model_path = diffusion_model
else:
_model_path = f'{cache_dir}/{diffusion_model}.pt'
model.load_state_dict(torch.load(_model_path, map_location='cpu'))
model.requires_grad_(False).eval().to(device)
for name, param in model.named_parameters():
if 'qkv' in name or 'norm' in name or 'proj' in name:
param.requires_grad_()
if model_config['use_fp16']:
model.convert_to_fp16()
return model, diffusion
class PromptParser(SimpleTokenizer):
def __init__(self, on_misspelled_token: str, **kwargs):
super().__init__(**kwargs)
self.spell = SpellChecker()
from . import __resources_path__
with open(f'{__resources_path__}/vocab.txt') as fp:
self.spell.word_frequency.load_words(
line.strip() for line in fp if len(line.strip()) > 1
)
self.on_misspelled_token = on_misspelled_token
@staticmethod
def _split_weight(prompt):
if ':' in prompt:
vals = prompt.rsplit(':', 1)
else:
vals = [prompt, 1]
return vals[0], float(vals[1])
def parse(self, text: str) -> Tuple[str, float]:
text, weight = self._split_weight(text)
text = whitespace_clean(basic_clean(text)).lower()
all_tokens = []
for token in re.findall(self.pat, text):
token = ''.join(self.byte_encoder[b] for b in token.encode('utf-8'))
all_tokens.append(token)
unknowns = [
v
for v in self.spell.unknown(all_tokens)
if len(v) > 2 and self.spell.correction(v) != v
]
if unknowns:
pairs = []
for v in unknowns:
vc = self.spell.correction(v)
pairs.append((v, vc))
if self.on_misspelled_token == 'correct':
for idx, ov in enumerate(all_tokens):
if ov == v:
all_tokens[idx] = vc
if pairs:
warning_str = '\n'.join(
f'Misspelled `{v}`, do you mean `{vc}`?' for v, vc in pairs
)
if self.on_misspelled_token == 'raise':
raise ValueError(warning_str)
elif self.on_misspelled_token == 'correct':
logger.warning(
'auto-corrected the following tokens:\n' + warning_str
)
else:
logger.warning(
'Found misspelled tokens in the prompt:\n' + warning_str
)
logger.debug(f'prompt: {all_tokens}, weight: {weight}')
return ' '.join(all_tokens), weight
def free_memory():
gc.collect()
torch.cuda.empty_cache()
def show_result_summary(_da, _name, _args):
from .config import print_args_table
_dp1, _fl, _ = get_ipython_funcs()
_dp1.clear_output(wait=True)
imcomplete_str = ''
fully_done = sum(_d.tags.get('completed', False) for _d in _da)
if _da and fully_done < _args.n_batches:
imcomplete_str = f'''
# ⚠️ Incomplete result ({fully_done}/{_args.n_batches})
Your `n_batches={_args.n_batches}` so supposedly {_args.n_batches} images will be generated,
but only {fully_done} images were fully completed. This may due to the following reasons:
- You cancel the process before it finishes;
- (On Google Colab) your GPU session is expired;
To avoid this, you can set `n_batches` to a smaller number in `create()`, say `create(n_batches=1)`.
'''
from rich.markdown import Markdown
md = Markdown(
f'''
{imcomplete_str}
# 👀 Result preview
This preview is **NOT** in HD. Do **NOT** use it for your final artworks.
To save the full-size images, please check out the instruction in the next section.
''',
code_theme='igor',
)
_dp1.display(md)
if _da and _da[0].uri:
_da.plot_image_sprites(skip_empty=True, show_index=True, keep_aspect_ratio=True)
print_args_table(vars(_args))
persist_file = _fl(
f'{_name}.protobuf.lz4',
result_html_prefix=f'▶ Download the local backup (in case cloud storage failed): ',
)
config_file = _fl(
f'{_name}.svg',
result_html_prefix=f'▶ Download the config as SVG image: ',
)
md = Markdown(
f'''
# 🖼️ Save images
Final results and intermediate results are created under the current working directory, e.g.
```text
./{_name}/[i]-step-[i].png
./{_name}/[i]-progress.png
./{_name}/[i]-done.png
```
where:
- `name-docarray` is the name of the run, you can specify it otherwise it is a random name.
- `i-*` is up to the value of `n_batches`.
- `*-done-*` is the final image on done.
- `*-step-*` is the intermediate image at certain step.
- `*-progress-*` is the sprite image of all intermediate results so far.
# 💾 Save & load the batch
Results are stored in a [DocumentArray](https://docarray.jina.ai/fundamentals/documentarray/) available both local and cloud.
You may also download the file manually and load it from local disk:
```python
da = DocumentArray.load_binary('{_name}.protobuf.lz4')
```
You can simply pull it from any machine:
```python
# pip install docarray[common]
from docarray import DocumentArray
da = DocumentArray.pull('{_name}')
```
More usage such as plotting, post-analysis can be found in the [README](https://github.com/jina-ai/discoart).
''',
code_theme='igor',
)
if is_google_colab():
_dp1.display(md)
else:
_dp1.display(config_file, persist_file, md)