mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-16 16:37:48 +01:00
Added FLAVR code, improved frame padding for FLAVR
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Flowframes.Data
|
||||
{
|
||||
public static AI rifeCuda = new AI("RIFE_CUDA", "RIFE", "CUDA/Pytorch Implementation of RIFE (Nvidia Only!)", "rife-cuda", true);
|
||||
public static AI rifeNcnn = new AI("RIFE_NCNN", "RIFE (NCNN)", "Vulkan/NCNN Implementation of RIFE", "rife-ncnn", false);
|
||||
public static AI flavrCuda = new AI("FLAVR_CUDA", "RIFE", "Experimental Pytorch Implementation of FLAVR (Nvidia Only!)", "flavr-cuda", true);
|
||||
public static AI flavrCuda = new AI("FLAVR_CUDA", "FLAVR", "Experimental Pytorch Implementation of FLAVR (Nvidia Only!)", "flavr-cuda", true);
|
||||
public static AI dainNcnn = new AI("DAIN_NCNN", "DAIN (NCNN)", "Vulkan/NCNN Implementation of DAIN", "dain-ncnn", true);
|
||||
|
||||
public static List<AI> networks = new List<AI>();
|
||||
@@ -18,7 +18,7 @@ namespace Flowframes.Data
|
||||
networks.Clear();
|
||||
networks.Add(rifeCuda);
|
||||
networks.Add(rifeNcnn);
|
||||
// networks.Add(flavrCuda);
|
||||
networks.Add(flavrCuda);
|
||||
networks.Add(dainNcnn);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,8 +29,17 @@ namespace Flowframes.Main
|
||||
public static void UpdateChunkAndBufferSizes ()
|
||||
{
|
||||
chunkSize = GetChunkSize((IOUtils.GetAmountOfFiles(Interpolate.current.framesFolder, false, "*.png") * Interpolate.current.interpFactor).RoundToInt());
|
||||
bool isNcnn = Interpolate.current.ai.aiName.ToUpper().Contains("NCNN");
|
||||
safetyBufferFrames = isNcnn ? Config.GetInt("autoEncSafeBufferNcnn", 90) : Config.GetInt("autoEncSafeBufferCuda", 30); // Use bigger safety buffer for NCNN
|
||||
|
||||
safetyBufferFrames = 90;
|
||||
|
||||
if (Interpolate.current.ai.aiName.ToUpper().Contains("NCNN"))
|
||||
safetyBufferFrames = Config.GetInt("autoEncSafeBufferNcnn", 160);
|
||||
|
||||
if (Interpolate.current.ai.aiName == Networks.rifeCuda.aiName)
|
||||
safetyBufferFrames = Config.GetInt("autoEncSafeBufferRifeCuda", 90);
|
||||
|
||||
if (Interpolate.current.ai.aiName == Networks.flavrCuda.aiName)
|
||||
safetyBufferFrames = Config.GetInt("autoEncSafeBufferFlavrCuda", 90);
|
||||
}
|
||||
|
||||
public static async Task MainLoop(string interpFramesPath)
|
||||
|
||||
@@ -17,11 +17,17 @@ namespace Flowframes
|
||||
{
|
||||
class FfmpegCommands
|
||||
{
|
||||
public static string divisionFilter = "pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2:color=black@0";
|
||||
//public static string padFilter = "pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2:color=black@0";
|
||||
public static string compr = "-compression_level 3";
|
||||
public static string mpDecDef = "\"mpdecimate\"";
|
||||
public static string mpDecAggr = "\"mpdecimate=hi=64*32:lo=64*32:frac=0.1\"";
|
||||
|
||||
public static string GetPadFilter ()
|
||||
{
|
||||
int divisibleBy = (Interpolate.current.ai.aiName == Networks.flavrCuda.aiName) ? 8 : 2; // FLAVR input needs to be divisible by 8
|
||||
return $"pad=width=ceil(iw/{divisibleBy})*{divisibleBy}:height=ceil(ih/{divisibleBy})*{divisibleBy}:color=black@0";
|
||||
}
|
||||
|
||||
public static async Task ConcatVideos(string concatFile, string outPath, int looptimes = -1)
|
||||
{
|
||||
Logger.Log($"Merging videos...", false, Logger.GetLastLine().Contains("frame"));
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Flowframes.Media
|
||||
public static async Task Encode(string inputFile, string vcodec, string acodec, int crf, int audioKbps = 0, bool delSrc = false)
|
||||
{
|
||||
string outPath = Path.ChangeExtension(inputFile, null) + "-convert.mp4";
|
||||
string args = $" -i {inputFile.Wrap()} -c:v {vcodec} -crf {crf} -pix_fmt yuv420p -c:a {acodec} -b:a {audioKbps}k -vf {divisionFilter} {outPath.Wrap()}";
|
||||
string args = $" -i {inputFile.Wrap()} -c:v {vcodec} -crf {crf} -pix_fmt yuv420p -c:a {acodec} -b:a {audioKbps}k -vf {GetPadFilter()} {outPath.Wrap()}";
|
||||
if (string.IsNullOrWhiteSpace(acodec))
|
||||
args = args.Replace("-c:a", "-an");
|
||||
if (audioKbps < 0)
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Flowframes.Media
|
||||
IOUtils.CreateDir(framesDir);
|
||||
string timecodeStr = /* timecodes ? $"-copyts -r {FrameOrder.timebase} -frame_pts true" : */ "-frame_pts true";
|
||||
string mpStr = deDupe ? ((Config.GetInt("mpdecimateMode") == 0) ? mpDecDef : mpDecAggr) : "";
|
||||
string filters = FormatUtils.ConcatStrings(new string[] { divisionFilter, mpStr });
|
||||
string filters = FormatUtils.ConcatStrings(new string[] { GetPadFilter(), mpStr });
|
||||
string vf = filters.Length > 2 ? $"-vf {filters}" : "";
|
||||
string rateArg = (rate > 0) ? $" -r {rate.ToStringDot()}" : "";
|
||||
string pixFmt = alpha ? "-pix_fmt rgba" : "-pix_fmt rgb24"; // Use RGBA for GIF for alpha support
|
||||
@@ -73,7 +73,7 @@ namespace Flowframes.Media
|
||||
|
||||
string sizeStr = (size.Width > 1 && size.Height > 1) ? $"-s {size.Width}x{size.Height}" : "";
|
||||
string pixFmt = alpha ? "-pix_fmt rgba" : "-pix_fmt rgb24"; // Use RGBA for GIF for alpha support
|
||||
string vf = alpha ? $"-filter_complex \"[0:v]{divisionFilter},split[a][b];[a]palettegen=reserve_transparent=on:transparency_color=ffffff[p];[b][p]paletteuse\"" : $"-vf {divisionFilter}";
|
||||
string vf = alpha ? $"-filter_complex \"[0:v]{GetPadFilter()},split[a][b];[a]palettegen=reserve_transparent=on:transparency_color=ffffff[p];[b][p]paletteuse\"" : $"-vf {GetPadFilter()}";
|
||||
string args = $"-f concat -safe 0 -i {concatFile.Wrap()} {compr} {sizeStr} {pixFmt} -vsync 0 {vf} \"{outpath}/%{Padding.inputFrames}d.png\"";
|
||||
LogMode logMode = IOUtils.GetAmountOfFiles(inpath, false) > 50 ? LogMode.OnlyLastLine : LogMode.Hidden;
|
||||
await RunFfmpeg(args, logMode, "panic", TaskType.ExtractFrames);
|
||||
@@ -142,7 +142,7 @@ namespace Flowframes.Media
|
||||
bool isPng = (Path.GetExtension(outPath).ToLower() == ".png");
|
||||
string comprArg = isPng ? compr : "";
|
||||
string pixFmt = "-pix_fmt " + (isPng ? $"rgb24 {comprArg}" : "yuvj420p");
|
||||
string args = $"-i {inputFile.Wrap()} {comprArg} {sizeStr} {pixFmt} -vf {divisionFilter} {outPath.Wrap()}";
|
||||
string args = $"-i {inputFile.Wrap()} {comprArg} {sizeStr} {pixFmt} -vf {GetPadFilter()} {outPath.Wrap()}";
|
||||
await RunFfmpeg(args, LogMode.Hidden, TaskType.ExtractFrames);
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,6 @@ namespace Flowframes
|
||||
|
||||
public static async Task RunRifeCudaProcess (string inPath, string outDir, string script, float interpFactor, string mdl)
|
||||
{
|
||||
//bool parallel = false;
|
||||
string outPath = Path.Combine(inPath.GetParentDir(), outDir);
|
||||
string uhdStr = await InterpolateUtils.UseUHD() ? "--UHD" : "";
|
||||
string wthreads = $"--wthreads {2 * (int)interpFactor}";
|
||||
@@ -145,8 +144,6 @@ namespace Flowframes
|
||||
//string scale = $"--scale {Config.GetFloat("rifeCudaScale", 1.0f).ToStringDot()}";
|
||||
string prec = Config.GetBool("rifeCudaFp16") ? "--fp16" : "";
|
||||
string args = $" --input {inPath.Wrap()} --output {outDir} --model {mdl} --exp {(int)Math.Log(interpFactor, 2)} {uhdStr} {wthreads} {rbuffer} {prec}";
|
||||
// if (parallel) args = $" --input {inPath.Wrap()} --output {outPath} --model {mdl} --factor {interpFactor}";
|
||||
// if (parallel) script = "rife-parallel.py";
|
||||
|
||||
Process rifePy = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd());
|
||||
AiStarted(rifePy, 3500);
|
||||
@@ -171,6 +168,72 @@ namespace Flowframes
|
||||
while (!rifePy.HasExited) await Task.Delay(1);
|
||||
}
|
||||
|
||||
public static async Task RunFlavrCuda(string framesPath, float interpFactor, string mdl)
|
||||
{
|
||||
if (Interpolate.currentlyUsingAutoEnc) // Ensure AutoEnc is not paused
|
||||
AutoEncode.paused = false;
|
||||
|
||||
try
|
||||
{
|
||||
string flavDir = Path.Combine(Paths.GetPkgPath(), Networks.flavrCuda.pkgDir);
|
||||
string script = "flavr.py";
|
||||
|
||||
if (!File.Exists(Path.Combine(flavDir, script)))
|
||||
{
|
||||
Interpolate.Cancel("FLAVR script not found! Make sure you didn't modify any files.");
|
||||
return;
|
||||
}
|
||||
|
||||
await RunFlavrCudaProcess(framesPath, Paths.interpDir, script, interpFactor, mdl);
|
||||
|
||||
if (!Interpolate.canceled && Interpolate.current.alpha)
|
||||
{
|
||||
InterpolateUtils.progressPaused = true;
|
||||
Logger.Log("Interpolating alpha channel...");
|
||||
await RunFlavrCudaProcess(framesPath + Paths.alphaSuffix, Paths.interpDir + Paths.alphaSuffix, script, interpFactor, mdl);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Log("Error running FLAVR-CUDA: " + e.Message);
|
||||
}
|
||||
|
||||
await AiFinished("FLAVR");
|
||||
}
|
||||
|
||||
public static async Task RunFlavrCudaProcess(string inPath, string outDir, string script, float interpFactor, string mdl)
|
||||
{
|
||||
string outPath = Path.Combine(inPath.GetParentDir(), outDir);
|
||||
//string uhdStr = await InterpolateUtils.UseUHD() ? "--UHD" : "";
|
||||
//string wthreads = $"--wthreads {2 * (int)interpFactor}";
|
||||
//string rbuffer = $"--rbuffer {Config.GetInt("flavrCudaBufferSize", 200)}";
|
||||
//string scale = $"--scale {Config.GetFloat("flavrCudaScale", 1.0f).ToStringDot()}";
|
||||
// string prec = Config.GetBool("flavrCudaFp16") ? "--fp16" : "";
|
||||
string args = $" --input {inPath.Wrap()} --output {outPath.Wrap()} --model {mdl}/{mdl}.pth --factor {interpFactor}";
|
||||
|
||||
Process flavrPy = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd());
|
||||
AiStarted(flavrPy, 4000);
|
||||
SetProgressCheck(Path.Combine(Interpolate.current.tempFolder, outDir), interpFactor);
|
||||
flavrPy.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {Path.Combine(Paths.GetPkgPath(), Networks.flavrCuda.pkgDir).Wrap()} & " +
|
||||
$"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Python.GetPyCmd()} {script} {args}";
|
||||
Logger.Log($"Running FLAVR (CUDA)...", false);
|
||||
Logger.Log("cmd.exe " + flavrPy.StartInfo.Arguments, true);
|
||||
|
||||
if (!OSUtils.ShowHiddenCmd())
|
||||
{
|
||||
flavrPy.OutputDataReceived += (sender, outLine) => { LogOutput(outLine.Data, "flavr-cuda-log"); };
|
||||
flavrPy.ErrorDataReceived += (sender, outLine) => { LogOutput("[E] " + outLine.Data, "flavr-cuda-log", true); };
|
||||
}
|
||||
flavrPy.Start();
|
||||
if (!OSUtils.ShowHiddenCmd())
|
||||
{
|
||||
flavrPy.BeginOutputReadLine();
|
||||
flavrPy.BeginErrorReadLine();
|
||||
}
|
||||
|
||||
while (!flavrPy.HasExited) await Task.Delay(1);
|
||||
}
|
||||
|
||||
public static async Task RunRifeNcnn (string framesPath, string outPath, int factor, string mdl)
|
||||
{
|
||||
processTimeMulti.Restart();
|
||||
|
||||
Reference in New Issue
Block a user