Problem bei der Integration des Machine Learning-Modells in Windows FormsC#

Ein Treffpunkt für C#-Programmierer
Guest
 Problem bei der Integration des Machine Learning-Modells in Windows Forms

Post by Guest »

Ich habe ein ML.NET-Modell für maschinelles Lernen in Microsoft Visual Studio trainiert, um Objekte auf Fotos zu finden. Jetzt möchte ich dieses Modell direkt in das Formular integrieren, damit ich wie in der Anwendung direkt ein Foto hochladen und auf klicken kann Klicken Sie auf die Schaltfläche „Analysieren“ und meine Textbox gibt mir den Namen des analysierten Objekts im Bild. Sagen Sie mir bitte, wie es geht. Ich habe es mit Hilfe von gpt versucht und hier ist sein Code

Code: Select all

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Image;
using Microsoft.ML.Vision;

namespace jest
{
public partial class Form1 : Form
{
private MLContext mlContext;
private ITransformer trainedModel;
private PredictionEngine predictionEngine;

public Form1()
{
InitializeComponent();
mlContext = new MLContext();
LoadModel();
}

private void LoadModel()
{

string modelPath = @"C:\Users\Danilkin\Desktop\kursach\jest\MLModel1.mlnet";

using (var stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
}

predictionEngine = mlContext.Model.CreatePredictionEngine(trainedModel);
}

private void btnLoad_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox.Image = Image.FromFile(openFileDialog.FileName);
}
}
}

private void btnAnalyze_Click(object sender, EventArgs e)
{
if (pictureBox.Image != null)
{

ImageData imageData = new ImageData { ImagePath = "path_to_loaded_image" };

var result = predictionEngine.Predict(imageData);
textBoxResult.Text = result.PredictedLabel;
}
else
{
MessageBox.Show("Please load an image first.");
}
}

private void textBoxResult_TextChanged(object sender, EventArgs e)
{

}
}

public class ImageData
{
[LoadColumn(0)]
public string ImagePath;

[ImageType(224, 224)]
public Bitmap Image;

}

public class ImagePrediction : ImageData
{
public string PredictedLabel;
public float[] Score;
}

}

Aber dieser Code gibt mir einen Fehler mit TorchCharp

Code: Select all

NotSupportedException: This application or script uses TorchSharp but doesn't contain a reference to libtorch-cpu-win-x64, Version=2.5.1.0.

Consider referencing one of the combination packages TorchSharp-cpu, TorchSharp-cuda-linux, TorchSharp-cuda-windows or call System.Runtime.InteropServices.NativeLibrary.Load(path-to-LibTorchSharp.dll) explicitly for a Python install of pytorch.  See https://github.com/dotnet/TorchSharp/issues/169.".

For CUDA, you may need to call 'TorchSharp.torch.InitializeDeviceType(TorchSharp.DeviceType.CUDA)' before any use of TorchSharp CUDA packages from scripts or notebooks.

Trace from LoadNativeBackend:

TorchSharp: LoadNativeBackend: Initialising native backend, useCudaBackend = False

Step 1 - First try regular load of native libtorch binaries.

Trying to load native component torch_cpu relative to C:\Users\Danilkin\Desktop\kursach\jest\bin\Debug\TorchSharp.dll
Failed to load native component torch_cpu relative to C:\Users\Danilkin\Desktop\kursach\jest\bin\Debug\TorchSharp.dll
Trying to load native component LibTorchSharp relative to C:\Users\Danilkin\Desktop\kursach\jest\bin\Debug\TorchSharp.dll
Failed to load native component LibTorchSharp relative to C:\Users\Danilkin\Desktop\kursach\jest\bin\Debug\TorchSharp.dll
Result from regular native load of LibTorchSharp is False

Step 3 - Alternative load from consolidated directory of native binaries from nuget packages

torchsharpLoc = C:\Users\Danilkin\Desktop\kursach\jest\bin\Debug
packagesDir = C:\Users\Danilkin\Desktop
torchsharpHome = C:\Users\Danilkin\Desktop\kursach\jest
Giving up, TorchSharp.dll does not appear to have been loaded from package directories
Und ich weiß nicht, wie ich das Problem beheben kann, weil ich alle erforderlichen Pakete installiert habe

Code: Select all








Debug
AnyCPU
{D1B60B40-313E-4A50-9DD9-E5C9105534C8}
WinExe
jest
jest
v4.7.2
512
true
true




x64
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4


AnyCPU
pdbonly
true
bin\Release\
TRACE
prompt
4



packages\Google.Protobuf.3.29.2\lib\net45\Google.Protobuf.dll


packages\SharpZipLib.1.4.2\lib\netstandard2.0\ICSharpCode.SharpZipLib.dll


packages\MethodBoundaryAspect.Fody.2.0.149\lib\net462\MethodBoundaryAspect.dll


packages\Microsoft.Bcl.AsyncInterfaces.9.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll


packages\Microsoft.Bcl.HashCode.6.0.0\lib\net462\Microsoft.Bcl.HashCode.dll


packages\Microsoft.Bcl.Memory.9.0.0\lib\net462\Microsoft.Bcl.Memory.dll


packages\Microsoft.Bcl.Numerics.9.0.0\lib\net462\Microsoft.Bcl.Numerics.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.Core.dll


packages\Microsoft.ML.CpuMath.4.0.0\lib\netstandard2.0\Microsoft.ML.CpuMath.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.Data.dll


packages\Microsoft.ML.DataView.4.0.0\lib\netstandard2.0\Microsoft.ML.DataView.dll


packages\Microsoft.ML.ImageAnalytics.4.0.0\lib\netstandard2.0\Microsoft.ML.ImageAnalytics.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.KMeansClustering.dll


packages\Microsoft.ML.OnnxRuntime.Managed.1.20.1\lib\netstandard2.0\Microsoft.ML.OnnxRuntime.dll


packages\Microsoft.ML.OnnxTransformer.4.0.0\lib\netstandard2.0\Microsoft.ML.OnnxTransformer.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.PCA.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.StandardTrainers.dll


packages\Microsoft.ML.TensorFlow.4.0.0\lib\netstandard2.0\Microsoft.ML.TensorFlow.dll


packages\Microsoft.ML.Tokenizers.1.0.0\lib\netstandard2.0\Microsoft.ML.Tokenizers.dll


packages\Microsoft.ML.TorchSharp.0.22.0\lib\netstandard2.0\Microsoft.ML.TorchSharp.dll


packages\Microsoft.ML.4.0.0\lib\netstandard2.0\Microsoft.ML.Transforms.dll


packages\Microsoft.ML.Vision.4.0.0\lib\netstandard2.0\Microsoft.ML.Vision.dll


packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll


packages\NumSharp.Lite.0.1.12\lib\netstandard2.0\NumSharp.Lite.dll


packages\OneOf.3.0.263\lib\net45\OneOf.dll


packages\Protobuf.Text.0.7.2\lib\netstandard2.0\Protobuf.Text.dll


packages\Razorvine.Pickle.1.4.0\lib\netstandard2.0\Razorvine.Pickle.dll


packages\Serilog.2.10.0\lib\net46\Serilog.dll


packages\Serilog.Sinks.Console.4.1.0\lib\net45\Serilog.Sinks.Console.dll


packages\SkiaSharp.3.116.1\lib\net462\SkiaSharp.dll



packages\System.Buffers.4.6.0\lib\net462\System.Buffers.dll


packages\System.CodeDom.9.0.0\lib\net462\System.CodeDom.dll


packages\System.Collections.Immutable.9.0.0\lib\net462\System.Collections.Immutable.dll



packages\System.IO.FileSystem.AccessControl.5.0.0\lib\net461\System.IO.FileSystem.AccessControl.dll


packages\System.IO.Pipelines.9.0.0\lib\net462\System.IO.Pipelines.dll


packages\System.Memory.4.6.0\lib\net462\System.Memory.dll



packages\System.Numerics.Tensors.9.0.0\lib\net462\System.Numerics.Tensors.dll


packages\System.Numerics.Vectors.4.6.0\lib\net462\System.Numerics.Vectors.dll


packages\System.Runtime.CompilerServices.Unsafe.6.1.0\lib\net462\System.Runtime.CompilerServices.Unsafe.dll


packages\System.Security.AccessControl.6.0.1\lib\net461\System.Security.AccessControl.dll


packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll


packages\System.Text.Encodings.Web.9.0.0\lib\net462\System.Text.Encodings.Web.dll


packages\System.Text.Json.9.0.0\lib\net462\System.Text.Json.dll


packages\System.Threading.Channels.9.0.0\lib\net462\System.Threading.Channels.dll


packages\System.Threading.Tasks.Extensions.4.6.0\lib\net462\System.Threading.Tasks.Extensions.dll


packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll











packages\TensorFlow.NET.0.150.0\lib\netstandard2.0\Tensorflow.Binding.dll


packages\TorchSharp.0.105.0\lib\netstandard2.0\TorchSharp.dll




Form


Form1.cs




Form1.cs


ResXFileCodeGenerator
Resources.Designer.cs
Designer


True
Resources.resx




SettingsSingleFileGenerator
Settings.Designer.cs


True
Settings.settings
True







MLModel1.mbconfig


MLModel1.mbconfig


MLModel1.mbconfig
PreserveNewest





Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их.  Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105.  Отсутствует следующий файл: {0}.















Alles, was ich brauche, ist ein Code für Form.cs, der mein maschinelles Lernmodell integriert und das durchsuchte Foto per Schaltfläche analysiert und nach der Analyse den Namen des Objekts im Foto in das Textfeld schreibt

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post