a29e1eff6e78ea4a9d86e6c28a2b9f01048ddd6a
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / README.md
1 # PCB-defect-detection
2 ## Note 
3 This project code is forked from https://github.com/DetectionTeamUCAS/FPN_Tensorflow. I have only made minor changes on this wonderful and clear project. Thanks for their perfect code. I can learn and apply it to a new problem. The Chinese blog is: https://blog.csdn.net/dlh_sycamore/article/details/87886198
4 ##  PCB defect dataset
5 The Open Lab on Human Robot Interaction of Peking University has released the PCB defect dataset. If you need to do experiments on this dataset, please contact the dataset author. 
6
7 You can download at http://robotics.pkusz.edu.cn/resources/dataset/. or https://www.dropbox.com/s/32kolsaa45z2mpj/PCB_DATASET.zip?dl=0
8
9
10 6 types of defects are made by photoshop, a graphics editor published by Adobe Systems. The defects defined in the dataset are: missing hole, mouse bite, open circuit, short, spur, and spurious copper. 
11 For example:
12
13 ![1](a(missinghole).png)
14 ### Dataset Update
15 The paper of this project will be update.
16 However, the defect images of raw dataset are high-resolution. 
17 With the respect of such small dataset, data augmentation techniques are adopted before data training. The images are then cropped
18 into 600 × 600 sub-images, forming our training set and testing set with 9920 and 2508 images, respectively.
19
20 ### Some instructions: 
21
22 1、This augmented dataset is privately owned, if you want to use it in your paper, please contact me.
23
24 2、If you have questions, please open an issue, I will reply as soon as possible. If you don't have some difficult problem about this project, maybe you **don't need** to send me an email and add wechat, just open an issue.
25
26 3、Of course, if you have other research questions like dataset and defect detection, welcome to send me an email.
27
28 4、If you use our code, please access this paper and cite: 
29 Ding, Runwei, et al. "TDD-Net: A Tiny Defect Detection Network for Printed Circuit Boards." CAAI Transactions on Intelligence Technology (https://ieeexplore.ieee.org/document/8757220)(2019).
30
31 5、Chinese Blog: https://blog.csdn.net/dlh_sycamore/article/details/87886198
32
33 ## Download Model
34 Please download [resnet50_v1](http://download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz)、[resnet101_v1](http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz) pre-trained models on Imagenet, put it to $PATH_ROOT/data/pretrained_weights. 
35 ## My Development Environment
36 1、python2.7 (anaconda recommend)             
37 2、CUDA Version 8.0.44 , CUDNN=5.1.10           
38 3、[opencv(cv2)](https://pypi.org/project/opencv-python/)    
39 4、[tfplot](https://github.com/wookayin/tensorflow-plot)             
40 5、tensorflow == 1.121 
41
42 ## Compile
43 ```  
44 cd $PATH_ROOT/libs/box_utils/cython_utils
45 python setup.py build_ext --inplace
46 ```
47
48 ## Demo(available)
49
50 Then download the corresponding weights:
51
52 For Baiduyun: [weights](https://pan.baidu.com/s/1rvHjihG1fL499SqU28Nang). code:shac 
53
54 For Google driver: [weights](https://drive.google.com/drive/folders/1oEHrEslnM5DBKB-610j-gO-x9-e90VwR?usp=sharing)
55
56
57 And then put the trained weights into 
58 ```
59 TRAINED_CKPT = os.path.join(ROOT_PATH, 'output/trained_weights')
60 ```
61 It depends on your project path. Please read '$PATH_ROOT/libs/configs/cfgs.py'.
62
63 ```   
64 cd $PATH_ROOT/tools
65 python inference.py --data_dir='/PATH/TO/THE/TO/BE/DETECTED/IMAGES/' 
66                     --save_dir='/PATH/TO/SAVE/RESULTS/' 
67                     --GPU='0'
68 ```
69 After running this code, you will get the detected image in your 'save_dir' path.
70
71 Why didn't you get the correct output image when run the inference.py?
72
73 **Note!!! If you didn't load the correct checkpoint but the pretrain model, you need to change the checkpoint file (FPN_Res101_0117_OHEM/checkpoint), because this file contains the wrong path, and you need to change it with your file path!!!**
74
75 ## Train
76 If you want to train your own data, please follow this project: https://github.com/DetectionTeamUCAS/FPN_Tensorflow
77
78 1、Select a configuration file in the folder ($PATH_ROOT/libs/configs/) and copy its contents into cfgs.py. Modify parameters (such as CLASS_NUM, DATASET_NAME, VERSION, etc.) in $PATH_ROOT/libs/configs/cfgs.py    
79
80 2、Generate train and test floder
81
82 ```  
83 cd $PATH_ROOT/data/io/  
84 python divide_data.py 
85 ```    
86 You should check the image_path and xml_path in the 'divide_data.py'
87
88 2、Make tfrecord 
89
90 (1)Modify parameters (such as VOC_dir, xml_dir, image_dir, dataset, etc.) in $PATH_ROOT/data/io/convert_data_to_tfrecord.py   
91 ```  
92 cd $PATH_ROOT/data/io/  
93 python convert_data_to_tfrecord.py 
94 ```     
95
96 3、Train
97 ```  
98 cd $PATH_ROOT/tools
99 python train.py
100 ```
101
102 ## Eval
103 ```  
104 cd $PATH_ROOT/tools
105 python eval.py --eval_imgs='/PATH/TO/THE/TO/BE/EVALED/IMAGES/'  
106                --annotation_dir='/PATH/TO/TEST/ANNOTATION/'
107                --GPU='0'
108 ```   
109 After running this code, you will get the precision, recall and AP of per defect type.
110
111 ## Some results 
112 [the more results](https://github.com/Ixiaohuihuihui/PCB-defect-detection/tree/master/tools/inference_results)
113 ![1](01_missing_hole_01.jpg)
114 ![2](04_mouse_bite_10.jpg)
115
116 ## The Precision and Recall curve (PR)
117 ![3](TDD_results.jpg)