Use Mask-RCNN to train your own data set. It is enough to read this article. From making a data set, I will teach you how to play Mask-RCNN step by step (nanny level tutorial)

1. Install labelme 

Neural network-based algorithms such as deep learning algorithms are all data-driven, and the quality of the data will affect the quality of your final model. When using Mask-RCNN, the first thing to do is to label the data set. Here we default You have configured the anaconda environment. If you have not configured it, you can refer to other people’s blogs . Create a virtual environment in the conda environment that has been configured. Enter the following command in the terminal to install the labeling tool labelme:

pip install labelme

pip install pyqt5

pip install pillow==4.0.0

2. Annotated dataset

The next step is to start labeling data. Entering the following code in the terminal will automatically open the labeling tool:

labelme

Then select the second item to open the folder, as shown in the following figure:

8879cc39540048369cca55e90b316781.png

Select the folder where the data pictures are located, and right-click to label the pictures one by one. And fill in the label information. After marking, click D to switch to the next one, and a save interface will pop up here, just save it directly to the folder where our original image is located. After marking all the pictures and getting all the following json files, close the terminal.

7c48b9f175724c0ead1958161f07282a.png

 3. Batch processing of Json files

The current .json contains the information we marked, but it is not yet a data format that can be read directly by the code. For different deep learning codes, the format of the data storage should be determined according to the writing method of the code. The following is the above mask- Take rcnn as an example to modify the data to the format we need. Create a new .txt file under the folder where the .json file is saved, and enter the following content:

@echo off
for %%i in (*.json) do labelme_json_to_dataset "%%i"
pause

After saving, change the file extension to .bat, change it to a script that changes the storage format of the file, and double-click to run after saving. Wait a while after running, you will get the following folder.

59d8b6b0d1374804b28906d87df601f2.png

Create a folder as shown below:

2442036c00ca464a97551b185c534adf.png

 

 

 

 

The labelme_json folder stores the folder obtained in the previous step.

The json folder stores the marked .json files.

The pic folder stores the original pictures.

The things stored in the above three folders can be directly copied and pasted. Here is a little trick. In the sorting of files on the computer, you can choose to sort by file type, which can facilitate our copying and pasting.

cv2_mask stores the mask file with the suffix .png generated in the folder obtained in the previous step. The approximate content is as follows:

4632b27dfead40b4a44b58450908ef6f.png

 

It is troublesome to copy .png files into folders one by one and rename them. Here we write a python script copy.py file for batch operations:

import os 
import shutil

for dir_name in os.listdir('./labelme_json'):
    pic_name = dir_name[:-5] + '.png'
    from_dir = './labelme_json/'+dir_name+'./label.png'
    to_dir = './cv2_mask/'+pic_name
    shutil.copyfile(from_dir, to_dir)
        
    print (from_dir)
    print (to_dir)

Save the above file.py file to the following directory and run it:

e3bbc2f388a84dfcb23f1d2a07dc2698.png

 

At this point our dataset is complete. 

 

4. Download Mask-RCNN source code

Download the modified code (if you want to modify the code, you can add the qq group: 817442229 or directly add the blogger’s qq: 2425113371 to get it for free), and rename the data set we made to mydata and put it in this folder. Use it directly, or you can download the official version code, but the official version requires a deep understanding of the code before it can be used. The github link of the official version source code is attached below. Novices suggest to get the source code directly from me: https://github.com/matterport /Mask_RCNN/releases/tag/v2.0

5. Environment construction

Those who engage in deep learning should be familiar with the tool conda. Here we assume that you have installed conda, a tool for managing virtual environments. Create a new virtual environment directly under the base. This source code has been developed for a long time. If the python version is too high, it will cause many problems in the future. It is best to use python2 to build the environment, but I use the python3 version to build it here. You can follow me directly. It’s okay to use python3. After setting up the environment, you need to modify the source code (more on this later):

conda create -n MaskRCNN python=3.6

After creation, use the following command to activate the environment:

source activate MaskRCNN

Here it should be under the ubuntu system:

conda activate MaskRCNN

Next, install tensorflow in this environment. Note that tensorflow cannot be too high or too low here. I am using version 1.5.0. Do not specify the version. If you do not specify the version, the highest version will be downloaded by default, and there will be question:

pip install tensorflow==1.5.0

You can also download the gpu version here. The gpu version will run much faster than the cpu, but the gpu version of tensorflow has a corresponding relationship with cuda and cudnn. If you want to use the gpu version, you may need to reinstall cuda and cudnn:

pip install tensorflow-gpu==1.5.0

The corresponding cuda version here is 9.0, check the corresponding pytorch version on the pytorch official website , and then use pytorch to help us install cuda and cudnn:

conda install pytorch==1.1.0 torchvision==0.3.0 cudatoolkit=9.0 -c pytorch

You can use the following code to check whether the installation is successful:

import torch
print(torch.cuda.is_available())
print(torch.backends.cudnn.is_available())
print(torch.cuda_version)
print(torch.backends.cudnn.version())

If the running result prints two trues, it means that the configuration is successful. The last two lines may report an error due to version reasons, but it doesn’t matter, just ignore it, and then go on.

Next, install keras. Note that the version of keras should correspond to tensorflow:

pip install keras==2.1.6

If the download is too slow, consider changing the source. After the above code, change the source with -i. For example, you can do the same when installing other packages, which is very useful:

pip install keras==2.1.6 -i https://pypi.tuna.tsinghua.edu.cn/simple

After installation, open the code folder with pycharm, click the terminal below, where you can use the following code to install all the required packages in requirements.txt:

pip install -r requirements.txt

If there is no problem after downloading and the code can be run, it means that the environment and configuration are complete. But there is a high probability that it will fail, because the blogger will fail... If this method fails, you must install the packages in requirements.txt one by one:

pip install numpy
pip install scipy
pip install Pillow
pip install cython
pip install matplotlib
pip install scikit-image
pip install opencv-python
pip install h5py
pip install imgaug
pip install IPython[all]

Run the above code line by line. It is recommended to run the test code after each package is installed to test whether tensorflow can still work normally, because some package versions that do not match may affect tensorflow. The following is the test code:

import tensorflow as tf
import keras
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()

If the above code reports an error when the package under requirements.txt is not installed, it is likely that the version of tensorflow and numpy does not correspond to cause a problem. The numpy version I'm using is 1.16.0, which is the minimum version that happens to run all packages and is compatible with tensorflow 1.5.0:

pip install numpy==1.16.0

If the numpy version is changed and the error is still reported, it may be a problem with the encoding format of python3. Just add all .decode('utf8') under topology.py as shown in the following figure:

02a4b87ec396462b9811b73a6dd07452.png

When installing opencv-python, there is a high probability that an error will be reported, because the default download is the highest version, and the highest version of cv is not compatible with tensorflow==1.5.0. I use:

pip install opencv-python==4.3.0.38

After installing the above packages, there may be warnings when running the code:

9945aba1219445bcb9c4c70bf8fd6281.png

 The reason for this warning is that the version of scikit-image is not compatible, just use the following command to reinstall:

pip install -U scikit-image==0.16.2

As for other warnings, they are irrelevant and can be ignored, but the red error report looks very uncomfortable, you can add the following content to the front of the code to block it:

# 版本不兼容报错,直接忽略
import warnings
warnings.filterwarnings("ignore")

6. Modify the downloaded source code

1. Modify line 57 of train_test.py and change it to the number of categories +1.

539fd28fdaf64d57bbab43aaf766cd18.png

2. Modify line 121 of train_test.py, comment out the original ones, and rewrite as many lines as there are categories. The second parameter of each line is the serial number, and the third parameter is the category name.

f1a86160ee9041cb8593addd9dcfd96e.png

 3. Modify lines 178~182 of train_test.py to your own path. The relative path is used here. If your dataset is placed in the way I said earlier, you don’t need to change it here.

e0a17ccf10774f58b450de1464c20bdb.png

 4. Modify DETECTION_MIN_CONFIDENCE under config.py, and lower this value to get more prediction results.

698d6403452c4ce4bdb0271f5a42140f.png

5. Modify line 61~62 of train_test.py to specify the image size.

536e940a95554597914c173d3bb4b8a0.png

 6. Modify lines 160~163 of train_test.py.

404025274657452e85b2ffecc68d7e4f.png

The rest of the parameters do not need to be modified. After completion, run the train_test file to start training.

Seven, test code

Running the test code To install pycocotools, run:

pip install pycocotools

If the following error occurs:

a1a4fecc07f9471f867aa5efbccc87f2.png

 It means that Microsoft Visual C++ 14.0 is missing, just go to the following address to install it:

https://my.visualstudio.com/Downloads?q=build%20tools

Search for build tools and download build tools in DVD format:

6ae1d524e57b49609a63a90ad279e77f.pngInstall it after the download is complete. But this method will take up a lot of memory, a more direct method, run in the terminal:

conda install libpython m2w64-toolchain -c msys2

For specific content, please refer to:

https://blog.csdn.net/qzzzxiaosheng/article/details/125119006

Guess you like

Origin blog.csdn.net/ekekex/article/details/130171832