From bc74f7e0efdab6f63dc2088a083e11443b7b1ef4 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 11 Feb 2021 09:05:10 +0100 Subject: [PATCH 1/5] implement faster progress updating for rife cuda --- Code/Main/InterpolateUtils.cs | 41 ++++++++++++++++++++++++++--------- Code/OS/AiProcess.cs | 22 ++++++++++++------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 37caa73..37a1f17 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -59,11 +59,13 @@ namespace Flowframes.Main return dotStr + "png"; } + public static int lastFrame; public static int targetFrames; public static string currentOutdir; public static float currentFactor; public static bool progressPaused = false; public static bool progCheckRunning = false; + public static async void GetProgressByFrameAmount(string outdir, int target) { progCheckRunning = true; @@ -79,23 +81,42 @@ namespace Flowframes.Main if (firstProgUpd && Program.mainForm.IsInFocus()) Program.mainForm.SetTab("preview"); firstProgUpd = false; - string[] frames = IOUtils.GetFilesSorted(currentOutdir, $"*.{GetOutExt()}"); - if (frames.Length > 1) - UpdateInterpProgress(frames.Length, targetFrames, frames[frames.Length - 1]); - if (frames.Length >= targetFrames) + if (lastFrame > 1) + UpdateInterpProgress(lastFrame, targetFrames, currentOutdir + lastFrame.ToString("00000000") + $".{GetOutExt()}"); + if (lastFrame >= targetFrames) break; - await Task.Delay(GetProgressWaitTime(frames.Length)); - } - else - { - await Task.Delay(200); } + await Task.Delay(100); } progCheckRunning = false; if (i.canceled) Program.mainForm.SetProgress(0); } + public static void UpdateLastFrameFromInterpOutput(string output) + { + Logger.Log(output); + switch (AiProcess.currentAiName) + { + case "RIFE-CUDA": + { + Regex frameRegex = new Regex($@"(?<=> )\d*(?=.{GetOutExt()})"); + if (!frameRegex.IsMatch(output)) return; + lastFrame = int.Parse(frameRegex.Match(output).Value); + break; + } + case "RIFE-NCNN": + { + break; + } + case "DAIN": + { + break; + } + default: return; + } + } + public static int interpolatedInputFramesCount; public static void UpdateInterpProgress(int frames, int target, string latestFramePath = "") @@ -119,7 +140,7 @@ namespace Flowframes.Main bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average Speed: "); - string logStr = $"Interpolated {frames}/{target} frames ({percent}%) - Average Speed: {fpsIn} FPS In / {fpsOut} FPS Out - "; + string logStr = $"Interpolated {frames}/{target} frames ({percent}%) - Average speed: {fpsIn} FPS in / {fpsOut} FPS out - "; logStr += $"Time: {FormatUtils.Time(AiProcess.processTime.Elapsed)} - ETA: {etaStr}"; if (AutoEncode.busy) logStr += " - Encoding..."; Logger.Log(logStr, false, replaceLine); diff --git a/Code/OS/AiProcess.cs b/Code/OS/AiProcess.cs index 7a3ba04..923e408 100644 --- a/Code/OS/AiProcess.cs +++ b/Code/OS/AiProcess.cs @@ -22,6 +22,7 @@ namespace Flowframes public static bool hasShownError; public static Process currentAiProcess; + public static string currentAiName; public static Stopwatch processTime = new Stopwatch(); public static Stopwatch processTimeMulti = new Stopwatch(); @@ -52,12 +53,12 @@ namespace Flowframes InterpolateUtils.GetProgressByFrameAmount(interpPath, target); } - static async Task AiFinished (string aiName) + static async Task AiFinished() { if (Interpolate.canceled) return; Program.mainForm.SetProgress(100); InterpolateUtils.UpdateInterpProgress(IOUtils.GetAmountOfFiles(Interpolate.current.interpFolder, false, "*.png"), InterpolateUtils.targetFrames); - string logStr = $"Done running {aiName} - Interpolation took {FormatUtils.Time(processTime.Elapsed)}"; + string logStr = $"Done running {currentAiName} - Interpolation took {FormatUtils.Time(processTime.Elapsed)}"; if (Interpolate.currentlyUsingAutoEnc && AutoEncode.HasWorkToDo()) logStr += " - Waiting for encoding to finish..."; Logger.Log(logStr); @@ -92,6 +93,8 @@ namespace Flowframes if(Interpolate.currentlyUsingAutoEnc) // Ensure AutoEnc is not paused AutoEncode.paused = false; + currentAiName = "RIFE-CUDA"; + string rifeDir = Path.Combine(Paths.GetPkgPath(), Path.GetFileNameWithoutExtension(Packages.rifeCuda.fileName)); string script = "rife.py"; @@ -110,12 +113,13 @@ namespace Flowframes await RunRifeCudaProcess(framesPath + Paths.alphaSuffix, Paths.interpDir + Paths.alphaSuffix, script, interpFactor, mdl); } - await AiFinished("RIFE"); + await AiFinished(); } public static async Task RunRifeCudaProcess (string inPath, string outDir, string script, float interpFactor, string mdl) { bool parallel = false; + bool unbuffered = true; string uhdStr = await InterpolateUtils.UseUHD() ? "--UHD" : ""; string outPath = Path.Combine(inPath.GetParentDir(), outDir); string args = $" --input {inPath.Wrap()} --output {outDir} --model {mdl} --exp {(int)Math.Log(interpFactor, 2)} "; @@ -126,7 +130,7 @@ namespace Flowframes AiStarted(rifePy, 3500); SetProgressCheck(Path.Combine(Interpolate.current.tempFolder, outDir), interpFactor); rifePy.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeCuda).Wrap()} & " + - $"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Python.GetPyCmd()} {script} {args}"; + $"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Python.GetPyCmd()} " + (unbuffered ? "-u" : "") + $" {script} {args}"; Logger.Log($"Running RIFE {(await InterpolateUtils.UseUHD() ? "(UHD Mode)" : "")} ({script})...".TrimWhitespaces(), false); Logger.Log("cmd.exe " + rifePy.StartInfo.Arguments, true); @@ -150,6 +154,8 @@ namespace Flowframes processTimeMulti.Restart(); Logger.Log($"Running RIFE{(await InterpolateUtils.UseUHD() ? " (UHD Mode)" : "")}...", false); + currentAiName = "RIFE-NCNN"; + await RunRifeNcnnMulti(framesPath, outPath, factor, mdl); if (!Interpolate.canceled && Interpolate.current.alpha) @@ -158,8 +164,6 @@ namespace Flowframes Logger.Log("Interpolating alpha channel..."); await RunRifeNcnnMulti(framesPath + Paths.alphaSuffix, outPath + Paths.alphaSuffix, factor, mdl); } - - await AiFinished("RIFE"); } static async Task RunRifeNcnnMulti(string framesPath, string outPath, int factor, string mdl) @@ -234,6 +238,8 @@ namespace Flowframes if (Interpolate.currentlyUsingAutoEnc) // Ensure AutoEnc is not paused AutoEncode.paused = false; + currentAiName = "DAIN"; + await RunDainNcnnProcess(framesPath, outPath, factor, mdl, tilesize); if (!Interpolate.canceled && Interpolate.current.alpha) @@ -242,8 +248,6 @@ namespace Flowframes Logger.Log("Interpolating alpha channel..."); await RunDainNcnnProcess(framesPath + Paths.alphaSuffix, outPath + Paths.alphaSuffix, factor, mdl, tilesize); } - - await AiFinished("DAIN"); } public static async Task RunDainNcnnProcess (string framesPath, string outPath, float factor, string mdl, int tilesize) @@ -338,6 +342,8 @@ namespace Flowframes if (hasShownError) Interpolate.Cancel(); + + InterpolateUtils.UpdateLastFrameFromInterpOutput(line); } static string GetNcnnPattern () From 68ffe9e682b144c2a649133b061ec62b8a2a2f39 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 11 Feb 2021 11:36:12 +0100 Subject: [PATCH 2/5] set last frame to 0 on restart --- Code/Main/InterpolateUtils.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 37a1f17..24fc2c5 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -74,6 +74,7 @@ namespace Flowframes.Main Logger.Log($"Starting GetProgressByFrameAmount() loop for outdir '{currentOutdir}', target is {target} frames", true); bool firstProgUpd = true; Program.mainForm.SetProgress(0); + lastFrame = 0; while (Program.busy) { if (!progressPaused && AiProcess.processTime.IsRunning && Directory.Exists(currentOutdir)) @@ -95,7 +96,6 @@ namespace Flowframes.Main public static void UpdateLastFrameFromInterpOutput(string output) { - Logger.Log(output); switch (AiProcess.currentAiName) { case "RIFE-CUDA": @@ -138,7 +138,7 @@ namespace Flowframes.Main float eta = framesLeft * secondsPerFrame; string etaStr = FormatUtils.Time(new TimeSpan(0, 0, eta.RoundToInt()), false); - bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average Speed: "); + bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average speed: "); string logStr = $"Interpolated {frames}/{target} frames ({percent}%) - Average speed: {fpsIn} FPS in / {fpsOut} FPS out - "; logStr += $"Time: {FormatUtils.Time(AiProcess.processTime.Elapsed)} - ETA: {etaStr}"; @@ -157,7 +157,7 @@ namespace Flowframes.Main catch { } } - public static async Task DeleteInterpolatedInputFrames () + public static async Task DeleteInterpolatedInputFrames() { interpolatedInputFramesCount = 0; string[] inputFrames = IOUtils.GetFilesSorted(i.current.framesFolder); @@ -172,7 +172,7 @@ namespace Flowframes.Main } } - public static void SetPreviewImg (Image img) + public static void SetPreviewImg(Image img) { if (img == null) return; From 62b7650e692bedf3aa25ba63a46cdcdc5124e948 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 11 Feb 2021 12:33:09 +0100 Subject: [PATCH 3/5] better implementation that works for all three AIs --- Code/Main/InterpolateUtils.cs | 23 ++++------------------- Code/OS/AiProcess.cs | 4 ++-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 24fc2c5..958ab90 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -96,25 +96,10 @@ namespace Flowframes.Main public static void UpdateLastFrameFromInterpOutput(string output) { - switch (AiProcess.currentAiName) - { - case "RIFE-CUDA": - { - Regex frameRegex = new Regex($@"(?<=> )\d*(?=.{GetOutExt()})"); - if (!frameRegex.IsMatch(output)) return; - lastFrame = int.Parse(frameRegex.Match(output).Value); - break; - } - case "RIFE-NCNN": - { - break; - } - case "DAIN": - { - break; - } - default: return; - } + string dainStr = AiProcess.currentAiName == "DAIN" ? " done" : ""; + Regex frameRegex = new Regex($@"(?<=.)\d*(?=.{GetOutExt()}{dainStr})"); + if (!frameRegex.IsMatch(output)) return; + lastFrame = Math.Max(int.Parse(frameRegex.Match(output).Value), lastFrame); } public static int interpolatedInputFramesCount; diff --git a/Code/OS/AiProcess.cs b/Code/OS/AiProcess.cs index 923e408..36d0b3e 100644 --- a/Code/OS/AiProcess.cs +++ b/Code/OS/AiProcess.cs @@ -93,7 +93,7 @@ namespace Flowframes if(Interpolate.currentlyUsingAutoEnc) // Ensure AutoEnc is not paused AutoEncode.paused = false; - currentAiName = "RIFE-CUDA"; + currentAiName = "RIFE"; string rifeDir = Path.Combine(Paths.GetPkgPath(), Path.GetFileNameWithoutExtension(Packages.rifeCuda.fileName)); string script = "rife.py"; @@ -154,7 +154,7 @@ namespace Flowframes processTimeMulti.Restart(); Logger.Log($"Running RIFE{(await InterpolateUtils.UseUHD() ? " (UHD Mode)" : "")}...", false); - currentAiName = "RIFE-NCNN"; + currentAiName = "RIFE"; await RunRifeNcnnMulti(framesPath, outPath, factor, mdl); From a417ae5a015a5f000560444e95c5f4d0603ccfa7 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 11 Feb 2021 12:44:42 +0100 Subject: [PATCH 4/5] title case --- Code/Main/InterpolateUtils.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 958ab90..53cc1a2 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -123,9 +123,9 @@ namespace Flowframes.Main float eta = framesLeft * secondsPerFrame; string etaStr = FormatUtils.Time(new TimeSpan(0, 0, eta.RoundToInt()), false); - bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average speed: "); + bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average Speed: "); - string logStr = $"Interpolated {frames}/{target} frames ({percent}%) - Average speed: {fpsIn} FPS in / {fpsOut} FPS out - "; + string logStr = $"Interpolated {frames}/{target} Frames ({percent}%) - Average Speed: {fpsIn} FPS In / {fpsOut} FPS Out - "; logStr += $"Time: {FormatUtils.Time(AiProcess.processTime.Elapsed)} - ETA: {etaStr}"; if (AutoEncode.busy) logStr += " - Encoding..."; Logger.Log(logStr, false, replaceLine); From 5e6ef033451133029965a53d81d710189166c388 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 11 Feb 2021 12:58:07 +0100 Subject: [PATCH 5/5] fix preview not updating --- Code/Main/InterpolateUtils.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 53cc1a2..d8d936a 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -82,8 +82,9 @@ namespace Flowframes.Main if (firstProgUpd && Program.mainForm.IsInFocus()) Program.mainForm.SetTab("preview"); firstProgUpd = false; + string lastFramePath = currentOutdir + "\\" + lastFrame.ToString("00000000") + $".{GetOutExt()}"; if (lastFrame > 1) - UpdateInterpProgress(lastFrame, targetFrames, currentOutdir + lastFrame.ToString("00000000") + $".{GetOutExt()}"); + UpdateInterpProgress(lastFrame, targetFrames, lastFramePath); if (lastFrame >= targetFrames) break; }