From 233f03771637743a19053b7a733765dc31147e05 Mon Sep 17 00:00:00 2001 From: n00mkrad Date: Wed, 27 Jul 2022 15:18:37 +0200 Subject: [PATCH] Save fixed factors for specific AI models in models.json --- Code/Data/Implementations.cs | 4 +- Code/Data/InterpSettings.cs | 4 +- Code/Data/ModelCollection.cs | 55 +++++++++++++----------- Code/Form1.cs | 2 +- Code/Forms/BatchForm.cs | 2 +- Code/IO/IoUtils.cs | 2 +- Code/IO/ModelDownloader.cs | 4 +- Code/Main/AiModels.cs | 10 ++--- Code/Main/Interpolate.cs | 24 +++++++---- Code/Main/InterpolateUtils.cs | 2 +- Code/MiscUtils/ModelDownloadFormUtils.cs | 10 ++--- Code/Ui/MainUiFunctions.cs | 2 +- Code/Ui/UiUtils.cs | 10 ++--- Pkgs/flavr-cuda/models.json | 13 +++--- Pkgs/rife-ncnn-vs/models.json | 6 +++ 15 files changed, 86 insertions(+), 64 deletions(-) diff --git a/Code/Data/Implementations.cs b/Code/Data/Implementations.cs index 9ca69e3..cf06323 100644 --- a/Code/Data/Implementations.cs +++ b/Code/Data/Implementations.cs @@ -39,7 +39,7 @@ namespace Flowframes.Data Backend = AI.AiBackend.Pytorch, NameInternal = "FLAVR_CUDA", NameLong = "Flow-Agnostic Video Representations", - FactorSupport = AI.InterpFactorSupport.AnyFloat, + FactorSupport = AI.InterpFactorSupport.Fixed, SupportedFactors = new int[] { 2, 4, 8 }, }; @@ -75,7 +75,7 @@ namespace Flowframes.Data { get { - return new List { rifeCuda, rifeNcnnVs, rifeNcnn, flavrCuda, dainNcnn, xvfiCuda, ifrnetNcnn }; + return new List { rifeNcnnVs, rifeNcnn, rifeCuda, flavrCuda, dainNcnn, xvfiCuda, ifrnetNcnn }; } } diff --git a/Code/Data/InterpSettings.cs b/Code/Data/InterpSettings.cs index 01c143c..a6d4b2b 100644 --- a/Code/Data/InterpSettings.cs +++ b/Code/Data/InterpSettings.cs @@ -181,7 +181,7 @@ namespace Flowframes { try { - bool alphaModel = model.supportsAlpha; + bool alphaModel = model.SupportsAlpha; bool png = outMode == Interpolate.OutMode.ImgPng; bool gif = outMode == Interpolate.OutMode.VidGif; bool proResAlpha = outMode == Interpolate.OutMode.VidProRes && Config.GetInt(Config.Key.proResProfile) > 3; @@ -240,7 +240,7 @@ namespace Flowframes s += $"OUTFPS|{outFps}\n"; s += $"INTERPFACTOR|{interpFactor}\n"; s += $"OUTMODE|{outMode}\n"; - s += $"MODEL|{model.name}\n"; + s += $"MODEL|{model.Name}\n"; s += $"INPUTRES|{InputResolution.Width}x{InputResolution.Height}\n"; s += $"OUTPUTRES|{ScaledResolution.Width}x{ScaledResolution.Height}\n"; s += $"ALPHA|{alpha}\n"; diff --git a/Code/Data/ModelCollection.cs b/Code/Data/ModelCollection.cs index ad1f224..8581244 100644 --- a/Code/Data/ModelCollection.cs +++ b/Code/Data/ModelCollection.cs @@ -1,58 +1,54 @@ using Flowframes.IO; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.IO; +using System.Linq; namespace Flowframes.Data { public class ModelCollection { - public AI ai; - public List models = new List(); + public AI Ai { get; set; } = null; + public List Models { get; set; } = new List(); public class ModelInfo { - public AI ai; - public string name; - public string desc; - public string dir; - public bool supportsAlpha; - public bool isDefault; + public AI Ai { get; set; } = null; + public string Name { get; set; } = ""; + public string Desc { get; set; } = ""; + public string Dir { get; set; } = ""; + public bool SupportsAlpha { get; set; } = false; + public bool IsDefault { get; set; } = false; + private int[] _fixedFactors = null; + public int[] FixedFactors { get { return _fixedFactors == null ? new int[0] : _fixedFactors; } set { _fixedFactors = value; } } - public ModelInfo(AI ai, string name, string desc, string dir, bool supportsAlpha, bool isDefault) - { - this.ai = ai; - this.name = name; - this.desc = desc; - this.dir = dir; - this.supportsAlpha = supportsAlpha; - this.isDefault = isDefault; - } + public ModelInfo() { } public string GetUiString() { - return $"{name} - {desc}{(supportsAlpha ? " (Supports Transparency)" : "")}{(isDefault ? " (Recommended)" : "")}"; + return $"{Name} - {Desc}{(SupportsAlpha ? " (Supports Transparency)" : "")}{(FixedFactors.Count() > 0 ? $" ({GetFactorsString()})" : "")}{(IsDefault ? " (Recommended)" : "")}"; } - public override string ToString() + public string GetFactorsString () { - return $"{name} - {desc} ({dir}){(supportsAlpha ? " (Supports Transparency)" : "")}{(isDefault ? " (Recommended)" : "")}"; + return string.Join(", ", FixedFactors.Select(x => $"{x}x")); } } public ModelCollection(AI ai) { - this.ai = ai; + Ai = ai; } public ModelCollection(AI ai, string jsonContentOrPath) { - this.ai = ai; + Ai = ai; if (IoUtils.IsPathValid(jsonContentOrPath) && File.Exists(jsonContentOrPath)) jsonContentOrPath = File.ReadAllText(jsonContentOrPath); - models = new List(); + Models = new List(); dynamic data = JsonConvert.DeserializeObject(jsonContentOrPath); foreach (var item in data) @@ -63,7 +59,18 @@ namespace Flowframes.Data bool def = false; bool.TryParse((string)item.isDefault, out def); - models.Add(new ModelInfo(ai, (string)item.name, (string)item.desc, (string)item.dir, alpha, def)); + ModelInfo modelInfo = new ModelInfo() + { + Ai = ai, + Name = (string)item.name, + Desc = (string)item.desc, + Dir = (string)item.dir, + SupportsAlpha = alpha, + IsDefault = def, + FixedFactors = ((JArray)item.fixedFactors)?.Select(x => (int)x).ToArray(), + }; + + Models.Add(modelInfo); } } } diff --git a/Code/Form1.cs b/Code/Form1.cs index 58b0916..1849d9e 100644 --- a/Code/Form1.cs +++ b/Code/Form1.cs @@ -337,7 +337,7 @@ namespace Flowframes { try { - return AiModels.GetModels(currentAi).models[aiModel.SelectedIndex]; + return AiModels.GetModels(currentAi).Models[aiModel.SelectedIndex]; } catch { diff --git a/Code/Forms/BatchForm.cs b/Code/Forms/BatchForm.cs index 7dbc6a1..759feed 100644 --- a/Code/Forms/BatchForm.cs +++ b/Code/Forms/BatchForm.cs @@ -34,7 +34,7 @@ namespace Flowframes.Forms InterpSettings entry = Program.batchQueue.ElementAt(i); string niceOutMode = entry.outMode.ToString().ToUpper().Remove("VID").Remove("IMG"); string str = $"#{i+1}: {Path.GetFileName(entry.inPath).Trunc(40)} - {entry.inFps.GetFloat()} FPS => " + - $"{entry.interpFactor}x {entry.ai.NameShort} ({entry.model.name}) => {niceOutMode}"; + $"{entry.interpFactor}x {entry.ai.NameShort} ({entry.model.Name}) => {niceOutMode}"; taskList.Items.Add(str); } } diff --git a/Code/IO/IoUtils.cs b/Code/IO/IoUtils.cs index a37f6f1..b46bc99 100644 --- a/Code/IO/IoUtils.cs +++ b/Code/IO/IoUtils.cs @@ -579,7 +579,7 @@ namespace Flowframes.IO filename = filename.Replace("[FULLNAME]", Path.GetFileName(curr.inPath)); filename = filename.Replace("[FACTOR]", curr.interpFactor.ToStringDot()); filename = filename.Replace("[AI]", curr.ai.NameShort.ToUpper()); - filename = filename.Replace("[MODEL]", curr.model.name.Remove(" ")); + filename = filename.Replace("[MODEL]", curr.model.Name.Remove(" ")); filename = filename.Replace("[FPS]", fps.ToStringDot()); filename = filename.Replace("[ROUNDFPS]", fps.RoundToInt().ToString()); filename = filename.Replace("[RES]", $"{outRes.Width}x{outRes.Height}"); diff --git a/Code/IO/ModelDownloader.cs b/Code/IO/ModelDownloader.cs index 27f00c2..ce82bc6 100644 --- a/Code/IO/ModelDownloader.cs +++ b/Code/IO/ModelDownloader.cs @@ -201,9 +201,9 @@ namespace Flowframes.IO string aiPkgFolder = Path.Combine(Paths.GetPkgPath(), ai.PkgDir); ModelCollection aiModels = AiModels.GetModels(ai); - foreach(ModelCollection.ModelInfo model in aiModels.models) + foreach(ModelCollection.ModelInfo model in aiModels.Models) { - string mdlFolder = Path.Combine(aiPkgFolder, model.dir); + string mdlFolder = Path.Combine(aiPkgFolder, model.Dir); if (Directory.Exists(mdlFolder)) modelPaths.Add(mdlFolder); diff --git a/Code/Main/AiModels.cs b/Code/Main/AiModels.cs index e562bfa..d2f2d01 100644 --- a/Code/Main/AiModels.cs +++ b/Code/Main/AiModels.cs @@ -29,7 +29,7 @@ namespace Flowframes.Main { string name = customModel.Remove("_alpha").Remove("_custom"); bool alpha = customModel.Contains("_alpha"); - modelCollection.models.Add(new ModelCollection.ModelInfo(ai, name, "Custom Model", customModel, alpha, false)); + modelCollection.Models.Add(new ModelCollection.ModelInfo() { Ai = ai, Name = name, Desc = "Custom Model", SupportsAlpha = alpha, IsDefault = false }); } return modelCollection; @@ -53,9 +53,9 @@ namespace Flowframes.Main { ModelCollection modelCollection = GetModels(ai); - foreach(ModelCollection.ModelInfo model in modelCollection.models) + foreach(ModelCollection.ModelInfo model in modelCollection.Models) { - if (model.name == modelName) + if (model.Name == modelName) return model; } @@ -66,9 +66,9 @@ namespace Flowframes.Main { ModelCollection modelCollection = GetModels(ai); - foreach (ModelCollection.ModelInfo model in modelCollection.models) + foreach (ModelCollection.ModelInfo model in modelCollection.Models) { - if (model.dir == dirName) + if (model.Dir == dirName) return model; } diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index 2b61fd7..0edaaf3 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -89,7 +89,7 @@ namespace Flowframes public static async Task Realtime () { - await AiProcess.RunRifeNcnnVs(currentSettings.framesFolder, "", currentSettings.interpFactor, currentSettings.model.dir, true); + await AiProcess.RunRifeNcnnVs(currentSettings.framesFolder, "", currentSettings.interpFactor, currentSettings.model.Dir, true); } public static async Task GetFrames() @@ -186,8 +186,14 @@ namespace Flowframes if (!ai.Piped || (ai.Piped && dedupe)) await Task.Run(async () => { await FrameOrder.CreateFrameOrderFile(currentSettings.framesFolder, Config.GetBool(Config.Key.enableLoop), currentSettings.interpFactor); }); + if (currentSettings.model.FixedFactors.Count() > 0 && (currentSettings.interpFactor != (int)currentSettings.interpFactor || !currentSettings.model.FixedFactors.Contains(currentSettings.interpFactor.RoundToInt()))) + Cancel($"The selected model does not support {currentSettings.interpFactor}x interpolation.\n\nSupported Factors: {currentSettings.model.GetFactorsString()}"); + + if (canceled) return; + Program.mainForm.SetStatus("Downloading models..."); - await ModelDownloader.DownloadModelFiles(ai, currentSettings.model.dir); + await ModelDownloader.DownloadModelFiles(ai, currentSettings.model.Dir); + if (canceled) return; currentlyUsingAutoEnc = Utils.CanUseAutoEnc(stepByStep, currentSettings); @@ -197,25 +203,25 @@ namespace Flowframes List tasks = new List(); if (ai.NameInternal == Implementations.rifeCuda.NameInternal) - tasks.Add(AiProcess.RunRifeCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunRifeCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.Dir)); if (ai.NameInternal == Implementations.rifeNcnn.NameInternal) - tasks.Add(AiProcess.RunRifeNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunRifeNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.Dir)); if (ai.NameInternal == Implementations.rifeNcnnVs.NameInternal) - tasks.Add(AiProcess.RunRifeNcnnVs(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunRifeNcnnVs(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.Dir)); if (ai.NameInternal == Implementations.flavrCuda.NameInternal) - tasks.Add(AiProcess.RunFlavrCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunFlavrCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.Dir)); if (ai.NameInternal == Implementations.dainNcnn.NameInternal) - tasks.Add(AiProcess.RunDainNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.dir, Config.GetInt(Config.Key.dainNcnnTilesize, 512))); + tasks.Add(AiProcess.RunDainNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.Dir, Config.GetInt(Config.Key.dainNcnnTilesize, 512))); if (ai.NameInternal == Implementations.xvfiCuda.NameInternal) - tasks.Add(AiProcess.RunXvfiCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunXvfiCuda(currentSettings.framesFolder, currentSettings.interpFactor, currentSettings.model.Dir)); if(ai.NameInternal == Implementations.ifrnetNcnn.NameInternal) - tasks.Add(AiProcess.RunIfrnetNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.dir)); + tasks.Add(AiProcess.RunIfrnetNcnn(currentSettings.framesFolder, outpath, currentSettings.interpFactor, currentSettings.model.Dir)); if (currentlyUsingAutoEnc) { diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 7d773d6..4f776ed 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -154,7 +154,7 @@ namespace Flowframes.Main return false; } - if (model == null || model.dir.Trim() == "") + if (model == null || model.Dir.Trim() == "") { UiUtils.ShowMessageBox("No valid AI model has been selected!", UiUtils.MessageType.Error); I.Cancel("No valid model selected.", true); diff --git a/Code/MiscUtils/ModelDownloadFormUtils.cs b/Code/MiscUtils/ModelDownloadFormUtils.cs index 7869c43..dc2a6b4 100644 --- a/Code/MiscUtils/ModelDownloadFormUtils.cs +++ b/Code/MiscUtils/ModelDownloadFormUtils.cs @@ -48,14 +48,14 @@ namespace Flowframes.MiscUtils { ModelCollection modelCollection = AiModels.GetModels(ai); - for (int i = 0; i < modelCollection.models.Count; i++) + for (int i = 0; i < modelCollection.Models.Count; i++) { if (canceled) return; - ModelCollection.ModelInfo modelInfo = modelCollection.models[i]; - form.SetStatus($"Downloading files for {modelInfo.ai.NameInternal.Replace("_", "-")}..."); - await ModelDownloader.DownloadModelFiles(ai, modelInfo.dir, false); + ModelCollection.ModelInfo modelInfo = modelCollection.Models[i]; + form.SetStatus($"Downloading files for {modelInfo.Ai.NameInternal.Replace("_", "-")}..."); + await ModelDownloader.DownloadModelFiles(ai, modelInfo.Dir, false); taskCounter++; UpdateProgressBar(); } @@ -79,7 +79,7 @@ namespace Flowframes.MiscUtils foreach(AI ai in ais) { ModelCollection modelCollection = AiModels.GetModels(ai); - count += modelCollection.models.Count; + count += modelCollection.Models.Count; } return count; diff --git a/Code/Ui/MainUiFunctions.cs b/Code/Ui/MainUiFunctions.cs index 239a4da..03134c5 100644 --- a/Code/Ui/MainUiFunctions.cs +++ b/Code/Ui/MainUiFunctions.cs @@ -161,7 +161,7 @@ namespace Flowframes.Ui { AI ai = Program.mainForm.GetAi(); - if (ai.NameInternal == Implementations.rifeNcnn.NameInternal && !Program.mainForm.GetModel(ai).dir.Contains("v4")) + if (ai.NameInternal == Implementations.rifeNcnn.NameInternal && !Program.mainForm.GetModel(ai).Dir.Contains("v4")) { if (factor != 2) Logger.Log($"{ai.FriendlyName} models before 4.0 only support 2x interpolation!"); diff --git a/Code/Ui/UiUtils.cs b/Code/Ui/UiUtils.cs index bc824cc..f4cc6d5 100644 --- a/Code/Ui/UiUtils.cs +++ b/Code/Ui/UiUtils.cs @@ -38,19 +38,19 @@ namespace Flowframes.Ui { ModelCollection modelCollection = AiModels.GetModels(ai); - if (modelCollection.models == null || modelCollection.models.Count < 1) + if (modelCollection.Models == null || modelCollection.Models.Count < 1) return combox; - for (int i = 0; i < modelCollection.models.Count; i++) + for (int i = 0; i < modelCollection.Models.Count; i++) { - ModelCollection.ModelInfo modelInfo = modelCollection.models[i]; + ModelCollection.ModelInfo modelInfo = modelCollection.Models[i]; - if (string.IsNullOrWhiteSpace(modelInfo.name)) + if (string.IsNullOrWhiteSpace(modelInfo.Name)) continue; combox.Items.Add(modelInfo.GetUiString()); - if (modelInfo.isDefault) + if (modelInfo.IsDefault) combox.SelectedIndex = i; } diff --git a/Pkgs/flavr-cuda/models.json b/Pkgs/flavr-cuda/models.json index d7dd508..7ef4f98 100644 --- a/Pkgs/flavr-cuda/models.json +++ b/Pkgs/flavr-cuda/models.json @@ -1,18 +1,21 @@ [ { "name": "FLAVR 2x", - "desc": "Official model, only works for 2x interpolation", + "desc": "Official model", "dir": "FLAVR2X", + "fixedFactors": [2], "isDefault": "true" }, { "name": "FLAVR 4x", - "desc": "Official model, only works for 4x interpolation", - "dir": "FLAVR4X" + "desc": "Official model", + "dir": "FLAVR4X", + "fixedFactors": [4], }, { "name": "FLAVR 8x", - "desc": "Official model, only works for 8x interpolation", - "dir": "FLAVR8X" + "desc": "Official model", + "dir": "FLAVR8X", + "fixedFactors": [8], } ] \ No newline at end of file diff --git a/Pkgs/rife-ncnn-vs/models.json b/Pkgs/rife-ncnn-vs/models.json index 4ebab26..28b7fe4 100644 --- a/Pkgs/rife-ncnn-vs/models.json +++ b/Pkgs/rife-ncnn-vs/models.json @@ -1,4 +1,10 @@ [ +{ + "name": "RIFE 2.3", + "desc": "Old Model", + "dir": "rife-v2.3", + "fixedFactors": [2] +}, { "name": "RIFE 4.0", "desc": "New Fast General Model",