MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / RIFESmoother

Class RIFESmoother

diffsynth/processors/RIFE.py:7–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7class RIFESmoother(VideoProcessor):
8 def __init__(self, model, device="cuda", scale=1.0, batch_size=4, interpolate=True):
9 self.model = model
10 self.device = device
11
12 # IFNet only does not support float16
13 self.torch_dtype = torch.float32
14
15 # Other parameters
16 self.scale = scale
17 self.batch_size = batch_size
18 self.interpolate = interpolate
19
20 @staticmethod
21 def from_model_manager(model_manager, **kwargs):
22 return RIFESmoother(model_manager.RIFE, device=model_manager.device, **kwargs)
23
24 def process_image(self, image):
25 width, height = image.size
26 if width % 32 != 0 or height % 32 != 0:
27 width = (width + 31) // 32
28 height = (height + 31) // 32
29 image = image.resize((width, height))
30 image = torch.Tensor(np.array(image, dtype=np.float32)[:, :, [2,1,0]] / 255).permute(2, 0, 1)
31 return image
32
33 def process_images(self, images):
34 images = [self.process_image(image) for image in images]
35 images = torch.stack(images)
36 return images
37
38 def decode_images(self, images):
39 images = (images[:, [2,1,0]].permute(0, 2, 3, 1) * 255).clip(0, 255).numpy().astype(np.uint8)
40 images = [Image.fromarray(image) for image in images]
41 return images
42
43 def process_tensors(self, input_tensor, scale=1.0, batch_size=4):
44 output_tensor = []
45 for batch_id in range(0, input_tensor.shape[0], batch_size):
46 batch_id_ = min(batch_id + batch_size, input_tensor.shape[0])
47 batch_input_tensor = input_tensor[batch_id: batch_id_]
48 batch_input_tensor = batch_input_tensor.to(device=self.device, dtype=self.torch_dtype)
49 flow, mask, merged = self.model(batch_input_tensor, [4/scale, 2/scale, 1/scale])
50 output_tensor.append(merged[2].cpu())
51 output_tensor = torch.concat(output_tensor, dim=0)
52 return output_tensor
53
54 @torch.no_grad()
55 def __call__(self, rendered_frames, **kwargs):
56 # Preprocess
57 processed_images = self.process_images(rendered_frames)
58
59 # Input
60 input_tensor = torch.cat((processed_images[:-2], processed_images[2:]), dim=1)
61
62 # Interpolate
63 output_tensor = self.process_tensors(input_tensor, scale=self.scale, batch_size=self.batch_size)
64

Callers 1

from_model_managerMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected