Merge pull request #19 from richardletshacks/main

faster progress update by reading stdout, completely avoiding I/O load
This commit is contained in:
N00MKRAD
2021-02-11 19:24:36 +01:00
committed by GitHub
2 changed files with 33 additions and 20 deletions

View File

@@ -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;
@@ -72,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))
@@ -79,23 +82,27 @@ 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)
string lastFramePath = currentOutdir + "\\" + lastFrame.ToString("00000000") + $".{GetOutExt()}";
if (lastFrame > 1)
UpdateInterpProgress(lastFrame, targetFrames, lastFramePath);
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)
{
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;
public static void UpdateInterpProgress(int frames, int target, string latestFramePath = "")
@@ -119,7 +126,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);
@@ -136,7 +143,7 @@ namespace Flowframes.Main
catch { }
}
public static async Task DeleteInterpolatedInputFrames ()
public static async Task DeleteInterpolatedInputFrames()
{
interpolatedInputFramesCount = 0;
string[] inputFrames = IOUtils.GetFilesSorted(i.current.framesFolder);
@@ -151,7 +158,7 @@ namespace Flowframes.Main
}
}
public static void SetPreviewImg (Image img)
public static void SetPreviewImg(Image img)
{
if (img == null)
return;

View File

@@ -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";
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";
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 ()