[OpenR8 solution] Image_PCB_MaskRCNN_Keras (Using MaskRCNN and Keras framework for PCB)
  1. Image_PCB_MaskRCNN_Keras Introduction


Mask R-CNN is an extended application of Faster R-CNN, adding a branch more than Faster R-CNN to segment the target pixels while detecting the target


This solution uses Mask R-CNN to determine the position of the capacitor. When marking, the target uses polygons to paint the outline of the object and mark its category.



Fig. 1. Process for marking training and testing samples.png

Fig. 1. Process for marking training and testing samples.




  1. Image_PCB_MaskRCNN_Keras Folder Introduction


“Image_PCB_MaskRCNN_Keras” is located in the solution folder of OpenR8, which contains:

  1. Folders:【data folder】、【src folder】、【tool folder】。
  2. flow files:【1_train.flow】、【2_inference.flow】。

※For the first time user, it is recommended to change only the file contents of the PCB folder in data, and then make your own changes to the desired location.


Fig. 2. Image_PCB_MaskRCNN_Keras Position.png

Fig. 2. Image_PCB_MaskRCNN_Keras Position.



Use and function


data Folder

Store training sample, categories, test sample, categories, and model files.



mask_rcnn_coco.h5(used as pretraining model (first use don't delete))

src Folder

Python files used in training of which capacitor.py are mainly for training and testing.










tool Folder

Marks the Web page used by the image.



A solution for training samples.



The solution for the test sample.


Table 1.Image_PCB_MaskRCNN_Keras folder introduction.



  1. Prepare training Sample + markup categories


Before we can train, we must decide the direction, take this file as an example, we want to detect the location of the capacitance, so we will sample picture one by one mark their category (capacitance).


Step1: Open the markup website page

Open the via.html page in the via-2.0.0 folder in the tool folder to mark the sample categories we want to train.


Fig. 3. markup website page.png

Fig. 3. markup website page.


Step2:Select sample image storage folder

Click "Open Dir" to open the folder location of the sample image. Take the solution here as an example, to train the image in "data/pcb/train", press "Add Files" to prepare the tag image, as shown in Fig. 4, Fig. 5.


Fig. 4. Click.png

Fig. 4. Click “Add Files” add files.


Fig. 5. Select and open the profile want to train.png

Fig. 5. Select and open the profile want to train.


Step3:label category

Use polygons to depict the area you want to recognize.


Fig. 6. Using polygon marker capacitors.png

Fig. 6. Using polygon marker capacitors.


If you use the Polygon bounding box, double-click  to end the Polygon bounding box , as shown in Fig. 7, Fig. 8.


Fig. 7. Using polygon marker capacitors.png

Fig. 7. Using polygon marker capacitors.


Fig. 8. Using polygon marker capacitors.png

Fig. 8. Using polygon marker capacitors.


Step4:Sample image tag categories

As shown in Figure 9, enter the name in the attribute name field of Attributes. Then, as shown in Figure 10, fill in the name of the category according to the number of the box.


Taking this document as an example, it is to judge the capacitance, so fill in the “capacitor”, and then press X to close it.


Continue to select the next sample image until all sample images are marked with categories.


Fig. 9. Add name.png

Fig. 9. Add name.


Fig. 10. Enter a category name.png

Fig. 10. Enter a category name.


Step5:Output label Category file

After all the tags have been marked, the tagged file is output, as shown in Fig. 11, and the “Export Annotations (as json)” in the “Annotation” above is output to output the json file for all image categories.


Fig. 11. Output label json file.png

Fig. 11. Output label json file.


Step6:Place the output json file in the “data/pcb/train” folder

The json file generated in Fig. 11 is placed in the data/pcb/train folder and the file name is confirmed as "via_region_data.json". If not, please rename it to "via_region_data.json", as shown in Fig. 12.


Fig. 12. Place the.png

Fig. 12. Place the output tag json file in the data/pcb/train folder


※Test samples in the Data/pcb/val folder, the same to do the step1 to the step6.



  1. Open 1_train.py to start training


If you want to use GPU acceleration, you can execute R8_Python3.6_GPU.bat. If you use CPU acceleration, you can execute R8_Python3.6_CPU.bat => "File" => "Open" => "to enter the solution folder under OpenR8" => "select Image_PCB_MaskRCNN_Keras folder" => "select 1_train.py to open", as shown in Fig. 13, Fig. 14.


Fig. 13. Select 1_train.png

Fig. 13. Select 1_train.py.


Fig. 14. Open 1_train.png

Fig. 14. Open 1_train.py.


※ If the sample is not placed in "data/pcb/", you need to set the dataPath additionally, as shown in Fig. 15.


Fig. 15. Set dataPath.png

Fig. 15. Set dataPath


※ Before running, if you don't want to use the previous model, delete all h5 files (but keep mask_rcnn_coco.h5). If you are unfamiliar, don't delete them.


※ Before running, if you want to change the "training model name", "Training number", "Classification category" ... such as parameter setting, please see Chapter VII-parameter introduction.


Press "Run" to start the training sample until "Press any key to continue..." is displayed.


※ When you press "Run", python has the potential to jump out of the error message, see Chapter VI-python error message.



  1. Execute 2_inference.py to see the results of the training.


After the completion of the “1_train.py training”, open “2_inference.py” to test images, as shown in Fig. 16, Fig. 17.


Fig. 16. Selete 2_inference.png

Fig. 16. Selete 2_inference.py.


Fig. 17. Open 2_inference.png

Fig. 17. Open 2_inference.py.


Fill in the sample image path to be trained and the trained h5 path, as shown in Fig. 18 and Fig. 19.

※If you have executed 1_train.py and successfully trained the model, be sure to confirm that the h5 file name in Fig. 19 is consistent with the training.


Fig. 18. Fill in the test sample path.png

Fig. 18. Fill in the test sample path.


Fig. 19. Fill in the h5 pretrained model path to be trained.png

Fig. 19. Fill in the h5 pretrained model path to be trained.


Pressing the execution to see the result, Mask_R_CNN and other ways of displaying the results are not the same. If there is a category, the area will be colored. Otherwise, if nothing is judged, it will be gray, as shown in Fig. 20, in the capacitor. The location is colored and the representation is marked.


Fig. 20. Test results for 2_inference.png

Fig. 20. Test results for 2_inference.py.



  1. python error message


※display “ImportError: No module named 'skimage'”, as soon in Fig. 21:

Solution: pip install scikit-image (pip usage please install pip according to Python installation method)


Fig. 21. Display.png

Fig. 21. Display “ImportError: No module named 'skimage'”



  1. Parameter introduction


※Change Read h5 File name: Fig. 22.

※Set the number of categories, Name: Fig. 23.

※Change the json Name: Fig. 24.

※Set the number of GPUs: Fig. 25.

“Config.py” there are also some parameter settings that may be set, please refer to them yourself.


Fig. 22. Change the read H5 file name in capacitor.png

Fig. 22. Change the read H5 file name in capacitor.py


Fig. 23. Setting the number of categories in capacitor.png

Fig. 23. Setting the number of categories in capacitor.py.


Fig. 24. Changing the read json name in capacitor.png

Fig. 24. Changing the read json name in capacitor.py.


Fig. 25. Changing the GPU count setting in capacitor.png

Fig. 25. Changing the GPU count setting in capacitor.py.


Recommended Article

OpenR8 Community Edition - AI Software for Everyone (Free Download)