Skip to content

Commit

Permalink
remove useless feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayzwer committed Feb 28, 2024
1 parent 4d36be7 commit c595948
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 69 deletions.
4 changes: 2 additions & 2 deletions OBB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public OBB(string model_path, bool use_cuda)
}
}

public List<OBBPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf, float iou_conf)
public List<OBBPrediction> Predict(Bitmap image, float conf, float iou_conf)
{
float x_scaler = image.Width * Imgsz_inv;
float y_scaler = image.Height * Imgsz_inv;
Expand All @@ -86,7 +86,7 @@ public List<OBBPrediction> Predict(Bitmap image, Dictionary<string, float> class
}
}
}
if (max_score > conf)
if (max_score >= conf)
{
YoloLabel label = new(max_score_idx, Labels.ElementAt(max_score_idx).Key, Labels.ElementAt(max_score_idx).Value);
RectangleF rectangle = new((output.ElementAt(col_len_cache[0] + j) - output.ElementAt(col_len_cache[2] + j) * .5f) * x_scaler,
Expand Down
17 changes: 6 additions & 11 deletions RTDETR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace YOLO
{
public class RTDETR : Yolo
public class RTDETR
{
InferenceSession InferenceSession { get; set; }
string[] OutputData { get; set; }
Expand Down Expand Up @@ -53,20 +53,20 @@ public RTDETR(string model_path, bool use_cuda)
InferenceSession.Run(namedOnnxValues, OutputData);
}

public override void SetupLabels(Dictionary<string, Color> labels)
public void SetupLabels(Dictionary<string, Color> labels)
{
Labels = labels;
}

public override List<YoloPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf, float iou_conf)
public List<YoloPrediction> Predict(Bitmap image, float conf, float iou_conf)
{
ResizeImage(image);
namedOnnxValues[0] = NamedOnnxValue.CreateFromTensor("images", Utils.ExtractPixels2(resized_img));
return Suppress(GetBboxes_n_Scores(InferenceSession.Run(namedOnnxValues, OutputData).ElementAt(0).AsTensor<float>(),
conf, class_conf, image.Width, image.Height), iou_conf);
conf, image.Width, image.Height), iou_conf);
}

public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf, Dictionary<string, float> class_conf, int image_width, int image_height)
public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf, int image_width, int image_height)
{
List<YoloPrediction> predictions = [];
Parallel.For(0, MAX_POSSIBLE_OBJECT, j =>
Expand All @@ -87,7 +87,7 @@ public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf,
}
}
}
if (max_score >= conf && max_score >= class_conf.ElementAt(max_score_idx).Value)
if (max_score >= conf)
{
predictions.Add(
new(
Expand Down Expand Up @@ -130,10 +130,5 @@ private List<YoloPrediction> Suppress(List<YoloPrediction> items, float iou_conf
}
return result;
}

public override int GetModelNClass()
{
return N_CLASS;
}
}
}
14 changes: 0 additions & 14 deletions Yolo.cs

This file was deleted.

11 changes: 3 additions & 8 deletions Yolov5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace YOLO
/// <summary>
/// yolov5、yolov6 模型,不包含nms结果
/// </summary>
public class Yolov5 : Yolo, IDisposable
public class Yolov5 : IDisposable
{
private readonly InferenceSession _inferenceSession;
private readonly YoloModel _model = new();
Expand Down Expand Up @@ -50,7 +50,7 @@ public Yolov5(string modelPath, bool useCuda = false)
_inferenceSession.Run(inputs, _model.Outputs);
}

public override void SetupLabels(Dictionary<string, Color> color_mapper)
public void SetupLabels(Dictionary<string, Color> color_mapper)
{
int i = 0;
foreach (KeyValuePair<string, Color> keyValuePair in color_mapper)
Expand All @@ -60,7 +60,7 @@ public override void SetupLabels(Dictionary<string, Color> color_mapper)
}
}

public override List<YoloPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf_thres = 0, float iou_thres = 0)
public List<YoloPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf_thres = 0, float iou_thres = 0)
{

using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> outputs = Inference(image);
Expand Down Expand Up @@ -220,10 +220,5 @@ public void Dispose()
{
_inferenceSession.Dispose();
}

public override int GetModelNClass()
{
return N_Class;
}
}
}
17 changes: 6 additions & 11 deletions Yolov7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace YOLO
{
public class Yolov7 : Yolo, IDisposable
public class Yolov7 : IDisposable
{
private readonly InferenceSession _inferenceSession;
private readonly YoloModel _model = new();
Expand Down Expand Up @@ -45,7 +45,7 @@ public Yolov7(string modelPath, bool useCuda = false)
_inferenceSession.Run(inputs, _model.Outputs);
}

public override void SetupLabels(Dictionary<string, Color> color_mapper)
public void SetupLabels(Dictionary<string, Color> color_mapper)
{
int i = 0;
foreach (KeyValuePair<string, Color> keyValuePair in color_mapper)
Expand All @@ -55,7 +55,7 @@ public override void SetupLabels(Dictionary<string, Color> color_mapper)
}
}

private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image, Dictionary<string, float> class_conf, float conf)
private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image, float conf)
{
ConcurrentBag<YoloPrediction> result = [];
var (w, h) = (image.Width, image.Height); // image w and h
Expand All @@ -68,7 +68,7 @@ private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image,
{
Span<float> span = output.Buffer.Span[(i * output.Strides[0])..];
YoloLabel label = _model.Labels[(int)span[5]];
if (span[6] >= class_conf[label.Name] && span[6] >= conf)
if (span[6] >= conf)
{
float xMin = (span[1] - xPad) * gain_inv;
float yMin = (span[2] - yPad) * gain_inv;
Expand Down Expand Up @@ -128,17 +128,12 @@ public void Dispose()
_inferenceSession.Dispose();
}

public override List<YoloPrediction> Predict(Bitmap img, Dictionary<string, float> class_conf, float conf_thres = 0, float iou_thres = 0)
public List<YoloPrediction> Predict(Bitmap img, Dictionary<string, float> class_conf, float conf_thres = 0, float iou_thres = 0)
{
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> outputs = Inference(img);
string firstOutput = _model.Outputs[0];
DenseTensor<float> output = (DenseTensor<float>)outputs.First(x => x.Name == firstOutput).Value;
return Suppress(ParseDetect(output, img, class_conf, conf_thres), iou_thres);
}

public override int GetModelNClass()
{
return N_Class;
return Suppress(ParseDetect(output, img, conf_thres), iou_thres);
}
}
}
21 changes: 8 additions & 13 deletions Yolov8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace YOLO
{
public class Yolov8 : Yolo, IDisposable
public class Yolov8 : IDisposable
{
private readonly InferenceSession _inferenceSession;
private readonly YoloModel _model = new();
Expand Down Expand Up @@ -45,7 +45,7 @@ public Yolov8(string modelPath, bool useCuda = false)
_inferenceSession.Run(inputs, _model.Outputs);
}

public override void SetupLabels(Dictionary<string, Color> color_mapper)
public void SetupLabels(Dictionary<string, Color> color_mapper)
{
int i = 0;
foreach (KeyValuePair<string, Color> keyValuePair in color_mapper)
Expand All @@ -55,10 +55,10 @@ public override void SetupLabels(Dictionary<string, Color> color_mapper)
}
}

public override List<YoloPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf_thres = 0, float iou_thres = 0)
public List<YoloPrediction> Predict(Bitmap image, float conf_thres = 0, float iou_thres = 0)
{
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> outputs = Inference(image);
return Suppress(ParseOutput(outputs, image, class_conf, conf_thres), iou_thres);
return Suppress(ParseOutput(outputs, image, conf_thres), iou_thres);
}

/// <summary>
Expand Down Expand Up @@ -137,14 +137,14 @@ private IDisposableReadOnlyCollection<DisposableNamedOnnxValue> Inference(Image
return _inferenceSession.Run(inputs, _model.Outputs);
}

private List<YoloPrediction> ParseOutput(IDisposableReadOnlyCollection<DisposableNamedOnnxValue> outputs, Image image, Dictionary<string, float> class_conf, float conf)
private List<YoloPrediction> ParseOutput(IDisposableReadOnlyCollection<DisposableNamedOnnxValue> outputs, Image image, float conf)
{
string firstOutput = _model.Outputs[0];
DenseTensor<float> output = (DenseTensor<float>)outputs.First(x => x.Name == firstOutput).Value;
return ParseDetect(output, image, class_conf, conf);
return ParseDetect(output, image, conf);
}

private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image, Dictionary<string, float> class_conf, float conf)
private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image, float conf)
{
ConcurrentBag<YoloPrediction> result = new();

Expand Down Expand Up @@ -174,7 +174,7 @@ private List<YoloPrediction> ParseDetect(DenseTensor<float> output, Image image,
{
float pred = span[(4 + l) * dim + j];

if (!(pred < conf || pred < class_conf[_model.Labels[l].Name]))
if (pred >= conf)
{
result.Add(new(_model.Labels[l], new(x_min * gain_inv, y_min * gain_inv, width, height), pred));
}
Expand All @@ -200,10 +200,5 @@ public void Dispose()
{
_inferenceSession.Dispose();
}

public override int GetModelNClass()
{
return N_Class;
}
}
}
15 changes: 5 additions & 10 deletions Yolov9.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public Yolov9(string model_path, bool use_cuda)
}
}

public override void SetupLabels(Dictionary<string, Color> labels)
public void SetupLabels(Dictionary<string, Color> labels)
{
Labels = labels;
}

public override List<YoloPrediction> Predict(Bitmap image, Dictionary<string, float> class_conf, float conf, float iou_conf)
public List<YoloPrediction> Predict(Bitmap image, float conf, float iou_conf)
{
ResizeImage(image);
namedOnnxValues[0] = NamedOnnxValue.CreateFromTensor("images", Utils.ExtractPixels2(resized_img));
return Suppress(GetBboxes_n_Scores(InferenceSession.Run(namedOnnxValues, OutputData).ElementAt(0).AsTensor<float>(),
conf, class_conf, image.Width, image.Height), iou_conf);
conf, image.Width, image.Height), iou_conf);
}

public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf, Dictionary<string, float> class_conf, int image_width, int image_height)
public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf, int image_width, int image_height)
{
List<YoloPrediction> predictions = [];
float width_scale = image_width * Imgsz_inv;
Expand All @@ -96,7 +96,7 @@ public List<YoloPrediction> GetBboxes_n_Scores(Tensor<float> input, float conf,
}
}
}
if (max_score >= conf && max_score >= class_conf.ElementAt(max_score_idx).Value)
if (max_score >= conf)
{
predictions.Add(
new(
Expand Down Expand Up @@ -139,10 +139,5 @@ private List<YoloPrediction> Suppress(List<YoloPrediction> items, float iou_conf
}
return result;
}

public override int GetModelNClass()
{
return N_CLASS;
}
}
}

0 comments on commit c595948

Please sign in to comment.