Remove multi processing as the performance is equal but memory consumtion better

This commit is contained in:
henryruhs 2023-06-13 17:53:32 +02:00
parent 10d62f1a59
commit e862700116
5 changed files with 7 additions and 44 deletions

View File

@ -52,8 +52,6 @@ options:
adjust output video quality adjust output video quality
--max-memory MAX_MEMORY --max-memory MAX_MEMORY
maximum amount of RAM in GB to be used maximum amount of RAM in GB to be used
--cpu-cores CPU_CORES
number of CPU cores to use
--execution-provider {cpu,...} [{cpu,...} ...] --execution-provider {cpu,...} [{cpu,...} ...]
execution provider execution provider
--execution-threads EXECUTION_THREADS --execution-threads EXECUTION_THREADS

View File

@ -15,11 +15,9 @@ import platform
import signal import signal
import shutil import shutil
import argparse import argparse
import psutil
import torch import torch
import onnxruntime import onnxruntime
import tensorflow import tensorflow
import multiprocessing
from opennsfw2 import predict_video_frames, predict_image from opennsfw2 import predict_video_frames, predict_image
import cv2 import cv2
@ -49,7 +47,6 @@ def parse_args() -> None:
parser.add_argument('--video-encoder', help='adjust output video encoder', dest='video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9']) parser.add_argument('--video-encoder', help='adjust output video encoder', dest='video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9'])
parser.add_argument('--video-quality', help='adjust output video quality', dest='video_quality', type=int, default=18) parser.add_argument('--video-quality', help='adjust output video quality', dest='video_quality', type=int, default=18)
parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', dest='max_memory', type=int, default=suggest_max_memory()) parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', dest='max_memory', type=int, default=suggest_max_memory())
parser.add_argument('--cpu-cores', help='number of CPU cores to use', dest='cpu_cores', type=int, default=suggest_cpu_cores())
parser.add_argument('--execution-provider', help='execution provider', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+') parser.add_argument('--execution-provider', help='execution provider', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
parser.add_argument('--execution-threads', help='number of threads to be use for the GPU', dest='execution_threads', type=int, default=suggest_execution_threads()) parser.add_argument('--execution-threads', help='number of threads to be use for the GPU', dest='execution_threads', type=int, default=suggest_execution_threads())
@ -67,7 +64,6 @@ def parse_args() -> None:
roop.globals.video_encoder = args.video_encoder roop.globals.video_encoder = args.video_encoder
roop.globals.video_quality = args.video_quality roop.globals.video_quality = args.video_quality
roop.globals.max_memory = args.max_memory roop.globals.max_memory = args.max_memory
roop.globals.cpu_cores = args.cpu_cores
roop.globals.execution_providers = decode_execution_providers(args.execution_provider) roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
roop.globals.execution_threads = args.execution_threads roop.globals.execution_threads = args.execution_threads
@ -91,12 +87,6 @@ def suggest_max_memory() -> int:
return 16 return 16
def suggest_cpu_cores() -> int:
if platform.system().lower() == 'darwin':
return 2
return int(max(psutil.cpu_count() / 2, 1))
def suggest_execution_providers() -> List[str]: def suggest_execution_providers() -> List[str]:
return encode_execution_providers(onnxruntime.get_available_providers()) return encode_execution_providers(onnxruntime.get_available_providers())
@ -139,22 +129,6 @@ def pre_check() -> None:
quit('ffmpeg is not installed.') quit('ffmpeg is not installed.')
def conditional_process_video(source_path: str, temp_frame_paths: List[str], process_video) -> None:
pool_amount = len(temp_frame_paths) // roop.globals.cpu_cores
if pool_amount > 2 and roop.globals.cpu_cores > 1 and roop.globals.execution_providers == ['CPUExecutionProvider']:
POOL = multiprocessing.Pool(roop.globals.cpu_cores, maxtasksperchild=1)
pools = []
for i in range(0, len(temp_frame_paths), pool_amount):
pool = POOL.apply_async(process_video, args=(source_path, temp_frame_paths[i:i + pool_amount], 'multi-processing'))
pools.append(pool)
for pool in pools:
pool.get()
POOL.close()
POOL.join()
else:
process_video(roop.globals.source_path, temp_frame_paths, 'multi-threading')
def update_status(message: str) -> None: def update_status(message: str) -> None:
value = 'Status: ' + message value = 'Status: ' + message
print(value) print(value)
@ -197,7 +171,7 @@ def start() -> None:
temp_frame_paths = get_temp_frame_paths(roop.globals.target_path) temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors): for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
update_status(f'{frame_processor} in progress...') update_status(f'{frame_processor} in progress...')
conditional_process_video(roop.globals.source_path, temp_frame_paths, frame_processor.process_video) frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
release_resources() release_resources()
if roop.globals.keep_fps: if roop.globals.keep_fps:
update_status('Detecting fps...') update_status('Detecting fps...')

View File

@ -9,7 +9,6 @@ many_faces = None
video_encoder = None video_encoder = None
video_quality = None video_quality = None
max_memory = None max_memory = None
cpu_cores = None
execution_providers = [] execution_providers = []
execution_threads = None execution_threads = None
headless = None headless = None

View File

@ -157,13 +157,9 @@ def multi_process_frame(source_img, frame_paths, progress) -> None:
thread.join() thread.join()
def process_video(source_path: str, frame_paths: list[str], mode: str) -> None: def process_video(source_path: str, frame_paths: list[str]) -> None:
progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]' progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
total = len(frame_paths) total = len(frame_paths)
with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress: with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress:
if mode == 'multi-processing': progress.set_postfix({'execution_providers': roop.globals.execution_providers, 'threads': roop.globals.execution_threads, 'memory': roop.globals.max_memory})
progress.set_postfix({'mode': mode, 'cores': roop.globals.cpu_cores, 'memory': roop.globals.max_memory}) multi_process_frame(source_path, frame_paths, progress)
process_frames(source_path, frame_paths, progress)
elif mode == 'multi-threading':
progress.set_postfix({'mode': mode, 'threads': roop.globals.execution_threads, 'memory': roop.globals.max_memory})
multi_process_frame(source_path, frame_paths, progress)

View File

@ -88,13 +88,9 @@ def process_image(source_path: str, target_path: str, output_path: str) -> None:
cv2.imwrite(output_path, result) cv2.imwrite(output_path, result)
def process_video(source_path: str, temp_frame_paths: List[str], mode: str) -> None: def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]' progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
total = len(temp_frame_paths) total = len(temp_frame_paths)
with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress: with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress:
if mode == 'multi-processing': progress.set_postfix({'execution_providers': roop.globals.execution_providers, 'threads': roop.globals.execution_threads, 'memory': roop.globals.max_memory})
progress.set_postfix({'mode': mode, 'cores': roop.globals.cpu_cores, 'memory': roop.globals.max_memory}) multi_process_frame(source_path, temp_frame_paths, progress)
process_frames(source_path, temp_frame_paths, progress)
elif mode == 'multi-threading':
progress.set_postfix({'mode': mode, 'threads': roop.globals.execution_threads, 'memory': roop.globals.max_memory})
multi_process_frame(source_path, temp_frame_paths, progress)