From 2190f499345546293c6fca0f3de09f747753781b Mon Sep 17 00:00:00 2001 From: net2cn Date: Thu, 16 Sep 2021 16:05:52 +0800 Subject: [PATCH] Fix GC not collecting after job done. Weird. Signed-off-by: net2cn --- Real-ESRGAN_GUI/MainWindow.xaml.cs | 1 + Real-ESRGAN_GUI/Model.cs | 12 +++++++----- scripts/onnx_quantitize_test.py | 7 +++++++ scripts/onnx_test.py | 6 +++--- scripts/requirements.txt | 4 ++++ 5 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 scripts/onnx_quantitize_test.py create mode 100644 scripts/requirements.txt diff --git a/Real-ESRGAN_GUI/MainWindow.xaml.cs b/Real-ESRGAN_GUI/MainWindow.xaml.cs index 875cb0d..11d6859 100644 --- a/Real-ESRGAN_GUI/MainWindow.xaml.cs +++ b/Real-ESRGAN_GUI/MainWindow.xaml.cs @@ -112,6 +112,7 @@ private async void StartButton_ClickAsync(object sender, RoutedEventArgs e) } model.Dispose(); + GC.Collect(); StartButton.IsEnabled = true; CancelButton.IsEnabled = true; } diff --git a/Real-ESRGAN_GUI/Model.cs b/Real-ESRGAN_GUI/Model.cs index d73b5d3..bea832e 100644 --- a/Real-ESRGAN_GUI/Model.cs +++ b/Real-ESRGAN_GUI/Model.cs @@ -47,12 +47,12 @@ public async Task Scale(string inputPath, string outputPath, string outputFormat logger.Log("Converting output tensor to image..."); image = ConvertFloatTensorToImageUnsafe(outMat); - var saveName = Path.GetFileName(inputPath); var savePath = $"{outputPath}{saveName.Split(".")[0]}_{modelName}.{outputFormat}"; logger.Log($"Writing image to {savePath}..."); image.Save(savePath); logger.Progress += 10; + image.Dispose(); } public async Task> Inference(Tensor input) @@ -141,10 +141,12 @@ public static Bitmap RemoveAlphaChannel(Bitmap bitmap) { Bitmap target = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb); target.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); // Set both bitmap to same dpi to prevent scaling. - Graphics g = Graphics.FromImage(target); - g.Clear(Color.White); - g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; - g.DrawImage(bitmap, 0, 0); + using (Graphics g = Graphics.FromImage(target)) + { + g.Clear(Color.White); + g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; + g.DrawImage(bitmap, 0, 0); + } return target; } diff --git a/scripts/onnx_quantitize_test.py b/scripts/onnx_quantitize_test.py new file mode 100644 index 0000000..af4608b --- /dev/null +++ b/scripts/onnx_quantitize_test.py @@ -0,0 +1,7 @@ +import os +from onnxruntime.quantization import quantize_qat, QuantType, CalibrationDataReader + +project_path=os.path.abspath("../Real-ESRGAN_GUI/") +input_model_path = os.path.join(project_path, "models/realesrgan-x4plus_anime_6B.onnx") +output_model_path = os.path.join(project_path,"models/realesrgan-x4plus_anime_6B_quantitized.onnx") +quantize_qat(input_model_path, output_model_path, weight_type=QuantType.QUInt8) \ No newline at end of file diff --git a/scripts/onnx_test.py b/scripts/onnx_test.py index 2baa0b0..571d676 100644 --- a/scripts/onnx_test.py +++ b/scripts/onnx_test.py @@ -35,14 +35,14 @@ def display_image(img, color_mode): sess = rt.InferenceSession(os.path.join(project_path, "models/realesrgan-x4plus_anime_6B.onnx")) print("loaded model.") -in_image = cv2.imread(os.path.join(project_path, "input.jpg"), cv2.IMREAD_UNCHANGED) +in_image = cv2.imread(os.path.join("../assets", "avatar_256px.png"), cv2.IMREAD_UNCHANGED) print("loaded input image.") -print("create in_mat from tiles.") in_mat = cv2.cvtColor(in_image, cv2.COLOR_BGR2RGB) in_mat = np.transpose(in_mat, (2, 1, 0))[np.newaxis] in_mat = in_mat.astype(np.float32) in_mat = in_mat/255 +print("loaded image.") # display_image(in_mat, cv2.COLOR_RGB2BGR) print("sess run.") @@ -53,8 +53,8 @@ def display_image(img, color_mode): print("convert out_mat to image") out_mat = np.squeeze(out_mat, axis=0) out_mat = np.clip(out_mat, 0, 1) -out_mat = out_mat.T out_mat = (out_mat*255.).round().astype(np.uint8) +out_mat = out_mat.T out_mat = cv2.cvtColor(out_mat, cv2.COLOR_RGB2BGR) cv2.imshow("out_mat", out_mat) cv2.waitKey() \ No newline at end of file diff --git a/scripts/requirements.txt b/scripts/requirements.txt new file mode 100644 index 0000000..183d872 --- /dev/null +++ b/scripts/requirements.txt @@ -0,0 +1,4 @@ +numpy +onnx +onnxruntime +opencv-python \ No newline at end of file