Skip to content

Commit

Permalink
Fix GC not collecting after job done.
Browse files Browse the repository at this point in the history
Weird.

Signed-off-by: net2cn <[email protected]>
  • Loading branch information
net2cn committed Sep 16, 2021
1 parent 13a8dfb commit 2190f49
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions Real-ESRGAN_GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private async void StartButton_ClickAsync(object sender, RoutedEventArgs e)
}

model.Dispose();
GC.Collect();
StartButton.IsEnabled = true;
CancelButton.IsEnabled = true;
}
Expand Down
12 changes: 7 additions & 5 deletions Real-ESRGAN_GUI/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tensor<float>> Inference(Tensor<float> input)
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions scripts/onnx_quantitize_test.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions scripts/onnx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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()
4 changes: 4 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numpy
onnx
onnxruntime
opencv-python

0 comments on commit 2190f49

Please sign in to comment.