1
+ import numpy as np
1
2
import torch
2
3
from modules import images , shared
3
4
from modules .devices import device , dtype_vae , torch_gc
@@ -25,13 +26,17 @@ def randomize(
25
26
init_alpha [init_alpha < 0 ] = 0
26
27
27
28
if params .last_frame is not None :
29
+ last_frame = params .last_frame
30
+ if type (last_frame ) == str :
31
+ from modules .api .api import decode_base64_to_image
32
+ last_frame = decode_base64_to_image (last_frame )
28
33
# Get last_alpha
29
34
last_alpha = [
30
35
1 - pow (i , params .latent_power_last ) / params .latent_scale_last
31
- for i in range (params .last_frame )
36
+ for i in range (params .video_length )
32
37
]
33
38
last_alpha .reverse ()
34
- logger .info (f"Randomizing last_latent according to { init_alpha } ." )
39
+ logger .info (f"Randomizing last_latent according to { last_alpha } ." )
35
40
last_alpha = torch .tensor (last_alpha , dtype = torch .float32 , device = device )[
36
41
:, None , None , None
37
42
]
@@ -43,13 +48,18 @@ def randomize(
43
48
scaling_factor = 1 / sum_alpha [mask_alpha ]
44
49
init_alpha [mask_alpha ] *= scaling_factor
45
50
last_alpha [mask_alpha ] *= scaling_factor
51
+ init_alpha [0 ] = 1
52
+ init_alpha [- 1 ] = 0
53
+ last_alpha [0 ] = 0
54
+ last_alpha [- 1 ] = 1
46
55
47
56
# Calculate last_latent
48
- last_frame = params .last_frame
49
57
if p .resize_mode != 3 :
50
58
last_frame = images .resize_image (
51
59
p .resize_mode , last_frame , p .width , p .height
52
- )[None , ...]
60
+ )
61
+ last_frame = np .array (last_frame ).astype (np .float32 ) / 255.0
62
+ last_frame = np .moveaxis (last_frame , 2 , 0 )[None , ...]
53
63
last_frame = torch .from_numpy (last_frame ).to (device ).to (dtype_vae )
54
64
last_latent = images_tensor_to_samples (
55
65
last_frame ,
@@ -64,11 +74,10 @@ def randomize(
64
74
size = (p .height // opt_f , p .width // opt_f ),
65
75
mode = "bilinear" ,
66
76
)
67
-
68
77
# Modify init_latent
69
78
p .init_latent = (
70
79
p .init_latent * init_alpha
71
- + p . last_latent * last_alpha
80
+ + last_latent * last_alpha
72
81
+ p .rng .next () * (1 - init_alpha - last_alpha )
73
82
)
74
83
else :
0 commit comments