Skip to content

Commit 2190f49

Browse files
committed
Fix GC not collecting after job done.
Weird. Signed-off-by: net2cn <[email protected]>
1 parent 13a8dfb commit 2190f49

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

Real-ESRGAN_GUI/MainWindow.xaml.cs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ private async void StartButton_ClickAsync(object sender, RoutedEventArgs e)
112112
}
113113

114114
model.Dispose();
115+
GC.Collect();
115116
StartButton.IsEnabled = true;
116117
CancelButton.IsEnabled = true;
117118
}

Real-ESRGAN_GUI/Model.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public async Task Scale(string inputPath, string outputPath, string outputFormat
4747
logger.Log("Converting output tensor to image...");
4848
image = ConvertFloatTensorToImageUnsafe(outMat);
4949

50-
5150
var saveName = Path.GetFileName(inputPath);
5251
var savePath = $"{outputPath}{saveName.Split(".")[0]}_{modelName}.{outputFormat}";
5352
logger.Log($"Writing image to {savePath}...");
5453
image.Save(savePath);
5554
logger.Progress += 10;
55+
image.Dispose();
5656
}
5757

5858
public async Task<Tensor<float>> Inference(Tensor<float> input)
@@ -141,10 +141,12 @@ public static Bitmap RemoveAlphaChannel(Bitmap bitmap)
141141
{
142142
Bitmap target = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
143143
target.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); // Set both bitmap to same dpi to prevent scaling.
144-
Graphics g = Graphics.FromImage(target);
145-
g.Clear(Color.White);
146-
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
147-
g.DrawImage(bitmap, 0, 0);
144+
using (Graphics g = Graphics.FromImage(target))
145+
{
146+
g.Clear(Color.White);
147+
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
148+
g.DrawImage(bitmap, 0, 0);
149+
}
148150
return target;
149151
}
150152

scripts/onnx_quantitize_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
from onnxruntime.quantization import quantize_qat, QuantType, CalibrationDataReader
3+
4+
project_path=os.path.abspath("../Real-ESRGAN_GUI/")
5+
input_model_path = os.path.join(project_path, "models/realesrgan-x4plus_anime_6B.onnx")
6+
output_model_path = os.path.join(project_path,"models/realesrgan-x4plus_anime_6B_quantitized.onnx")
7+
quantize_qat(input_model_path, output_model_path, weight_type=QuantType.QUInt8)

scripts/onnx_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def display_image(img, color_mode):
3535
sess = rt.InferenceSession(os.path.join(project_path, "models/realesrgan-x4plus_anime_6B.onnx"))
3636
print("loaded model.")
3737

38-
in_image = cv2.imread(os.path.join(project_path, "input.jpg"), cv2.IMREAD_UNCHANGED)
38+
in_image = cv2.imread(os.path.join("../assets", "avatar_256px.png"), cv2.IMREAD_UNCHANGED)
3939
print("loaded input image.")
4040

41-
print("create in_mat from tiles.")
4241
in_mat = cv2.cvtColor(in_image, cv2.COLOR_BGR2RGB)
4342
in_mat = np.transpose(in_mat, (2, 1, 0))[np.newaxis]
4443
in_mat = in_mat.astype(np.float32)
4544
in_mat = in_mat/255
45+
print("loaded image.")
4646

4747
# display_image(in_mat, cv2.COLOR_RGB2BGR)
4848
print("sess run.")
@@ -53,8 +53,8 @@ def display_image(img, color_mode):
5353
print("convert out_mat to image")
5454
out_mat = np.squeeze(out_mat, axis=0)
5555
out_mat = np.clip(out_mat, 0, 1)
56-
out_mat = out_mat.T
5756
out_mat = (out_mat*255.).round().astype(np.uint8)
57+
out_mat = out_mat.T
5858
out_mat = cv2.cvtColor(out_mat, cv2.COLOR_RGB2BGR)
5959
cv2.imshow("out_mat", out_mat)
6060
cv2.waitKey()

scripts/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy
2+
onnx
3+
onnxruntime
4+
opencv-python

0 commit comments

Comments
 (0)