GithubHelp home page GithubHelp logo

Comments (9)

glenn-jocher avatar glenn-jocher commented on July 24, 2024 2

Hello @userwatch,

Thank you for providing the details and the example labels. It looks like you're on the right track with converting YOLOv8 segmentation labels to a format suitable for Mask R-CNN.

To help you further, here's a Python script that will loop through your YOLOv8 label files and convert them to the JSON format required by Mask R-CNN:

import os
import json

def yolo_to_maskrcnn(yolo_txt_path, image_width, image_height, output_json_path):
    maskrcnn_format = {}
    
    for txt_file in os.listdir(yolo_txt_path):
        if txt_file.endswith('.txt'):
            with open(os.path.join(yolo_txt_path, txt_file), 'r') as file:
                lines = file.readlines()
                
                for line in lines:
                    parts = line.strip().split()
                    class_id = parts[0]
                    points = list(map(float, parts[1:]))
                    
                    all_points_x = [int(points[i] * image_width) for i in range(0, len(points), 2)]
                    all_points_y = [int(points[i] * image_height) for i in range(1, len(points), 2)]
                    
                    filename = txt_file.replace('.txt', '.jpg')  # Assuming your image files are .jpg
                    size = os.path.getsize(os.path.join(yolo_txt_path, filename))
                    
                    maskrcnn_format[filename + str(size)] = {
                        "filename": filename,
                        "size": size,
                        "regions": [{
                            "shape_attributes": {
                                "name": "polygon",
                                "all_points_x": all_points_x,
                                "all_points_y": all_points_y
                            },
                            "region_attributes": {
                                "name": "not_defined",
                                "type": "unknown",
                                "image_quality": {
                                    "good": True,
                                    "frontal": True,
                                    "good_illumination": True
                                },
                                "names": "Car"  # Replace with your actual class name
                            }
                        }],
                        "file_attributes": {
                            "caption": "",
                            "public_domain": "no",
                            "image_url": ""
                        }
                    }
    
    with open(output_json_path, 'w') as json_file:
        json.dump(maskrcnn_format, json_file, indent=4)

# Example usage
yolo_txt_path = 'path/to/your/yolo_labels_folder'
image_width = 1280  # Replace with your actual image width
image_height = 720  # Replace with your actual image height
output_json_path = 'maskrcnn_labels.json'

yolo_to_maskrcnn(yolo_txt_path, image_width, image_height, output_json_path)

This script will:

  1. Loop through all .txt files in your YOLOv8 label folder.
  2. Read each file and convert the normalized coordinates to absolute pixel values.
  3. Format the data into the JSON structure required by Mask R-CNN.
  4. Save the output to a JSON file.

Make sure to adjust the image_width, image_height, and paths according to your setup.

If you encounter any issues or need further assistance, please provide a minimum reproducible code example as outlined in our guide. This will help us investigate and provide a solution more effectively.

Happy coding! 😊

from ultralytics.

github-actions avatar github-actions commented on July 24, 2024

👋 Hello @userwatch, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 24, 2024

@userwatch hello,

Thank you for reaching out with your question on converting YOLOv8 segmentation labels to a format suitable for Mask R-CNN. I understand that you have YOLOv8 segmentation labels in .txt format and need to convert them to a JSON format compatible with Mask R-CNN.

To assist you better, could you please confirm the following:

  1. Are you using the latest versions of torch and ultralytics? If not, please update them and try again.
  2. Could you provide a minimum reproducible code example that demonstrates how you are currently handling the YOLOv8 labels? This will help us understand your setup and provide a more accurate solution. You can refer to our guide on creating a minimum reproducible example here.

In the meantime, here's a general approach to convert YOLOv8 segmentation labels to the JSON format required by Mask R-CNN:

  1. Read the YOLOv8 .txt file: Each line in the file represents a polygon with normalized coordinates.
  2. Convert normalized coordinates to absolute pixel values: Multiply the normalized coordinates by the image dimensions.
  3. Format the data into the required JSON structure.

Here's a Python code snippet to illustrate this process:

import json

def yolo_to_maskrcnn(yolo_txt_path, image_width, image_height):
    maskrcnn_format = {}
    
    with open(yolo_txt_path, 'r') as file:
        lines = file.readlines()
        
        for line in lines:
            parts = line.strip().split()
            class_id = parts[0]
            points = list(map(float, parts[1:]))
            
            all_points_x = [int(points[i] * image_width) for i in range(0, len(points), 2)]
            all_points_y = [int(points[i] * image_height) for i in range(1, len(points), 2)]
            
            filename = "your_image_filename.jpg"  # Replace with your actual image filename
            size = 123456  # Replace with your actual image size in bytes
            
            maskrcnn_format[filename + str(size)] = {
                "filename": filename,
                "size": size,
                "regions": [{
                    "shape_attributes": {
                        "name": "polygon",
                        "all_points_x": all_points_x,
                        "all_points_y": all_points_y
                    },
                    "region_attributes": {
                        "name": "not_defined",
                        "type": "unknown",
                        "image_quality": {
                            "good": True,
                            "frontal": True,
                            "good_illumination": True
                        },
                        "names": "Car"  # Replace with your actual class name
                    }
                }],
                "file_attributes": {
                    "caption": "",
                    "public_domain": "no",
                    "image_url": ""
                }
            }
    
    return maskrcnn_format

# Example usage
yolo_txt_path = 'path/to/your/yolo_labels.txt'
image_width = 1280  # Replace with your actual image width
image_height = 720  # Replace with your actual image height

maskrcnn_labels = yolo_to_maskrcnn(yolo_txt_path, image_width, image_height)

# Save to JSON file
with open('maskrcnn_labels.json', 'w') as json_file:
    json.dump(maskrcnn_labels, json_file, indent=4)

This code reads the YOLOv8 labels, converts the coordinates, and formats them into the JSON structure required by Mask R-CNN. Adjust the filename, size, and other attributes as needed.

Feel free to reach out if you have any further questions or need additional assistance!

from ultralytics.

userwatch avatar userwatch commented on July 24, 2024

Hello @glenn-jocher thank you for reply. I will add a loop to the code you gave.
My label folder:
image


0 0.5507985796875 0.708180884375 0.566902571875 0.74693175625 0.6265820140625 0.762094990625 0.8160406421875 0.7401923609375001 0.8226715765625 0.7048113625 0.9278212703125 0.694702375 0.9979208546875 0.74861648125 0.9941317265625 0.027515509375000004 0.7999368078125 0.012352239062500001 0.7430990921875 0.044363681249999995 0.6786831203125 0.014036999999999999 0.4958555859375 0.007297745312499999 0.4930137 0.26507453124999997 0.4257559234375 0.26507453124999997 0.337657703125 0.2684440875 0.3206063875 0.23643261093749998 0.20882583906250002 0.2381174421875 0.1813543828125 0.28192266718750003 0.1643030671875 0.694702375 0.1804070609375 0.73682276875 0.2571378171875 0.73345310625 0.281605084375 0.7899831578125001 0.4653848625 0.7805119984375 0.479594290625 0.7030105359375 0.5507985796875 0.708180884375


0 0.30166058906250004 0.47904601874999997 0.3821804734375 0.4975789875 0.4153357015625 0.573395653125 0.6028996859375 0.603722475 0.8321446343750001 0.6020375375 0.8065677421874999 0.37121780625 0.7137329125 0.4099686078125 0.6947871140624999 0.2886618859375 0.6171091140625 0.2802378 0.5962687515625 0.2583352046875 0.59540716875 0.178872046875 0.37839126562500003 0.1791488765625 0.37270749375 0.2970859359375 0.30166058906250004 0.2954011390625 0.30166058906250004 0.47904601874999997


0 0.3575508234375 0.6138312499999999 0.4295451296875 0.603722475 0.44943825156250006 0.6256249640625 0.5792173578125 0.6155160468750001 0.7326788718749999 0.6003527421875 0.71941684375 0.3577393671875 0.65689535625 0.3392063296875 0.6483698578125 0.2684440875 0.55269314375 0.2701289171875 0.54795669375 0.3004555984375 0.53374734375 0.3004555984375 0.5318527 0.24991111875 0.5460621296875 0.21116038750000002 0.53848379375 0.1067018375 0.2950294953125 0.12018034687500001 0.292187609375 0.266759290625 0.18655300156250001 0.28766834218750004 0.18419626718749998 0.531275259375 0.23440281093749998 0.531275259375 0.24292838906249997 0.612146525 0.3575508234375 0.6138312499999999

from ultralytics.

userwatch avatar userwatch commented on July 24, 2024

Thank you so much, you are a hero :)

Best wishes,

from ultralytics.

userwatch avatar userwatch commented on July 24, 2024

Hello again, I made the convert and encountered this error while training.
That's my json file.
https://github.com/userwatch/convert/blob/main/result.json
Thats my error
image
Any ideas about this error?
Image size 1920x1080
After labelling it for yolov8 in roboflow it becomes 640x640

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 24, 2024

Hello @userwatch,

Thank you for providing the JSON file and the error screenshot. It looks like there might be an issue with the format or content of the JSON file used for training.

To help us investigate further, could you please provide a minimum reproducible code example that demonstrates how you are loading and using the JSON file for training? This will help us understand your setup better and pinpoint the issue. You can refer to our guide on creating a minimum reproducible example here.

Additionally, please ensure that you are using the latest versions of torch and ultralytics. If not, please upgrade your packages and try again to see if the issue persists.

In the meantime, here are a few things you can check:

  1. JSON Structure: Ensure that the JSON structure matches the expected format for Mask R-CNN. Verify that all required fields are present and correctly formatted.
  2. Image Paths and Sizes: Confirm that the image paths in the JSON file are correct and that the image sizes match the dimensions specified in the JSON.
  3. Coordinate Conversion: Ensure that the coordinates in the JSON file are correctly converted from YOLOv8 format to the Mask R-CNN format.

If you need further assistance, please provide the requested code example, and we'll be happy to help you troubleshoot the issue.

Best wishes and happy coding! 😊

from ultralytics.

userwatch avatar userwatch commented on July 24, 2024

image_width = 640
image_height = 640
When tagged in roboflow, the image size was 640x640. I updated the width and height values.
and training started.
Thank you :)

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 24, 2024

Hello @userwatch,

Thank you for the update! I'm glad to hear that updating the width and height values to match the 640x640 dimensions resolved the issue and allowed you to start training. 🎉

If you encounter any further issues or have additional questions, feel free to reach out. We're here to help!

Happy training and

from ultralytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.