mteb/FreshStackRetrieval huggingface dataset:
def convert_coco_to_yolov8(coco_file):

    with open(coco_file) as f:
        coco = json.load(f)


    images = coco['images']
    annotations = coco['annotations'] 
    categories = {cat['id']: cat['name'] for cat in coco['categories']}

    
    os.makedirs('labels', exist_ok=True)

 
    for image in tqdm(images, desc='Converting images'):
        image_id = image['id']
        filename = image['file_name']



        label_filename = filename.split('.png')[0]
        label_path = os.path.join('labels', f'{label_filename}.txt')
        with open(label_path, 'w') as f:

            for ann in annotations:
                if ann['image_id'] != image_id:
                    continue

                img_width = image['width']
                img_height = image['height']

                xmin, ymin, width, height = ann['bbox']
              
                xmax = xmin + width
                ymax = ymin + height
                xcen = (xmin + xmax) / 2
                ycen = (ymin + ymax) / 2
                # xcen = (xmin + xmax) / 2 / img_width
                # ycen = (ymin + ymax) / 2 / img_height
                w = xmax - xmin
                h = ymax - ymin
                label = categories[ann['category_id']]
                label_id = ann['category_id']
             
            

                segmentation_points_list = []
                for segmentation in ann['segmentation']:
                    segmentation_points = [str(point / img_width) for point in segmentation]
                    segmentation_points_list.append(' '.join(segmentation_points))
                segmentation_points_string = ' '.join(segmentation_points_list)


                                

             
                line = '{} {} {} {} {} {}\n'.format(label_id, xcen / img_width, ycen / img_height, w / img_width, h / img_height, segmentation_points_string )
                f.write(line)

the script is getting the labels but when i train for YOLOv8 the labels are seems wrong ,I need to convert a coco json to YOLOV8 txt file . label should contain segmentation also. Note my JSON file have different image size for all images
I'm training YOLOv8 in Colab on a custom dataset. How can I save the model after some epochs and continue the training later. I did the first epoch like this:
import torch

model = YOLO("yolov8x.pt")
model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)

While looking for the options it seems that with YOLOv5 it would be possible to save the model or the weights dict. I tried these but either the save or load doesn't seem to work in this case:
torch.save(model, 'yolov8_model.pt')
torch.save(model.state_dict(), 'yolov8x_model_state.pt')
I have a laptop with following configurations
 Processor : AMD Ryzen 7 4800H with Radeon Graphics            2.90 GHz
 Installed RAM : 16.0 GB (15.4 GB usable)
 Windows Edition : Windows 11 Home Single Language
 Version : 22H2
 OS : 22621.1555
 NVIDIA GTX GEFORCE 1650 GRAPHICS CARD
 NVIDIA DRIVER : 31.0.15.3161

Its a brand new laptop
With following Python and CUDA installed:
Python 3.10.11

---------------------------------------------------------------------------------------+
| NVIDIA-SMI 531.61                 Driver Version: 531.61       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                      TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf            Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce GTX 1650       WDDM | 00000000:01:00.0 Off |                  N/A |
| N/A   44C    P0               15W /  N/A|      0MiB /  4096MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|  No running processes found                                                           |
+---------------------------------------------------------------------------------------+

But whenever I try to run my YOLOV8 model for object detection on this, it shuts down during 1st epoch only. Not sure why its happening. Any help is highly appreciated.
My Python  code
import tensorflow as tf
from ultralytics import YOLO
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(tf.test.is_built_with_cuda())
print(tf.config.list_physical_devices('GPU'))

# Create a TensorFlow session with GPU growth enabled
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(tf.test.is_built_with_cuda())
# Run your code in the session
with sess.as_default():
   # Load the model.
   model = YOLO('yolov8n.pt')
   
   # Training.
   results = model.train(
      data='data.yaml',
      imgsz=640,
      epochs=5,
      batch=8,
      name='yolov8n_custom')

PS I would also like to know what are the hardware requirements for YOLOV8
I'm currently working in a project in which I'm using Flask and Yolov8 together.
When I run this code
from ultralytics import YOLO

model = YOLO("./yolov8n.pt")

results = model.predict(source="../TEST/doggy.jpg", save=True, save_txt=True)

the output will be saved in this default directory /run/detect/
like
Ultralytics YOLOv8.0.9  Python-3.10.8 torch-2.0.0+cpu CPU
Fusing layers... 
YOLOv8n summary: 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs
Results saved to d:\runs\detect\predict4
1 labels saved to d:\runs\detect\predict4\labels

and what I want is the predict directory number or the entire directory path in a variable.
I tried capturing the path using sys.stdout methods but i want a direct solution.
I want to segment an image using yolo8 and then create a mask for all objects in the image with specific class.
I have developed this code:
img=cv2.imread('images/bus.jpg')
model = YOLO('yolov8m-seg.pt')
results = model.predict(source=img.copy(), save=False, save_txt=False)
class_ids = np.array(results[0].boxes.cls.cpu(), dtype="int")
for i in range(len(class_ids)):
    if class_ids[i]==0:
         empty_image = np.zeros((height, width,3), dtype=np.uint8)
         res_plotted = results[0][i].plot(boxes=0, img=empty_image)


In the above code, res_plotted is the mask for one object, in RGB. I want to add all of these images to each other and create a mask for all objects with class 0 (it is a pedestrian in this example)
My questions:

How can I complete this code?
Is there any better way to achieve this without having a loop?
Is there any utility function in the yolo8 library to do this?
I have this output that was generated by model.predict()
0: 480x640 1 Hole, 234.1ms
Speed: 3.0ms preprocess, 234.1ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)

0: 480x640 1 Hole, 193.6ms
Speed: 3.0ms preprocess, 193.6ms inference, 3.5ms postprocess per image at shape (1, 3, 640, 640)

...

How do I hide the output from terminal?
I can't find out the information in this official link
https://docs.ultralytics.com/modes/predict/#arguments
I am trying to draw a segmentation mask from a YOLO segmentation mask dataset. The annotation line I am reading looks like this:
36 0.6158357764423077 0.814453125 0.6158357764423077 0.8095703125 0.6070381225961539 0.8095703125 0.6041055721153846 0.8115234375 0.5894428149038462 0.8154296875 0.5747800576923077 0.8125 0.5513196490384615 0.8134765625 0.5483870961538462 0.81640625 0.5923753653846154 0.818359375 0.6158357764423077 0.814453125
I am using cv2.polylines to draw the shape but am getting an error:
image_height, image_width, c = img.shape
isClosed = True
color = (255, 0, 0)
thickness = 2

with open(annotation_file) as f:
    for line in f:
        split_line = line.split()
        class_id = split_line[0]
        mask_shape = [float(numeric_string) for numeric_string in split_line[1:len(split_line)]]
        mask_points = []
        for i in range(0,len(mask_shape),2):
            x,y = mask_shape[i:i+2]
            mask_points.append((x * image_width, y * image_height))
        points = np.array([mask_points])
        image = cv2.polylines(img, points,
                              isClosed, color, thickness)
        break

Error:
OpenCV(4.7.0) /Users/xperience/GHA-OCV-Python/_work/opencv-python/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2434: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'
getting IndexError: index 1 is out of bounds for dimension 1 with size 1  while training yolo v8
I am trying defect detection with yolov8 and i am expecting a detection of those defect on full scale image
Input is 416 * 416 image
using  Python 3.8.6,windows11 pro
data.yaml file:
path: /Users/pksha/yolo
train: train/images
val: val/images
nc: 0
names: ['particle']
**python code:
pip install ultralytics
from ultralytics import YOLO
# LOAD model
 model=YOLO("yolov8l.yaml")
 results=model.train(data="data.yaml",epochs=1)

I have tried both increasing and decresing to check if it works but both showed error.
I have a YOLOv8 object detection model trained on custom. It takes image as input and annotates the different objects my question is How do I get coordinates of different objects? I want these coordinate data to further crop the images.
If the object detected is a person I want coordinates of that same for cat and dog.
Thanks for help in advance.
My code looks something like this
infer = YOLO(r'path_to_trained_model.pt')
image_path = r'image'
results = infer(image_path)

class_name = "Person"  
confidence_threshold = 0.5 

for r in results:
    r.boxes.xyxy ## I was also getting this error 'list' object has no attribute 'xyxy'


I know I am missing quite a bit of data here since I have no idea how it works.
I want to train the YOLO v8 in transfer learning on my custom dataset.
I have different classes than the base training on the COCO dataset.
Yet I don't want to learn again the feature extraction.
Hence I though following the Ultralytics YOLOv8 Docs - Train.
Yet, When I train on my small dataset I want to freeze the backbone.
How can I do that?
I looked at the documentation and couldn't find how to do so.
I am working on an Android app where I am already using OpenCV, I got a model which is in onnx format from YOLOv8 after conversion. Here is the output metadata of it.

name - output0
type - float32[1,5,8400]

So far I am successfully running the model but in the end, the output that I got I can't comprehend.
This is the print statement from the output
Mat [ 1* 5* 8400*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x72345b4840, dataAddr=0x723076b000 ]
class Detector(private val context: Context) {
    private var net: Net? = null

    fun detect(frame: Bitmap) {
        // preprocess image
        val mat = Mat()
        Utils.bitmapToMat(resizedBitmap, mat)
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGBA2RGB)
        val inputBlob = Dnn.blobFromImage(mat, 1.0/255.0, Size(640.0, 640.0), Scalar(0.0), true, false)
        net?.setInput(inputBlob)
        val outputBlob = net?.forward() ?: return
        println(outputBlob)
    }

    fun setupDetector() {
        val modelFile = File(context.cacheDir, MODEL_NAME)
        if (!modelFile.exists()) {
            try {
                val inputStream = context.assets.open(MODEL_NAME)
                val size = inputStream.available()
                val buffer = ByteArray(size)
                inputStream.read(buffer)
                inputStream.close()
                val outputStream = FileOutputStream(modelFile)
                outputStream.write(buffer)
                outputStream.close()
                net = Dnn.readNetFromONNX(modelFile.absolutePath)
            } catch (e: Exception) {
                throw RuntimeException(e)
            }
        } else {
            net = Dnn.readNetFromONNX(modelFile.absolutePath)
        }
    }

    companion object {
        private const val MODEL_NAME = "model.onnx"
        private const val TENSOR_WIDTH = 640
        private const val TENSOR_HEIGHT = 640
    }
}

What could be the general approach to get bounding box, the confidence score and class labels? And  if you have any solution for onnx model with OpenCV then you can provide as well. Also this question isn't android specific.
I have found a solution but the problem is I have way too many points and I want to reduce them. Which is not a major issue, but still I would like to have fewer points.
I have searched for other answers here but I have found nothing pertaining to my issue. I have attached a sample mask for reference.
The sample binarised mask
Just to be clear, I have written a script in Python that does just that. The steps I had taken were:

Convert the colorised mask to a binarized one (since I have only one class).
Apply contours on the image and find the edge points of the mask.
Ravel the detected contour points.
Based on major shifts in angles, noted down only those points.
Save all the points in YOLO segmentation text format (cls_name x1 y1 x2 y2 .... xn yn)

EDIT: I did manage to do all these. But in the end I came to a horrible realization that, because it considers the outside borders, and if the labels are all overlapped, it becomes one big blob that contains unwanted region too. So I had to drop this and use those masks in UNET where I am getting the outputs I desire.
What is the use of imgsz in inference on Yolov8 model ?
Looking at current documentation's example, we can write :
model.predict(source, save=True, imgsz=320, conf=0.5)
(https://docs.ultralytics.com/modes/predict/#inference-sources).
It is not documented as argument, but can be passed.
Can it differ from the value used in training ?
I need to extract data from any middle layer.
I need it for making comparing images, the idea is that if the model recognizes a cat it would check if that image of a cat matches the previous images of cats.
I have done the following:
model = YOLO("yolov8s")
del model.model.model[-1]

This way I am deleting the final layer, but I can't extract data from it.
When I call the predict method I get the following:
File "\Lib\site-packages\ultralytics\utils\ops.py", line 219, in non_max_suppression
    x = x[xc[xi]]  # confidence
        ~^^^^^^^^
IndexError: The shape of the mask [12, 20] at index 0 does not match the shape of the indexed tensor [512, 20, 12] at index 0

What can I do differently to get the data? It would be better not create a model and delete the final layer( or x amount) because I still need the final result.
as the title says, how do I set parameters for augmentation while using YOLOv8? I want to use the Python SDK and not the CLI commands.
The mantainer of the repo refer several times to
https://docs.ultralytics.com/modes/predict/#arguments
but I don't understand where to store these parameters?
Also, I found this.
[Comment section from this site](https://i.sstatic.net/4qWX2.png)
It would be nice to seperate the settings from data.yaml, but a working solution is the priority
I am working on a project involving creating an AI-powered bot to full play Counter-Strike by itself. I have a working script that uses the YOLOv8 object detection algorithm to identify players on the screen, and draw a the bounding box around them.
I'm encountering an issue where the bot will consistently look downward when an enemy is detected on the screen. I have tried adjusting the mouse movement calculations and implementing crosshair adjustments, but the issue persists. I even looked at a similar implementation using win32, when I tried implementing that code to my project it still would have the bot look as far down as it could. Has anyone encountered a similar behavior or have suggestions on what might be causing this? I would greatly appreciate any insights, advice, or strategies that could help me pinpoint and address the cause of the issue.
Thank You!
Here is my code below:
import os
import time
from ultralytics import YOLO
import cv2
import numpy as np
from windowcap import WindowCapture
import win32gui, win32ui, win32con, win32api
import pydirectinput


wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
model_path = os.path.join('.','runs','detect','train10','weights','last.pt')
model = YOLO(model_path)

while True:

    frame = wincap.get_screenshot()

    H, W, _ = frame.shape 
    threshold = 0.5


    results = model(frame)[0]

    for result in results.boxes.data.tolist():
        x1, y1, x2, y2, score, class_id = result

        if score > threshold:
            cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
            cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)

            box_center_x = (x1 + x2) / 2
            box_center_y = (y1 + y2) / 2
            screen_width = win32api.GetSystemMetrics(0)
            screen_height = win32api.GetSystemMetrics(1)

            # moving crosshair to model box
            target_x = int(screen_width * box_center_x / W)
            target_y = int(screen_height * box_center_y / H)
            pydirectinput.moveTo(target_x, target_y)
    
    cv2.imshow("COMPUTER VISION",frame)

    if cv2.waitKey(1) == ord('q'):
        cv2.destroyAllWindows()
I'm trying to hide the bounding boxes and the labels after a prediction in YOLOv8. I've set the necessary attributes but I still see the bounding boxes and labels in the final render.show(). What am I doing wrong?
# load model
model = YOLO('ultralyticsplus/yolov8m')

# set model parameters
conf = 0.2
iou = 0.5
ag_nms = False

# try to turn off bounding boxes and labels
model.overrides['hide_labels'] = True
model.overrides['hide_conf'] = True
model.overrides['show'] = False

# set image
image = 'pic.jpg'

# perform inference
results = model.predict(
    image,
    show=False,
    hide_labels=True,
    hide_conf=True,
    conf=conf,
    iou=iou,
)

# observe results
render = render_result(model=model, image=image, result=results[0])
render.show() # still sees the bounding boxes here
I'm using the Ultralytics YOLOv8 implementation to perform object detection on an image. However, when I try to retrieve the classification probabilities using the probs attribute from the results object, it returns None. Here's my code:
from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # pretrained YOLOv8n model

# Run batched inference on a list of images
results = model('00000.png')  # return a list of Results objects

# Process results list
for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs
    masks = result.masks  # Masks object for segmentation masks outputs
    keypoints = result.keypoints  # Keypoints object for pose outputs
    probs = result.probs  # Probs object for classification outputs

print(probs)

When I run the above code, the output for print(probs) is None. The remaining output is
image 1/1 00000.png: 640x640 1 person, 1 zebra, 7.8ms
Speed: 2.6ms preprocess, 7.8ms inference, 1.3ms postprocess per image at shape (1, 3, 640, 640)

Why is the probs attribute returning None, and how can I retrieve the classification probabilities for each detected object? Is there a specific design reason behind this behavior in the Ultralytics YOLOv8 implementation?
this is the code
from ultralytics import YOLO
license_plate_detector = YOLO('./model/best.pt')
license_plates = license_plate_detector('./42.jpg')

and this the output
640x608 1 number-plate, 342.0ms
Speed: 12.4ms preprocess, 342.0ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 608)

i want to convert this output to image and save it to use with esayocr
the class don't have any save method so how to do this
I am trying to export .engine from onnx for the pretrained yolov8m model but get into trtexec issue. Note that I am targeting for a model supporting dynamic batch-size.
I got the onnx by following the official instructions from ultralytics.
from ultralytics import YOLO

# Load a model
model = YOLO('yolov8m.pt')  # load an official model
model = YOLO('path/to/best.pt')  # load a custom trained

# Export the model
model.export(format='onnx',dynamic=True) # Note the dynamic arg

I get the corresponding onnx. Now when I try to run trtexec
trtexec --onnx=yolov8m.onnx --workspace=8144 --fp16 --minShapes=input:1x3x640x640 --optShapes=input:2x3x640x640 --maxShapes=input:10x3x640x640 --saveEngine=my.engine

I get
[08/10/2023-23:53:10] [I] TensorRT version: 8.2.5
[08/10/2023-23:53:11] [I] [TRT] [MemUsageChange] Init CUDA: CPU +336, GPU +0, now: CPU 348, GPU 4361 (MiB)
[08/10/2023-23:53:11] [I] [TRT] [MemUsageSnapshot] Begin constructing builder kernel library: CPU 348 MiB, GPU 4361 MiB
[08/10/2023-23:53:12] [I] [TRT] [MemUsageSnapshot] End constructing builder kernel library: CPU 483 MiB, GPU 4393 MiB
[08/10/2023-23:53:12] [I] Start parsing network model
[08/10/2023-23:53:12] [I] [TRT] ----------------------------------------------------------------
[08/10/2023-23:53:12] [I] [TRT] Input filename:   yolov8m.onnx
[08/10/2023-23:53:12] [I] [TRT] ONNX IR version:  0.0.8
[08/10/2023-23:53:12] [I] [TRT] Opset version:    17
[08/10/2023-23:53:12] [I] [TRT] Producer name:    pytorch
[08/10/2023-23:53:12] [I] [TRT] Producer version: 2.0.1
[08/10/2023-23:53:12] [I] [TRT] Domain:           
[08/10/2023-23:53:12] [I] [TRT] Model version:    0
[08/10/2023-23:53:12] [I] [TRT] Doc string:       
[08/10/2023-23:53:12] [I] [TRT] ----------------------------------------------------------------
[08/10/2023-23:53:12] [W] [TRT] onnx2trt_utils.cpp:366: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:773: While parsing node number 305 [Range -> "/model.22/Range_output_0"]:
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:774: --- Begin node ---
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:775: input: "/model.22/Constant_8_output_0"
input: "/model.22/Cast_output_0"
input: "/model.22/Constant_9_output_0"
output: "/model.22/Range_output_0"
name: "/model.22/Range"
op_type: "Range"

 

[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:776: --- End node ---
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:779: ERROR: builtin_op_importers.cpp:3353 In function importRange:
[8] Assertion failed: inputs.at(0).isInt32() && "For range operator with dynamic inputs, this version of TensorRT only supports INT32!"
[08/10/2023-23:53:12] [E] Failed to parse onnx file
[08/10/2023-23:53:12] [I] Finish parsing network model
[08/10/2023-23:53:12] [E] Parsing model failed
[08/10/2023-23:53:12] [E] Failed to create engine from model.

I am aware that some people suggest upgrading to latest TRT version but I am looking for an alternate solution.