Skip to content

Commit

Permalink
pause working
Browse files Browse the repository at this point in the history
  • Loading branch information
TNTwise committed Sep 15, 2024
1 parent 69450f6 commit 418a3f8
Show file tree
Hide file tree
Showing 9 changed files with 646 additions and 521 deletions.
5 changes: 4 additions & 1 deletion REAL-Video-Enhancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ def onRenderCompletion(self):
pass # pass just incase internet error caused a skip
# reset image preview
self.previewLabel.clear()
self.startRenderButton.setEnabled(True)
self.startRenderButton.setVisible(True)
self.pauseRenderButton.setVisible(False)
self.startRenderButton.clicked.disconnect()
self.startRenderButton.clicked.connect(self.startRender)
self.enableProcessPage()

def disableProcessPage(self):
Expand Down
7 changes: 7 additions & 0 deletions backend/rve-backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
benchmark=self.args.benchmark,
encoder=self.args.custom_encoder,
# misc settingss
pausedFile=self.args.pausedFile,
sceneDetectMethod=self.args.sceneDetectMethod,
sceneDetectSensitivity=self.args.sceneDetectSensitivity,
sharedMemoryID=self.args.shared_memory_id,
Expand Down Expand Up @@ -197,6 +198,12 @@ def handleArguments(self) -> argparse.ArgumentParser:
help="list out available backends",
action="store_true",
)
parser.add_argument(
"--pausedFile",
help="File to store paused state (True means paused, false means stopped)",
type=str,
default=None,
)
return parser.parse_args()

def fullModelPathandName(self):
Expand Down
23 changes: 18 additions & 5 deletions backend/src/InterpolateNCNN.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rife_ncnn_vulkan_python import Rife

from time import sleep

class InterpolateRIFENCNN:
def __init__(
Expand All @@ -13,17 +13,30 @@ def __init__(
self.interpolateModelPath = interpolateModelPath
self.width = width
self.height = height
self.gpuid = gpuid
self.threads = threads
self._load()

def _load(self):
self.render = Rife(
gpuid=gpuid,
num_threads=threads,
gpuid=self.gpuid,
num_threads=self.threads,
model=self.interpolateModelPath,
uhd_mode=False,
channels=3,
height=height,
width=width,
height=self.height,
width=self.width,
)

def hotUnload(self):
self.render = None

def hotReload(self):
self._load()

def process(self, img0, img1, timestep) -> bytes:
while self.render is None:
sleep(1)
return self.render.process_bytes(img0, img1, timestep)

def uncacheFrame(self, n):
Expand Down
24 changes: 12 additions & 12 deletions backend/src/InterpolateTorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
check_bfloat16_support,
log,
)

from time import sleep
torch.set_float32_matmul_precision("medium")
torch.set_grad_enabled(False)
logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -358,23 +358,23 @@ def handlePrecision(self, precision):
return torch.float16

def hotUnload(self):
try:
del self.flownet
del self.encode
del self.tenFlow_div
del self.backwarp_tenGrid
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_max_memory_allocated()
torch.cuda.reset_max_memory_cached()
except:
pass
self.flownet = None
self.encode
self.tenFlow_div
self.backwarp_tenGrid
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_max_memory_allocated()
torch.cuda.reset_max_memory_cached()

@torch.inference_mode()
def hotReload(self):
self._load()

@torch.inference_mode()
def process(self, img0, img1, timestep):
while self.flownet is None:
sleep(1)
with torch.cuda.stream(self.stream):
timestep = self.timestepDict[timestep]
if not self.rife46:
Expand Down
11 changes: 10 additions & 1 deletion backend/src/RenderVideo.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ def __init__(
overwrite: bool = False,
crf: str = "18",
# misc
pausedFile = None,
sceneDetectMethod: str = "pyscenedetect",
sceneDetectSensitivity: float = 3.0,
sharedMemoryID: str = None,
trt_optimization_level: int = 3,
):
if pausedFile == None:
pausedFile = os.path.basename(inputFile) + '_paused_state.txt'
self.inputFile = inputFile
self.pausedFile = os.path.basename(inputFile) + '_paused_state.txt'
self.pausedFile = pausedFile
with open(self.pausedFile, 'w') as f:
f.write("False")
self.backend = backend
Expand Down Expand Up @@ -148,7 +151,9 @@ def inputSTDINThread(self):
if activate:
if self.isPaused:
self.hotUnload()
print("\nRender Paused")
else:
print("\nResuming Render")
self.hotReload()
self.prevState = self.isPaused
sleep(1)
Expand Down Expand Up @@ -285,6 +290,8 @@ def setupUpscale(self):
)
self.setupRender = self.returnFrame
self.upscale = upscaleNCNN.Upscale
self.hotUnload = upscaleNCNN.hotUnload
self.hotReload = upscaleNCNN.hotReload
if self.backend == "directml":
upscaleONNX = UpscaleONNX(
modelPath=self.upscaleModel,
Expand Down Expand Up @@ -318,6 +325,8 @@ def setupInterpolate(self):
self.setupRender = self.returnFrame
self.undoSetup = interpolateRifeNCNN.uncacheFrame
self.interpolate = interpolateRifeNCNN.process
self.hotReload = interpolateRifeNCNN.hotReload
self.hotUnload = interpolateRifeNCNN.hotUnload

if self.backend == "pytorch" or self.backend == "tensorrt":
interpolateRifePytorch = InterpolateRifeTorch(
Expand Down
28 changes: 21 additions & 7 deletions backend/src/UpscaleNCNN.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import os
from time import sleep

from upscale_ncnn_py import UPSCALE

Expand Down Expand Up @@ -67,18 +68,31 @@ def __init__(
):
# only import if necessary

self.model = UPSCALE(
gpuid=gpuid,
model_str=modelPath,
num_threads=num_threads,
scale=scale,
tilesize=tilesize,
)
self.gpuid = gpuid
self.modelPath = modelPath
self.scale = scale
self.tilesize = tilesize
self.width = width
self.height = height
self.scale = scale
self.threads = num_threads
self._load()
def _load(self):
self.model = UPSCALE(
gpuid=self.gpuid,
model_str=self.modelPath,
num_threads=self.threads,
scale=self.scale,
tilesize=self.tilesize,
)
def hotUnload(self):
self.model = None

def hotReload(self):
self._load()
def Upscale(self, imageChunk):
while self.model is None:
sleep(1)
output = self.model.process_bytes(imageChunk, self.width, self.height, 3)
return np.ascontiguousarray(
np.frombuffer(
Expand Down
5 changes: 4 additions & 1 deletion backend/src/UpscaleTorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gc
import torch as torch
import torch.nn.functional as F

from time import sleep

from src.Util import (
currentDirectory,
Expand Down Expand Up @@ -179,6 +179,7 @@ def handlePrecision(self, precision):
return torch.float16

def hotUnload(self):

self.model = None
gc.collect()
torch.cuda.empty_cache()
Expand Down Expand Up @@ -225,6 +226,8 @@ def renderImage(self, image: torch.Tensor) -> torch.Tensor:

@torch.inference_mode()
def renderToNPArray(self, image: torch.Tensor) -> torch.Tensor:
while self.model is None:
sleep(0)
if self.tilesize == 0:
output = self.renderImage(image)
else:
Expand Down
26 changes: 26 additions & 0 deletions src/ui/ProcessTab.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def onTilingSwitch(self):

def QConnect(self):
# connect file select buttons

self.parent.inputFileSelectButton.clicked.connect(self.parent.openInputFile)
self.parent.inputFileText.textChanged.connect(self.parent.openFileFromYoutubeLink)
self.parent.outputFileSelectButton.clicked.connect(self.parent.openOutputFolder)
Expand All @@ -112,6 +113,9 @@ def QConnect(self):
self.parent.modelComboBox.currentIndexChanged.connect(
self.parent.updateVideoGUIDetails
)
#connect up pausing
self.parent.pauseRenderButton.setVisible(False)
self.parent.pauseRenderButton.clicked.connect(self.pauseRender)

def killRenderProcess(self):
try: # kills render process if necessary
Expand Down Expand Up @@ -190,6 +194,14 @@ def run(
self.outputVideoWidth = videoWidth * self.upscaleTimes
self.outputVideoHeight = videoHeight * self.upscaleTimes

# set up pausing
self.pausedFile = os.path.join(currentDirectory(), os.path.basename(inputFile)+ "_pausedState.txt")
self.parent.pauseRenderButton.setVisible(True) # switch to pause button on render
self.parent.startRenderButton.setVisible(False)
self.parent.startRenderButton.clicked.disconnect()
self.parent.startRenderButton.clicked.connect(self.resumeRender)


# get most recent settings
settings = Settings()
settings.readSettings()
Expand All @@ -214,6 +226,18 @@ def run(
writeThread.start()
self.startGUIUpdate()

def pauseRender(self):
with open(self.pausedFile,'w') as f:
f.write("True")
self.parent.pauseRenderButton.setVisible(False)
self.parent.startRenderButton.setVisible(True)
self.parent.startRenderButton.setEnabled(True)
def resumeRender(self):
with open(self.pausedFile,'w') as f:
f.write("False")
self.parent.pauseRenderButton.setVisible(True)
self.parent.pauseRenderButton.setEnabled(True)
self.parent.startRenderButton.setVisible(False)
def startGUIUpdate(self):
self.workerThread = UpdateGUIThread(
parent=self,
Expand Down Expand Up @@ -254,6 +278,8 @@ def renderToPipeThread(self, method: str, backend: str, interpolateTimes: int):
f"-c:v {self.settings['encoder']} -crf {qualityToCRF[self.settings['video_quality']]}",
"--tensorrt_opt_profile",
f"{self.settings['tensorrt_optimization_level']}",
"--pausedFile",
f"{self.pausedFile}"
]
if method == "Upscale":
command += [
Expand Down
Loading

0 comments on commit 418a3f8

Please sign in to comment.