Yolov8 output difference in Flutter and Python

55 views Asked by At

I cannot replicate the Yolov8 results in python in flutter call on the same image.

I trained my model using Custom dataset. !yolo task=detect mode=train model=yolov8s.pt data={dataset.location}/data.yaml epochs=100 imgsz=640 plots=True

Exported myModel and running inference gives: Python Output

After loading the model in flutter and running inference: Flutter Output

In my understanding, I need to apply the preprocessing and postprocessing steps in the flutter myself. Kindly help me identify all the steps. Preprocessing: Resize: 640*640

Postprocessing: Resize: (1 * 42000) -> (1 * 5 * 8400) 5 -> (center_x, center_y, width, height, confidence) Apply Non-Maximum Suppression.

Do I need to scale my Pixel Values? Scaling: Pixel values(0-255) ->(0-1) Does the Yolov8 models automatically scales the pixels to (0-1) while training? Is there any other step that is missing from my pipeline?

Preprocessing:

ImageProcessor imageProcessor = ImageProcessorBuilder().
                                                add(ResizeOp(640, 640,ResizeMethod.NEAREST_NEIGHBOUR)).
                                                add(DequantizeOp(0, 1.0/255.0)).build();

Postprocessing:

final out = outputBuffer1.getDoubleList();

              final detections = out;
              var threshold = 0.7;
              const width = 640;
              const height = 640;

              List results = [];
              for (int i = 0; i < detections.length / 5; i++) {
                double confidence = detections[i * 5 + 4];
                if (confidence > threshold) {
                  var x = detections[i * 5] * width;
                  var y = detections[i * 5 + 1] * height;
                  var w = detections[i * 5 + 2] * width;
                  var h = detections[i * 5 + 3] * height;
                  results.add([x, y, w, h, confidence]);
                }
              }

              for(var result in results) {
                result[0] = result[0] - result[2]/2;
                result[1] = result[1] - result[3]/2;
                result[2] = result[0] + result[2]/2;
                result[3] = result[1] + result[3]/2;
              }

              List FinalDetections = [];
              results.sort((a, b) => a[4].compareTo(b[4]));
              results = results.reversed.toList();

              int ii = 1;
              while (results.isNotEmpty){
                debugPrint("Processing $ii");
                List overlap = [];
                var box_top = results.removeAt(0);
                double boxx = box_top[0];
                double boxy = box_top[1];
                double boxw = box_top[2];
                double boxh = box_top[3];
                double box_conf = box_top[4];

                FinalDetections.add([boxx, boxy, boxw, boxh, box_conf]);

                for (var (idx, result) in results.indexed){
                  double min_1 = min(boxw, result[2] as double);
                  double max_1 = max(boxx, result[0] as double);

                  double min_2 = min(boxh, result[3] as double);
                  double max_2 = max(boxy, result[1] as double);

                  double intersection = max(0, min_1 - max_1) * max(0, min_2 - max_2);
                  double union = (boxw - boxx) * (boxh - boxy) + (result[2] - result[0]) * (result[3] - result[1]) - intersection;
                  double iou = intersection / union ;

                  if (iou > 0.40){
                    overlap.add(idx);
                  }
                }
                overlap.sort((a, b) => b.compareTo(a));
                for(int i in overlap){
                  results.removeAt(i);
                }
                ii += 1;
              }
0

There are 0 answers