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

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 your model in production wi

How to install and compile YOLO v4 with GPU enable settings in Windows 10?

Another post starts with you beautiful people! Last year I had shared a post about  installing and compiling Darknet YOLOv3   in your Windows machine and also how to detect an object using  YOLOv3 with Keras . This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my  previous post  by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce GTX 1660 Ti Version 445.87

How to use opencv-python with Darknet's YOLOv4?

Another post starts with you beautiful people 😊 Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one- Setup Darknet's YOLOv4 Train custom dataset with YOLOv4 Create production-ready API of YOLOv4 model Create a web app for your YOLOv4 model Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome computer vision and machine learning software library-  OpenCV  and of course with Python 🐍. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command- pip install opencv-python --up