Due to Covid-19, the social distancing level is elevated and gathering more than 5 people is prohibited in Korea. We designed this program in order to detect face mask and the gathering of people using deep learning and computer vision. We use YOLOv5 which is a pre-trained object detection model in Google Colab and Visual Studio Code through Anaconda virtual environment.
1. Download Image and Annotation files
1.1. Download Zip file
(Annotation file's format : XML)
1.png
1.2. XML to Text(Yolov5) Conversion
Object detection by using YOLOv5 requires a compatible label format. Since the annotation downloaded from kaggle has a .xml format, we convert it to a .txt file according to the method presented.
Skip tutorial and create new object detection project. Roboflow provides object detection type basically and you do not need to change.
tuto_3x.png
Now, you can upload data to roboflow. Select the path to your images and annotations.
Then, the labels are indicated on the images in position.
tuto_6x.png
You can assign the proportion of training, testing and validation set. For this step, we continue as default set since we do not need to separate data set. (You can set all data to single set such as training, testing, or validation)
tuto_7x.png
You can preprocess the data such as 'Auto-Orient' and 'Resize' before converting. These process are recommanded but not essential and you can skip.
Click 'Generate' button on the bottom of the page and finish conversion.
tuto_9x.png
Select YOLOv5 Pytorch data format and download.
tuto_10xx.PNG
2. Training
2.1. Recall converted image and annotation files
If you have run the code, you can see the images and label folders for train, test, and valid created in the file window on the left.
KakaoTalk_20210611_141933313.png
2.2. Recall YOLOv5 at github clone
1231.png
2.3. Install Packages for YOLOv5
2.4. Check that the yaml file
Check that the data set information is written correctly in the yaml file.
2.5. Divide Dataset
Currently, the train and validation datasets are randomly divided. But we want to shuffle the data every time we train. So put all the data together and shuffle them randomly.
Create a list for all image files
Divide the train set and validation set by a ratio of 0.25.
Save the training image file and the image path of the validation image file as .txt file
asdasds.png
2.6 Imageset Training
You can set the various parameters needed for training.
2.7 Training result
You can see the result of training at the tensorboard
and You MUST save the weights(best.pt) at the left file-folder at your own computer
with open('/content/train.txt', 'w') as f:
f.write('\n'.join(train_img_list) + '\n')
with open('/content/valid.txt', 'w') as f:
f.write('\n'.join(valid_img_list) + '\n')
import yaml
with open('/content/data.yaml', 'r') as f:
data = yaml.load(f)
print(data)
data['train'] = '/content/train.txt'
data['val'] = '/content/valid.txt'
with open('/content/data.yaml', 'w') as f:
yaml.dump(data, f)
print(data)