diffsynth 1.0.0__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.
- diffsynth/__init__.py +6 -0
- diffsynth/configs/__init__.py +0 -0
- diffsynth/configs/model_config.py +243 -0
- diffsynth/controlnets/__init__.py +2 -0
- diffsynth/controlnets/controlnet_unit.py +53 -0
- diffsynth/controlnets/processors.py +51 -0
- diffsynth/data/__init__.py +1 -0
- diffsynth/data/simple_text_image.py +35 -0
- diffsynth/data/video.py +148 -0
- diffsynth/extensions/ESRGAN/__init__.py +118 -0
- diffsynth/extensions/FastBlend/__init__.py +63 -0
- diffsynth/extensions/FastBlend/api.py +397 -0
- diffsynth/extensions/FastBlend/cupy_kernels.py +119 -0
- diffsynth/extensions/FastBlend/data.py +146 -0
- diffsynth/extensions/FastBlend/patch_match.py +298 -0
- diffsynth/extensions/FastBlend/runners/__init__.py +4 -0
- diffsynth/extensions/FastBlend/runners/accurate.py +35 -0
- diffsynth/extensions/FastBlend/runners/balanced.py +46 -0
- diffsynth/extensions/FastBlend/runners/fast.py +141 -0
- diffsynth/extensions/FastBlend/runners/interpolation.py +121 -0
- diffsynth/extensions/RIFE/__init__.py +242 -0
- diffsynth/extensions/__init__.py +0 -0
- diffsynth/models/__init__.py +1 -0
- diffsynth/models/attention.py +89 -0
- diffsynth/models/downloader.py +66 -0
- diffsynth/models/hunyuan_dit.py +451 -0
- diffsynth/models/hunyuan_dit_text_encoder.py +163 -0
- diffsynth/models/kolors_text_encoder.py +1363 -0
- diffsynth/models/lora.py +195 -0
- diffsynth/models/model_manager.py +536 -0
- diffsynth/models/sd3_dit.py +798 -0
- diffsynth/models/sd3_text_encoder.py +1107 -0
- diffsynth/models/sd3_vae_decoder.py +81 -0
- diffsynth/models/sd3_vae_encoder.py +95 -0
- diffsynth/models/sd_controlnet.py +588 -0
- diffsynth/models/sd_ipadapter.py +57 -0
- diffsynth/models/sd_motion.py +199 -0
- diffsynth/models/sd_text_encoder.py +321 -0
- diffsynth/models/sd_unet.py +1108 -0
- diffsynth/models/sd_vae_decoder.py +336 -0
- diffsynth/models/sd_vae_encoder.py +282 -0
- diffsynth/models/sdxl_ipadapter.py +122 -0
- diffsynth/models/sdxl_motion.py +104 -0
- diffsynth/models/sdxl_text_encoder.py +759 -0
- diffsynth/models/sdxl_unet.py +1899 -0
- diffsynth/models/sdxl_vae_decoder.py +24 -0
- diffsynth/models/sdxl_vae_encoder.py +24 -0
- diffsynth/models/svd_image_encoder.py +505 -0
- diffsynth/models/svd_unet.py +2004 -0
- diffsynth/models/svd_vae_decoder.py +578 -0
- diffsynth/models/svd_vae_encoder.py +139 -0
- diffsynth/models/tiler.py +106 -0
- diffsynth/pipelines/__init__.py +9 -0
- diffsynth/pipelines/base.py +34 -0
- diffsynth/pipelines/dancer.py +178 -0
- diffsynth/pipelines/hunyuan_image.py +274 -0
- diffsynth/pipelines/pipeline_runner.py +105 -0
- diffsynth/pipelines/sd3_image.py +132 -0
- diffsynth/pipelines/sd_image.py +173 -0
- diffsynth/pipelines/sd_video.py +266 -0
- diffsynth/pipelines/sdxl_image.py +191 -0
- diffsynth/pipelines/sdxl_video.py +223 -0
- diffsynth/pipelines/svd_video.py +297 -0
- diffsynth/processors/FastBlend.py +142 -0
- diffsynth/processors/PILEditor.py +28 -0
- diffsynth/processors/RIFE.py +77 -0
- diffsynth/processors/__init__.py +0 -0
- diffsynth/processors/base.py +6 -0
- diffsynth/processors/sequencial_processor.py +41 -0
- diffsynth/prompters/__init__.py +6 -0
- diffsynth/prompters/base_prompter.py +57 -0
- diffsynth/prompters/hunyuan_dit_prompter.py +69 -0
- diffsynth/prompters/kolors_prompter.py +353 -0
- diffsynth/prompters/prompt_refiners.py +77 -0
- diffsynth/prompters/sd3_prompter.py +92 -0
- diffsynth/prompters/sd_prompter.py +73 -0
- diffsynth/prompters/sdxl_prompter.py +61 -0
- diffsynth/schedulers/__init__.py +3 -0
- diffsynth/schedulers/continuous_ode.py +59 -0
- diffsynth/schedulers/ddim.py +79 -0
- diffsynth/schedulers/flow_match.py +51 -0
- diffsynth/tokenizer_configs/__init__.py +0 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/special_tokens_map.json +7 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/tokenizer_config.json +16 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/vocab.txt +47020 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer/vocab_org.txt +21128 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/config.json +28 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/special_tokens_map.json +1 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/spiece.model +0 -0
- diffsynth/tokenizer_configs/hunyuan_dit/tokenizer_t5/tokenizer_config.json +1 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/tokenizer.model +0 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/tokenizer_config.json +12 -0
- diffsynth/tokenizer_configs/kolors/tokenizer/vocab.txt +0 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/special_tokens_map.json +24 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/tokenizer_config.json +34 -0
- diffsynth/tokenizer_configs/stable_diffusion/tokenizer/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/special_tokens_map.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/tokenizer_config.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_1/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/merges.txt +48895 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/special_tokens_map.json +30 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/tokenizer_config.json +38 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_2/vocab.json +49410 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/special_tokens_map.json +125 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/spiece.model +0 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/tokenizer.json +129428 -0
- diffsynth/tokenizer_configs/stable_diffusion_3/tokenizer_3/tokenizer_config.json +940 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/merges.txt +40213 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/special_tokens_map.json +24 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/tokenizer_config.json +38 -0
- diffsynth/tokenizer_configs/stable_diffusion_xl/tokenizer_2/vocab.json +49411 -0
- diffsynth/trainers/__init__.py +0 -0
- diffsynth/trainers/text_to_image.py +253 -0
- diffsynth-1.0.0.dist-info/LICENSE +201 -0
- diffsynth-1.0.0.dist-info/METADATA +23 -0
- diffsynth-1.0.0.dist-info/RECORD +120 -0
- diffsynth-1.0.0.dist-info/WHEEL +5 -0
- diffsynth-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from .runners.fast import TableManager, PyramidPatchMatcher
|
|
2
|
+
from PIL import Image
|
|
3
|
+
import numpy as np
|
|
4
|
+
import cupy as cp
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class FastBlendSmoother:
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.batch_size = 8
|
|
10
|
+
self.window_size = 64
|
|
11
|
+
self.ebsynth_config = {
|
|
12
|
+
"minimum_patch_size": 5,
|
|
13
|
+
"threads_per_block": 8,
|
|
14
|
+
"num_iter": 5,
|
|
15
|
+
"gpu_id": 0,
|
|
16
|
+
"guide_weight": 10.0,
|
|
17
|
+
"initialize": "identity",
|
|
18
|
+
"tracking_window_size": 0,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@staticmethod
|
|
22
|
+
def from_model_manager(model_manager):
|
|
23
|
+
# TODO: fetch GPU ID from model_manager
|
|
24
|
+
return FastBlendSmoother()
|
|
25
|
+
|
|
26
|
+
def run(self, frames_guide, frames_style, batch_size, window_size, ebsynth_config):
|
|
27
|
+
frames_guide = [np.array(frame) for frame in frames_guide]
|
|
28
|
+
frames_style = [np.array(frame) for frame in frames_style]
|
|
29
|
+
table_manager = TableManager()
|
|
30
|
+
patch_match_engine = PyramidPatchMatcher(
|
|
31
|
+
image_height=frames_style[0].shape[0],
|
|
32
|
+
image_width=frames_style[0].shape[1],
|
|
33
|
+
channel=3,
|
|
34
|
+
**ebsynth_config
|
|
35
|
+
)
|
|
36
|
+
# left part
|
|
37
|
+
table_l = table_manager.build_remapping_table(frames_guide, frames_style, patch_match_engine, batch_size, desc="FastBlend Step 1/4")
|
|
38
|
+
table_l = table_manager.remapping_table_to_blending_table(table_l)
|
|
39
|
+
table_l = table_manager.process_window_sum(frames_guide, table_l, patch_match_engine, window_size, batch_size, desc="FastBlend Step 2/4")
|
|
40
|
+
# right part
|
|
41
|
+
table_r = table_manager.build_remapping_table(frames_guide[::-1], frames_style[::-1], patch_match_engine, batch_size, desc="FastBlend Step 3/4")
|
|
42
|
+
table_r = table_manager.remapping_table_to_blending_table(table_r)
|
|
43
|
+
table_r = table_manager.process_window_sum(frames_guide[::-1], table_r, patch_match_engine, window_size, batch_size, desc="FastBlend Step 4/4")[::-1]
|
|
44
|
+
# merge
|
|
45
|
+
frames = []
|
|
46
|
+
for (frame_l, weight_l), frame_m, (frame_r, weight_r) in zip(table_l, frames_style, table_r):
|
|
47
|
+
weight_m = -1
|
|
48
|
+
weight = weight_l + weight_m + weight_r
|
|
49
|
+
frame = frame_l * (weight_l / weight) + frame_m * (weight_m / weight) + frame_r * (weight_r / weight)
|
|
50
|
+
frames.append(frame)
|
|
51
|
+
frames = [Image.fromarray(frame.clip(0, 255).astype("uint8")) for frame in frames]
|
|
52
|
+
return frames
|
|
53
|
+
|
|
54
|
+
def __call__(self, rendered_frames, original_frames=None, **kwargs):
|
|
55
|
+
frames = self.run(
|
|
56
|
+
original_frames, rendered_frames,
|
|
57
|
+
self.batch_size, self.window_size, self.ebsynth_config
|
|
58
|
+
)
|
|
59
|
+
mempool = cp.get_default_memory_pool()
|
|
60
|
+
pinned_mempool = cp.get_default_pinned_memory_pool()
|
|
61
|
+
mempool.free_all_blocks()
|
|
62
|
+
pinned_mempool.free_all_blocks()
|
|
63
|
+
return frames
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
from .runners import AccurateModeRunner, FastModeRunner, BalancedModeRunner, InterpolationModeRunner, InterpolationModeSingleFrameRunner
|
|
2
|
+
from .data import VideoData, get_video_fps, save_video, search_for_images
|
|
3
|
+
import os
|
|
4
|
+
import gradio as gr
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def check_input_for_blending(video_guide, video_guide_folder, video_style, video_style_folder):
|
|
8
|
+
frames_guide = VideoData(video_guide, video_guide_folder)
|
|
9
|
+
frames_style = VideoData(video_style, video_style_folder)
|
|
10
|
+
message = ""
|
|
11
|
+
if len(frames_guide) < len(frames_style):
|
|
12
|
+
message += f"The number of frames mismatches. Only the first {len(frames_guide)} frames of style video will be used.\n"
|
|
13
|
+
frames_style.set_length(len(frames_guide))
|
|
14
|
+
elif len(frames_guide) > len(frames_style):
|
|
15
|
+
message += f"The number of frames mismatches. Only the first {len(frames_style)} frames of guide video will be used.\n"
|
|
16
|
+
frames_guide.set_length(len(frames_style))
|
|
17
|
+
height_guide, width_guide = frames_guide.shape()
|
|
18
|
+
height_style, width_style = frames_style.shape()
|
|
19
|
+
if height_guide != height_style or width_guide != width_style:
|
|
20
|
+
message += f"The shape of frames mismatches. The frames in style video will be resized to (height: {height_guide}, width: {width_guide})\n"
|
|
21
|
+
frames_style.set_shape(height_guide, width_guide)
|
|
22
|
+
return frames_guide, frames_style, message
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def smooth_video(
|
|
26
|
+
video_guide,
|
|
27
|
+
video_guide_folder,
|
|
28
|
+
video_style,
|
|
29
|
+
video_style_folder,
|
|
30
|
+
mode,
|
|
31
|
+
window_size,
|
|
32
|
+
batch_size,
|
|
33
|
+
tracking_window_size,
|
|
34
|
+
output_path,
|
|
35
|
+
fps,
|
|
36
|
+
minimum_patch_size,
|
|
37
|
+
num_iter,
|
|
38
|
+
guide_weight,
|
|
39
|
+
initialize,
|
|
40
|
+
progress = None,
|
|
41
|
+
):
|
|
42
|
+
# input
|
|
43
|
+
frames_guide, frames_style, message = check_input_for_blending(video_guide, video_guide_folder, video_style, video_style_folder)
|
|
44
|
+
if len(message) > 0:
|
|
45
|
+
print(message)
|
|
46
|
+
# output
|
|
47
|
+
if output_path == "":
|
|
48
|
+
if video_style is None:
|
|
49
|
+
output_path = os.path.join(video_style_folder, "output")
|
|
50
|
+
else:
|
|
51
|
+
output_path = os.path.join(os.path.split(video_style)[0], "output")
|
|
52
|
+
os.makedirs(output_path, exist_ok=True)
|
|
53
|
+
print("No valid output_path. Your video will be saved here:", output_path)
|
|
54
|
+
elif not os.path.exists(output_path):
|
|
55
|
+
os.makedirs(output_path, exist_ok=True)
|
|
56
|
+
print("Your video will be saved here:", output_path)
|
|
57
|
+
frames_path = os.path.join(output_path, "frames")
|
|
58
|
+
video_path = os.path.join(output_path, "video.mp4")
|
|
59
|
+
os.makedirs(frames_path, exist_ok=True)
|
|
60
|
+
# process
|
|
61
|
+
if mode == "Fast" or mode == "Balanced":
|
|
62
|
+
tracking_window_size = 0
|
|
63
|
+
ebsynth_config = {
|
|
64
|
+
"minimum_patch_size": minimum_patch_size,
|
|
65
|
+
"threads_per_block": 8,
|
|
66
|
+
"num_iter": num_iter,
|
|
67
|
+
"gpu_id": 0,
|
|
68
|
+
"guide_weight": guide_weight,
|
|
69
|
+
"initialize": initialize,
|
|
70
|
+
"tracking_window_size": tracking_window_size,
|
|
71
|
+
}
|
|
72
|
+
if mode == "Fast":
|
|
73
|
+
FastModeRunner().run(frames_guide, frames_style, batch_size=batch_size, window_size=window_size, ebsynth_config=ebsynth_config, save_path=frames_path)
|
|
74
|
+
elif mode == "Balanced":
|
|
75
|
+
BalancedModeRunner().run(frames_guide, frames_style, batch_size=batch_size, window_size=window_size, ebsynth_config=ebsynth_config, save_path=frames_path)
|
|
76
|
+
elif mode == "Accurate":
|
|
77
|
+
AccurateModeRunner().run(frames_guide, frames_style, batch_size=batch_size, window_size=window_size, ebsynth_config=ebsynth_config, save_path=frames_path)
|
|
78
|
+
# output
|
|
79
|
+
try:
|
|
80
|
+
fps = int(fps)
|
|
81
|
+
except:
|
|
82
|
+
fps = get_video_fps(video_style) if video_style is not None else 30
|
|
83
|
+
print("Fps:", fps)
|
|
84
|
+
print("Saving video...")
|
|
85
|
+
video_path = save_video(frames_path, video_path, num_frames=len(frames_style), fps=fps)
|
|
86
|
+
print("Success!")
|
|
87
|
+
print("Your frames are here:", frames_path)
|
|
88
|
+
print("Your video is here:", video_path)
|
|
89
|
+
return output_path, fps, video_path
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class KeyFrameMatcher:
|
|
93
|
+
def __init__(self):
|
|
94
|
+
pass
|
|
95
|
+
|
|
96
|
+
def extract_number_from_filename(self, file_name):
|
|
97
|
+
result = []
|
|
98
|
+
number = -1
|
|
99
|
+
for i in file_name:
|
|
100
|
+
if ord(i)>=ord("0") and ord(i)<=ord("9"):
|
|
101
|
+
if number == -1:
|
|
102
|
+
number = 0
|
|
103
|
+
number = number*10 + ord(i) - ord("0")
|
|
104
|
+
else:
|
|
105
|
+
if number != -1:
|
|
106
|
+
result.append(number)
|
|
107
|
+
number = -1
|
|
108
|
+
if number != -1:
|
|
109
|
+
result.append(number)
|
|
110
|
+
result = tuple(result)
|
|
111
|
+
return result
|
|
112
|
+
|
|
113
|
+
def extract_number_from_filenames(self, file_names):
|
|
114
|
+
numbers = [self.extract_number_from_filename(file_name) for file_name in file_names]
|
|
115
|
+
min_length = min(len(i) for i in numbers)
|
|
116
|
+
for i in range(min_length-1, -1, -1):
|
|
117
|
+
if len(set(number[i] for number in numbers))==len(file_names):
|
|
118
|
+
return [number[i] for number in numbers]
|
|
119
|
+
return list(range(len(file_names)))
|
|
120
|
+
|
|
121
|
+
def match_using_filename(self, file_names_a, file_names_b):
|
|
122
|
+
file_names_b_set = set(file_names_b)
|
|
123
|
+
matched_file_name = []
|
|
124
|
+
for file_name in file_names_a:
|
|
125
|
+
if file_name not in file_names_b_set:
|
|
126
|
+
matched_file_name.append(None)
|
|
127
|
+
else:
|
|
128
|
+
matched_file_name.append(file_name)
|
|
129
|
+
return matched_file_name
|
|
130
|
+
|
|
131
|
+
def match_using_numbers(self, file_names_a, file_names_b):
|
|
132
|
+
numbers_a = self.extract_number_from_filenames(file_names_a)
|
|
133
|
+
numbers_b = self.extract_number_from_filenames(file_names_b)
|
|
134
|
+
numbers_b_dict = {number: file_name for number, file_name in zip(numbers_b, file_names_b)}
|
|
135
|
+
matched_file_name = []
|
|
136
|
+
for number in numbers_a:
|
|
137
|
+
if number in numbers_b_dict:
|
|
138
|
+
matched_file_name.append(numbers_b_dict[number])
|
|
139
|
+
else:
|
|
140
|
+
matched_file_name.append(None)
|
|
141
|
+
return matched_file_name
|
|
142
|
+
|
|
143
|
+
def match_filenames(self, file_names_a, file_names_b):
|
|
144
|
+
matched_file_name = self.match_using_filename(file_names_a, file_names_b)
|
|
145
|
+
if sum([i is not None for i in matched_file_name]) > 0:
|
|
146
|
+
return matched_file_name
|
|
147
|
+
matched_file_name = self.match_using_numbers(file_names_a, file_names_b)
|
|
148
|
+
return matched_file_name
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def detect_frames(frames_path, keyframes_path):
|
|
152
|
+
if not os.path.exists(frames_path) and not os.path.exists(keyframes_path):
|
|
153
|
+
return "Please input the directory of guide video and rendered frames"
|
|
154
|
+
elif not os.path.exists(frames_path):
|
|
155
|
+
return "Please input the directory of guide video"
|
|
156
|
+
elif not os.path.exists(keyframes_path):
|
|
157
|
+
return "Please input the directory of rendered frames"
|
|
158
|
+
frames = [os.path.split(i)[-1] for i in search_for_images(frames_path)]
|
|
159
|
+
keyframes = [os.path.split(i)[-1] for i in search_for_images(keyframes_path)]
|
|
160
|
+
if len(frames)==0:
|
|
161
|
+
return f"No images detected in {frames_path}"
|
|
162
|
+
if len(keyframes)==0:
|
|
163
|
+
return f"No images detected in {keyframes_path}"
|
|
164
|
+
matched_keyframes = KeyFrameMatcher().match_filenames(frames, keyframes)
|
|
165
|
+
max_filename_length = max([len(i) for i in frames])
|
|
166
|
+
if sum([i is not None for i in matched_keyframes])==0:
|
|
167
|
+
message = ""
|
|
168
|
+
for frame, matched_keyframe in zip(frames, matched_keyframes):
|
|
169
|
+
message += frame + " " * (max_filename_length - len(frame) + 1)
|
|
170
|
+
message += "--> No matched keyframes\n"
|
|
171
|
+
else:
|
|
172
|
+
message = ""
|
|
173
|
+
for frame, matched_keyframe in zip(frames, matched_keyframes):
|
|
174
|
+
message += frame + " " * (max_filename_length - len(frame) + 1)
|
|
175
|
+
if matched_keyframe is None:
|
|
176
|
+
message += "--> [to be rendered]\n"
|
|
177
|
+
else:
|
|
178
|
+
message += f"--> {matched_keyframe}\n"
|
|
179
|
+
return message
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def check_input_for_interpolating(frames_path, keyframes_path):
|
|
183
|
+
# search for images
|
|
184
|
+
frames = [os.path.split(i)[-1] for i in search_for_images(frames_path)]
|
|
185
|
+
keyframes = [os.path.split(i)[-1] for i in search_for_images(keyframes_path)]
|
|
186
|
+
# match frames
|
|
187
|
+
matched_keyframes = KeyFrameMatcher().match_filenames(frames, keyframes)
|
|
188
|
+
file_list = [file_name for file_name in matched_keyframes if file_name is not None]
|
|
189
|
+
index_style = [i for i, file_name in enumerate(matched_keyframes) if file_name is not None]
|
|
190
|
+
frames_guide = VideoData(None, frames_path)
|
|
191
|
+
frames_style = VideoData(None, keyframes_path, file_list=file_list)
|
|
192
|
+
# match shape
|
|
193
|
+
message = ""
|
|
194
|
+
height_guide, width_guide = frames_guide.shape()
|
|
195
|
+
height_style, width_style = frames_style.shape()
|
|
196
|
+
if height_guide != height_style or width_guide != width_style:
|
|
197
|
+
message += f"The shape of frames mismatches. The rendered keyframes will be resized to (height: {height_guide}, width: {width_guide})\n"
|
|
198
|
+
frames_style.set_shape(height_guide, width_guide)
|
|
199
|
+
return frames_guide, frames_style, index_style, message
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def interpolate_video(
|
|
203
|
+
frames_path,
|
|
204
|
+
keyframes_path,
|
|
205
|
+
output_path,
|
|
206
|
+
fps,
|
|
207
|
+
batch_size,
|
|
208
|
+
tracking_window_size,
|
|
209
|
+
minimum_patch_size,
|
|
210
|
+
num_iter,
|
|
211
|
+
guide_weight,
|
|
212
|
+
initialize,
|
|
213
|
+
progress = None,
|
|
214
|
+
):
|
|
215
|
+
# input
|
|
216
|
+
frames_guide, frames_style, index_style, message = check_input_for_interpolating(frames_path, keyframes_path)
|
|
217
|
+
if len(message) > 0:
|
|
218
|
+
print(message)
|
|
219
|
+
# output
|
|
220
|
+
if output_path == "":
|
|
221
|
+
output_path = os.path.join(keyframes_path, "output")
|
|
222
|
+
os.makedirs(output_path, exist_ok=True)
|
|
223
|
+
print("No valid output_path. Your video will be saved here:", output_path)
|
|
224
|
+
elif not os.path.exists(output_path):
|
|
225
|
+
os.makedirs(output_path, exist_ok=True)
|
|
226
|
+
print("Your video will be saved here:", output_path)
|
|
227
|
+
output_frames_path = os.path.join(output_path, "frames")
|
|
228
|
+
output_video_path = os.path.join(output_path, "video.mp4")
|
|
229
|
+
os.makedirs(output_frames_path, exist_ok=True)
|
|
230
|
+
# process
|
|
231
|
+
ebsynth_config = {
|
|
232
|
+
"minimum_patch_size": minimum_patch_size,
|
|
233
|
+
"threads_per_block": 8,
|
|
234
|
+
"num_iter": num_iter,
|
|
235
|
+
"gpu_id": 0,
|
|
236
|
+
"guide_weight": guide_weight,
|
|
237
|
+
"initialize": initialize,
|
|
238
|
+
"tracking_window_size": tracking_window_size
|
|
239
|
+
}
|
|
240
|
+
if len(index_style)==1:
|
|
241
|
+
InterpolationModeSingleFrameRunner().run(frames_guide, frames_style, index_style, batch_size=batch_size, ebsynth_config=ebsynth_config, save_path=output_frames_path)
|
|
242
|
+
else:
|
|
243
|
+
InterpolationModeRunner().run(frames_guide, frames_style, index_style, batch_size=batch_size, ebsynth_config=ebsynth_config, save_path=output_frames_path)
|
|
244
|
+
try:
|
|
245
|
+
fps = int(fps)
|
|
246
|
+
except:
|
|
247
|
+
fps = 30
|
|
248
|
+
print("Fps:", fps)
|
|
249
|
+
print("Saving video...")
|
|
250
|
+
video_path = save_video(output_frames_path, output_video_path, num_frames=len(frames_guide), fps=fps)
|
|
251
|
+
print("Success!")
|
|
252
|
+
print("Your frames are here:", output_frames_path)
|
|
253
|
+
print("Your video is here:", video_path)
|
|
254
|
+
return output_path, fps, video_path
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def on_ui_tabs():
|
|
258
|
+
with gr.Blocks(analytics_enabled=False) as ui_component:
|
|
259
|
+
with gr.Tab("Blend"):
|
|
260
|
+
gr.Markdown("""
|
|
261
|
+
# Blend
|
|
262
|
+
|
|
263
|
+
Given a guide video and a style video, this algorithm will make the style video fluent according to the motion features of the guide video. Click [here](https://github.com/Artiprocher/sd-webui-fastblend/assets/35051019/208d902d-6aba-48d7-b7d5-cd120ebd306d) to see the example. Note that this extension doesn't support long videos. Please use short videos (e.g., several seconds). The algorithm is mainly designed for 512*512 resolution. Please use a larger `Minimum patch size` for higher resolution.
|
|
264
|
+
""")
|
|
265
|
+
with gr.Row():
|
|
266
|
+
with gr.Column():
|
|
267
|
+
with gr.Tab("Guide video"):
|
|
268
|
+
video_guide = gr.Video(label="Guide video")
|
|
269
|
+
with gr.Tab("Guide video (images format)"):
|
|
270
|
+
video_guide_folder = gr.Textbox(label="Guide video (images format)", value="")
|
|
271
|
+
with gr.Column():
|
|
272
|
+
with gr.Tab("Style video"):
|
|
273
|
+
video_style = gr.Video(label="Style video")
|
|
274
|
+
with gr.Tab("Style video (images format)"):
|
|
275
|
+
video_style_folder = gr.Textbox(label="Style video (images format)", value="")
|
|
276
|
+
with gr.Column():
|
|
277
|
+
output_path = gr.Textbox(label="Output directory", value="", placeholder="Leave empty to use the directory of style video")
|
|
278
|
+
fps = gr.Textbox(label="Fps", value="", placeholder="Leave empty to use the default fps")
|
|
279
|
+
video_output = gr.Video(label="Output video", interactive=False, show_share_button=True)
|
|
280
|
+
btn = gr.Button(value="Blend")
|
|
281
|
+
with gr.Row():
|
|
282
|
+
with gr.Column():
|
|
283
|
+
gr.Markdown("# Settings")
|
|
284
|
+
mode = gr.Radio(["Fast", "Balanced", "Accurate"], label="Inference mode", value="Fast", interactive=True)
|
|
285
|
+
window_size = gr.Slider(label="Sliding window size", value=15, minimum=1, maximum=1000, step=1, interactive=True)
|
|
286
|
+
batch_size = gr.Slider(label="Batch size", value=8, minimum=1, maximum=128, step=1, interactive=True)
|
|
287
|
+
tracking_window_size = gr.Slider(label="Tracking window size (only for accurate mode)", value=0, minimum=0, maximum=10, step=1, interactive=True)
|
|
288
|
+
gr.Markdown("## Advanced Settings")
|
|
289
|
+
minimum_patch_size = gr.Slider(label="Minimum patch size (odd number)", value=5, minimum=5, maximum=99, step=2, interactive=True)
|
|
290
|
+
num_iter = gr.Slider(label="Number of iterations", value=5, minimum=1, maximum=10, step=1, interactive=True)
|
|
291
|
+
guide_weight = gr.Slider(label="Guide weight", value=10.0, minimum=0.0, maximum=100.0, step=0.1, interactive=True)
|
|
292
|
+
initialize = gr.Radio(["identity", "random"], label="NNF initialization", value="identity", interactive=True)
|
|
293
|
+
with gr.Column():
|
|
294
|
+
gr.Markdown("""
|
|
295
|
+
# Reference
|
|
296
|
+
|
|
297
|
+
* Output directory: the directory to save the video.
|
|
298
|
+
* Inference mode
|
|
299
|
+
|
|
300
|
+
|Mode|Time|Memory|Quality|Frame by frame output|Description|
|
|
301
|
+
|-|-|-|-|-|-|
|
|
302
|
+
|Fast|■|■■■|■■|No|Blend the frames using a tree-like data structure, which requires much RAM but is fast.|
|
|
303
|
+
|Balanced|■■|■|■■|Yes|Blend the frames naively.|
|
|
304
|
+
|Accurate|■■■|■|■■■|Yes|Blend the frames and align them together for higher video quality. When [batch size] >= [sliding window size] * 2 + 1, the performance is the best.|
|
|
305
|
+
|
|
306
|
+
* Sliding window size: our algorithm will blend the frames in a sliding windows. If the size is n, each frame will be blended with the last n frames and the next n frames. A large sliding window can make the video fluent but sometimes smoggy.
|
|
307
|
+
* Batch size: a larger batch size makes the program faster but requires more VRAM.
|
|
308
|
+
* Tracking window size (only for accurate mode): The size of window in which our algorithm tracks moving objects. Empirically, 1 is enough.
|
|
309
|
+
* Advanced settings
|
|
310
|
+
* Minimum patch size (odd number): the minimum patch size used for patch matching. (Default: 5)
|
|
311
|
+
* Number of iterations: the number of iterations of patch matching. (Default: 5)
|
|
312
|
+
* Guide weight: a parameter that determines how much motion feature applied to the style video. (Default: 10)
|
|
313
|
+
* NNF initialization: how to initialize the NNF (Nearest Neighbor Field). (Default: identity)
|
|
314
|
+
""")
|
|
315
|
+
btn.click(
|
|
316
|
+
smooth_video,
|
|
317
|
+
inputs=[
|
|
318
|
+
video_guide,
|
|
319
|
+
video_guide_folder,
|
|
320
|
+
video_style,
|
|
321
|
+
video_style_folder,
|
|
322
|
+
mode,
|
|
323
|
+
window_size,
|
|
324
|
+
batch_size,
|
|
325
|
+
tracking_window_size,
|
|
326
|
+
output_path,
|
|
327
|
+
fps,
|
|
328
|
+
minimum_patch_size,
|
|
329
|
+
num_iter,
|
|
330
|
+
guide_weight,
|
|
331
|
+
initialize
|
|
332
|
+
],
|
|
333
|
+
outputs=[output_path, fps, video_output]
|
|
334
|
+
)
|
|
335
|
+
with gr.Tab("Interpolate"):
|
|
336
|
+
gr.Markdown("""
|
|
337
|
+
# Interpolate
|
|
338
|
+
|
|
339
|
+
Given a guide video and some rendered keyframes, this algorithm will render the remaining frames. Click [here](https://github.com/Artiprocher/sd-webui-fastblend/assets/35051019/3490c5b4-8f67-478f-86de-f9adc2ace16a) to see the example. The algorithm is experimental and is only tested for 512*512 resolution.
|
|
340
|
+
""")
|
|
341
|
+
with gr.Row():
|
|
342
|
+
with gr.Column():
|
|
343
|
+
with gr.Row():
|
|
344
|
+
with gr.Column():
|
|
345
|
+
video_guide_folder_ = gr.Textbox(label="Guide video (images format)", value="")
|
|
346
|
+
with gr.Column():
|
|
347
|
+
rendered_keyframes_ = gr.Textbox(label="Rendered keyframes (images format)", value="")
|
|
348
|
+
with gr.Row():
|
|
349
|
+
detected_frames = gr.Textbox(label="Detected frames", value="Please input the directory of guide video and rendered frames", lines=9, max_lines=9, interactive=False)
|
|
350
|
+
video_guide_folder_.change(detect_frames, inputs=[video_guide_folder_, rendered_keyframes_], outputs=detected_frames)
|
|
351
|
+
rendered_keyframes_.change(detect_frames, inputs=[video_guide_folder_, rendered_keyframes_], outputs=detected_frames)
|
|
352
|
+
with gr.Column():
|
|
353
|
+
output_path_ = gr.Textbox(label="Output directory", value="", placeholder="Leave empty to use the directory of rendered keyframes")
|
|
354
|
+
fps_ = gr.Textbox(label="Fps", value="", placeholder="Leave empty to use the default fps")
|
|
355
|
+
video_output_ = gr.Video(label="Output video", interactive=False, show_share_button=True)
|
|
356
|
+
btn_ = gr.Button(value="Interpolate")
|
|
357
|
+
with gr.Row():
|
|
358
|
+
with gr.Column():
|
|
359
|
+
gr.Markdown("# Settings")
|
|
360
|
+
batch_size_ = gr.Slider(label="Batch size", value=8, minimum=1, maximum=128, step=1, interactive=True)
|
|
361
|
+
tracking_window_size_ = gr.Slider(label="Tracking window size", value=0, minimum=0, maximum=10, step=1, interactive=True)
|
|
362
|
+
gr.Markdown("## Advanced Settings")
|
|
363
|
+
minimum_patch_size_ = gr.Slider(label="Minimum patch size (odd number, larger is better)", value=15, minimum=5, maximum=99, step=2, interactive=True)
|
|
364
|
+
num_iter_ = gr.Slider(label="Number of iterations", value=5, minimum=1, maximum=10, step=1, interactive=True)
|
|
365
|
+
guide_weight_ = gr.Slider(label="Guide weight", value=10.0, minimum=0.0, maximum=100.0, step=0.1, interactive=True)
|
|
366
|
+
initialize_ = gr.Radio(["identity", "random"], label="NNF initialization", value="identity", interactive=True)
|
|
367
|
+
with gr.Column():
|
|
368
|
+
gr.Markdown("""
|
|
369
|
+
# Reference
|
|
370
|
+
|
|
371
|
+
* Output directory: the directory to save the video.
|
|
372
|
+
* Batch size: a larger batch size makes the program faster but requires more VRAM.
|
|
373
|
+
* Tracking window size (only for accurate mode): The size of window in which our algorithm tracks moving objects. Empirically, 1 is enough.
|
|
374
|
+
* Advanced settings
|
|
375
|
+
* Minimum patch size (odd number): the minimum patch size used for patch matching. **This parameter should be larger than that in blending. (Default: 15)**
|
|
376
|
+
* Number of iterations: the number of iterations of patch matching. (Default: 5)
|
|
377
|
+
* Guide weight: a parameter that determines how much motion feature applied to the style video. (Default: 10)
|
|
378
|
+
* NNF initialization: how to initialize the NNF (Nearest Neighbor Field). (Default: identity)
|
|
379
|
+
""")
|
|
380
|
+
btn_.click(
|
|
381
|
+
interpolate_video,
|
|
382
|
+
inputs=[
|
|
383
|
+
video_guide_folder_,
|
|
384
|
+
rendered_keyframes_,
|
|
385
|
+
output_path_,
|
|
386
|
+
fps_,
|
|
387
|
+
batch_size_,
|
|
388
|
+
tracking_window_size_,
|
|
389
|
+
minimum_patch_size_,
|
|
390
|
+
num_iter_,
|
|
391
|
+
guide_weight_,
|
|
392
|
+
initialize_,
|
|
393
|
+
],
|
|
394
|
+
outputs=[output_path_, fps_, video_output_]
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
return [(ui_component, "FastBlend", "FastBlend_ui")]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import cupy as cp
|
|
2
|
+
|
|
3
|
+
remapping_kernel = cp.RawKernel(r'''
|
|
4
|
+
extern "C" __global__
|
|
5
|
+
void remap(
|
|
6
|
+
const int height,
|
|
7
|
+
const int width,
|
|
8
|
+
const int channel,
|
|
9
|
+
const int patch_size,
|
|
10
|
+
const int pad_size,
|
|
11
|
+
const float* source_style,
|
|
12
|
+
const int* nnf,
|
|
13
|
+
float* target_style
|
|
14
|
+
) {
|
|
15
|
+
const int r = (patch_size - 1) / 2;
|
|
16
|
+
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
|
17
|
+
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
|
18
|
+
if (x >= height or y >= width) return;
|
|
19
|
+
const int z = blockIdx.z * (height + pad_size * 2) * (width + pad_size * 2) * channel;
|
|
20
|
+
const int pid = (x + pad_size) * (width + pad_size * 2) + (y + pad_size);
|
|
21
|
+
const int min_px = x < r ? -x : -r;
|
|
22
|
+
const int max_px = x + r > height - 1 ? height - 1 - x : r;
|
|
23
|
+
const int min_py = y < r ? -y : -r;
|
|
24
|
+
const int max_py = y + r > width - 1 ? width - 1 - y : r;
|
|
25
|
+
int num = 0;
|
|
26
|
+
for (int px = min_px; px <= max_px; px++){
|
|
27
|
+
for (int py = min_py; py <= max_py; py++){
|
|
28
|
+
const int nid = (x + px) * width + y + py;
|
|
29
|
+
const int x_ = nnf[blockIdx.z * height * width * 2 + nid*2 + 0] - px;
|
|
30
|
+
const int y_ = nnf[blockIdx.z * height * width * 2 + nid*2 + 1] - py;
|
|
31
|
+
if (x_ < 0 or y_ < 0 or x_ >= height or y_ >= width)continue;
|
|
32
|
+
const int pid_ = (x_ + pad_size) * (width + pad_size * 2) + (y_ + pad_size);
|
|
33
|
+
num++;
|
|
34
|
+
for (int c = 0; c < channel; c++){
|
|
35
|
+
target_style[z + pid * channel + c] += source_style[z + pid_ * channel + c];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (int c = 0; c < channel; c++){
|
|
40
|
+
target_style[z + pid * channel + c] /= num;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
''', 'remap')
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
patch_error_kernel = cp.RawKernel(r'''
|
|
47
|
+
extern "C" __global__
|
|
48
|
+
void patch_error(
|
|
49
|
+
const int height,
|
|
50
|
+
const int width,
|
|
51
|
+
const int channel,
|
|
52
|
+
const int patch_size,
|
|
53
|
+
const int pad_size,
|
|
54
|
+
const float* source,
|
|
55
|
+
const int* nnf,
|
|
56
|
+
const float* target,
|
|
57
|
+
float* error
|
|
58
|
+
) {
|
|
59
|
+
const int r = (patch_size - 1) / 2;
|
|
60
|
+
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
|
61
|
+
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
|
62
|
+
const int z = blockIdx.z * (height + pad_size * 2) * (width + pad_size * 2) * channel;
|
|
63
|
+
if (x >= height or y >= width) return;
|
|
64
|
+
const int x_ = nnf[blockIdx.z * height * width * 2 + (x * width + y)*2 + 0];
|
|
65
|
+
const int y_ = nnf[blockIdx.z * height * width * 2 + (x * width + y)*2 + 1];
|
|
66
|
+
float e = 0;
|
|
67
|
+
for (int px = -r; px <= r; px++){
|
|
68
|
+
for (int py = -r; py <= r; py++){
|
|
69
|
+
const int pid = (x + pad_size + px) * (width + pad_size * 2) + y + pad_size + py;
|
|
70
|
+
const int pid_ = (x_ + pad_size + px) * (width + pad_size * 2) + y_ + pad_size + py;
|
|
71
|
+
for (int c = 0; c < channel; c++){
|
|
72
|
+
const float diff = target[z + pid * channel + c] - source[z + pid_ * channel + c];
|
|
73
|
+
e += diff * diff;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
error[blockIdx.z * height * width + x * width + y] = e;
|
|
78
|
+
}
|
|
79
|
+
''', 'patch_error')
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
pairwise_patch_error_kernel = cp.RawKernel(r'''
|
|
83
|
+
extern "C" __global__
|
|
84
|
+
void pairwise_patch_error(
|
|
85
|
+
const int height,
|
|
86
|
+
const int width,
|
|
87
|
+
const int channel,
|
|
88
|
+
const int patch_size,
|
|
89
|
+
const int pad_size,
|
|
90
|
+
const float* source_a,
|
|
91
|
+
const int* nnf_a,
|
|
92
|
+
const float* source_b,
|
|
93
|
+
const int* nnf_b,
|
|
94
|
+
float* error
|
|
95
|
+
) {
|
|
96
|
+
const int r = (patch_size - 1) / 2;
|
|
97
|
+
const int x = blockDim.x * blockIdx.x + threadIdx.x;
|
|
98
|
+
const int y = blockDim.y * blockIdx.y + threadIdx.y;
|
|
99
|
+
const int z = blockIdx.z * (height + pad_size * 2) * (width + pad_size * 2) * channel;
|
|
100
|
+
if (x >= height or y >= width) return;
|
|
101
|
+
const int z_nnf = blockIdx.z * height * width * 2 + (x * width + y) * 2;
|
|
102
|
+
const int x_a = nnf_a[z_nnf + 0];
|
|
103
|
+
const int y_a = nnf_a[z_nnf + 1];
|
|
104
|
+
const int x_b = nnf_b[z_nnf + 0];
|
|
105
|
+
const int y_b = nnf_b[z_nnf + 1];
|
|
106
|
+
float e = 0;
|
|
107
|
+
for (int px = -r; px <= r; px++){
|
|
108
|
+
for (int py = -r; py <= r; py++){
|
|
109
|
+
const int pid_a = (x_a + pad_size + px) * (width + pad_size * 2) + y_a + pad_size + py;
|
|
110
|
+
const int pid_b = (x_b + pad_size + px) * (width + pad_size * 2) + y_b + pad_size + py;
|
|
111
|
+
for (int c = 0; c < channel; c++){
|
|
112
|
+
const float diff = source_a[z + pid_a * channel + c] - source_b[z + pid_b * channel + c];
|
|
113
|
+
e += diff * diff;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
error[blockIdx.z * height * width + x * width + y] = e;
|
|
118
|
+
}
|
|
119
|
+
''', 'pairwise_patch_error')
|