ivilson / Yolov7net

Yolo Detector for .Net 8
83 stars 25 forks source link

inconsistent results #8

Closed celadaris closed 1 year ago

celadaris commented 1 year ago

ive had great results on both yolov7net and ONNX-YOLOv7-Object-Detection using models from here but...

i trained a custom model and if i use it in ONNX-YOLOv7-Object-Detection i get results, however if i use Yolov7net i got no results. Im still very new at training custom models but when im seeing results on one library and not on another it makes me question on whether im doing something wrong or if theres something wrong with yolov7net...

Note:

  1. The same image was put into both programs, but they got resized after becuase i had to screen capture them to post here.
  2. the image from ONNX-YOLOv7-Object-Detection says "person" because I didn't know how to change it (I don't really know python).
  3. also when i mean inconsistent i mean that i dont get any results from yolov7net, (multiple images/videos tested)

results from using ONNX-YOLOv7-Object-Detection: image

results from using Yolov7net: image

code from yolov7net:

using System.Drawing;
using Yolov7net;
using OpenCvSharp;
using OpenCvSharp.Extensions;

// init Yolov7 with onnx (include nms results)file path
using var yolo = new Yolov7("C:\\Users\\imnot\\Downloads\\yolov7-main\\yolov7-main\\runs\\train\\yolov7-custom2\\weights\\best.onnx", true);
// setup labels of onnx model 
yolo.SetupYoloDefaultLabels();
using var image = (Bitmap)Image.FromFile("C:\\Users\\imnot\\Desktop\\Test.jpg");
var predictions = yolo.Predict(image, 0.01f, 0.5f);

// draw box
Mat img = BitmapConverter.ToMat(image);

foreach (var prediction in predictions) // iterate predictions to draw results
{
    int x = (int)prediction.Rectangle.X;
    int y = (int)prediction.Rectangle.Y;
    int width = (int)prediction.Rectangle.Width;
    int height = (int)prediction.Rectangle.Height;

    Cv2.Rectangle(img, new OpenCvSharp.Rect(x, y, width, height), new Scalar(0, 255, 0), 3);
    Cv2.Rectangle(img, new OpenCvSharp.Rect(x, y - 42, 90, 40), new Scalar(0, 0, 0), -1);
    Cv2.PutText(img, prediction.Label.Name, new OpenCvSharp.Point(x, y - 10), HersheyFonts.HersheySimplex, 1.5, new Scalar(36, 255, 12), 4);

    Console.WriteLine(prediction.Label.Name);
}

var newImg = BitmapConverter.ToBitmap(img);
newImg.Save("C:\\Users\\imnot\\Desktop\\newimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

code from ONNX-YOLOv7-Object-Detection:

import cv2
from imread_from_url import imread_from_url

from yolov7 import YOLOv7

# Initialize yolov7 object detector
model_path = "C:/Users/imnot/Downloads/yolov7-main/yolov7-main/runs/train/yolov7-custom2/weights/best.onnx"
yolov7_detector = YOLOv7(model_path, conf_thres=0.02, iou_thres=0.3)

# Read image
img_url = "https://i.imgur.com/P8MBg2M.jpg"
img = imread_from_url(img_url)

# Detect Objects
boxes, scores, class_ids = yolov7_detector(img)

# Draw detections
combined_img = yolov7_detector.draw_detections(img)
cv2.namedWindow("Detected Objects", cv2.WINDOW_NORMAL)
cv2.imshow("Detected Objects", combined_img)
cv2.imwrite("doc/img/detected_objects.jpg", combined_img)
cv2.waitKey(0)
celadaris commented 1 year ago

the results above appear when trying to export yolov7 without including --end2end for example:

python export.py --weights "C:\Users\imnot\Downloads\yolov7-main\yolov7-main\runs\train\yolov7-custom2\weights\best.pt" --grid --simplify --topk-all 100 --img-size 640 640 --max-wh 640

but if i include --end2end like so:

python export.py --weights "C:\Users\imnot\Downloads\yolov7-main\yolov7-main\runs\train\yolov7-custom2\weights\best.pt" --grid --end2end --simplify --topk-all 100 --img-size 640 640 --max-wh 640

then I get other problems, when using ONNX-YOLOv7-Object-Detection i get no inferences on the object (pokeball)

and in yolov7net i get the following error: image

so im really confused on if im supposed to use --end2end when exporting to .onnx because with or without i run into problems.

celadaris commented 1 year ago

while im still a little confused on the inconsistency, after downloading a pre-made dataset from roboflow and getting it to work, i think my problems have to do with either my webcam quality, my sample size, or both.