Skip to content

gshoanganh/mask-ai

Repository files navigation

Mask object detection AI using the latest YOLOv5 model

👋 Hello! This set of Notebooks was written to serve as a template for future AI projects.

Nowadays, you can find numerous models which could be used to develop AI with object detection projects. However, Yolov5 is becoming the fastest and most powerful. After many tries with different ones, I can assure you that Yolov5 will be the best choice for you to develop your Object Detection project.

📚 This guide explains how to train your own custom dataset with YOLOv5 🚀
🍀 Author: Gs Hoang Anh (Youtube channel.)

Stochastic Loss Function

A conceptual visualization for our stochastic loss function. Given a main network f(·; w) and a decision network h(·; v), a group of loss functions are predefined. During the forward propagation, for the input x and output y, the main network generates the estimated output p = f(x; w), and the loss functions can be selected by the decision network with Gumbel Softmax. Then, the loss in SLF is obtained by combing these selected loss functions. During the backward propagation, the standard back-propagation is in a position to calculate the gradients of the network weights w and the loss parameters v.

For simplicity, we denote stochastic loss function with n ordered loss functions as L = {1, ··· , n}, including mean squared error, categorical hinge, cross-entropy, and so on. With these loss functions, the stochastic loss function can be formulated as


Before You Start

- install: Python>=3.7.0 🌟

- Organize Directories: create a folder mask-ai is your project.

+ data/(images,labels): Include the image and the folder used to hold the txt file (tag label)
+ Training: This is a file for writing python code, code for training.


Data should be well labeled as yolo format and store in the path of training/validation data defined previously.
- In the directory, you should have to folder of images and labels:
In each folder, you shold have train and val folders that contains the same numbers of items in both images and labels.

💡 ProTip: You can skip this step by cloning my project git clone https://github.com/gshoanganh/mask-ai.git

Yolov5

Step 1. From mask-ai folder, run the command:

git clone https://github.com/ultralytics/yolov5

Step 2. Modify path of data and classes

Find file path at yolov5/data/coco128.yaml, duplicate an new file and rename as comask.yaml

Open the file and modify our config as follows

train: ../train_data_new/images/train/  # path of training data
val: ../train_data_new/images/val/  # path of validation data
test:  # test images (optional)

# Classes
nc: 2  # number of classes
names: ['mask','notmask']  # class names

Prepare images and Create Labels

Step 1. You need to prepare about more than 20 images for more favorable training results.
note: after you have the images, add them to mask-ai\data\images\(train,val) folders.
Step 2. Create labels for images by visiting the website: makesense.ai.
note: Of course you can also use other tools to assign your own labels. Example: imagelabel, Use Roboflow to label, ....
Step 3. After successfully exporting the txt files, add them to the directory: mask-ai\data\labels\(train,val)

Getting started

Step 1. Activate env

From smoke-ai folder, open command:

python -m venv yolo # method1
python3 -m venv yolo # method2

Step 2. Activate your virtual environment
source yolo/bin/activate # Linux/Mac
.\yolo\Scripts\activate # Windows 

Step 3. Install dependencies and add virtual environment to the Python Kernel
python -m pip install --upgrade pip
pip install ipykernel
python -m ipykernel install --user --name=yolo

💡 Bug: You can use pip2 and python3


Step 4. install all requirements
cd yolov5
install requirements.txt

Step 5. Run notebook

cd ..
python3 -m notebook

💡 Warning: In this step, you will perform some checks to see if the default yolov5 is running correctly.

Step 6. TRAINING...

Open a new tab command, then repeat step 1 and step 2 again

next, run:

python train.py --img 640 --batch 3 --epochs 100 --data ccmask.yaml --weights yolov5l.pt --cache --worked 2

If you have GPU usage. Please run:

python train.py --img 640 --batch 3 --epochs 100 --data ccmask.yaml --weights yolov5l.pt --cache --device 0
Good luck ☘️

© Gs Hoang Anh

About

Mask object detection AI using the latest YOLOv5 model

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published