inference.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. """
  2. python inference.py \
  3. --variant mobilenetv3 \
  4. --checkpoint "CHECKPOINT" \
  5. --device cuda \
  6. --input-source "input.mp4" \
  7. --output-type video \
  8. --output-composition "composition.mp4" \
  9. --output-alpha "alpha.mp4" \
  10. --output-foreground "foreground.mp4" \
  11. --output-video-mbps 4 \
  12. --seq-chunk 1
  13. """
  14. import torch
  15. import os
  16. from torch.utils.data import DataLoader
  17. from torchvision import transforms
  18. from typing import Optional, Tuple
  19. from tqdm.auto import tqdm
  20. from inference_utils import VideoReader, VideoWriter, ImageSequenceReader, ImageSequenceWriter
  21. def convert_video(model,
  22. input_source: str,
  23. input_resize: Optional[Tuple[int, int]] = None,
  24. downsample_ratio: Optional[float] = None,
  25. output_type: str = 'video',
  26. output_composition: Optional[str] = None,
  27. output_alpha: Optional[str] = None,
  28. output_foreground: Optional[str] = None,
  29. output_video_mbps: Optional[float] = None,
  30. seq_chunk: int = 1,
  31. num_workers: int = 0,
  32. progress: bool = True,
  33. device: Optional[str] = None,
  34. dtype: Optional[torch.dtype] = None):
  35. """
  36. Args:
  37. input_source:A video file, or an image sequence directory. Images must be sorted in accending order, support png and jpg.
  38. input_resize: If provided, the input are first resized to (w, h).
  39. downsample_ratio: The model's downsample_ratio hyperparameter. If not provided, model automatically set one.
  40. output_type: Options: ["video", "png_sequence"].
  41. output_composition:
  42. The composition output path. File path if output_type == 'video'. Directory path if output_type == 'png_sequence'.
  43. If output_type == 'video', the composition has green screen background.
  44. If output_type == 'png_sequence'. the composition is RGBA png images.
  45. output_alpha: The alpha output from the model.
  46. output_foreground: The foreground output from the model.
  47. seq_chunk: Number of frames to process at once. Increase it for better parallelism.
  48. num_workers: PyTorch's DataLoader workers. Only use >0 for image input.
  49. progress: Show progress bar.
  50. device: Only need to manually provide if model is a TorchScript freezed model.
  51. dtype: Only need to manually provide if model is a TorchScript freezed model.
  52. """
  53. assert downsample_ratio is None or (downsample_ratio > 0 and downsample_ratio <= 1), 'Downsample ratio must be between 0 (exclusive) and 1 (inclusive).'
  54. assert any([output_composition, output_alpha, output_foreground]), 'Must provide at least one output.'
  55. assert output_type in ['video', 'png_sequence'], 'Only support "video" and "png_sequence" output modes.'
  56. assert seq_chunk >= 1, 'Sequence chunk must be >= 1'
  57. assert num_workers >= 0, 'Number of workers must be >= 0'
  58. # Initialize transform
  59. if input_resize is not None:
  60. transform = transforms.Compose([
  61. transforms.Resize(input_resize[::-1]),
  62. transforms.ToTensor()
  63. ])
  64. else:
  65. transform = transforms.ToTensor()
  66. # Initialize reader
  67. if os.path.isfile(input_source):
  68. source = VideoReader(input_source, transform)
  69. else:
  70. source = ImageSequenceReader(input_source, transform)
  71. reader = DataLoader(source, batch_size=seq_chunk, pin_memory=True, num_workers=num_workers)
  72. # Initialize writers
  73. if output_type == 'video':
  74. frame_rate = source.frame_rate if isinstance(source, VideoReader) else 30
  75. output_video_mbps = 1 if output_video_mbps is None else output_video_mbps
  76. if output_composition is not None:
  77. writer_com = VideoWriter(
  78. path=output_composition,
  79. frame_rate=frame_rate,
  80. bit_rate=int(output_video_mbps * 1000000))
  81. if output_alpha is not None:
  82. writer_pha = VideoWriter(
  83. path=output_alpha,
  84. frame_rate=frame_rate,
  85. bit_rate=int(output_video_mbps * 1000000))
  86. if output_foreground is not None:
  87. writer_fgr = VideoWriter(
  88. path=output_foreground,
  89. frame_rate=frame_rate,
  90. bit_rate=int(output_video_mbps * 1000000))
  91. else:
  92. if output_composition is not None:
  93. writer_com = ImageSequenceWriter(output_composition, 'png')
  94. if output_alpha is not None:
  95. writer_pha = ImageSequenceWriter(output_alpha, 'png')
  96. if output_foreground is not None:
  97. writer_fgr = ImageSequenceWriter(output_foreground, 'png')
  98. # Inference
  99. model = model.eval()
  100. if device is None or dtype is None:
  101. param = next(model.parameters())
  102. dtype = param.dtype
  103. device = param.device
  104. if (output_composition is not None) and (output_type == 'video'):
  105. bgr = torch.tensor([120, 255, 155], device=device, dtype=dtype).div(255).view(1, 1, 3, 1, 1)
  106. try:
  107. with torch.no_grad():
  108. bar = tqdm(total=len(source), disable=not progress, dynamic_ncols=True)
  109. rec = [None] * 4
  110. for src in reader:
  111. if downsample_ratio is None:
  112. downsample_ratio = auto_downsample_ratio(*src.shape[2:])
  113. src = src.to(device, dtype, non_blocking=True).unsqueeze(0) # [B, T, C, H, W]
  114. fgr, pha, *rec = model(src, *rec, downsample_ratio)
  115. if output_foreground is not None:
  116. writer_fgr.write(fgr[0])
  117. if output_alpha is not None:
  118. writer_pha.write(pha[0])
  119. if output_composition is not None:
  120. if output_type == 'video':
  121. com = fgr * pha + bgr * (1 - pha)
  122. else:
  123. fgr = fgr * pha.gt(0)
  124. com = torch.cat([fgr, pha], dim=-3)
  125. writer_com.write(com[0])
  126. bar.update(src.size(1))
  127. finally:
  128. # Clean up
  129. if output_composition is not None:
  130. writer_com.close()
  131. if output_alpha is not None:
  132. writer_pha.close()
  133. if output_foreground is not None:
  134. writer_fgr.close()
  135. def auto_downsample_ratio(h, w):
  136. """
  137. Automatically find a downsample ratio so that the largest side of the resolution be 512px.
  138. """
  139. return min(512 / max(h, w), 1)
  140. class Converter:
  141. def __init__(self, variant: str, checkpoint: str, device: str):
  142. self.model = MattingNetwork(variant).eval().to(device)
  143. self.model.load_state_dict(torch.load(checkpoint, map_location=device))
  144. self.model = torch.jit.script(self.model)
  145. self.model = torch.jit.freeze(self.model)
  146. self.device = device
  147. def convert(self, *args, **kwargs):
  148. convert_video(self.model, device=self.device, dtype=torch.float32, *args, **kwargs)
  149. if __name__ == '__main__':
  150. import argparse
  151. from model import MattingNetwork
  152. parser = argparse.ArgumentParser()
  153. parser.add_argument('--variant', type=str, required=True, choices=['mobilenetv3', 'resnet50'])
  154. parser.add_argument('--checkpoint', type=str, required=True)
  155. parser.add_argument('--device', type=str, required=True)
  156. parser.add_argument('--input-source', type=str, required=True)
  157. parser.add_argument('--input-resize', type=int, default=None, nargs=2)
  158. parser.add_argument('--downsample-ratio', type=float)
  159. parser.add_argument('--output-composition', type=str)
  160. parser.add_argument('--output-alpha', type=str)
  161. parser.add_argument('--output-foreground', type=str)
  162. parser.add_argument('--output-type', type=str, required=True, choices=['video', 'png_sequence'])
  163. parser.add_argument('--output-video-mbps', type=int, default=1)
  164. parser.add_argument('--seq-chunk', type=int, default=1)
  165. parser.add_argument('--num-workers', type=int, default=0)
  166. parser.add_argument('--disable-progress', action='store_true')
  167. args = parser.parse_args()
  168. converter = Converter(args.variant, args.checkpoint, args.device)
  169. converter.convert(
  170. input_source=args.input_source,
  171. input_resize=args.input_resize,
  172. downsample_ratio=args.downsample_ratio,
  173. output_type=args.output_type,
  174. output_composition=args.output_composition,
  175. output_alpha=args.output_alpha,
  176. output_foreground=args.output_foreground,
  177. output_video_mbps=args.output_video_mbps,
  178. seq_chunk=args.seq_chunk,
  179. num_workers=args.num_workers,
  180. progress=not args.disable_progress
  181. )