Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YoloModel - Custom Model #11

Closed
alijundi opened this issue Sep 5, 2021 · 22 comments
Closed

YoloModel - Custom Model #11

alijundi opened this issue Sep 5, 2021 · 22 comments

Comments

@alijundi
Copy link

alijundi commented Sep 5, 2021

Hi @kkarahainko @keesschollaart81

I added my custom model and exported my .onnx from colab I ran the project after making my custom model.

I get System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
in function ParseDetect. I am not sure what to change in my custom model class to avoid this!

By the way. The code works well on the models and test image provided.

@kkarahainko
Copy link
Contributor

@alijundi please provide a screenshot from Netron (like this https://i.imgur.com/2X2H2Co.png) and your custom model class (code) with parameters.

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko

Thank you for your prompt answer. Below is the code:

`using System.Collections.Generic;
using Yolov5Net.Scorer.Models.Abstract;

namespace Yolov5Net.Scorer.Models
{
public class YoloCocoP7Model : YoloModel
{
public override int Width { get; set; } = 416;
public override int Height { get; set; } = 416;
public override int Depth { get; set; } = 3;

    public override int Dimensions { get; set; } = 85;

    public override float[] Strides { get; set; } = new float[] { 8, 16, 32, 64 };

    public override float[][][] Anchors { get; set; } = new float[][][]
    {
        new float[][] { new float[] { 019, 027 }, new float[] { 044, 040 }, new float[] { 038, 094 } },
        new float[][] { new float[] { 096, 068 }, new float[] { 086, 152 }, new float[] { 180, 137 } },
        new float[][] { new float[] { 140, 301 }, new float[] { 303, 264 }, new float[] { 238, 542 } },
        new float[][] { new float[] { 436, 615 }, new float[] { 739, 380 }, new float[] { 925, 792 } }
    };

    public override int[] Shapes { get; set; } = new int[] { 20, 40, 80, 160 };

    public override float Confidence { get; set; } = 0.20f;
    public override float MulConfidence { get; set; } = 0.25f;
    public override float Overlap { get; set; } = 0.45f;

    public override string[] Outputs { get; set; } = new[] { "output" };

    public override List<YoloLabel> Labels { get; set; } = new List<YoloLabel>()
    {
        new YoloLabel { Id = 15, Name = "FEEDER_MCCB_3P_100A" },
        new YoloLabel { Id = 16, Name = "FEEDER_MCCB_3P_40A" },
        new YoloLabel { Id = 17, Name = "INCOMER_MCCB_3P_160A" },
    };

    public override bool UseDetect { get; set; } = true;

    public YoloCocoP7Model()
    {

    }
}

}
`
.onnx file download link: https://drive.google.com/file/d/1-4TR22metrnzV9JXHuzstrKLHf5L7v7M/view?usp=sharing
Netron image file: https://drive.google.com/file/d/1PzZvqlOaYoZjxoKSHOmy-S-YWEiffi45/view?usp=sharing

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

using System.Collections.Generic;
using Yolov5Net.Scorer.Models.Abstract;

namespace Yolov5Net.Scorer.Models
{
    public class YoloCocoP7Model : YoloModel
    {
        public override int Width { get; set; } = 416;
        public override int Height { get; set; } = 416;
        public override int Depth { get; set; } = 3;

        public override int Dimensions { get; set; } = 85;

        public override float[] Strides { get; set; } = new float[] { 8, 16, 32, 64 };

        public override float[][][] Anchors { get; set; } = new float[][][]
        {
            new float[][] { new float[] { 019, 027 }, new float[] { 044, 040 }, new float[] { 038, 094 } },
            new float[][] { new float[] { 096, 068 }, new float[] { 086, 152 }, new float[] { 180, 137 } },
            new float[][] { new float[] { 140, 301 }, new float[] { 303, 264 }, new float[] { 238, 542 } },
            new float[][] { new float[] { 436, 615 }, new float[] { 739, 380 }, new float[] { 925, 792 } }
        };

        public override int[] Shapes { get; set; } = new int[] { 20, 40, 80, 160 };

        public override float Confidence { get; set; } = 0.20f;
        public override float MulConfidence { get; set; } = 0.25f;
        public override float Overlap { get; set; } = 0.45f;

        public override string[] Outputs { get; set; } = new[] { "output" };

        public override List<YoloLabel> Labels { get; set; } = new List<YoloLabel>()
        {
            new YoloLabel { Id = 15, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 16, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 17, Name = "INCOMER_MCCB_3P_160A" },
        };

        public override bool UseDetect { get; set; } = true;

        public YoloCocoP7Model()
        {

        }
    }
}

@kkarahainko
Copy link
Contributor

@alijundi

  1. you have no Detect layer exported (detect layer looks like this https://i.imgur.com/FgjTDTQ.png)
  2. wrong dimensions, should be 36 instead of 85, wrong shapes, should be { 13, 26, 52 } instead of { 20, 40, 80 };
  3. looks like your model trained on yolov5 P5 version but you use P6 code as a reference, use YoloCocoP5Model example as a reference;
  4. you have 31 labels but I see only 3 labels in your model class, all labels should be described;

my recommendation - clone and use the latest ultralytics/yolov5 version in collab, it exports Detect layer by default, I'm not sure you've exported your model right.

so, your model class should look like this:

public class MyCustomModel : YoloModel
{
	public override int Width { get; set; } = 416;
	public override int Height { get; set; } = 416;
	public override int Depth { get; set; } = 3;

	public override int Dimensions { get; set; } = 36;

	public override float[] Strides { get; set; } = new float[] { 8, 16, 32 };

	public override float[][][] Anchors { get; set; } = new float[][][]
	{
		new float[][] { new float[] { 010, 13 }, new float[] { 016, 030 }, new float[] { 033, 023 } },
		new float[][] { new float[] { 030, 61 }, new float[] { 062, 045 }, new float[] { 059, 119 } },
		new float[][] { new float[] { 116, 90 }, new float[] { 156, 198 }, new float[] { 373, 326 } }
	};

	public override int[] Shapes { get; set; } = new int[] { 13, 26, 52 };

	public override float Confidence { get; set; } = 0.20f;
	public override float MulConfidence { get; set; } = 0.25f;
	public override float Overlap { get; set; } = 0.45f;

	public override string[] Outputs { get; set; } = new[] { "output", "465", "485" };

	public override List<YoloLabel> Labels { get; set; } = new List<YoloLabel>()
	{
		// all 31 lables described here
	};

	public override bool UseDetect { get; set; } = false;

	public YoloCocoP5Model()
	{

	}
}

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko Thank you for your feedback:

I updated the code to the below:

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

using System.Collections.Generic;
using Yolov5Net.Scorer.Models.Abstract;

namespace Yolov5Net.Scorer.Models
{
    public class YoloCocoP7Model : YoloModel
    {
        public override int Width { get; set; } = 416;
        public override int Height { get; set; } = 416;
        public override int Depth { get; set; } = 3;

        public override int Dimensions { get; set; } = 36;

        public override float[] Strides { get; set; } = new float[] { 8, 16, 32 };

        public override float[][][] Anchors { get; set; } = new float[][][]
        {
            new float[][] { new float[] { 010, 13 }, new float[] { 016, 030 }, new float[] { 033, 023 } },
            new float[][] { new float[] { 030, 61 }, new float[] { 062, 045 }, new float[] { 059, 119 } },
            new float[][] { new float[] { 116, 90 }, new float[] { 156, 198 }, new float[] { 373, 326 } }
        };

        public override int[] Shapes { get; set; } = new int[] { 13, 26, 52 };

        public override float Confidence { get; set; } = 0.20f;
        public override float MulConfidence { get; set; } = 0.25f;
        public override float Overlap { get; set; } = 0.45f;

        public override string[] Outputs { get; set; } = new[] { "output", "465", "485" };

        public override List<YoloLabel> Labels { get; set; } = new List<YoloLabel>()
        {
            new YoloLabel { Id = 0, Name = "DOG" },
            new YoloLabel { Id = 1, Name = "DOG" },
            new YoloLabel { Id = 2, Name = "DOG" },
            new YoloLabel { Id = 3, Name = "DOG" },
            new YoloLabel { Id = 4, Name = "DOG" },
            new YoloLabel { Id = 5, Name = "DOG" },
            new YoloLabel { Id = 6, Name = "DOG" },
            new YoloLabel { Id = 7, Name = "DOG" },
            new YoloLabel { Id = 8, Name = "DOG" },
            new YoloLabel { Id = 9, Name = "DOG" },
            new YoloLabel { Id = 10, Name = "DOG" },
            new YoloLabel { Id = 11, Name = "DOG" },
            new YoloLabel { Id = 12, Name = "DOG" },
            new YoloLabel { Id = 13, Name = "DOG" },
            new YoloLabel { Id = 14, Name = "DOG" },
            new YoloLabel { Id = 15, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 16, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 17, Name = "INCOMER_MCCB_3P_160A" },
            new YoloLabel { Id = 18, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 19, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 20, Name = "INCOMER_MCCB_3P_160A" },
            new YoloLabel { Id = 21, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 22, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 23, Name = "INCOMER_MCCB_3P_160A" },
            new YoloLabel { Id = 24, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 25, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 26, Name = "INCOMER_MCCB_3P_160A" },
            new YoloLabel { Id = 27, Name = "FEEDER_MCCB_3P_100A" },
            new YoloLabel { Id = 28, Name = "FEEDER_MCCB_3P_40A" },
            new YoloLabel { Id = 29, Name = "INCOMER_MCCB_3P_160A" },
            new YoloLabel { Id = 30, Name = "FEEDER_MCCB_3P_100A" }
        };

        public override bool UseDetect { get; set; } = false;

        public YoloCocoP7Model()
        {

        }
    }
}

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

VS-Debug-Yolov5

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko I am getting the above exception now. Am I doings something wrong here?

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

I used the below code in colab to export .pt trained model to .onnx

!python /content/yolov5/models/export.py --weights /content/yolov5/runs/train/yolov5s_results/weights/best.pt --img 416 --batch 1 # export

Any thoughts?

@kkarahainko
Copy link
Contributor

@alijundi pls, change Shapes order "public override int[] Shapes { get; set; } = new int[] { 52, 26, 13 };"

@kkarahainko
Copy link
Contributor

@alijundi can you provide a test image?

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko lemme try it now...

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko result is being outputed with no predictions.. INPUT == OUTPUT

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

test3

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

above is test input

@kkarahainko
Copy link
Contributor

@alijundi is yolov5/detect.py detects something? did you try?

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

2021-09-05 17_38_58-Copy of Roboflow-Custom-YOLOv5 - Colaboratory

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

The output is from Roboflow-Custom-YOLOv5 that is what I used to train my model

@kkarahainko
Copy link
Contributor

@alijundi I mean did you try detect.py on the test image you've sent to me?

@kkarahainko
Copy link
Contributor

@alijundi I suppose it doesn't detect too?

@alijundi
Copy link
Author

alijundi commented Sep 5, 2021

@kkarahainko I am sorry I took sometime to respond to you. I was actually on the way home.

and YES! I tried all images that make up the model with no success. It is not detecting anything!

Do you know why?

@kkarahainko
Copy link
Contributor

@alijundi it's all about pixels, the test image is b/w no labels etc, training image is colored with labels, you should train on the similar images you will use further (to predict), or not enough epochs, low mAP etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants