Skip to content

Experimental render core with keyframe redistribution logic for Parseq. #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 156 commits into
base: automatic1111-webui
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
e0ba750
Update README.md
Tok Jun 1, 2024
6bd5e7c
Update README.md
Tok Jun 1, 2024
152f396
Started refacoring the render_animation method. Extracted some logic …
Jun 1, 2024
091a1fb
Update README.md
Tok Jun 1, 2024
3ec5a0d
Extracted looper and color match initialization. Introduced some temp…
Jun 1, 2024
72e6cf9
Merge branch 'refactor' of https://github.com/Tok/sd-webui-deforum in…
Jun 1, 2024
842c2b4
Opts schedule extracted.
Jun 1, 2024
6817c01
Replaced sample tuple with new class and applied the values.
Jun 1, 2024
fa05a2e
Moved the new Schedule class into it's own module and prepared a new …
Jun 1, 2024
49bce6f
Moved schedule related code to the new Schedule class and enforced pr…
Jun 1, 2024
4ef2432
Replaced tuples with proper classes.
Jun 1, 2024
c40c8b5
Prepared new dataclass for key collections.
Jun 1, 2024
b1eb3ca
Isolated agument expansion and parseq switch to static context.
Jun 1, 2024
36b8dde
Reverted all changes in animation_key_frames and moved new code to a …
Jun 1, 2024
1af714f
Turned all new classes into data classes and renamed the render_tools…
Jun 1, 2024
4240f75
Moved animation mode initialization to static context, trying to redu…
Jun 2, 2024
81c3b94
More code consolidation.
Jun 2, 2024
021a3f4
Moved raft- and depth map initialization into AnimationMode.
Jun 2, 2024
bc8790a
New Step class defined.
Jun 2, 2024
53e8ce2
Created initialization module to bundle render init code and tried to…
Jun 2, 2024
843a91c
Data package reorganized.
Jun 2, 2024
10bc067
Prepared new util package for modules with static helper functions an…
Jun 2, 2024
1fff071
Started switching some args to init.args and did some cleanup. Rename…
Jun 2, 2024
1b11a8a
All new dataclasses frozen and configured down to the required essent…
Jun 2, 2024
9184cf6
User-independent metadata for PyCharm users.
Jun 2, 2024
04f1edb
Added setup to easily run tests and start the webui with API enabled …
Jun 2, 2024
ac6abce
Replaced all aruments with the ones provided by init.args in order to…
Jun 2, 2024
bb06999
Started delegating args trough new methods in init and fixed some ref…
Jun 2, 2024
de2a407
Created helper to group arguments for calling anim_frame_warp. Also j…
Jun 3, 2024
573dac9
Merge pull request #1 from Tok/refactor
Tok Jun 3, 2024
bfa7f04
Created call_utils to funnel some calls and regroup their arguments.
Jun 3, 2024
a4107a0
More calls delegated.
Jun 3, 2024
7249563
Unnecessary divisor removed.
Jun 3, 2024
d62d98b
New put_all helper and more cleanup.
Jun 3, 2024
692f53e
Invalid opts ref fixed.
Jun 3, 2024
53b10da
Started isolating turbo frame generation related data and did some cl…
Jun 4, 2024
7435cf1
Step initialization.
Jun 4, 2024
680031c
Root data proxy prepared.
Jun 4, 2024
4b327a4
Moved proxy and wrapper stuff to a different branch.
Jun 7, 2024
dc03db8
Context managenemt.
Jun 8, 2024
3284a69
Merge pull request #2 from Tok/refactor
Tok Jun 8, 2024
986e781
Mask init and resume logic extracted.
Jun 8, 2024
4c0b4a6
Juggled some logic and moved indices and images into new dataclasses.
Jun 8, 2024
f5c4348
Accidental change reverted.
Jun 8, 2024
52b40dd
Separated web-UI and memory handling and started extracting progressi…
Jun 8, 2024
2a048a0
Index recontextualization. Calling concern separation. Utilities unt…
Jun 8, 2024
4a1ca7e
Namespace cleanup and new utility module for file naming.
Jun 8, 2024
bb66790
Optimized some code and defined simple combo_context.
Jun 9, 2024
b511cad
Merge pull request #3 from Tok/refactor
Tok Jun 9, 2024
1326a3f
Step and SubStep preparation. Info printing separation.
Jun 9, 2024
5cf6c08
Subtitle writing moved to Step. Cadence flow moved to TweenStep.
Jun 9, 2024
3317b1b
Adjusted some calls related to noise and image masking.
Jun 9, 2024
887f10f
Extracted depth prediction and started extracting hybrid motion logic…
Jun 9, 2024
bce30e7
Image transformation step extraction.
Jun 9, 2024
4fbe9d2
Image transformation step isolation.
Jun 9, 2024
0bf186f
Image transformation pipe.
Jun 9, 2024
3d2d9cb
SubSeed scheduling and transformation pipes for contrast- and noise i…
Jun 9, 2024
d06419b
Step preparation.
Jun 9, 2024
ae82028
Generated update for readme.
Jun 10, 2024
1f3d7a5
Merge pull request #4 from Tok/refactor
Tok Jun 10, 2024
fd52bff
Extracted tween frame emission and moved random seed generation to ut…
Jun 10, 2024
c939f2d
Prepared conditions. Removed direct root from step and added more typ…
Jun 11, 2024
95ad88f
Uncurried image transformation tubes into separate calls for creation…
Jun 11, 2024
539261e
Merge branch 'refactor' into automatic1111-webui
Jun 11, 2024
744eadf
Optical flow tube.
Jun 11, 2024
8459d6d
Cleanup.
Jun 12, 2024
eba86d3
Extracted diffusion redo and generation state update. Image to image …
Jun 14, 2024
2877398
Removed code that abused contextmanagers to control scope because it'…
Jun 14, 2024
9e65e7a
Added processing predicate to make img2img tubes conditional.
Jun 14, 2024
8748d5c
Merge pull request #5 from Tok/refactor
Tok Jun 14, 2024
a49fd53
Noise and contrast tubes combined and conditional redoes extracted.
Jun 14, 2024
185cd69
Removed manual garbage collection because memory profile is changing.
Jun 14, 2024
4864843
Warning when iteration is skipped after the call to "generate" return…
Jun 15, 2024
3136f61
Merge pull request #6 from Tok/refactor
Tok Jun 15, 2024
e17cadd
Created typealias for tubes and added some typehints and comments to …
Jun 15, 2024
0f0f146
Removing .idea directory from main branches.
Jun 15, 2024
62c0d08
Merge pull request #7 from Tok/refactor
Tok Jun 15, 2024
4c88c8c
Links in README.
Jun 15, 2024
458e164
Tween step generation refactored.
Jun 15, 2024
e4b5284
Merge pull request #8 from Tok/refactor
Tok Jun 15, 2024
6a56672
Reduced argument complexity by moving state objects to RenderInit.
Jun 15, 2024
235b61f
RenderInit renamed to RenderData and StepInit renamed to StepData.
Jun 15, 2024
e288f25
Moved tween frame generation to TweenFrame class.
Jun 15, 2024
f54887b
Step logic moved.
Jun 15, 2024
318bf99
Tween index generation detached.
Jun 15, 2024
e50419a
Turbo cleanup.
Jun 15, 2024
c3593c0
Merge pull request #9 from Tok/refactor
Tok Jun 15, 2024
661910b
Cadence logging combined.
Jun 16, 2024
90ad9f1
Switched main render loop to process a list of precalculated steps.
Jun 16, 2024
fb55f56
Step objects renamed and moved step side effects from creation to exe…
Jun 16, 2024
5da3e0e
Merge pull request #10 from Tok/refactor
Tok Jun 16, 2024
fa50ec1
Removed key step reference from tween step instances and fixed new co…
Jun 16, 2024
4f3b1b6
Tween step precalculation (WIP).
Jun 17, 2024
b161dbb
Tween step precalculation and randomized key frame distribution (proo…
Jun 17, 2024
6f7f995
Key step sorting fixed.
Jun 17, 2024
958b0eb
Merge pull request #11 from Tok/refactor
Tok Jun 17, 2024
952ba4b
Cleanup and fixed new info in log.
Jun 18, 2024
dbc362a
Merge branch 'refactor' into automatic1111-webui
Jun 18, 2024
23dea4d
Fixed uniform index calculation and some issues with saving frames.
Jun 19, 2024
a255199
Merge branch 'refactor' into automatic1111-webui
Jun 19, 2024
3dce017
Table print to CLI suppressed, because it's not compatible with varia…
Jun 19, 2024
2abe737
Cleanup.
Jun 19, 2024
77b5f19
New annotation to suppress table printing.
Jun 19, 2024
30f5745
Moved index distribution logic to new module.
Jun 19, 2024
05c5d9b
Progress bar for tween frames.
Jun 20, 2024
e82f47e
Merge pull request #12 from Tok/refactor
Tok Jun 20, 2024
7cf9b72
Animation key regeneration.
Jun 22, 2024
e863251
Parseq key frame index distribution.
Jun 22, 2024
f463ec3
Parseq keyframe replacement.
Jun 22, 2024
eca0dad
Depth prediction fix.
Jun 22, 2024
affbdb4
Merge branch 'refactor' into automatic1111-webui
Jun 22, 2024
8d06015
Parseq key frame distribution described in README.
Jun 22, 2024
7b449b6
Med & low VRAM fix.
Jun 23, 2024
de95944
Merge pull request #13 from Tok/refactor
Tok Jun 23, 2024
748da5b
Animation resume logic prepared.
Jun 23, 2024
330dfbf
Parseq only keyframe distribution.
Jun 29, 2024
abfbe1f
Variable pseudo cadence setup.
Jun 29, 2024
d720872
Merge branch 'refactor' into automatic1111-webui
Jun 29, 2024
5817392
Readme update.
Jun 30, 2024
c033199
Merge branch 'refactor' into automatic1111-webui
Jun 30, 2024
794d117
RAFT fix (wip).
Jul 1, 2024
753fd6d
Added new tab and fixed some things.
Jul 6, 2024
c4c15b3
Merge branch 'refactor' of https://github.com/Tok/sd-webui-deforum in…
Jul 6, 2024
95cbb20
Merge pull request #14 from Tok/refactor
Tok Jul 6, 2024
62c9b8b
Flow factor tuning.
Jul 6, 2024
76ff811
Example clip added to readme.
Jul 7, 2024
767dfd6
Readme update.
Jul 7, 2024
b98e01b
Readme update.
Jul 7, 2024
13dc5c4
Cleanup and fixed critical bug that would ignore transformation param…
Jul 13, 2024
a2a527c
Renamed key step to key frame and renamed step package to frame so it…
Jul 13, 2024
abf20f3
More dataclasses frozen. Prepared fixing color coherence & more cleanup.
Jul 13, 2024
7d8584d
More cleanup and fixes.
Jul 13, 2024
9cb15b8
More code simplification and cleanup.
Jul 14, 2024
568e504
Removed new tab and integrated keyframe redistrubution selection to e…
Jul 31, 2024
41fae0d
Obsolete call wrapper removed.
Aug 3, 2024
f852a78
New type for PIL image tubes and improved type hints in img_2_img tub…
Aug 3, 2024
cda6215
Moved conversion methods to image utils.
Aug 3, 2024
cb5bce3
Improved description of the new key frame redistribution modes.
Aug 3, 2024
74ba60e
Merge pull request #15 from Tok/feature/integration
Tok Aug 3, 2024
6e960b0
Fixed crash when using keyframe redistribution logic combined with op…
Aug 5, 2024
755f566
Obsolete table suppression code removed.
Aug 5, 2024
840140d
Tqdm behaviour fixed.
Aug 5, 2024
16d4be8
Merge branch 'feature/integration' into automatic1111-webui
Aug 5, 2024
7041ac1
Constants for ANSI codes.
Aug 5, 2024
93501bb
Cleanup.
Aug 5, 2024
dbfb516
Removed opts reference from render data and moved all global opts acc…
Aug 5, 2024
5ab4e86
Flow factor schedule fix.
Aug 5, 2024
59b92b2
More cleanup.
Aug 5, 2024
1028aba
Moved some depth related code to a new utility module and solved some…
Aug 6, 2024
a5874f8
Renamed new core to experimental core and removed unused transformati…
Aug 6, 2024
59bd7e1
More cleanup.
Aug 6, 2024
766add5
Reverted some of the removed code for prev transformation, but added …
Aug 6, 2024
0fd9878
README reverted back to original (PR preps).
Aug 16, 2024
5846810
User specific gitignores removed.
Aug 16, 2024
ce02d25
Logging a new warning for when Parseq keyframe redistribution is setu…
Aug 16, 2024
5c34eb7
Final 0.
Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/default_settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
"hybrid_comp_save_extra_frames": false,
"parseq_manifest": "",
"parseq_use_deltas": true,
"parseq_non_schedule_overrides": true,
"parseq_non_schedule_overrides": true,
"parseq_key_frame_redistribution": "off",
"use_looper": false,
"init_images": "{\n \"0\": \"https://deforum.github.io/a1/Gi1.png\",\n \"max_f/4-5\": \"https://deforum.github.io/a1/Gi2.png\",\n \"max_f/2-10\": \"https://deforum.github.io/a1/Gi3.png\",\n \"3*max_f/4-15\": \"https://deforum.github.io/a1/Gi4.jpg\",\n \"max_f-20\": \"https://deforum.github.io/a1/Gi1.png\"\n}",
"image_strength_schedule": "0:(0.75)",
Expand Down
17 changes: 12 additions & 5 deletions scripts/deforum_helpers/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import modules.paths as ph
import modules.shared as sh
from modules.processing import get_fixed_seed
from .defaults import get_guided_imgs_default_json, mask_fill_choices, get_samplers_list, get_schedulers_list
from .defaults import (get_guided_imgs_default_json, get_parseq_keyframe_redistributions_list,
get_samplers_list, get_schedulers_list)
from .deforum_controlnet import controlnet_component_names
from .general_utils import get_os, substitute_placeholders

Expand Down Expand Up @@ -928,7 +929,7 @@ def DeforumArgs():
"type": "checkbox",
"value": False,
"info": "Preview motion only. Uses a static picture for init, and draw motion reference rectangle."
},
},
}

def LoopArgs():
Expand Down Expand Up @@ -990,7 +991,14 @@ def ParseqArgs():
"type": "checkbox",
"value": True,
"info": "Recommended. If you uncheck this, the FPS, max_frames and cadence in the Parseq doc are ignored, and the values in the A1111 UI are used instead."
}
},
"parseq_key_frame_redistribution": {
"label": "Parseq key frame redistribution.",
"type": "dropdown",
"choices": get_parseq_keyframe_redistributions_list().values(),
"value": "None",
"info": "Gain Parseq precision at the cost of cadence regularity. Allows for fast generations at high cadence."
},
}

def DeforumOutputArgs():
Expand Down Expand Up @@ -1119,8 +1127,7 @@ def DeforumOutputArgs():
"value": False,
"info": "Interpolate upscaled images, if available",
"visible": False
},

},
}

def get_component_names():
Expand Down
7 changes: 7 additions & 0 deletions scripts/deforum_helpers/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def get_schedulers_list():
'sgm uniform': 'SGM Uniform'
}

def get_parseq_keyframe_redistributions_list():
return {
'off': 'Off',
'parseq_only': 'Parseq Only (no cadence)',
'uniform_with_parseq': 'Uniform with Parseq (pseudo-cadence)'
}

def DeforumAnimPrompts():
return r"""{
"0": "tiny cute bunny, vibrant diffraction, highly detailed, intricate, ultra hd, sharp photo, crepuscular rays, in focus",
Expand Down
12 changes: 9 additions & 3 deletions scripts/deforum_helpers/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@
from .prompt import prepare_prompt
from modules.shared import opts, cmd_opts, state, sd_model
from modules import lowvram, devices, sd_hijack
from .rendering import experimental_core
from .RAFT import RAFT

from deforum_api import JobStatusTracker

def render_animation(args, anim_args, video_args, parseq_args, loop_args, controlnet_args, root):
is_use_parseq = parseq_args.parseq_manifest and parseq_args.parseq_manifest.strip()
is_use_key_frame_redistribution = parseq_args.parseq_key_frame_redistribution != "Off"
is_use_new_render_core = is_use_parseq and is_use_key_frame_redistribution
if is_use_new_render_core:
experimental_core.render_animation(args, anim_args, video_args, parseq_args, loop_args, controlnet_args, root)
return

# initialise Parseq adapter
parseq_adapter = ParseqAdapter(parseq_args, anim_args, video_args, controlnet_args, loop_args)
Expand Down Expand Up @@ -538,7 +545,7 @@ def render_animation(args, anim_args, video_args, parseq_args, loop_args, contro
devices.torch_gc()
lowvram.setup_for_low_vram(sd_model, cmd_opts.medvram)
sd_hijack.model_hijack.hijack(sd_model)

optical_flow_redo_generation = anim_args.optical_flow_redo_generation if not args.motion_preview_mode else 'None'

# optical flow redo before generation
Expand Down Expand Up @@ -637,7 +644,7 @@ def render_animation(args, anim_args, video_args, parseq_args, loop_args, contro

args.seed = next_seed(args, root)

last_preview_frame = render_preview(args, anim_args, video_args, root, frame_idx, last_preview_frame)
last_preview_frame = render_preview(args, anim_args, video_args, root, frame_idx, last_preview_frame)

JobStatusTracker().update_phase(root.job_id, phase="GENERATING", progress=frame_idx/anim_args.max_frames)

Expand All @@ -647,4 +654,3 @@ def render_animation(args, anim_args, video_args, parseq_args, loop_args, contro

if load_raft:
raft_model.delete_model()

Empty file.
6 changes: 6 additions & 0 deletions scripts/deforum_helpers/rendering/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .images import Images
from .indexes import Indexes
from .mask import Mask
from .schedule import Schedule
from .render_data import RenderData
from .turbo import Turbo
2 changes: 2 additions & 0 deletions scripts/deforum_helpers/rendering/data/anim/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .animation_keys import AnimationKeys
from .animation_mode import AnimationMode
24 changes: 24 additions & 0 deletions scripts/deforum_helpers/rendering/data/anim/animation_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from dataclasses import dataclass

from ....animation_key_frames import DeformAnimKeys, LooperAnimKeys


@dataclass(init=True, frozen=True, repr=False, eq=False)
class AnimationKeys:
deform_keys: DeformAnimKeys
looper_keys: LooperAnimKeys

@staticmethod
def _choose_default_or_parseq_keys(default_keys, parseq_keys, parseq_adapter):
return default_keys if not parseq_adapter.use_parseq else parseq_keys

@staticmethod
def from_args(step_args, parseq_adapter, seed):
ada = parseq_adapter

def _choose(default_keys):
return AnimationKeys._choose_default_or_parseq_keys(default_keys, ada.anim_keys, ada)

# Parseq keys are decorated, see ParseqAnimKeysDecorator and ParseqLooperKeysDecorator
return AnimationKeys(_choose(DeformAnimKeys(step_args.anim_args, seed)),
_choose(LooperAnimKeys(step_args.loop_args, step_args.anim_args, seed)))
95 changes: 95 additions & 0 deletions scripts/deforum_helpers/rendering/data/anim/animation_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any

from ...util import opt_utils
from ....RAFT import RAFT
from ....hybrid_video import hybrid_generation


@dataclass(init=True, frozen=False, repr=False, eq=False)
class AnimationMode:
has_video_input: bool = False
hybrid_input_files: Any = None
hybrid_frame_path: str = ""
prev_flow: Any | None = None
is_keep_in_vram: bool = False
depth_model: Any = None
raft_model: Any = None

def is_predicting_depths(self) -> bool:
return self.depth_model is not None

def is_raft_active(self) -> bool:
return self.raft_model is not None

def unload_raft_and_depth_model(self):
if self.is_predicting_depths() and not self.is_keep_in_vram:
self.depth_model.delete_model() # handles adabins too
if self.is_raft_active():
self.raft_model.delete_model()

@staticmethod
def _has_video_input(anim_args) -> bool:
return AnimationMode._is_2d_or_3d_mode(anim_args) and AnimationMode._is_using_hybrid_frames(anim_args)

@staticmethod
def _is_2d_or_3d_mode(anim_args):
return anim_args.animation_mode in ['2D', '3D']

@staticmethod
def _is_using_hybrid_frames(anim_args):
return (anim_args.hybrid_composite != 'None'
or anim_args.hybrid_motion in ['Affine', 'Perspective', 'Optical Flow'])

@staticmethod
def _is_requiring_hybrid_frames(anim_args):
return AnimationMode._is_2d_or_3d_mode(anim_args) and AnimationMode._is_using_hybrid_frames(anim_args)

@staticmethod
def _is_load_depth_model_for_3d(args, anim_args):
is_depth_warped_3d = anim_args.animation_mode == '3D' and anim_args.use_depth_warping
has_depth_or_depth_video_mask = anim_args.hybrid_comp_mask_type in ['Depth', 'Video Depth']
is_composite_with_depth_mask = anim_args.hybrid_composite and has_depth_or_depth_video_mask
is_depth_used = is_depth_warped_3d or anim_args.save_depth_maps or is_composite_with_depth_mask
return is_depth_used and not args.motion_preview_mode

@staticmethod
def load_raft_if_active(anim_args, args):
is_cadenced_raft = anim_args.optical_flow_cadence == "RAFT" and int(anim_args.diffusion_cadence) > 1
is_optical_flow_raft = anim_args.hybrid_motion == "Optical Flow" and anim_args.hybrid_flow_method == "RAFT"
is_raft_redo = anim_args.optical_flow_redo_generation == "RAFT"
is_load_raft = (is_cadenced_raft or is_optical_flow_raft or is_raft_redo) and not args.motion_preview_mode
if is_load_raft:
print("Loading RAFT model...")
return RAFT() if is_load_raft else None

@staticmethod
def load_depth_model_if_active(args, anim_args):
return AnimationMode._is_load_depth_model_for_3d(args, anim_args) \
if opt_utils.keep_3d_models_in_vram() else None

@staticmethod
def initial_hybrid_files(sa) -> list[Path]:
"""Returns a list of initial hybrid input files if required, otherwise an empty list."""
if AnimationMode._is_requiring_hybrid_frames(sa.anim_args):
# may cause side effects on args and anim_args.
_, __, init_hybrid_input_files = hybrid_generation(sa.args, sa.anim_args, sa.root)
return init_hybrid_input_files
return []

@staticmethod
def from_args(step_args):
sa = step_args # RenderInitArgs
# path required by hybrid functions, even if hybrid_comp_save_extra_frames is False
hybrid_input_files: Any = os.path.join(sa.args.outdir, 'hybridframes')
previous_flow = None
return AnimationMode(
AnimationMode._has_video_input(sa.anim_args),
AnimationMode.initial_hybrid_files(sa),
hybrid_input_files,
previous_flow,
opt_utils.keep_3d_models_in_vram(),
AnimationMode.load_depth_model_if_active(sa.args, sa.anim_args),
AnimationMode.load_raft_if_active(sa.anim_args, sa.args))
4 changes: 4 additions & 0 deletions scripts/deforum_helpers/rendering/data/frame/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .key_frame_distribution import KeyFrameDistribution
from .key_frame_data import KeyFrameData
from .key_frame import KeyFrame
from .tween_frame import Tween
Loading