Skip to main content

How to train a custom dataset with YOLOv7 for instance segmentation?

 

Another post starts with you beautiful people! It is overwhelming for me to see massive interest in my last three posts about the YOLOv7 series💓. Your response keeps me motivated to share my learning with you all 💝. If you have not checked my previous posts about YOLOv7, then I am sharing here links to read those once and then proceed with this post-

  1. Train a custom dataset with YOLOv7
  2. Export custom YOLOv7 model to ONNX
  3. Export custom YOLOv7 model to TensorRT
Till now we have learned about object detection with YOLOv7. In this post, we are going to learn how can we train a custom dataset for instance segmentation task with YOLOv7 👌. For your information instance segmentation is the task of detecting and delineating each distinct object of interest appearing in an image. For our hands-on we need a dataset having images and their annotations in polygon format and of course in YOLO format. So I have found and downloaded the American Sign Language dataset in the required format from this link.

This dataset has all letters A through Z in American Sign Language labeled with polygon labels. To our knowledge, the alphabet and its correspondence signs look like below-

For this post also I am using the google colab environment. Let's first install its required library and restart the colab notebook runtime once it is installed-

Next, we will download the code repo of the YOLOv7's segmentation branch since while writing this post author has not officially declared the main branch for the segmentation code. So I have downloaded the u7 branch and uploaded it to my gdrive so that I can access it in my colab notebook 😀. After uploading the branch code to drive, I unzipped it like as below-

The unzipped folder structure of the dataset will look like as below-

Next, we will prepare the required YAML files for our instance segmentation training. First, we will create our configuration YAML file where will define the path of the training and validation images, the number of the target classes, and the names of the target classes as below-

Here I have provided the train and validation images path, and the number of classes as 26 since our classes are from the alphabet A-Z. Next, we will create a second YAML file having the content of it's default yolov7-seg.yaml but with one difference- the value of 'nc' parameter from 80 to 26-

Next, we will create the hyperparameter YAML file having the same content as its official hyp.scratch-high.yaml as below-

Next, we will download the pre-trained weight file for the training as below-

Now we are ready to start our instance segmentation training with the below command-

Here, change the .yaml files paths and the weight file path as per yours. In my colab environment for 100 epochs it has taken 1 hour to complete. for better accuracy, you should train it for 300 epochs for this dataset. Once the training is completed, you will see the following output in the console-

After completion of the training, let's check the trained model on a test image as below-

You will see the following output in the console after successfully running the above command-

Let's open the processed image and see what it looks like-

See our trained model is able to do the instance segmentation and is able to predict the target class of a sign 💥. Let me share a few more output images-




Indeed we have a strong sign language detection model. Let's run the inference on a video-

The output in the console will look like the below after a successful run of the above command-

Let's open this video in our colab notebook-

And it will show the processed video file as below-

That's it for today's guys! We have learned another useful technique with YOLOv7. Here I am sharing my colab notebook for this tutorial. So no need to rest, just copy the notebook into your colab environment and try it with your own dataset. In my next post I will share something useful again till then 👉 Go chase your dreams, have an awesome day, make every second count, and see you later in my next post😇







Comments

  1. Hello first of all thank you for the great tutorial, however I cant seem to reproduce your colab. It's giving me an error on loss.py at line 198 anchors, shape = self.anchors[i], p[i].shape, saying that list object has no attribute shape. Did you encounter this problem?

    ReplyDelete
  2. Nvm I was running the wrong file. For anyone going to have this mistake, run train.py located in the folder /seg/segment NOT the one in /seg. Thanks for the tutorial cheers!

    ReplyDelete
  3. yes, the correct train.py is inside the /seg/segment/ folder as I mentioned already in this post.

    ReplyDelete
  4. I was also trying to run instance segmentation on custom dataset of occluded objects. I am facing problems annotating objects formed by two polygons/contour rather than a single one. In coco style format, I can use segmentation as a list of lists. How should I do it in yolo format? Should I put all x, y coordinate pairs of two polygons at the same line? if I put them on separate lines, they will be recognized as two distinct instance of same class as far as my understanding. Can you suggest how to handle this?

    ReplyDelete
    Replies
    1. If the object is one then it's annotation should also be one. May I know what kind of object do you have?

      Delete
  5. Amazing write-up. Thankyou for such informative blog. Python course in Greater Noida is the good platform for learning python in deep.

    ReplyDelete
  6. Our Data Science Course in Noida encompasses a well-structured curriculum that delves into essential concepts such as data manipulation, statistical analysis, machine learning, and more. With hands-on projects and practical exercises, students gain the confidence and skills required to tackle real-world data challenges. This hands-on approach sets APTRON Solution apart, as it empowers students to apply their knowledge effectively in their future careers.

    ReplyDelete
  7. APTRON caters to learners of all levels. With state-of-the-art facilities and a commitment to excellence, we strive to empower our students to harness the power of data and drive meaningful insights. Join APTRON's Data Science Training in Gurgaon and embark on a journey towards a rewarding and impactful career in the realm of data science.

    ReplyDelete
  8. Excellent article. Thanks for sharing.
    Microsoft Fabric Training
    Microsoft Azure Fabric Training
    Microsoft Fabric Course in Hyderabad
    Microsoft Fabric Online Training Course
    Microsoft Fabric Online Training Institute
    Microsoft Fabric Training In Ameerpet
    Microsoft Fabric Online Training

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Great article! I really enjoyed reading it and found the information very helpful. Looking forward to more insightful posts like this

    Kerala Tour Packages from Delhi
    One Day Trip Places in Kerala
    Best Time to Visit Kerala with Family

    ReplyDelete

Post a Comment

Popular posts from this blog

How to deploy your ML model as Fast API?

Another post starts with you beautiful people! Thank you all for showing so much interests in my last posts about object detection and recognition using YOLOv4. I was very happy to see many aspiring data scientists have learnt from my past three posts about using YOLOv4. Today I am going to share you all a new skill to learn. Most of you have seen my post about  deploying and consuming ML models as Flask API   where we have learnt to deploy and consume a keras model with Flask API  . In this post you are going to learn a new framework-  FastAPI to deploy your model as Rest API. After completing this post you will have a new industry standard skill. What is FastAPI? FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to learn, fast to code and ready for production . Yes, you heard it right! Flask is not meant to be used in production but with FastAPI you can use you...

Learn the fastest way to build data apps

Another post starts with you beautiful people! I hope you have enjoyed and learned something new from my previous three posts about machine learning model deployment. In one post we have learned  How to deploy a model as FastAPI?  I n the second post, we have learned  How to deploy a deep learning model as RestAPI ? and in the third post, we have also learned  How to scale your deep learning model API?   If you are following my blog posts, you have seen how easily you have transit yourselves from aspiring to a mature data scientist. In this new post, I am going to share a new framework-  Streamlit which will help you to easily create a beautiful app with Python only. I will show here how had I used the Streamlit framework to create an app for my YOLOv3 custom model. What is Streamlit? Streamlit’s open-source app framework is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours!...

How can I make a simple ChatBot?

Another post starts with you beautiful people! It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem. So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot? Quite interesting right! Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this? And like always Python has helped me this time also. There is a Python library available with name as  ChatterBot   which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another...