Optimize interpolation progress display, use rolling avg. for fps

This commit is contained in:
N00MKRAD
2024-09-26 18:51:07 +02:00
parent f40896c12b
commit 8824a17f32
4 changed files with 112 additions and 46 deletions

View File

@@ -517,6 +517,7 @@
<Compile Include="Ui\QuickSettingsTab.cs" />
<Compile Include="Utilities\ColorDataUtils.cs" />
<Compile Include="Utilities\NcnnUtils.cs" />
<Compile Include="Utilities\RollingAverage.cs" />
<EmbeddedResource Include="Forms\Main\Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Designer</SubType>

View File

@@ -104,7 +104,7 @@ namespace Flowframes.Os
if (!Interpolate.currentSettings.ai.Piped)
InterpolationProgress.UpdateInterpProgress(interpFramesCount, InterpolationProgress.targetFrames);
string logStr = $"Done running {aiName} - Interpolation took {FormatUtils.Time(processTime.Elapsed)}. Peak Output FPS: {InterpolationProgress.peakFpsOut.ToString("0.00")}";
string logStr = $"Done running {aiName} - Interpolation took {FormatUtils.Time(processTime.Elapsed)}. Output FPS: {InterpolationProgress.LastFps.ToString("0.0")}";
if (Interpolate.currentlyUsingAutoEnc && AutoEncode.HasWorkToDo())
{
@@ -113,7 +113,7 @@ namespace Flowframes.Os
}
if (Interpolate.currentSettings.outSettings.Format != Enums.Output.Format.Realtime)
Logger.Log(logStr);
Logger.Log(logStr, replaceLastLine: Logger.LastUiLine.Contains("FPS"));
processTime.Stop();

View File

@@ -33,7 +33,9 @@ namespace Flowframes.Ui
progCheckRunning = true;
deletedFramesCount = 0;
lastFrame = 0;
peakFpsOut = 0f;
LastFps = 0f;
_framesAtTime = null;
_fpsRollAvg.Reset();
Program.mainForm.SetProgress(0);
}
@@ -125,9 +127,9 @@ namespace Flowframes.Ui
}
public static int interpolatedInputFramesCount;
public static float peakFpsOut;
public static int previewUpdateRateMs = 200;
public static float LastFps;
private static Tuple<int, DateTime> _framesAtTime = null;
private static Utilities.RollingAverage<float> _fpsRollAvg = new Utilities.RollingAverage<float>(10);
public static void UpdateInterpProgress(int frames, int target, string latestFramePath = "")
{
@@ -136,43 +138,48 @@ namespace Flowframes.Ui
//ResumeUtils.Save();
target = (target / Interpolate.InterpProgressMultiplier).RoundToInt();
frames = frames.Clamp(0, target);
int percent = (int)Math.Round(((float)frames / target) * 100f);
if (_framesAtTime == null)
{
_framesAtTime = new Tuple<int, DateTime>(frames, DateTime.Now);
}
if (frames > _framesAtTime.Item1 && frames > 0)
{
float fpsCurrent = (frames - _framesAtTime.Item1) / (float)(DateTime.Now - _framesAtTime.Item2).TotalSeconds;
_fpsRollAvg.AddDataPoint(fpsCurrent);
_framesAtTime = new Tuple<int, DateTime>(frames, DateTime.Now);
}
int percent = (((float)frames / target) * 100f).RoundToInt();
Program.mainForm.SetProgress(percent);
float generousTime = ((AiProcess.processTime.ElapsedMilliseconds - AiProcess.lastStartupTimeMs) / 1000f);
float fps = ((float)frames / generousTime).Clamp(0, 9999);
string fpsIn = (fps / currentFactor).ToString("0.00");
string fpsOut = fps.ToString("0.00");
float fps = _fpsRollAvg.CurrentSize > 2 ? (float)_fpsRollAvg.Average : 0f;
string fpsIn = (fps / currentFactor).ToString("0.0");
string fpsOut = fps.ToString("0.0");
LastFps = fps;
if (fps > peakFpsOut)
peakFpsOut = fps;
float eta = fps == 0f ? 0f : (target - frames) * (1f / fps); // ETA = Remaining frames * seconds per frame. Set to 0 if FPS is 0 to avoid div. by zero
string etaStr = eta > 3f ? $" - ETA: {FormatUtils.Time(TimeSpan.FromSeconds(eta), false)}" : "";
string timeStr = AiProcess.processTime.ElapsedMilliseconds > 0 ? $" - Time: {FormatUtils.Time(AiProcess.processTime.Elapsed)}" : "";
float secondsPerFrame = generousTime / (float)frames;
int framesLeft = target - frames;
float eta = framesLeft * secondsPerFrame;
string etaStr = FormatUtils.Time(new TimeSpan(0, 0, eta.RoundToInt()), false);
bool replaceLine = Logger.LastUiLine.MatchesWildcard("Interpolated*/* Frames *");
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}";
string logStr = $"Interpolated {frames}/{target} Frames ({percent}%) - Speed: {fpsIn} FPS In / {fpsOut} FPS Out{timeStr}{etaStr}";
if (AutoEncode.busy) logStr += " - Encoding...";
Logger.Log(logStr, false, replaceLine);
try
{
if (latestFramePath.IsNotEmpty() && frames > currentFactor)
{
if (bigPreviewForm == null && (preview == null || !preview.Visible) /* ||Program.mainForm.WindowState != FormWindowState.Minimized */ /* || !Program.mainForm.IsInFocus()*/) return; // Skip if the preview is not visible or the form is not in focus
if (timeSinceLastPreviewUpdate.IsRunning && timeSinceLastPreviewUpdate.ElapsedMilliseconds < previewUpdateRateMs) return;
Image img = IoUtils.GetImage(latestFramePath, false, false);
SetPreviewImg(img);
}
}
catch (Exception e)
{
//Logger.Log("Error updating preview: " + e.Message, true);
}
// try
// {
// if (latestFramePath.IsNotEmpty() && frames > currentFactor)
// {
// if (bigPreviewForm == null && (preview == null || !preview.Visible) /* ||Program.mainForm.WindowState != FormWindowState.Minimized */ /* || !Program.mainForm.IsInFocus()*/) return; // Skip if the preview is not visible or the form is not in focus
// }
// }
// catch (Exception e)
// {
// //Logger.Log("Error updating preview: " + e.Message, true);
// }
}
public static async Task DeleteInterpolatedInputFrames()
@@ -193,16 +200,5 @@ namespace Flowframes.Ui
}
public static Stopwatch timeSinceLastPreviewUpdate = new Stopwatch();
public static void SetPreviewImg(Image img)
{
if (img == null || preview == null)
return;
timeSinceLastPreviewUpdate.Restart();
preview.Image = img;
bigPreviewForm?.SetImage(img);
}
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Flowframes.Utilities
{
public class RollingAverage<T> where T : struct
{
public int CurrentSize { get => _values.Count; }
public double Average { get => GetAverage(); }
private Queue<T> _values;
public Queue<T> Queue { get => _values; }
private int _size;
public RollingAverage(int size)
{
_values = new Queue<T>(size);
_size = size;
}
public void AddDataPoint(T dataPoint)
{
if (_values.Count >= _size)
{
_values.Dequeue();
}
_values.Enqueue(dataPoint);
}
public double GetAverage()
{
// Convert the values to double before averaging, this is necessary because Average() does not work directly on generic types
if(_values == null || _values.Count == 0)
{
return 0;
}
return _values.Select(val => Convert.ToDouble(val)).Average();
}
public double GetAverage(int lastXSamples)
{
if (lastXSamples <= 0)
{
lastXSamples = 1;
}
else if (lastXSamples > _values.Count)
{
lastXSamples = _values.Count;
}
// Take the last X samples and calculate the average
return _values.Skip(Math.Max(0, _values.Count - lastXSamples)).Select(val => Convert.ToDouble(val)).Average();
}
public double GetAverage(float percentile)
{
int lastXSamples = (int)Math.Ceiling(_size * percentile);
return GetAverage(lastXSamples);
}
public void Reset()
{
_values.Clear();
}
}
}