1. Description and preparation
The experimental data set production this time is the production of PASCAL VOC data set. The PASCAL VOC data set is produced for mask-RCNN instance segmentation and is demonstrated in the pycharm environment.
1. First manually create the following folder:
Manually create the four folders above to facilitate later operations.
2. Installation environment
pip install labelimg
pip install labelme
Enter the above code in the terminal of pycharm software and install the labelimg library and labelme library.
2. XML file production under the Annotations folder (target detection data annotation)
To label xml files, use the labelimg tool. The operation is as follows:
1. Open the environment
As shown in the picture below, the first opening failed and the error was reported as follows. The reason is that labelimg does not support version 3.10 of python but supports version 3.9. So I switched to the conda environment with python3.9 installed.
2. Load the data set
Select the data set that needs to be annotated, that is, the original image storage location, which I stored in the JPEGImages folder.
Select annotation type: select a rectangular box for annotation
3. Label
Frame the target object with a rectangular frame, and then enter the corresponding label:
After the annotation is completed, select the saved folder and save it in the Annotations file created above:
the first picture is annotated, and then the shortcut keys are used to annotate other pictures:
Annotation shortcut keys:
Press W to mark
ctrl+s save
d to switch to the next one,
Until the annotation is completed, you will get the xml file under the Annotations file in the VOC. The
xml file after the annotation is completed is as shown below:
Part of the content of the xml annotation file is shown below:
<annotation>
<folder>images</folder>
<filename>0ada72de6a864e998c6da2192e327c22.jpg</filename>
<path>C:\Users\xxx\Desktop\VOC数据集制作\images\0ada72de6a864e998c6da2192e327c22.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>640</width>
<height>640</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>horse</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>36</xmin>
<ymin>92</ymin>
<xmax>510</xmax>
<ymax>626</ymax>
</bndbox>
</object>
</annotation>
xml file annotation completed
3. Annotation of instance segmentation files under the SegmentationObject folder
1. Use the labelme tool to label this file. Open it in pycharm and enter labelme in the terminal.
2. Select annotation data-select the JPEGImages folder
3. Select the labeling method
4. Start labeling - mark the target object on the edge - complete the labeling and enter the corresponding label (note that the label name needs to be consistent with the label name in the xml labeling above)
After the annotation is completed, press ctrl+s to save it and save it to the original folder:
Continue annotation:
the last picture after annotation corresponds to a json file, as shown below (I only annotated three pictures):
5. Download the labelme processing file
Download URL: labelme-instance segmentation processing source code
. Click to enter the above URL and download the source code as follows:
After decompressing the downloaded source code, you will get a folder named labelme-main.
Copy the annotated pictures and corresponding json files to this path in the downloaded source code:
pycharm terminal path cd to the following path:
labelme-main\examples\instance_segmentation\data_annotated
Open the following txt folder and put the label name in Inside, leave the first and second lines unchanged, and put the label names starting from the third line:
Enter the following command in the terminal to perform data annotation processing:
python labelme2voc.py data_annotated data_dataset_voc --labels labels.txt
After the processing is completed, the data_dataset_voc folder will be generated in the following location:
This folder contains the following data:
Copy the instance segmentation annotation file to the SegmentationObject folder created at the beginning
6. Create ImageSets/Segmentation data
Run the following code:
from sklearn.model_selection import train_test_split
import os
imagedir = r'C:\Users\xxx\Desktop\VOC数据集制作\SegmentationObject'
outdir = r'C:\Users\xxx\Desktop\VOC数据集制作\ImageSets\Segmentation/'
images = []
for file in os.listdir(imagedir):
filename = file.split('.')[0]
images.append(filename)
train, test = train_test_split(images, train_size=0.9,random_state=0)#90%
val, test = train_test_split(test, train_size=0.2 / 0.3, random_state=0)#剩下的百分之十,前者占2后者占3
with open(outdir + "train.txt", 'w') as f:
f.write('\n'.join(train))
with open(outdir + "val.txt", 'w') as f:
f.write('\n'.join(val))
with open(outdir + "test.txt", 'w') as f:
f.write('\n'.join(test))
After the operation is completed, the following data will be generated in the directory:
At this point, the standard VOC data set required by the Mask_RCNN model has been produced.
Standard data Mask-RCN source code for Mask-RCNN