hcpdiff 0.9.1__py3-none-any.whl → 2.2__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 (211) 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 +252 -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 +90 -0
  17. hcpdiff/data/handler/text.py +111 -0
  18. hcpdiff/data/source/__init__.py +3 -3
  19. hcpdiff/data/source/folder_class.py +12 -29
  20. hcpdiff/data/source/text.py +40 -0
  21. hcpdiff/data/source/text2img.py +36 -74
  22. hcpdiff/data/source/text2img_cond.py +9 -15
  23. hcpdiff/diffusion/__init__.py +0 -0
  24. hcpdiff/diffusion/noise/__init__.py +2 -0
  25. hcpdiff/diffusion/noise/pyramid_noise.py +42 -0
  26. hcpdiff/diffusion/noise/zero_terminal.py +39 -0
  27. hcpdiff/diffusion/sampler/__init__.py +5 -0
  28. hcpdiff/diffusion/sampler/base.py +72 -0
  29. hcpdiff/diffusion/sampler/ddpm.py +20 -0
  30. hcpdiff/diffusion/sampler/diffusers.py +66 -0
  31. hcpdiff/diffusion/sampler/edm.py +22 -0
  32. hcpdiff/diffusion/sampler/sigma_scheduler/__init__.py +3 -0
  33. hcpdiff/diffusion/sampler/sigma_scheduler/base.py +14 -0
  34. hcpdiff/diffusion/sampler/sigma_scheduler/ddpm.py +197 -0
  35. hcpdiff/diffusion/sampler/sigma_scheduler/edm.py +48 -0
  36. hcpdiff/easy/__init__.py +2 -0
  37. hcpdiff/easy/cfg/__init__.py +3 -0
  38. hcpdiff/easy/cfg/sd15_train.py +207 -0
  39. hcpdiff/easy/cfg/sdxl_train.py +147 -0
  40. hcpdiff/easy/cfg/t2i.py +228 -0
  41. hcpdiff/easy/model/__init__.py +2 -0
  42. hcpdiff/easy/model/cnet.py +31 -0
  43. hcpdiff/easy/model/loader.py +79 -0
  44. hcpdiff/easy/sampler.py +46 -0
  45. hcpdiff/evaluate/__init__.py +1 -0
  46. hcpdiff/evaluate/previewer.py +60 -0
  47. hcpdiff/loss/__init__.py +4 -1
  48. hcpdiff/loss/base.py +41 -0
  49. hcpdiff/loss/gw.py +35 -0
  50. hcpdiff/loss/ssim.py +37 -0
  51. hcpdiff/loss/vlb.py +79 -0
  52. hcpdiff/loss/weighting.py +66 -0
  53. hcpdiff/models/__init__.py +2 -2
  54. hcpdiff/models/cfg_context.py +17 -14
  55. hcpdiff/models/compose/compose_hook.py +44 -23
  56. hcpdiff/models/compose/compose_tokenizer.py +21 -8
  57. hcpdiff/models/compose/sdxl_composer.py +4 -4
  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/init_proj.py +3 -21
  74. hcpdiff/tools/lora_convert.py +18 -17
  75. hcpdiff/tools/save_model.py +12 -0
  76. hcpdiff/tools/sd2diffusers.py +1 -1
  77. hcpdiff/train_colo.py +1 -1
  78. hcpdiff/train_deepspeed.py +1 -1
  79. hcpdiff/trainer_ac.py +79 -0
  80. hcpdiff/trainer_ac_single.py +31 -0
  81. hcpdiff/utils/__init__.py +0 -2
  82. hcpdiff/utils/inpaint_pipe.py +7 -2
  83. hcpdiff/utils/net_utils.py +29 -6
  84. hcpdiff/utils/pipe_hook.py +24 -7
  85. hcpdiff/utils/utils.py +21 -4
  86. hcpdiff/workflow/__init__.py +15 -10
  87. hcpdiff/workflow/daam/__init__.py +1 -0
  88. hcpdiff/workflow/daam/act.py +66 -0
  89. hcpdiff/workflow/daam/hook.py +109 -0
  90. hcpdiff/workflow/diffusion.py +118 -128
  91. hcpdiff/workflow/fast.py +31 -0
  92. hcpdiff/workflow/flow.py +67 -0
  93. hcpdiff/workflow/io.py +36 -130
  94. hcpdiff/workflow/model.py +46 -43
  95. hcpdiff/workflow/text.py +60 -47
  96. hcpdiff/workflow/utils.py +32 -12
  97. hcpdiff/workflow/vae.py +37 -38
  98. hcpdiff-2.2.dist-info/METADATA +299 -0
  99. hcpdiff-2.2.dist-info/RECORD +115 -0
  100. {hcpdiff-0.9.1.dist-info → hcpdiff-2.2.dist-info}/WHEEL +1 -1
  101. hcpdiff-2.2.dist-info/entry_points.txt +5 -0
  102. hcpdiff/ckpt_manager/base.py +0 -16
  103. hcpdiff/ckpt_manager/ckpt_diffusers.py +0 -45
  104. hcpdiff/ckpt_manager/ckpt_pkl.py +0 -138
  105. hcpdiff/ckpt_manager/ckpt_safetensor.py +0 -64
  106. hcpdiff/ckpt_manager/ckpt_webui.py +0 -54
  107. hcpdiff/data/bucket.py +0 -358
  108. hcpdiff/data/caption_loader.py +0 -80
  109. hcpdiff/data/cond_dataset.py +0 -40
  110. hcpdiff/data/crop_info_dataset.py +0 -40
  111. hcpdiff/data/data_processor.py +0 -33
  112. hcpdiff/data/pair_dataset.py +0 -146
  113. hcpdiff/data/sampler.py +0 -54
  114. hcpdiff/data/source/base.py +0 -30
  115. hcpdiff/data/utils.py +0 -80
  116. hcpdiff/deprecated/__init__.py +0 -1
  117. hcpdiff/deprecated/cfg_converter.py +0 -81
  118. hcpdiff/deprecated/lora_convert.py +0 -31
  119. hcpdiff/infer_workflow.py +0 -57
  120. hcpdiff/loggers/__init__.py +0 -13
  121. hcpdiff/loggers/base_logger.py +0 -76
  122. hcpdiff/loggers/cli_logger.py +0 -40
  123. hcpdiff/loggers/preview/__init__.py +0 -1
  124. hcpdiff/loggers/preview/image_previewer.py +0 -149
  125. hcpdiff/loggers/tensorboard_logger.py +0 -30
  126. hcpdiff/loggers/wandb_logger.py +0 -31
  127. hcpdiff/loggers/webui_logger.py +0 -9
  128. hcpdiff/loss/min_snr_loss.py +0 -52
  129. hcpdiff/models/layers.py +0 -81
  130. hcpdiff/models/plugin.py +0 -348
  131. hcpdiff/models/wrapper.py +0 -75
  132. hcpdiff/noise/__init__.py +0 -3
  133. hcpdiff/noise/noise_base.py +0 -16
  134. hcpdiff/noise/pyramid_noise.py +0 -50
  135. hcpdiff/noise/zero_terminal.py +0 -44
  136. hcpdiff/train_ac.py +0 -566
  137. hcpdiff/train_ac_single.py +0 -39
  138. hcpdiff/utils/caption_tools.py +0 -105
  139. hcpdiff/utils/cfg_net_tools.py +0 -321
  140. hcpdiff/utils/cfg_resolvers.py +0 -16
  141. hcpdiff/utils/ema.py +0 -52
  142. hcpdiff/utils/img_size_tool.py +0 -248
  143. hcpdiff/vis/__init__.py +0 -3
  144. hcpdiff/vis/base_interface.py +0 -12
  145. hcpdiff/vis/disk_interface.py +0 -48
  146. hcpdiff/vis/webui_interface.py +0 -17
  147. hcpdiff/viser_fast.py +0 -138
  148. hcpdiff/visualizer.py +0 -265
  149. hcpdiff/visualizer_reloadable.py +0 -237
  150. hcpdiff/workflow/base.py +0 -59
  151. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/anime/text2img_anime.yaml +0 -21
  152. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/anime/text2img_anime_lora.yaml +0 -58
  153. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/change_vae.yaml +0 -6
  154. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/euler_a.yaml +0 -8
  155. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/img2img.yaml +0 -10
  156. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/img2img_controlnet.yaml +0 -19
  157. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/inpaint.yaml +0 -11
  158. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/load_lora.yaml +0 -26
  159. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/load_unet_part.yaml +0 -18
  160. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/offload_2GB.yaml +0 -6
  161. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/save_model.yaml +0 -44
  162. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img.yaml +0 -53
  163. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img_DA++.yaml +0 -34
  164. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/infer/text2img_sdxl.yaml +0 -9
  165. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/plugins/plugin_controlnet.yaml +0 -17
  166. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/te_struct.txt +0 -193
  167. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/dataset/base_dataset.yaml +0 -29
  168. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/dataset/regularization_dataset.yaml +0 -31
  169. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/CustomDiffusion.yaml +0 -74
  170. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamArtist++.yaml +0 -135
  171. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamArtist.yaml +0 -45
  172. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/DreamBooth.yaml +0 -62
  173. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/FT_sdxl.yaml +0 -33
  174. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/Lion_optimizer.yaml +0 -17
  175. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/TextualInversion.yaml +0 -41
  176. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/add_logger_tensorboard_wandb.yaml +0 -15
  177. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/controlnet.yaml +0 -53
  178. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/ema.yaml +0 -10
  179. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/fine-tuning.yaml +0 -53
  180. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/locon.yaml +0 -24
  181. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_anime_character.yaml +0 -77
  182. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_conventional.yaml +0 -56
  183. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/lora_sdxl.yaml +0 -41
  184. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/min_snr.yaml +0 -7
  185. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples/preview_in_training.yaml +0 -6
  186. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/DreamBooth.yaml +0 -70
  187. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/TextualInversion.yaml +0 -45
  188. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/fine-tuning.yaml +0 -45
  189. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/examples_noob/lora.yaml +0 -63
  190. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/train_base.yaml +0 -81
  191. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/train/tuning_base.yaml +0 -42
  192. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/unet_struct.txt +0 -932
  193. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/highres_fix_latent.yaml +0 -86
  194. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/highres_fix_pixel.yaml +0 -99
  195. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/text2img.yaml +0 -59
  196. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/workflow/text2img_lora.yaml +0 -70
  197. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/zero2.json +0 -32
  198. hcpdiff-0.9.1.data/data/hcpdiff/cfgs/zero3.json +0 -39
  199. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/caption.txt +0 -1
  200. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name.txt +0 -1
  201. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name_2pt_caption.txt +0 -1
  202. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/name_caption.txt +0 -1
  203. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/object.txt +0 -27
  204. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/object_caption.txt +0 -27
  205. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/style.txt +0 -19
  206. hcpdiff-0.9.1.data/data/hcpdiff/prompt_tuning_template/style_caption.txt +0 -19
  207. hcpdiff-0.9.1.dist-info/METADATA +0 -199
  208. hcpdiff-0.9.1.dist-info/RECORD +0 -160
  209. hcpdiff-0.9.1.dist-info/entry_points.txt +0 -2
  210. {hcpdiff-0.9.1.dist-info → hcpdiff-2.2.dist-info/licenses}/LICENSE +0 -0
  211. {hcpdiff-0.9.1.dist-info → hcpdiff-2.2.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/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)