-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrun.py
407 lines (351 loc) · 17.1 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# %%
# !! {"metadata":{
# !! "id": "ByGXyiHZWM_q"
# !! }}
"""
# **Deforum Stable Diffusion (v0.7.1)**
**Help keep these resources free for everyone**, please consider supporting us on [Patreon](https://www.patreon.com/deforum). Every bit of support is deeply appreciated!
- **Looking for a latest in Deforum development?** Check out the [Deforum Automatic1111 Extension](https://github.com/deforum-art/sd-webui-deforum)
- **Something not working properly?** Use our github page to submit a [New Issue](https://github.com/deforum-art/deforum-stable-diffusion/issues)
- **Need help?** For support please join our community [Discord](https://discord.gg/deforum)
"""
# %%
# !! {"metadata":{
# !! "cellView": "form",
# !! "id": "IJjzzkKlWM_s"
# !! }}
#@markdown **NVIDIA GPU**
#@markdown **Environment Setup**
import subprocess, time, gc, os, sys
sys.path.extend(['./deforum_flux', './deforum_flux/src', './x-flux'])
import torch
import random
from IPython import display
from types import SimpleNamespace
from helpers.save_images import get_output_folder
from helpers.settings import load_args
from helpers.render import render_animation, render_input_video, render_image_batch, render_interpolation
from helpers.prompts import Prompts
import argparse
import sys
import os
import re
import time
from glob import iglob
from io import BytesIO
import torch
from dataclasses import dataclass
from einops import rearrange
from PIL import ExifTags, Image
from torchvision import transforms
from transformers import pipeline
from src.flux.modules.layers import DoubleStreamBlockLoraProcessor
from src.flux.sampling import denoise, get_noise, get_schedule, prepare, unpack
from src.flux.util import (
configs,
load_ae,
load_clip,
load_flow_model_quintized,
load_flow_model,
load_t5
)
from helpers.ffmpeg_helpers import (
get_extension_maxframes,
get_auto_outdir_timestring,
get_ffmpeg_path,
make_mp4_ffmpeg,
make_gif_ffmpeg,
patrol_cycle
)
def ffmpegArgs():
ffmpeg_mode = "auto" #@param ["auto","manual","timestring"]
ffmpeg_outdir = "" #@param {type:"string"}
ffmpeg_timestring = "" #@param {type:"string"}
ffmpeg_image_path = "" #@param {type:"string"}
ffmpeg_mp4_path = "" #@param {type:"string"}
ffmpeg_gif_path = "" #@param {type:"string"}
ffmpeg_extension = "png" #@param {type:"string"}
ffmpeg_maxframes = 200 #@param
ffmpeg_fps = 12 #@param
# determine auto paths
if ffmpeg_mode == 'auto':
ffmpeg_outdir, ffmpeg_timestring = get_auto_outdir_timestring(args,ffmpeg_mode)
if ffmpeg_mode in ["auto","timestring"]:
ffmpeg_extension, ffmpeg_maxframes = get_extension_maxframes(args,ffmpeg_outdir,ffmpeg_timestring)
ffmpeg_image_path, ffmpeg_mp4_path, ffmpeg_gif_path = get_ffmpeg_path(ffmpeg_outdir, ffmpeg_timestring, ffmpeg_extension)
return locals()
class Model:
def __init__(self, name="flux-dev", quantized=True):
self.dit, self.ae, self.t5, self.clip = self.get_models(name, 'cuda', offload=False, is_schnell=False, quantized=quantized)
def get_models(self, name:str, device: torch.device, offload: bool, is_schnell: bool, quantized=True):
t5 = load_t5(device, max_length=256 if is_schnell else 512)
clip = load_clip(device)
if quantized:
dit = load_flow_model_quintized("flux-dev-fp8", device="cpu" if offload else device)
else:
dit = load_flow_model(name, device="cpu" if offload else device)
ae = load_ae(name, device="cpu" if offload else device)
return dit, ae, t5, clip
#@markdown **Path Setup**
def PathSetup():
output_path = "outputs" #@param {type:"string"}
#
# models_path = "./models" #@param {type:"string"}
return locals()
root = SimpleNamespace(**PathSetup())
#@markdown **Model Setup**
def ModelSetup():
custom_config_path = "" #@param {type:"string"}
custom_checkpoint_path = "" #@param {type:"string"}
map_location = "cuda" #@param ["cpu", "cuda"]
device = torch.device(map_location)
model = Model("flux-dev-fp8", quantized=True)
return locals()
root.__dict__.update(ModelSetup())
# %%
# !! {"metadata":{
# !! "id": "6JxwhBwtWM_t"
# !! }}
"""
# Settings
"""
# %%
# !! {"metadata":{
# !! "cellView": "form",
# !! "id": "E0tJVYA4WM_u"
# !! }}
def DeforumAnimArgs():
#@markdown ####**Animation:**
animation_mode = '2D' #@param ['None', '2D', '3D', 'Video Input', 'Interpolation'] {type:'string'}
max_frames = 1000 #@param {type:"number"}
border = 'replicate' #@param ['wrap', 'replicate'] {type:'string'}
#@markdown ####**Motion Parameters:**
angle = "0:(0)"#@param {type:"string"}
zoom = "0:(1.04)"#@param {type:"string"}
translation_x = "0:(0)"#@param {type:"string"}
translation_y = "0:(0)"#@param {type:"string"}
translation_z = "0:(7.5)"#@param {type:"string"}
rotation_3d_x = "0:(0)"#@param {type:"string"}
rotation_3d_y = "0:(0)"#@param {type:"string"}
rotation_3d_z = "0:(0)"#@param {type:"string"}
flip_2d_perspective = False #@param {type:"boolean"}
perspective_flip_theta = "0:(0)"#@param {type:"string"}
perspective_flip_phi = "0:(t%15)"#@param {type:"string"}
perspective_flip_gamma = "0:(0)"#@param {type:"string"}
perspective_flip_fv = "0:(53)"#@param {type:"string"}
noise_schedule = "0: (0.02)"#@param {type:"string"}
# strength_schedule = "0: (0.65), 24: (0.65), 25: (0.8), 47: (0.8), 48: (0.65)"#@param {type:"string"}
strength_schedule = "0: (0.65), 12: (0.70), 24: (0.8), 36: (0.85), 38: (0.65)"
contrast_schedule = "0: (1.0)"#@param {type:"string"}
hybrid_comp_alpha_schedule = "0:(1)" #@param {type:"string"}
hybrid_comp_mask_blend_alpha_schedule = "0:(0.5)" #@param {type:"string"}
hybrid_comp_mask_contrast_schedule = "0:(1)" #@param {type:"string"}
hybrid_comp_mask_auto_contrast_cutoff_high_schedule = "0:(100)" #@param {type:"string"}
hybrid_comp_mask_auto_contrast_cutoff_low_schedule = "0:(0)" #@param {type:"string"}
#@markdown ####**Sampler Scheduling:**
enable_schedule_samplers = False #@param {type:"boolean"}
sampler_schedule = "0:('Default Flux Scheduler')" #@param {type:"string"}
#@markdown ####**Unsharp mask (anti-blur) Parameters:**
kernel_schedule = "0: (5)"#@param {type:"string"}
sigma_schedule = "0: (1.0)"#@param {type:"string"}
amount_schedule = "0: (0.2)"#@param {type:"string"}
threshold_schedule = "0: (0.0)"#@param {type:"string"}
#@markdown ####**Coherence:**
color_coherence = 'Match Frame 0 RGB' #@param ['None', 'Match Frame 0 HSV', 'Match Frame 0 LAB', 'Match Frame 0 RGB', 'Video Input'] {type:'string'}
color_coherence_video_every_N_frames = 1 #@param {type:"integer"}
color_force_grayscale = False #@param {type:"boolean"}
diffusion_cadence = '1' #@param ['1','2','3','4','5','6','7','8'] {type:'string'}
#@markdown ####**3D Depth Warping:**
use_depth_warping = True #@param {type:"boolean"}
midas_weight = 0.3#@param {type:"number"}
near_plane = 200
far_plane = 10000
fov = 40#@param {type:"number"}
padding_mode = 'border'#@param ['border', 'reflection', 'zeros'] {type:'string'}
sampling_mode = 'bicubic'#@param ['bicubic', 'bilinear', 'nearest'] {type:'string'}
save_depth_maps = False #@param {type:"boolean"}
#@markdown ####**Video Input:**
video_init_path ='/content/video_in.mp4'#@param {type:"string"}
extract_nth_frame = 1#@param {type:"number"}
overwrite_extracted_frames = True #@param {type:"boolean"}
use_mask_video = False #@param {type:"boolean"}
video_mask_path ='/content/video_in.mp4'#@param {type:"string"}
#@markdown ####**Hybrid Video for 2D/3D Animation Mode:**
hybrid_generate_inputframes = False #@param {type:"boolean"}
hybrid_use_first_frame_as_init_image = True #@param {type:"boolean"}
hybrid_motion = "None" #@param ['None','Optical Flow','Perspective','Affine']
hybrid_motion_use_prev_img = False #@param {type:"boolean"}
hybrid_flow_method = "DIS Medium" #@param ['DenseRLOF','DIS Medium','Farneback','SF']
hybrid_composite = False #@param {type:"boolean"}
hybrid_comp_mask_type = "None" #@param ['None', 'Depth', 'Video Depth', 'Blend', 'Difference']
hybrid_comp_mask_inverse = False #@param {type:"boolean"}
hybrid_comp_mask_equalize = "None" #@param ['None','Before','After','Both']
hybrid_comp_mask_auto_contrast = False #@param {type:"boolean"}
hybrid_comp_save_extra_frames = False #@param {type:"boolean"}
hybrid_use_video_as_mse_image = False #@param {type:"boolean"}
#@markdown ####**Interpolation:**
interpolate_key_frames = False #@param {type:"boolean"}
interpolate_x_frames = 32 #@param {type:"number"}
#@markdown ####**Resume Animation:**
resume_from_timestring = False #@param {type:"boolean"}
resume_timestring = "20240810001544" #@param {type:"string"}
return locals()
# %%
# !! {"metadata":{
# !! "id": "i9fly1RIWM_u"
# !! }}
# prompts
prompts = {
0: "super realism, 4k, a highly detailed close-up view of a woman's mesmerizing blue eye, with realistic reflections and an intense natural sparkle. The iris displays intricate patterns of deep blues and subtle hints of lighter hues, while delicate veins add to the eye's natural complexity. Soft, diffused lighting enhances the eye's depth, with a blurred background to emphasize the eye's captivating beauty and detail.",
12: "super realism, 4k, the woman's blue eye transforms into a stunning cosmic scene. Tiny, luminous stars begin to appear within the iris, creating a sense of depth. Nebulae with swirling, ethereal colors—rich purples, blues, and pinks—emerge, blending seamlessly with the natural textures of the eye. The transition is smooth, with the cosmic elements gradually overtaking the eye's surface, adding a layer of wonder and vastness.",
24: "super realism, 4k, grand cosmic vista contained within the eye. The eye now features swirling galaxies with vibrant, spiraling arms, and floating celestial bodies such as distant planets and shimmering asteroids. Nebulae continue to swirl with dynamic, vivid colors, creating a surreal and expansive universe that feels both infinite and intimately contained within the eye's bounds.",
36: "super realism, 4k, The swirling galaxies and celestial bodies are now accompanied by pulsating stars and radiant supernovae, with intricate light effects and a sense of motion. The smooth transition ensures that the cosmic wonder within the eye remains captivating and visually coherent.",
}
neg_prompts = {
0: "wtf, bad image, corrupted image, deformed",
}
# can be a string, list, or dictionary
#prompts = [
# "a beautiful lake by Asher Brown Durand, trending on Artstation",
# "a beautiful portrait of a woman by Artgerm, trending on Artstation",
#]
#prompts = "a beautiful lake by Asher Brown Durand, trending on Artstation"
# %%
# !! {"metadata":{
# !! "cellView": "form",
# !! "id": "XVzhbmizWM_u"
# !! }}
#@markdown **Load Settings**
override_settings_with_file = False #@param {type:"boolean"}
settings_file = "custom" #@param ["custom", "512x512_aesthetic_0.json","512x512_aesthetic_1.json","512x512_colormatch_0.json","512x512_colormatch_1.json","512x512_colormatch_2.json","512x512_colormatch_3.json"]
custom_settings_file = "/content/drive/MyDrive/Settings.txt"#@param {type:"string"}
def DeforumArgs():
#@markdown **Image Settings**
W = 1024 #@param
H = 1024 #@param
W, H = map(lambda x: x - x % 64, (W, H)) # resize to integer multiple of 64
bit_depth_output = 8 #@param [8, 16, 32] {type:"raw"}
#@markdown **Sampling Settings**
seed = -1 #@param
sampler = 'Flux Default Sampler' # sampler schedule is not supported
steps = 30 #@param
scale = 3.5 #@param
dynamic_threshold = None
static_threshold = None
#@markdown **Save & Display Settings**
save_samples = True #@param {type:"boolean"}
save_settings = True #@param {type:"boolean"}
display_samples = True #@param {type:"boolean"}
save_sample_per_step = False #@param {type:"boolean"}
show_sample_per_step = False #@param {type:"boolean"}
#@markdown **Batch Settings**
n_batch = 1 #@param
n_samples = 1 #@param
batch_name = "StableFun" #@param {type:"string"}
filename_format = "{timestring}_{index}_{prompt}.png" #@param ["{timestring}_{index}_{seed}.png","{timestring}_{index}_{prompt}.png"]
seed_behavior = "iter" #@param ["iter","fixed","random","ladder","alternate"]
seed_iter_N = 1 #@param {type:'integer'}
make_grid = False #@param {type:"boolean"}
grid_rows = 2 #@param
outdir = get_output_folder(root.output_path, batch_name)
#@markdown **Init Settings**
use_init = False #@param {type:"boolean"}
strength = 1.0 #@param {type:"number"}
strength_0_no_init = True # Set the strength to 0 automatically when no init image is used
init_image = "https://cdn.pixabay.com/photo/2022/07/30/13/10/green-longhorn-beetle-7353749_1280.jpg" #@param {type:"string"}
add_init_noise = False #@param {type:"boolean"}
init_noise = 0.01 #@param
# Whiter areas of the mask are areas that change more
use_mask = False #@param {type:"boolean"}
use_alpha_as_mask = False # use the alpha channel of the init image as the mask
mask_file = "https://www.filterforge.com/wiki/images/archive/b/b7/20080927223728%21Polygonal_gradient_thumb.jpg" #@param {type:"string"}
invert_mask = False #@param {type:"boolean"}
# Adjust mask image, 1.0 is no adjustment. Should be positive numbers.
mask_brightness_adjust = 1.0 #@param {type:"number"}
mask_contrast_adjust = 1.0 #@param {type:"number"}
# Overlay the masked image at the end of the generation so it does not get degraded by encoding and decoding
overlay_mask = True # {type:"boolean"}
# Blur edges of final overlay mask, if used. Minimum = 0 (no blur)
mask_overlay_blur = 5 # {type:"number"}
#@markdown **Exposure/Contrast Conditional Settings**
mean_scale = 0 #@param {type:"number"}
var_scale = 0 #@param {type:"number"}
exposure_scale = 0 #@param {type:"number"}
exposure_target = 0.5 #@param {type:"number"}
#@markdown **Color Match Conditional Settings**
colormatch_scale = 0 #@param {type:"number"}
colormatch_image = "https://www.saasdesign.io/wp-content/uploads/2021/02/palette-3-min-980x588.png" #@param {type:"string"}
# colormatch_image = None
colormatch_n_colors = 4 #@param {type:"number"}
ignore_sat_weight = 0 #@param {type:"number"}
#@markdown **Other Conditional Settings**
init_mse_scale = 0 #@param {type:"number"}
init_mse_image = "https://cdn.pixabay.com/photo/2022/07/30/13/10/green-longhorn-beetle-7353749_1280.jpg" #@param {type:"string"}
blue_scale = 0 #@param {type:"number"}
#@markdown **Conditional Gradient Settings**
gradient_wrt = 'x0_pred' #@param ["x", "x0_pred"]
gradient_add_to = 'both' #@param ["cond", "uncond", "both"]
decode_method = 'linear' #@param ["autoencoder","linear"]
grad_threshold_type = 'dynamic' #@param ["dynamic", "static", "mean", "schedule"]
clamp_grad_threshold = 0.2 #@param {type:"number"}
clamp_start = 0.2 #@param
clamp_stop = 0.01 #@param
grad_inject_timing = list(range(1,10)) #@param
#@markdown **Speed vs VRAM Settings**
cond_uncond_sync = True #@param {type:"boolean"}
precision = 'autocast'
C = 4
f = 8
cond_prompt = ""
cond_prompts = ""
uncond_prompt = ""
uncond_prompts = ""
timestring = ""
init_latent = None
init_sample = None
init_sample_raw = None
mask_sample = None
init_c = None
seed_internal = 0
return locals()
args_dict = DeforumArgs()
anim_args_dict = DeforumAnimArgs()
if override_settings_with_file:
load_args(args_dict, anim_args_dict, settings_file, custom_settings_file, verbose=False)
args = SimpleNamespace(**args_dict)
anim_args = SimpleNamespace(**anim_args_dict)
args.timestring = time.strftime('%Y%m%d%H%M%S')
args.strength = max(0.0, min(1.0, args.strength))
if args.seed == -1:
args.seed = random.randint(0, 2**32 - 1)
if not args.use_init:
args.init_image = None
if args.sampler == 'plms' and (args.use_init or anim_args.animation_mode != 'None'):
print(f"Init images aren't supported with PLMS yet, switching to KLMS")
args.sampler = 'klms'
if args.sampler != 'ddim':
args.ddim_eta = 0
if anim_args.animation_mode == 'None':
anim_args.max_frames = 1
elif anim_args.animation_mode == 'Video Input':
args.use_init = True
# clean up unused memory
gc.collect()
torch.cuda.empty_cache()
# get prompts
cond, uncond = Prompts(prompt=prompts,neg_prompt=neg_prompts).as_dict()
try:
# dispatch to appropriate renderer
if anim_args.animation_mode == '2D' or anim_args.animation_mode == '3D':
render_animation(root, anim_args, args, cond, uncond)
elif anim_args.animation_mode == 'Video Input':
render_input_video(root, anim_args, args, cond, uncond)
elif anim_args.animation_mode == 'Interpolation':
render_interpolation(root, anim_args, args, cond, uncond)
else:
render_image_batch(root, args, cond, uncond)
except Exception as e: print(e)
finally:
ffmpeg_args_dict = ffmpegArgs()
ffmpeg_args = SimpleNamespace(**ffmpeg_args_dict)
make_mp4_ffmpeg(ffmpeg_args, display_ffmpeg=True, debug=False)