Skip to content

Commit

Permalink
Better NCNN converter error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Apr 3, 2021
1 parent cf5f163 commit 8a3000e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Code/IO/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static void Uninstall (bool full)
}
catch (Exception e)
{
Logger.Log("Failed to uninstall.\nClose Cupscale and try deleting %APPDATA%/Cupscale manually. " + e.Message);
Logger.Log("Failed to uninstall.\nClose Cupscale and try deleting CupscaleData manually. " + e.Message);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Code/Main/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static string GetSessionLog ()

public static MsgBox ErrorMessage (string msg, Exception e)
{
string text = $"{msg}\n{e.Message}\n\nStack Trace:\n{e.StackTrace}";
string text = $"{msg}\n\n{e.Message}\n\nStack Trace:\n{e.StackTrace}";
Clipboard.SetText(text);
Log(text);
return Program.ShowMessage(text + "\n\nThe error message was copied to the clipboard.", "Error");
Expand Down
16 changes: 12 additions & 4 deletions Code/OS/NcnnUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -15,6 +16,7 @@ namespace Cupscale.OS
class NcnnUtils
{
static Process currentProcess;
public static string lastNcnnOutput;
static string ncnnDir = "";

public static async Task ConvertNcnnModel(string modelPath)
Expand All @@ -31,7 +33,11 @@ public static async Task ConvertNcnnModel(string modelPath)
Logger.Log("Running model converter...");
DialogForm dialog = new DialogForm("Converting ESRGAN model to NCNN format...");
await RunConverter(modelPath);
string moveFrom = Path.Combine(Paths.esrganPath, Path.ChangeExtension(modelName, null));

if (lastNcnnOutput.Contains("Error:"))
throw new Exception(lastNcnnOutput.SplitIntoLines().Where(x => x.Contains("Error:")).First());

string moveFrom = Path.Combine(Paths.esrganPath, Path.ChangeExtension(modelName, null));
Logger.Log("Moving " + moveFrom + " to " + outPath);
await IOUtils.CopyDir(moveFrom, outPath, "*", true);
Directory.Delete(moveFrom, true);
Expand All @@ -46,12 +52,13 @@ public static async Task ConvertNcnnModel(string modelPath)
}
catch (Exception e)
{
Logger.ErrorMessage("Failed to convert Pytorch model to NCNN format!", e);
Logger.ErrorMessage("Failed to convert Pytorch model to NCNN format! It might be incompatible.", e);
}
}

static async Task RunConverter(string modelPath)
{
{
lastNcnnOutput = "";
bool showWindow = Config.GetInt("cmdDebugMode") > 0;
bool stayOpen = Config.GetInt("cmdDebugMode") == 2;

Expand Down Expand Up @@ -90,7 +97,8 @@ private static void OutputHandler(object sendingProcess, DataReceivedEventArgs o

string data = output.Data;
Logger.Log("[NcnnUtils] Model Converter Output: " + data);
}
lastNcnnOutput += $"{data}\n";
}

public static int GetNcnnModelScale(string modelDir)
{
Expand Down
5 changes: 5 additions & 0 deletions Code/UI/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,10 @@ public static string TrimWhitespaces(this string str)
}
return newString.ToString();
}

public static string[] SplitIntoLines(this string str)
{
return Regex.Split(str, "\r\n|\r|\n");
}
}
}

0 comments on commit 8a3000e

Please sign in to comment.