hcpdiff 0.9.0__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.
Files changed (208) hide show
  1. hcpdiff/__init__.py +4 -4
  2. hcpdiff/ckpt_manager/__init__.py +4 -5
  3. hcpdiff/ckpt_manager/ckpt.py +24 -0
  4. hcpdiff/ckpt_manager/format/__init__.py +4 -0
  5. hcpdiff/ckpt_manager/format/diffusers.py +59 -0
  6. hcpdiff/ckpt_manager/format/emb.py +21 -0
  7. hcpdiff/ckpt_manager/format/lora_webui.py +244 -0
  8. hcpdiff/ckpt_manager/format/sd_single.py +41 -0
  9. hcpdiff/ckpt_manager/loader.py +64 -0
  10. hcpdiff/data/__init__.py +4 -28
  11. hcpdiff/data/cache/__init__.py +1 -0
  12. hcpdiff/data/cache/vae.py +102 -0
  13. hcpdiff/data/dataset.py +20 -0
  14. hcpdiff/data/handler/__init__.py +3 -0
  15. hcpdiff/data/handler/controlnet.py +18 -0
  16. hcpdiff/data/handler/diffusion.py +80 -0
  17. hcpdiff/data/handler/text.py +111 -0
  18. hcpdiff/data/source/__init__.py +1 -2
  19. hcpdiff/data/source/folder_class.py +12 -29
  20. hcpdiff/data/source/text2img.py +36 -74
  21. hcpdiff/data/source/text2img_cond.py +9 -15
  22. hcpdiff/diffusion/__init__.py +0 -0
  23. hcpdiff/diffusion/noise/__init__.py +2 -0
  24. hcpdiff/diffusion/noise/pyramid_noise.py +42 -0
  25. hcpdiff/diffusion/noise/zero_terminal.py +39 -0
  26. hcpdiff/diffusion/sampler/__init__.py +5 -0
  27. hcpdiff/diffusion/sampler/base.py +72 -0
  28. hcpdiff/diffusion/sampler/ddpm.py +20 -0
  29. hcpdiff/diffusion/sampler/diffusers.py +66 -0
  30. hcpdiff/diffusion/sampler/edm.py +22 -0
  31. hcpdiff/diffusion/sampler/sigma_scheduler/__init__.py +3 -0
  32. hcpdiff/diffusion/sampler/sigma_scheduler/base.py +14 -0
  33. hcpdiff/diffusion/sampler/sigma_scheduler/ddpm.py +197 -0
  34. hcpdiff/diffusion/sampler/sigma_scheduler/edm.py +48 -0
  35. hcpdiff/easy/__init__.py +2 -0
  36. hcpdiff/easy/cfg/__init__.py +3 -0
  37. hcpdiff/easy/cfg/sd15_train.py +201 -0
  38. hcpdiff/easy/cfg/sdxl_train.py +140 -0
  39. hcpdiff/easy/cfg/t2i.py +177 -0
  40. hcpdiff/easy/model/__init__.py +2 -0
  41. hcpdiff/easy/model/cnet.py +31 -0
  42. hcpdiff/easy/model/loader.py +79 -0
  43. hcpdiff/easy/sampler.py +46 -0
  44. hcpdiff/evaluate/__init__.py +1 -0
  45. hcpdiff/evaluate/previewer.py +60 -0
  46. hcpdiff/loss/__init__.py +4 -1
  47. hcpdiff/loss/base.py +41 -0
  48. hcpdiff/loss/gw.py +35 -0
  49. hcpdiff/loss/ssim.py +37 -0
  50. hcpdiff/loss/vlb.py +79 -0
  51. hcpdiff/loss/weighting.py +66 -0
  52. hcpdiff/models/__init__.py +2 -2
  53. hcpdiff/models/cfg_context.py +17 -14
  54. hcpdiff/models/compose/compose_hook.py +44 -23
  55. hcpdiff/models/compose/compose_tokenizer.py +21 -8
  56. hcpdiff/models/compose/sdxl_composer.py +4 -4
  57. hcpdiff/models/container.py +1 -1
  58. hcpdiff/models/controlnet.py +16 -16
  59. hcpdiff/models/lora_base_patch.py +14 -25
  60. hcpdiff/models/lora_layers.py +3 -9
  61. hcpdiff/models/lora_layers_patch.py +14 -24
  62. hcpdiff/models/text_emb_ex.py +84 -6
  63. hcpdiff/models/textencoder_ex.py +54 -18
  64. hcpdiff/models/wrapper/__init__.py +3 -0
  65. hcpdiff/models/wrapper/pixart.py +19 -0
  66. hcpdiff/models/wrapper/sd.py +218 -0
  67. hcpdiff/models/wrapper/utils.py +20 -0
  68. hcpdiff/parser/__init__.py +1 -0
  69. hcpdiff/parser/embpt.py +32 -0
  70. hcpdiff/tools/convert_caption_txt2json.py +1 -1
  71. hcpdiff/tools/dataset_generator.py +94 -0
  72. hcpdiff/tools/download_hf_model.py +24 -0
  73. hcpdiff/tools/embedding_convert.py +6 -2
  74. hcpdiff/tools/init_proj.py +3 -21
  75. hcpdiff/tools/lora_convert.py +19 -15
  76. hcpdiff/tools/save_model.py +12 -0
  77. hcpdiff/tools/sd2diffusers.py +1 -1
  78. hcpdiff/train_colo.py +1 -1
  79. hcpdiff/train_deepspeed.py +1 -1
  80. hcpdiff/trainer_ac.py +79 -0
  81. hcpdiff/trainer_ac_single.py +31 -0
  82. hcpdiff/utils/__init__.py +0 -2
  83. hcpdiff/utils/inpaint_pipe.py +790 -0
  84. hcpdiff/utils/net_utils.py +29 -6
  85. hcpdiff/utils/pipe_hook.py +46 -33
  86. hcpdiff/utils/utils.py +21 -4
  87. hcpdiff/workflow/__init__.py +15 -10
  88. hcpdiff/workflow/daam/__init__.py +1 -0
  89. hcpdiff/workflow/daam/act.py +66 -0
  90. hcpdiff/workflow/daam/hook.py +109 -0
  91. hcpdiff/workflow/diffusion.py +128 -136
  92. hcpdiff/workflow/fast.py +31 -0
  93. hcpdiff/workflow/flow.py +67 -0
  94. hcpdiff/workflow/io.py +36 -68
  95. hcpdiff/workflow/model.py +46 -43
  96. hcpdiff/workflow/text.py +84 -52
  97. hcpdiff/workflow/utils.py +32 -12
  98. hcpdiff/workflow/vae.py +37 -38
  99. hcpdiff-2.1.dist-info/METADATA +285 -0
  100. hcpdiff-2.1.dist-info/RECORD +114 -0
  101. {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info}/WHEEL +1 -1
  102. hcpdiff-2.1.dist-info/entry_points.txt +5 -0
  103. hcpdiff/ckpt_manager/base.py +0 -16
  104. hcpdiff/ckpt_manager/ckpt_diffusers.py +0 -45
  105. hcpdiff/ckpt_manager/ckpt_pkl.py +0 -138
  106. hcpdiff/ckpt_manager/ckpt_safetensor.py +0 -60
  107. hcpdiff/ckpt_manager/ckpt_webui.py +0 -54
  108. hcpdiff/data/bucket.py +0 -358
  109. hcpdiff/data/caption_loader.py +0 -80
  110. hcpdiff/data/cond_dataset.py +0 -40
  111. hcpdiff/data/crop_info_dataset.py +0 -40
  112. hcpdiff/data/data_processor.py +0 -33
  113. hcpdiff/data/pair_dataset.py +0 -146
  114. hcpdiff/data/sampler.py +0 -54
  115. hcpdiff/data/source/base.py +0 -30
  116. hcpdiff/data/utils.py +0 -80
  117. hcpdiff/infer_workflow.py +0 -57
  118. hcpdiff/loggers/__init__.py +0 -13
  119. hcpdiff/loggers/base_logger.py +0 -76
  120. hcpdiff/loggers/cli_logger.py +0 -40
  121. hcpdiff/loggers/preview/__init__.py +0 -1
  122. hcpdiff/loggers/preview/image_previewer.py +0 -149
  123. hcpdiff/loggers/tensorboard_logger.py +0 -30
  124. hcpdiff/loggers/wandb_logger.py +0 -31
  125. hcpdiff/loggers/webui_logger.py +0 -9
  126. hcpdiff/loss/min_snr_loss.py +0 -52
  127. hcpdiff/models/layers.py +0 -81
  128. hcpdiff/models/plugin.py +0 -348
  129. hcpdiff/models/wrapper.py +0 -75
  130. hcpdiff/noise/__init__.py +0 -3
  131. hcpdiff/noise/noise_base.py +0 -16
  132. hcpdiff/noise/pyramid_noise.py +0 -50
  133. hcpdiff/noise/zero_terminal.py +0 -44
  134. hcpdiff/train_ac.py +0 -565
  135. hcpdiff/train_ac_single.py +0 -39
  136. hcpdiff/utils/caption_tools.py +0 -105
  137. hcpdiff/utils/cfg_net_tools.py +0 -321
  138. hcpdiff/utils/cfg_resolvers.py +0 -16
  139. hcpdiff/utils/ema.py +0 -52
  140. hcpdiff/utils/img_size_tool.py +0 -248
  141. hcpdiff/vis/__init__.py +0 -3
  142. hcpdiff/vis/base_interface.py +0 -12
  143. hcpdiff/vis/disk_interface.py +0 -48
  144. hcpdiff/vis/webui_interface.py +0 -17
  145. hcpdiff/visualizer.py +0 -258
  146. hcpdiff/visualizer_reloadable.py +0 -237
  147. hcpdiff/workflow/base.py +0 -59
  148. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/anime/text2img_anime.yaml +0 -21
  149. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/anime/text2img_anime_lora.yaml +0 -58
  150. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/change_vae.yaml +0 -6
  151. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/euler_a.yaml +0 -8
  152. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/img2img.yaml +0 -10
  153. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/img2img_controlnet.yaml +0 -19
  154. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/inpaint.yaml +0 -11
  155. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/load_lora.yaml +0 -26
  156. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/load_unet_part.yaml +0 -18
  157. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/offload_2GB.yaml +0 -6
  158. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/save_model.yaml +0 -44
  159. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img.yaml +0 -53
  160. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img_DA++.yaml +0 -34
  161. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/infer/text2img_sdxl.yaml +0 -9
  162. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/plugins/plugin_controlnet.yaml +0 -17
  163. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/te_struct.txt +0 -193
  164. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/dataset/base_dataset.yaml +0 -29
  165. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/dataset/regularization_dataset.yaml +0 -31
  166. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/CustomDiffusion.yaml +0 -74
  167. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamArtist++.yaml +0 -135
  168. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamArtist.yaml +0 -45
  169. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/DreamBooth.yaml +0 -62
  170. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/FT_sdxl.yaml +0 -33
  171. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/Lion_optimizer.yaml +0 -17
  172. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/TextualInversion.yaml +0 -41
  173. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/add_logger_tensorboard_wandb.yaml +0 -15
  174. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/controlnet.yaml +0 -53
  175. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/ema.yaml +0 -10
  176. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/fine-tuning.yaml +0 -53
  177. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/locon.yaml +0 -24
  178. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_anime_character.yaml +0 -77
  179. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_conventional.yaml +0 -56
  180. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/lora_sdxl.yaml +0 -41
  181. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/min_snr.yaml +0 -7
  182. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples/preview_in_training.yaml +0 -6
  183. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/DreamBooth.yaml +0 -70
  184. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/TextualInversion.yaml +0 -45
  185. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/fine-tuning.yaml +0 -45
  186. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/examples_noob/lora.yaml +0 -63
  187. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/train_base.yaml +0 -81
  188. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/train/tuning_base.yaml +0 -42
  189. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/unet_struct.txt +0 -932
  190. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/highres_fix_latent.yaml +0 -86
  191. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/highres_fix_pixel.yaml +0 -99
  192. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/text2img.yaml +0 -57
  193. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/workflow/text2img_lora.yaml +0 -70
  194. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/zero2.json +0 -32
  195. hcpdiff-0.9.0.data/data/hcpdiff/cfgs/zero3.json +0 -39
  196. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/caption.txt +0 -1
  197. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name.txt +0 -1
  198. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name_2pt_caption.txt +0 -1
  199. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/name_caption.txt +0 -1
  200. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/object.txt +0 -27
  201. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/object_caption.txt +0 -27
  202. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/style.txt +0 -19
  203. hcpdiff-0.9.0.data/data/hcpdiff/prompt_tuning_template/style_caption.txt +0 -19
  204. hcpdiff-0.9.0.dist-info/METADATA +0 -199
  205. hcpdiff-0.9.0.dist-info/RECORD +0 -155
  206. hcpdiff-0.9.0.dist-info/entry_points.txt +0 -2
  207. {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info/licenses}/LICENSE +0 -0
  208. {hcpdiff-0.9.0.dist-info → hcpdiff-2.1.dist-info}/top_level.txt +0 -0
@@ -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
@@ -1,3 +0,0 @@
1
- from .base_interface import BaseInterface
2
- from .disk_interface import DiskInterface
3
- from .webui_interface import WebUIInterface
@@ -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
@@ -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
@@ -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/visualizer.py DELETED
@@ -1,258 +0,0 @@
1
- import argparse
2
- import os
3
- import random
4
- from typing import List
5
-
6
- import hydra
7
- import torch
8
- from PIL import Image
9
- from accelerate import infer_auto_device_map, dispatch_model
10
- from diffusers.utils.import_utils import is_xformers_available
11
- from hcpdiff.models import TokenizerHook, LoraBlock
12
- from hcpdiff.models.compose import ComposeTEEXHook, ComposeEmbPTHook, ComposeTextEncoder
13
- from hcpdiff.utils.cfg_net_tools import HCPModelLoader, make_plugin
14
- from hcpdiff.utils.net_utils import to_cpu, to_cuda, auto_tokenizer, auto_text_encoder
15
- from hcpdiff.utils.pipe_hook import HookPipe_T2I, HookPipe_I2I, HookPipe_Inpaint
16
- from hcpdiff.utils.utils import load_config_with_cli, load_config, size_to_int, int_to_size, prepare_seed, is_list, pad_attn_bias
17
- from omegaconf import OmegaConf
18
- from torch.cuda.amp import autocast
19
-
20
- class Visualizer:
21
- dtype_dict = {'fp32':torch.float32, 'fp16':torch.float16, 'bf16':torch.bfloat16}
22
-
23
- def __init__(self, cfgs):
24
- self.cfgs_raw = cfgs
25
- self.cfgs = hydra.utils.instantiate(self.cfgs_raw)
26
- self.cfg_merge = self.cfgs.merge
27
- self.offload = 'offload' in self.cfgs and self.cfgs.offload is not None
28
- self.dtype = self.dtype_dict[self.cfgs.dtype]
29
-
30
- self.need_inter_imgs = any(item.need_inter_imgs for item in self.cfgs.interface)
31
-
32
- self.pipe = self.load_model(self.cfgs.pretrained_model)
33
-
34
- if self.cfg_merge:
35
- self.merge_model()
36
-
37
- self.pipe = self.pipe.to(torch_dtype=self.dtype)
38
-
39
- if isinstance(self.pipe.text_encoder, ComposeTextEncoder):
40
- self.pipe.vae = self.pipe.vae.to(dtype=torch.float32)
41
-
42
- if 'save_model' in self.cfgs and self.cfgs.save_model is not None:
43
- self.save_model(self.cfgs.save_model)
44
- os._exit(0)
45
-
46
- self.build_optimize()
47
-
48
- def load_model(self, pretrained_model):
49
- pipeline = self.get_pipeline()
50
- te = auto_text_encoder(pretrained_model, subfolder="text_encoder", torch_dtype=self.dtype, resume_download=True)
51
- tokenizer = auto_tokenizer(pretrained_model, subfolder="tokenizer", use_fast=False)
52
-
53
- return pipeline.from_pretrained(pretrained_model, safety_checker=None, requires_safety_checker=False,
54
- text_encoder=te, tokenizer=tokenizer, resume_download=True,
55
- torch_dtype=self.dtype, **self.cfgs.new_components)
56
-
57
- def build_optimize(self):
58
- if self.offload:
59
- self.build_offload(self.cfgs.offload)
60
- else:
61
- self.pipe.unet.to('cuda')
62
- self.build_vae_offload()
63
-
64
- if getattr(self.cfgs, 'vae_optimize', None) is not None:
65
- if self.cfgs.vae_optimize.tiling:
66
- self.pipe.vae.enable_tiling()
67
- if self.cfgs.vae_optimize.slicing:
68
- self.pipe.vae.enable_slicing()
69
-
70
- self.emb_hook, _ = ComposeEmbPTHook.hook_from_dir(self.cfgs.emb_dir, self.pipe.tokenizer, self.pipe.text_encoder,
71
- N_repeats=self.cfgs.N_repeats)
72
- self.te_hook = ComposeTEEXHook.hook_pipe(self.pipe, N_repeats=self.cfgs.N_repeats, clip_skip=self.cfgs.clip_skip,
73
- clip_final_norm=self.cfgs.clip_final_norm, use_attention_mask=self.cfgs.encoder_attention_mask)
74
- self.token_ex = TokenizerHook(self.pipe.tokenizer)
75
-
76
- if is_xformers_available():
77
- self.pipe.unet.enable_xformers_memory_efficient_attention()
78
- # self.te_hook.enable_xformers()
79
-
80
- def save_model(self, save_cfg):
81
- for k, v in self.pipe.unet.named_modules():
82
- if isinstance(v, LoraBlock):
83
- v.reparameterization_to_host()
84
- v.remove()
85
- for k, v in self.pipe.text_encoder.named_modules():
86
- if isinstance(v, LoraBlock):
87
- v.reparameterization_to_host()
88
- v.remove()
89
-
90
- if save_cfg.path.endswith('.ckpt'):
91
- from hcpdiff.tools.diffusers2sd import save_state_dict
92
- save_state_dict(save_cfg.path, self.pipe.unet.state_dict(), self.pipe.vae.state_dict(), self.pipe.text_encoder.state_dict(),
93
- use_safetensors=save_cfg.to_safetensors)
94
-
95
- else:
96
- self.pipe.save_pretrained(save_cfg.path, safe_serialization=save_cfg.to_safetensors)
97
-
98
- def get_pipeline(self):
99
- if self.cfgs.condition is None:
100
- pipe_cls = HookPipe_T2I
101
- else:
102
- if self.cfgs.condition.type == 'i2i':
103
- pipe_cls = HookPipe_I2I
104
- elif self.cfgs.condition.type == 'inpaint':
105
- pipe_cls = HookPipe_Inpaint
106
- else:
107
- raise NotImplementedError(f'No condition type named {self.cfgs.condition.type}')
108
-
109
- return pipe_cls
110
-
111
- def build_offload(self, offload_cfg):
112
- vram = size_to_int(offload_cfg.max_VRAM)
113
- device_map = infer_auto_device_map(self.pipe.unet, max_memory={0:int_to_size(vram >> 1), "cpu":offload_cfg.max_RAM}, dtype=self.dtype)
114
- self.pipe.unet = dispatch_model(self.pipe.unet, device_map)
115
- if not offload_cfg.vae_cpu:
116
- device_map = infer_auto_device_map(self.pipe.vae, max_memory={0:int_to_size(vram >> 5), "cpu":offload_cfg.max_RAM}, dtype=self.dtype)
117
- self.pipe.vae = dispatch_model(self.pipe.vae, device_map)
118
-
119
- def build_vae_offload(self):
120
- def vae_decode_offload(latents, return_dict=True, decode_raw=self.pipe.vae.decode):
121
- if self.need_inter_imgs:
122
- to_cuda(self.pipe.vae)
123
- res = decode_raw(latents, return_dict=return_dict)
124
- else:
125
- to_cpu(self.pipe.unet)
126
-
127
- if self.offload and self.cfgs.offload.vae_cpu:
128
- self.pipe.vae.to(dtype=torch.float32)
129
- res = decode_raw(latents.cpu().to(dtype=torch.float32), return_dict=return_dict)
130
- else:
131
- to_cuda(self.pipe.vae)
132
- res = decode_raw(latents.to(dtype=self.pipe.vae.dtype), return_dict=return_dict)
133
-
134
- to_cpu(self.pipe.vae)
135
- to_cuda(self.pipe.unet)
136
- return res
137
-
138
- self.pipe.vae.decode = vae_decode_offload
139
-
140
- def vae_encode_offload(x, return_dict=True, encode_raw=self.pipe.vae.encode):
141
- to_cuda(self.pipe.vae)
142
- res = encode_raw(x.to(dtype=self.pipe.vae.dtype), return_dict=return_dict)
143
- to_cpu(self.pipe.vae)
144
- return res
145
-
146
- self.pipe.vae.encode = vae_encode_offload
147
-
148
- def merge_model(self):
149
- if 'plugin_cfg' in self.cfg_merge: # Build plugins
150
- if isinstance(self.cfg_merge.plugin_cfg, str):
151
- plugin_cfg = load_config(self.cfg_merge.plugin_cfg)
152
- plugin_cfg = {'plugin_unet': hydra.utils.instantiate(plugin_cfg['plugin_unet']),
153
- 'plugin_TE': hydra.utils.instantiate(plugin_cfg['plugin_TE'])}
154
- else:
155
- plugin_cfg = self.cfg_merge.plugin_cfg
156
- make_plugin(self.pipe.unet, plugin_cfg['plugin_unet'])
157
- make_plugin(self.pipe.text_encoder, plugin_cfg['plugin_TE'])
158
-
159
- load_ema = self.cfg_merge.get('load_ema', False)
160
- for cfg_group in self.cfg_merge.values():
161
- if hasattr(cfg_group, 'type'):
162
- if cfg_group.type == 'unet':
163
- HCPModelLoader(self.pipe.unet).load_all(cfg_group, load_ema=load_ema)
164
- elif cfg_group.type == 'TE':
165
- HCPModelLoader(self.pipe.text_encoder).load_all(cfg_group, load_ema=load_ema)
166
-
167
- def set_scheduler(self, scheduler):
168
- self.pipe.scheduler = scheduler
169
-
170
- def get_ex_input(self):
171
- ex_input_dict, pipe_input_dict = {}, {}
172
- if self.cfgs.condition is not None:
173
- if self.cfgs.condition.type == 'i2i':
174
- pipe_input_dict['image'] = Image.open(self.cfgs.condition.image).convert('RGB')
175
- elif self.cfgs.condition.type == 'inpaint':
176
- pipe_input_dict['image'] = Image.open(self.cfgs.condition.image).convert('RGB')
177
- pipe_input_dict['mask_image'] = Image.open(self.cfgs.condition.mask).convert('L')
178
-
179
- if getattr(self.cfgs, 'ex_input', None) is not None:
180
- for key, processor in self.cfgs.ex_input.items():
181
- ex_input_dict[key] = processor(self.cfgs.infer_args.width, self.cfgs.infer_args.height, self.cfgs.bs*2, 'cuda', self.dtype)
182
- return ex_input_dict, pipe_input_dict
183
-
184
- @torch.no_grad()
185
- def vis_images(self, prompt, negative_prompt='', seeds: List[int] = None, **kwargs):
186
- G = prepare_seed(seeds or [None]*len(prompt))
187
-
188
- ex_input_dict, pipe_input_dict = self.get_ex_input()
189
- kwargs.update(pipe_input_dict)
190
-
191
- to_cuda(self.pipe.text_encoder)
192
-
193
- mult_p, clean_text_p = self.token_ex.parse_attn_mult(prompt)
194
- mult_n, clean_text_n = self.token_ex.parse_attn_mult(negative_prompt)
195
- with autocast(enabled=self.cfgs.amp, dtype=self.dtype):
196
- emb, pooled_output, attention_mask = self.te_hook.encode_prompt_to_emb(clean_text_n+clean_text_p)
197
- if self.cfgs.encoder_attention_mask:
198
- emb, attention_mask = pad_attn_bias(emb, attention_mask)
199
- else:
200
- attention_mask = None
201
- emb_n, emb_p = emb.chunk(2)
202
- emb_p = self.te_hook.mult_attn(emb_p, mult_p)
203
- emb_n = self.te_hook.mult_attn(emb_n, mult_n)
204
-
205
- to_cpu(self.pipe.text_encoder)
206
- to_cuda(self.pipe.unet)
207
-
208
- if hasattr(self.pipe.unet, 'input_feeder'):
209
- for feeder in self.pipe.unet.input_feeder:
210
- feeder(ex_input_dict)
211
-
212
- images = self.pipe(prompt_embeds=emb_p, negative_prompt_embeds=emb_n, callback=self.inter_callback, generator=G,
213
- pooled_output=pooled_output[-1], encoder_attention_mask=attention_mask, **kwargs).images
214
- return images
215
-
216
- def inter_callback(self, i, t, num_t, latents):
217
- images = None
218
- interrupt = False
219
- for interface in self.cfgs.interface:
220
- if interface.show_steps>0 and i%interface.show_steps == 0:
221
- if self.need_inter_imgs and images is None:
222
- images = self.pipe.decode_latents(latents)
223
- images = self.pipe.numpy_to_pil(images)
224
- feed_back = interface.on_inter_step(i, num_t, t, latents, images)
225
- interrupt |= bool(feed_back)
226
- return interrupt
227
-
228
- def save_images(self, images, prompt, negative_prompt='', seeds: List[int] = None):
229
- for interface in self.cfgs.interface:
230
- interface.on_infer_finish(images, prompt, negative_prompt, self.cfgs_raw, seeds=seeds)
231
-
232
- def vis_to_dir(self, prompt, negative_prompt='', seeds: List[int] = None, **kwargs):
233
- seeds = [s or random.randint(0, 1 << 30) for s in seeds]
234
-
235
- images = self.vis_images(prompt, negative_prompt, seeds=seeds, **kwargs)
236
- self.save_images(images, prompt, negative_prompt, seeds=seeds)
237
-
238
- if __name__ == '__main__':
239
- parser = argparse.ArgumentParser(description='Stable Diffusion Training')
240
- parser.add_argument('--cfg', type=str, default='')
241
- args, cfg_args = parser.parse_known_args()
242
- cfgs = load_config_with_cli(args.cfg, args_list=cfg_args) # skip --cfg
243
-
244
- if cfgs.seed is not None:
245
- if is_list(cfgs.seed):
246
- assert len(cfgs.seed) == cfgs.num*cfgs.bs, 'seed list length should be equal to num*bs'
247
- seeds = list(cfgs.seed)
248
- else:
249
- seeds = list(range(cfgs.seed, cfgs.seed+cfgs.num*cfgs.bs))
250
- else:
251
- seeds = [None]*(cfgs.num*cfgs.bs)
252
-
253
- viser = Visualizer(cfgs)
254
- for i in range(cfgs.num):
255
- prompt = cfgs.prompt[i*cfgs.bs:(i+1)*cfgs.bs] if is_list(cfgs.prompt) else [cfgs.prompt]*cfgs.bs
256
- negative_prompt = cfgs.neg_prompt[i*cfgs.bs:(i+1)*cfgs.bs] if is_list(cfgs.neg_prompt) else [cfgs.neg_prompt]*cfgs.bs
257
- viser.vis_to_dir(prompt=prompt, negative_prompt=negative_prompt,
258
- seeds=seeds[i*cfgs.bs:(i+1)*cfgs.bs], save_cfg=cfgs.save.save_cfg, **cfgs.infer_args)