Код: Выделить всё
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using VOModel;
using Python.Runtime;
using System.Diagnostics;
namespace Memory_Test
{
public partial class Form1 : Form
{
Detection od_model = new Detection();
Detection_result od_res = new Detection_result();
PyObject model_obj_OD;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Load
string pypath = @"C:\Users\user\AppData\Local\Programs\Python\Python38";
string wpath = @"C:\Users\user\Desktop\DL\best.pt";
string vood_path = @"C:\Users\user\Desktop\DL\VOModels";
od_model.SetEnvironment(pypath);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//model_objs_OD = od_model.LoadNet_OD_v2(vood_path, wpath);
model_obj_OD = od_model.LoadNet_OD(vood_path, wpath);
stopwatch.Stop();
Console.WriteLine("time : " + stopwatch.ElapsedMilliseconds + "ms");
}
private void button3_Click(object sender, EventArgs e)
{
// Dispose of the model_obj_OD if it's initialized
if (model_obj_OD != null)
{
model_obj_OD.Dispose();
model_obj_OD = null;
}
// Perform garbage collection to release any remaining resources
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
// Optional: Print memory statistics to verify memory release
long memoryUsed = GC.GetTotalMemory(true);
Console.WriteLine("Memory used after release: " + memoryUsed / 1024 + " KB");
}
}
}
Код: Выделить всё
public PyObject LoadNet_OD(string vomodel_path, string weights_path)
{
using (Py.GIL())
{
model_path = vomodel_path;
Directory.SetCurrentDirectory(vomodel_path);
PyObject pyObject = Py.Import("sys");
pyObject.GetAttr("path").InvokeMethod("append", new
PyString(vomodel_path));
dynamic val = Py.Import("loadnet_od");
dynamic val2 = val.load_net(weights_path);
model_object = val2;
Console.WriteLine("Load Done");
return val2;
Подробнее здесь: https://stackoverflow.com/questions/786 ... in-c-sharp