hcpdiff 0.9.1__py3-none-any.whl → 2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hcpdiff/__init__.py +4 -4
- hcpdiff/ckpt_manager/__init__.py +4 -5
- hcpdiff/ckpt_manager/ckpt.py +24 -0
- hcpdiff/ckpt_manager/format/__init__.py +4 -0
- hcpdiff/ckpt_manager/format/diffusers.py +59 -0
- hcpdiff/ckpt_manager/format/emb.py +21 -0
- hcpdiff/ckpt_manager/format/lora_webui.py +244 -0
- hcpdiff/ckpt_manager/format/sd_single.py +41 -0
- hcpdiff/ckpt_manager/loader.py +64 -0
- hcpdiff/data/__init__.py +4 -28
- hcpdiff/data/cache/__init__.py +1 -0
- hcpdiff/data/cache/vae.py +102 -0
- hcpdiff/data/dataset.py +20 -0
- hcpdiff/data/handler/__init__.py +3 -0
- hcpdiff/data/handler/controlnet.py +18 -0
- hcpdiff/data/handler/diffusion.py +80 -0
- hcpdiff/data/handler/text.py +111 -0
- hcpdiff/data/source/__init__.py +1 -2
- hcpdiff/data/source/folder_class.py +12 -29
- hcpdiff/data/source/text2img.py +36 -74
- hcpdiff/data/source/text2img_cond.py +9 -15
- hcpdiff/diffusion/__init__.py +0 -0
- hcpdiff/diffusion/noise/__init__.py +2 -0
- hcpdiff/diffusion/noise/pyramid_noise.py +42 -0
- hcpdiff/diffusion/noise/zero_terminal.py +39 -0
- hcpdiff/diffusion/sampler/__init__.py +5 -0
- hcpdiff/diffusion/sampler/base.py +72 -0
- hcpdiff/diffusion/sampler/ddpm.py +20 -0
- hcpdiff/diffusion/sampler/diffusers.py +66 -0
- hcpdiff/diffusion/sampler/edm.py +22 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/__init__.py +3 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/base.py +14 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/ddpm.py +197 -0
- hcpdiff/diffusion/sampler/sigma_scheduler/edm.py +48 -0
- hcpdiff/easy/__init__.py +2 -0
- hcpdiff/easy/cfg/__init__.py +3 -0
- hcpdiff/easy/cfg/sd15_train.py +201 -0
- hcpdiff/easy/cfg/sdxl_train.py +140 -0
- hcpdiff/easy/cfg/t2i.py +177 -0
- hcpdiff/easy/model/__init__.py +2 -0
- hcpdiff/easy/model/cnet.py +31 -0
- hcpdiff/easy/model/loader.py +79 -0
- hcpdiff/easy/sampler.py +46 -0
- hcpdiff/evaluate/__init__.py +1 -0
- hcpdiff/evaluate/previewer.py +60 -0
- hcpdiff/loss/__init__.py +4 -1
- hcpdiff/loss/base.py +41 -0
- hcpdiff/loss/gw.py +35 -0
- hcpdiff/loss/ssim.py +37 -0
- hcpdiff/loss/vlb.py +79 -0
- hcpdiff/loss/weighting.py +66 -0
- hcpdiff/models/__init__.py +2 -2
- hcpdiff/models/cfg_context.py +17 -14
- hcpdiff/models/compose/compose_hook.py +44 -23
- hcpdiff/models/compose/compose_tokenizer.py +21 -8
- hcpdiff/models/compose/sdxl_composer.py +4 -4
- hcpdiff/models/controlnet.py +16 -16
- hcpdiff/models/lora_base_patch.py +14 -25
- hcpdiff/models/lora_layers.py +3 -9
- hcpdiff/models/lora_layers_patch.py +14 -24
- hcpdiff/models/text_emb_ex.py +84 -6
- hcpdiff/models/textencoder_ex.py +54 -18
- hcpdiff/models/wrapper/__init__.py +3 -0
- hcpdiff/models/wrapper/pixart.py +19 -0
- hcpdiff/models/wrapper/sd.py +218 -0
- hcpdiff/models/wrapper/utils.py +20 -0
- hcpdiff/parser/__init__.py +1 -0
- hcpdiff/parser/embpt.py +32 -0
- hcpdiff/tools/convert_caption_txt2json.py +1 -1
- hcpdiff/tools/dataset_generator.py +94 -0
- hcpdiff/tools/download_hf_model.py +24 -0
- hcpdiff/tools/init_proj.py +3 -21
- hcpdiff/tools/lora_convert.py +18 -17
- hcpdiff/tools/save_model.py +12 -0
- hcpdiff/tools/sd2diffusers.py +1 -1
- hcpdiff/train_colo.py +1 -1
- hcpdiff/train_deepspeed.py +1 -1
- hcpdiff/trainer_ac.py +79 -0
- hcpdiff/trainer_ac_single.py +31 -0
- hcpdiff/utils/__init__.py +0 -2
- hcpdiff/utils/inpaint_pipe.py +7 -2
- hcpdiff/utils/net_utils.py +29 -6
- hcpdiff/utils/pipe_hook.py +24 -7
- hcpdiff/utils/utils.py +21 -4
- hcpdiff/workflow/__init__.py +15 -10
- hcpdiff/workflow/daam/__init__.py +1 -0
- hcpdiff/workflow/daam/act.py +66 -0
- hcpdiff/workflow/daam/hook.py +109 -0
- hcpdiff/workflow/diffusion.py +114 -125
- hcpdiff/workflow/fast.py +31 -0
- hcpdiff/workflow/flow.py +67 -0
- hcpdiff/workflow/io.py +36 -130
- hcpdiff/workflow/model.py +46 -43
- hcpdiff/workflow/text.py +78 -46
- hcpdiff/workflow/utils.py +32 -12
- hcpdiff/workflow/vae.py +37 -38
- hcpdiff-2.1.dist-info/METADATA +285 -0
- hcpdiff-2.1.dist-info/RECORD +114 -0
- {hcpdiff-0.9.1.dist-info → hcpdiff-2.1.dist-info}/WHEEL +1 -1
- hcpdiff-2.1.dist-info/entry_points.txt +5 -0
- hcpdiff/ckpt_manager/base.py +0 -16
- hcpdiff/ckpt_manager/ckpt_diffusers.py +0 -45
- hcpdiff/ckpt_manager/ckpt_pkl.py +0 -138
- hcpdiff/ckpt_manager/ckpt_safetensor.py +0 -64
- hcpdiff/ckpt_manager/ckpt_webui.py +0 -54
- hcpdiff/data/bucket.py +0 -358
- hcpdiff/data/caption_loader.py +0 -80
- hcpdiff/data/cond_dataset.py +0 -40
- hcpdiff/data/crop_info_dataset.py +0 -40
- hcpdiff/data/data_processor.py +0 -33
- hcpdiff/data/pair_dataset.py +0 -146
- hcpdiff/data/sampler.py +0 -54
- hcpdiff/data/source/base.py +0 -30
- hcpdiff/data/utils.py +0 -80
- hcpdiff/deprecated/__init__.py +0 -1
- hcpdiff/deprecated/cfg_converter.py +0 -81
- hcpdiff/deprecated/lora_convert.py +0 -31
- hcpdiff/infer_workflow.py +0 -57
- hcpdiff/loggers/__init__.py +0 -13
- hcpdiff/loggers/base_logger.py +0 -76
- hcpdiff/loggers/cli_logger.py +0 -40
- hcpdiff/loggers/preview/__init__.py +0 -1
- hcpdiff/loggers/preview/image_previewer.py +0 -149
- hcpdiff/loggers/tensorboard_logger.py +0 -30
- hcpdiff/loggers/wandb_logger.py +0 -31
- hcpdiff/loggers/webui_logger.py +0 -9
- hcpdiff/loss/min_snr_loss.py +0 -52
- hcpdiff/models/layers.py +0 -81
- hcpdiff/models/plugin.py +0 -348
- hcpdiff/models/wrapper.py +0 -75
- hcpdiff/noise/__init__.py +0 -3
- hcpdiff/noise/noise_base.py +0 -16
- hcpdiff/noise/pyramid_noise.py +0 -50
- hcpdiff/noise/zero_terminal.py +0 -44
- hcpdiff/train_ac.py +0 -566
- hcpdiff/train_ac_single.py +0 -39
- hcpdiff/utils/caption_tools.py +0 -105
- hcpdiff/utils/cfg_net_tools.py +0 -321
- hcpdiff/utils/cfg_resolvers.py +0 -16
- hcpdiff/utils/ema.py +0 -52
- hcpdiff/utils/img_size_tool.py +0 -248
- hcpdiff/vis/__init__.py +0 -3
- hcpdiff/vis/base_interface.py +0 -12
- hcpdiff/vis/disk_interface.py +0 -48
- hcpdiff/vis/webui_interface.py +0 -17
- hcpdiff/viser_fast.py +0 -138
- hcpdiff/visualizer.py +0 -265
- hcpdiff/visualizer_reloadable.py +0 -237
- hcpdiff/workflow/base.py +0 -59
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/anime/text2img_anime.yaml +0 -21
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/anime/text2img_anime_lora.yaml +0 -58
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/change_vae.yaml +0 -6
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/euler_a.yaml +0 -8
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/img2img.yaml +0 -10
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/img2img_controlnet.yaml +0 -19
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/inpaint.yaml +0 -11
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/load_lora.yaml +0 -26
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/load_unet_part.yaml +0 -18
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/offload_2GB.yaml +0 -6
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/save_model.yaml +0 -44
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img.yaml +0 -53
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img_DA++.yaml +0 -34
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img_sdxl.yaml +0 -9
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/plugins/plugin_controlnet.yaml +0 -17
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/te_struct.txt +0 -193
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/dataset/base_dataset.yaml +0 -29
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/dataset/regularization_dataset.yaml +0 -31
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/CustomDiffusion.yaml +0 -74
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamArtist++.yaml +0 -135
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamArtist.yaml +0 -45
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamBooth.yaml +0 -62
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/FT_sdxl.yaml +0 -33
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/Lion_optimizer.yaml +0 -17
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/TextualInversion.yaml +0 -41
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/add_logger_tensorboard_wandb.yaml +0 -15
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/controlnet.yaml +0 -53
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/ema.yaml +0 -10
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/fine-tuning.yaml +0 -53
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/locon.yaml +0 -24
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_anime_character.yaml +0 -77
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_conventional.yaml +0 -56
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_sdxl.yaml +0 -41
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/min_snr.yaml +0 -7
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/preview_in_training.yaml +0 -6
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/DreamBooth.yaml +0 -70
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/TextualInversion.yaml +0 -45
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/fine-tuning.yaml +0 -45
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/lora.yaml +0 -63
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/train_base.yaml +0 -81
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/tuning_base.yaml +0 -42
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/unet_struct.txt +0 -932
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/highres_fix_latent.yaml +0 -86
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/highres_fix_pixel.yaml +0 -99
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/text2img.yaml +0 -59
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/text2img_lora.yaml +0 -70
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/zero2.json +0 -32
- hcpdiff-0.9.1.data/data/hcpdiff/cfgs/zero3.json +0 -39
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/caption.txt +0 -1
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name.txt +0 -1
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name_2pt_caption.txt +0 -1
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name_caption.txt +0 -1
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/object.txt +0 -27
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/object_caption.txt +0 -27
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/style.txt +0 -19
- hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/style_caption.txt +0 -19
- hcpdiff-0.9.1.dist-info/METADATA +0 -199
- hcpdiff-0.9.1.dist-info/RECORD +0 -160
- hcpdiff-0.9.1.dist-info/entry_points.txt +0 -2
- {hcpdiff-0.9.1.dist-info → hcpdiff-2.1.dist-info/licenses}/LICENSE +0 -0
- {hcpdiff-0.9.1.dist-info → hcpdiff-2.1.dist-info}/top_level.txt +0 -0
hcpdiff/utils/img_size_tool.py
DELETED
@@ -1,248 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
|
3
|
-
img_size_tool.py
|
4
|
-
====================
|
5
|
-
|
6
|
-
:Name: get_image_size
|
7
|
-
:Purpose: extract image dimensions given a file path
|
8
|
-
|
9
|
-
:Author: Paulo Scardine (based on code from Emmanuel VAÏSSE)
|
10
|
-
Dong Ziyi, add webp support
|
11
|
-
|
12
|
-
:Created: 26/09/2013
|
13
|
-
:Modified: 02/03/2023
|
14
|
-
:Copyright: (c) Paulo Scardine 2013
|
15
|
-
:Licence: MIT
|
16
|
-
|
17
|
-
"""
|
18
|
-
|
19
|
-
import collections
|
20
|
-
import os
|
21
|
-
import io
|
22
|
-
import struct
|
23
|
-
from PIL import Image
|
24
|
-
|
25
|
-
FILE_UNKNOWN = "Sorry, don't know how to get size for this file."
|
26
|
-
|
27
|
-
class UnknownImageFormat(Exception):
|
28
|
-
pass
|
29
|
-
|
30
|
-
types_support = ['bmp', 'gif', 'ico', 'jpeg', 'jpg', 'png', 'tiff', 'webp']
|
31
|
-
|
32
|
-
def get_image_size(file_path):
|
33
|
-
"""
|
34
|
-
Return (width, height) for a given img file content - no external
|
35
|
-
dependencies except the os and struct builtin modules
|
36
|
-
"""
|
37
|
-
width, height = get_image_metadata(file_path)
|
38
|
-
return width, height
|
39
|
-
|
40
|
-
|
41
|
-
def get_image_size_from_bytesio(input, size):
|
42
|
-
"""
|
43
|
-
Return (width, height) for a given img file content - no external
|
44
|
-
dependencies except the os and struct builtin modules
|
45
|
-
|
46
|
-
Args:
|
47
|
-
input (io.IOBase): io object support read & seek
|
48
|
-
size (int): size of buffer in byte
|
49
|
-
"""
|
50
|
-
width, height = get_image_metadata_from_bytesio(input, size)
|
51
|
-
return width, height
|
52
|
-
|
53
|
-
|
54
|
-
def get_image_metadata(file_path):
|
55
|
-
"""
|
56
|
-
Return an `Image` object for a given img file content - no external
|
57
|
-
dependencies except the os and struct builtin modules
|
58
|
-
|
59
|
-
Args:
|
60
|
-
file_path (str): path to an image file
|
61
|
-
|
62
|
-
Returns:
|
63
|
-
(width, height)
|
64
|
-
"""
|
65
|
-
size = os.path.getsize(file_path)
|
66
|
-
|
67
|
-
# be explicit with open arguments - we need binary mode
|
68
|
-
with io.open(file_path, "rb") as input:
|
69
|
-
return get_image_metadata_from_bytesio(input, size, file_path)
|
70
|
-
|
71
|
-
|
72
|
-
def get_image_metadata_from_bytesio(input, size, file_path=None):
|
73
|
-
"""
|
74
|
-
Return an `Image` object for a given img file content - no external
|
75
|
-
dependencies except the os and struct builtin modules
|
76
|
-
|
77
|
-
Args:
|
78
|
-
input (io.IOBase): io object support read & seek
|
79
|
-
size (int): size of buffer in byte
|
80
|
-
file_path (str): path to an image file
|
81
|
-
|
82
|
-
Returns:
|
83
|
-
(width, height)
|
84
|
-
"""
|
85
|
-
height = -1
|
86
|
-
width = -1
|
87
|
-
data = input.read(30)
|
88
|
-
msg = " raised while trying to decode as JPEG."
|
89
|
-
|
90
|
-
if (size >= 10) and data[:6] in (b'GIF87a', b'GIF89a'):
|
91
|
-
# GIFs
|
92
|
-
#imgtype = GIF
|
93
|
-
w, h = struct.unpack("<HH", data[6:10])
|
94
|
-
width = int(w)
|
95
|
-
height = int(h)
|
96
|
-
elif (size >= 24) and data[8:12] == b'WEBP':
|
97
|
-
# WEBPs
|
98
|
-
#imgtype = WEBP
|
99
|
-
if data[15]==b'X': #VP8X
|
100
|
-
w = int.from_bytes(data[24:27], 'little')+1
|
101
|
-
h = int.from_bytes(data[27:30], 'little')+1
|
102
|
-
elif data[15]==b' ': #VP8
|
103
|
-
w, h = struct.unpack("<HH", data[0x1A:0x1E])
|
104
|
-
else:
|
105
|
-
w, h = Image.open(file_path).size
|
106
|
-
|
107
|
-
width = int(w)
|
108
|
-
height = int(h)
|
109
|
-
elif ((size >= 24) and data.startswith(b'\211PNG\r\n\032\n')
|
110
|
-
and (data[12:16] == b'IHDR')):
|
111
|
-
# PNGs
|
112
|
-
#imgtype = PNG
|
113
|
-
w, h = struct.unpack(">LL", data[16:24])
|
114
|
-
width = int(w)
|
115
|
-
height = int(h)
|
116
|
-
elif (size >= 16) and data.startswith(b'\211PNG\r\n\032\n'):
|
117
|
-
# older PNGs
|
118
|
-
#imgtype = PNG
|
119
|
-
w, h = struct.unpack(">LL", data[8:16])
|
120
|
-
width = int(w)
|
121
|
-
height = int(h)
|
122
|
-
elif (size >= 2) and data.startswith(b'\377\330'):
|
123
|
-
# JPEG
|
124
|
-
#imgtype = JPEG
|
125
|
-
input.seek(0)
|
126
|
-
input.read(2)
|
127
|
-
b = input.read(1)
|
128
|
-
try:
|
129
|
-
while (b and ord(b) != 0xDA):
|
130
|
-
while (ord(b) != 0xFF):
|
131
|
-
b = input.read(1)
|
132
|
-
while (ord(b) == 0xFF):
|
133
|
-
b = input.read(1)
|
134
|
-
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
|
135
|
-
input.read(3)
|
136
|
-
h, w = struct.unpack(">HH", input.read(4))
|
137
|
-
break
|
138
|
-
else:
|
139
|
-
input.read(
|
140
|
-
int(struct.unpack(">H", input.read(2))[0]) - 2)
|
141
|
-
b = input.read(1)
|
142
|
-
width = int(w)
|
143
|
-
height = int(h)
|
144
|
-
except struct.error:
|
145
|
-
raise UnknownImageFormat("StructError" + msg)
|
146
|
-
except ValueError:
|
147
|
-
raise UnknownImageFormat("ValueError" + msg)
|
148
|
-
except Exception as e:
|
149
|
-
raise UnknownImageFormat(e.__class__.__name__ + msg)
|
150
|
-
elif (size >= 26) and data.startswith(b'BM'):
|
151
|
-
# BMP
|
152
|
-
#imgtype = BMP
|
153
|
-
headersize = struct.unpack("<I", data[14:18])[0]
|
154
|
-
if headersize == 12:
|
155
|
-
w, h = struct.unpack("<HH", data[18:22])
|
156
|
-
width = int(w)
|
157
|
-
height = int(h)
|
158
|
-
elif headersize >= 40:
|
159
|
-
w, h = struct.unpack("<ii", data[18:26])
|
160
|
-
width = int(w)
|
161
|
-
# as h is negative when stored upside down
|
162
|
-
height = abs(int(h))
|
163
|
-
else:
|
164
|
-
raise UnknownImageFormat(
|
165
|
-
"Unkown DIB header size:" +
|
166
|
-
str(headersize))
|
167
|
-
elif (size >= 8) and data[:4] in (b"II\052\000", b"MM\000\052"):
|
168
|
-
# Standard TIFF, big- or little-endian
|
169
|
-
# BigTIFF and other different but TIFF-like formats are not
|
170
|
-
# supported currently
|
171
|
-
#imgtype = TIFF
|
172
|
-
byteOrder = data[:2]
|
173
|
-
boChar = ">" if byteOrder == "MM" else "<"
|
174
|
-
# maps TIFF type id to size (in bytes)
|
175
|
-
# and python format char for struct
|
176
|
-
tiffTypes = {
|
177
|
-
1: (1, boChar + "B"), # BYTE
|
178
|
-
2: (1, boChar + "c"), # ASCII
|
179
|
-
3: (2, boChar + "H"), # SHORT
|
180
|
-
4: (4, boChar + "L"), # LONG
|
181
|
-
5: (8, boChar + "LL"), # RATIONAL
|
182
|
-
6: (1, boChar + "b"), # SBYTE
|
183
|
-
7: (1, boChar + "c"), # UNDEFINED
|
184
|
-
8: (2, boChar + "h"), # SSHORT
|
185
|
-
9: (4, boChar + "l"), # SLONG
|
186
|
-
10: (8, boChar + "ll"), # SRATIONAL
|
187
|
-
11: (4, boChar + "f"), # FLOAT
|
188
|
-
12: (8, boChar + "d") # DOUBLE
|
189
|
-
}
|
190
|
-
ifdOffset = struct.unpack(boChar + "L", data[4:8])[0]
|
191
|
-
try:
|
192
|
-
countSize = 2
|
193
|
-
input.seek(ifdOffset)
|
194
|
-
ec = input.read(countSize)
|
195
|
-
ifdEntryCount = struct.unpack(boChar + "H", ec)[0]
|
196
|
-
# 2 bytes: TagId + 2 bytes: type + 4 bytes: count of values + 4
|
197
|
-
# bytes: value offset
|
198
|
-
ifdEntrySize = 12
|
199
|
-
for i in range(ifdEntryCount):
|
200
|
-
entryOffset = ifdOffset + countSize + i * ifdEntrySize
|
201
|
-
input.seek(entryOffset)
|
202
|
-
tag = input.read(2)
|
203
|
-
tag = struct.unpack(boChar + "H", tag)[0]
|
204
|
-
if(tag == 256 or tag == 257):
|
205
|
-
# if type indicates that value fits into 4 bytes, value
|
206
|
-
# offset is not an offset but value itself
|
207
|
-
type = input.read(2)
|
208
|
-
type = struct.unpack(boChar + "H", type)[0]
|
209
|
-
if type not in tiffTypes:
|
210
|
-
raise UnknownImageFormat(
|
211
|
-
"Unkown TIFF field type:" +
|
212
|
-
str(type))
|
213
|
-
typeSize = tiffTypes[type][0]
|
214
|
-
typeChar = tiffTypes[type][1]
|
215
|
-
input.seek(entryOffset + 8)
|
216
|
-
value = input.read(typeSize)
|
217
|
-
value = int(struct.unpack(typeChar, value)[0])
|
218
|
-
if tag == 256:
|
219
|
-
width = value
|
220
|
-
else:
|
221
|
-
height = value
|
222
|
-
if width > -1 and height > -1:
|
223
|
-
break
|
224
|
-
except Exception as e:
|
225
|
-
raise UnknownImageFormat(str(e))
|
226
|
-
elif size >= 2:
|
227
|
-
# see http://en.wikipedia.org/wiki/ICO_(file_format)
|
228
|
-
#imgtype = 'ICO'
|
229
|
-
input.seek(0)
|
230
|
-
reserved = input.read(2)
|
231
|
-
if 0 != struct.unpack("<H", reserved)[0]:
|
232
|
-
raise UnknownImageFormat(FILE_UNKNOWN)
|
233
|
-
format = input.read(2)
|
234
|
-
assert 1 == struct.unpack("<H", format)[0]
|
235
|
-
num = input.read(2)
|
236
|
-
num = struct.unpack("<H", num)[0]
|
237
|
-
if num > 1:
|
238
|
-
import warnings
|
239
|
-
warnings.warn("ICO File contains more than one image")
|
240
|
-
# http://msdn.microsoft.com/en-us/library/ms997538.aspx
|
241
|
-
w = input.read(1)
|
242
|
-
h = input.read(1)
|
243
|
-
width = ord(w)
|
244
|
-
height = ord(h)
|
245
|
-
else:
|
246
|
-
raise UnknownImageFormat(FILE_UNKNOWN)
|
247
|
-
|
248
|
-
return width, height
|
hcpdiff/vis/__init__.py
DELETED
hcpdiff/vis/base_interface.py
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
|
2
|
-
class BaseInterface:
|
3
|
-
need_inter_imgs = False
|
4
|
-
|
5
|
-
def __init__(self, show_steps=0):
|
6
|
-
self.show_steps = show_steps
|
7
|
-
|
8
|
-
def on_inter_step(self, i, num_steps, t, latents, images):
|
9
|
-
pass
|
10
|
-
|
11
|
-
def on_infer_finish(self, images, prompt, negative_prompt, save_cfg=False, seeds=None):
|
12
|
-
pass
|
hcpdiff/vis/disk_interface.py
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
|
3
|
-
from hcpdiff.utils.img_size_tool import types_support
|
4
|
-
from hcpdiff.utils.utils import to_validate_file
|
5
|
-
from omegaconf import OmegaConf
|
6
|
-
|
7
|
-
from .base_interface import BaseInterface
|
8
|
-
|
9
|
-
class DiskInterface(BaseInterface):
|
10
|
-
def __init__(self, save_root, save_cfg=True, image_type='png', quality=95, show_steps=0):
|
11
|
-
super(DiskInterface, self).__init__(show_steps=show_steps)
|
12
|
-
os.makedirs(save_root, exist_ok=True)
|
13
|
-
self.save_root = save_root
|
14
|
-
self.save_cfg = save_cfg
|
15
|
-
self.image_type = image_type
|
16
|
-
self.quality = quality
|
17
|
-
|
18
|
-
self.inter_imgs = []
|
19
|
-
if show_steps>0:
|
20
|
-
self.need_inter_imgs = True
|
21
|
-
|
22
|
-
def on_inter_step(self, i, num_steps, t, latents, images):
|
23
|
-
if len(self.inter_imgs) == 0:
|
24
|
-
for _ in range(len(images)):
|
25
|
-
self.inter_imgs.append([])
|
26
|
-
for u, img in enumerate(images):
|
27
|
-
self.inter_imgs[u].append(img)
|
28
|
-
|
29
|
-
def on_save_one(self, num_img_exist, img_path):
|
30
|
-
pass
|
31
|
-
|
32
|
-
def on_infer_finish(self, images, prompt, negative_prompt, cfgs_raw=None, seeds=None):
|
33
|
-
num_img_exist = max([0]+[int(x.split('-', 1)[0]) for x in os.listdir(self.save_root) if x.rsplit('.', 1)[-1] in types_support])+1
|
34
|
-
|
35
|
-
for bid, (p, pn, img) in enumerate(zip(prompt, negative_prompt, images)):
|
36
|
-
img_path = os.path.join(self.save_root, f"{num_img_exist}-{seeds[bid]}-{to_validate_file(prompt[0])}.{self.image_type}")
|
37
|
-
img.save(img_path, quality=self.quality)
|
38
|
-
self.on_save_one(num_img_exist, img_path)
|
39
|
-
|
40
|
-
if self.save_cfg and cfgs_raw is not None:
|
41
|
-
with open(os.path.join(self.save_root, f"{num_img_exist}-{seeds[bid]}-info.yaml"), 'w', encoding='utf-8') as f:
|
42
|
-
cfgs_raw.seed = seeds[bid]
|
43
|
-
f.write(OmegaConf.to_yaml(cfgs_raw))
|
44
|
-
if self.need_inter_imgs:
|
45
|
-
inter = self.inter_imgs[bid]
|
46
|
-
inter[0].save(os.path.join(self.save_root, f'{num_img_exist}-{seeds[bid]}-steps.webp'), "webp", save_all=True,
|
47
|
-
append_images=inter[1:], duration=100)
|
48
|
-
num_img_exist += 1
|
hcpdiff/vis/webui_interface.py
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
from .disk_interface import DiskInterface
|
2
|
-
from loguru import logger
|
3
|
-
|
4
|
-
class WebUIInterface(DiskInterface):
|
5
|
-
|
6
|
-
def __init__(self, save_root, image_type='png', quality=95, show_steps=1, show_inter=False):
|
7
|
-
super(WebUIInterface, self).__init__(save_root, image_type, quality, show_steps)
|
8
|
-
self.show_inter = show_inter
|
9
|
-
self.need_inter_imgs = self.need_inter_imgs and show_inter
|
10
|
-
|
11
|
-
def on_inter_step(self, i, num_steps, t, latents, images):
|
12
|
-
if self.show_inter:
|
13
|
-
super(WebUIInterface, self).on_inter_step(i, num_steps, t, latents, images)
|
14
|
-
logger.info(f'\nthis progress steps: {i}/{num_steps}')
|
15
|
-
|
16
|
-
def on_save_one(self, num_img_exist, img_path):
|
17
|
-
logger.info(f'this images output path: {img_path}')
|
hcpdiff/viser_fast.py
DELETED
@@ -1,138 +0,0 @@
|
|
1
|
-
import argparse
|
2
|
-
import os
|
3
|
-
from typing import List
|
4
|
-
|
5
|
-
import hydra
|
6
|
-
import torch
|
7
|
-
from sfast.compilers.diffusion_pipeline_compiler import (compile_unet, CompilationConfig)
|
8
|
-
from torch.cuda.amp import autocast
|
9
|
-
|
10
|
-
from hcpdiff import Visualizer
|
11
|
-
from hcpdiff.models import TokenizerHook
|
12
|
-
from hcpdiff.models.compose import ComposeTEEXHook, ComposeEmbPTHook, ComposeTextEncoder
|
13
|
-
from hcpdiff.utils.net_utils import to_cuda
|
14
|
-
from hcpdiff.utils.utils import load_config_with_cli, prepare_seed, is_list, pad_attn_bias
|
15
|
-
|
16
|
-
class VisualizerFast(Visualizer):
|
17
|
-
dtype_dict = {'fp32':torch.float32, 'fp16':torch.float16, 'bf16':torch.bfloat16}
|
18
|
-
|
19
|
-
def __init__(self, cfgs):
|
20
|
-
self.cfgs_raw = cfgs
|
21
|
-
self.cfgs = hydra.utils.instantiate(self.cfgs_raw)
|
22
|
-
self.cfg_merge = self.cfgs.merge
|
23
|
-
self.offload = 'offload' in self.cfgs and self.cfgs.offload is not None
|
24
|
-
self.dtype = self.dtype_dict[self.cfgs.dtype]
|
25
|
-
|
26
|
-
self.need_inter_imgs = any(item.need_inter_imgs for item in self.cfgs.interface)
|
27
|
-
|
28
|
-
self.pipe = self.load_model(self.cfgs.pretrained_model)
|
29
|
-
|
30
|
-
if self.cfg_merge:
|
31
|
-
self.merge_model()
|
32
|
-
|
33
|
-
# self.pipe = self.pipe.to(torch_dtype=self.dtype)
|
34
|
-
|
35
|
-
if isinstance(self.pipe.text_encoder, ComposeTextEncoder):
|
36
|
-
self.pipe.vae = self.pipe.vae.to(dtype=torch.float32)
|
37
|
-
|
38
|
-
if 'save_model' in self.cfgs and self.cfgs.save_model is not None:
|
39
|
-
self.save_model(self.cfgs.save_model)
|
40
|
-
os._exit(0)
|
41
|
-
|
42
|
-
self.build_optimize()
|
43
|
-
self.compile_model()
|
44
|
-
|
45
|
-
def build_optimize(self):
|
46
|
-
if self.offload:
|
47
|
-
self.build_offload(self.cfgs.offload)
|
48
|
-
else:
|
49
|
-
self.pipe.unet.to('cuda')
|
50
|
-
self.build_vae_offload()
|
51
|
-
|
52
|
-
if getattr(self.cfgs, 'vae_optimize', None) is not None:
|
53
|
-
if self.cfgs.vae_optimize.tiling:
|
54
|
-
self.pipe.vae.enable_tiling()
|
55
|
-
if self.cfgs.vae_optimize.slicing:
|
56
|
-
self.pipe.vae.enable_slicing()
|
57
|
-
|
58
|
-
self.emb_hook, _ = ComposeEmbPTHook.hook_from_dir(self.cfgs.emb_dir, self.pipe.tokenizer, self.pipe.text_encoder,
|
59
|
-
N_repeats=self.cfgs.N_repeats)
|
60
|
-
self.te_hook = ComposeTEEXHook.hook_pipe(self.pipe, N_repeats=self.cfgs.N_repeats, clip_skip=self.cfgs.clip_skip,
|
61
|
-
clip_final_norm=self.cfgs.clip_final_norm, use_attention_mask=self.cfgs.encoder_attention_mask)
|
62
|
-
self.token_ex = TokenizerHook(self.pipe.tokenizer)
|
63
|
-
|
64
|
-
def compile_model(self):
|
65
|
-
# compile model
|
66
|
-
config = CompilationConfig.Default()
|
67
|
-
config.enable_xformers = False
|
68
|
-
try:
|
69
|
-
import xformers
|
70
|
-
config.enable_xformers = True
|
71
|
-
except ImportError:
|
72
|
-
print('xformers not installed, skip')
|
73
|
-
# NOTE:
|
74
|
-
# When GPU VRAM is insufficient or the architecture is too old, Triton might be slow.
|
75
|
-
# Disable Triton if you encounter this problem.
|
76
|
-
try:
|
77
|
-
import tritonx
|
78
|
-
config.enable_triton = True
|
79
|
-
except ImportError:
|
80
|
-
print('Triton not installed, skip')
|
81
|
-
config.enable_cuda_graph = True
|
82
|
-
|
83
|
-
self.pipe.unet = compile_unet(self.pipe.unet, config)
|
84
|
-
|
85
|
-
@torch.inference_mode()
|
86
|
-
def vis_images(self, prompt, negative_prompt='', seeds: List[int] = None, **kwargs):
|
87
|
-
G = prepare_seed(seeds or [None]*len(prompt))
|
88
|
-
|
89
|
-
ex_input_dict, pipe_input_dict = self.get_ex_input()
|
90
|
-
kwargs.update(pipe_input_dict)
|
91
|
-
|
92
|
-
to_cuda(self.pipe.text_encoder)
|
93
|
-
|
94
|
-
mult_p, clean_text_p = self.token_ex.parse_attn_mult(prompt)
|
95
|
-
mult_n, clean_text_n = self.token_ex.parse_attn_mult(negative_prompt)
|
96
|
-
with autocast(enabled=self.cfgs.amp, dtype=self.dtype):
|
97
|
-
emb, pooled_output, attention_mask = self.te_hook.encode_prompt_to_emb(clean_text_n+clean_text_p)
|
98
|
-
if self.cfgs.encoder_attention_mask:
|
99
|
-
emb, attention_mask = pad_attn_bias(emb, attention_mask)
|
100
|
-
else:
|
101
|
-
attention_mask = None
|
102
|
-
emb_n, emb_p = emb.chunk(2)
|
103
|
-
emb_p = self.te_hook.mult_attn(emb_p, mult_p)
|
104
|
-
emb_n = self.te_hook.mult_attn(emb_n, mult_n)
|
105
|
-
|
106
|
-
# to_cpu(self.pipe.text_encoder)
|
107
|
-
# to_cuda(self.pipe.unet)
|
108
|
-
|
109
|
-
if hasattr(self.pipe.unet, 'input_feeder'):
|
110
|
-
for feeder in self.pipe.unet.input_feeder:
|
111
|
-
feeder(ex_input_dict)
|
112
|
-
|
113
|
-
images = self.pipe(prompt_embeds=emb_p, negative_prompt_embeds=emb_n, callback=None, generator=G,
|
114
|
-
pooled_output=pooled_output[-1], encoder_attention_mask=attention_mask, **kwargs).images
|
115
|
-
return images
|
116
|
-
|
117
|
-
if __name__ == '__main__':
|
118
|
-
parser = argparse.ArgumentParser(description='Fast HCP Diffusion Inference')
|
119
|
-
parser.add_argument('--cfg', type=str, default='cfgs/infer/text2img.yaml')
|
120
|
-
args, cfg_args = parser.parse_known_args()
|
121
|
-
cfgs = load_config_with_cli(args.cfg, args_list=cfg_args) # skip --cfg
|
122
|
-
|
123
|
-
if cfgs.seed is not None:
|
124
|
-
if is_list(cfgs.seed):
|
125
|
-
assert len(cfgs.seed) == cfgs.num*cfgs.bs, 'seed list length should be equal to num*bs'
|
126
|
-
seeds = list(cfgs.seed)
|
127
|
-
else:
|
128
|
-
seeds = list(range(cfgs.seed, cfgs.seed+cfgs.num*cfgs.bs))
|
129
|
-
else:
|
130
|
-
seeds = [None]*(cfgs.num*cfgs.bs)
|
131
|
-
|
132
|
-
viser = VisualizerFast(cfgs)
|
133
|
-
|
134
|
-
for i in range(cfgs.num):
|
135
|
-
prompt = cfgs.prompt[i*cfgs.bs:(i+1)*cfgs.bs] if is_list(cfgs.prompt) else [cfgs.prompt]*cfgs.bs
|
136
|
-
negative_prompt = cfgs.neg_prompt[i*cfgs.bs:(i+1)*cfgs.bs] if is_list(cfgs.neg_prompt) else [cfgs.neg_prompt]*cfgs.bs
|
137
|
-
viser.vis_to_dir(prompt=prompt, negative_prompt=negative_prompt,
|
138
|
-
seeds=seeds[i*cfgs.bs:(i+1)*cfgs.bs], save_cfg=cfgs.save.save_cfg, **cfgs.infer_args)
|