defect-detection-yolov5
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.5%) to scientific vocabulary
Repository
Basic Info
- Host: GitHub
- Owner: toncula
- License: agpl-3.0
- Language: Jupyter Notebook
- Default Branch: main
- Size: 70.9 MB
Statistics
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Defect-Detection-yolov5
This project is based on ultralytics' yolov5 project. The original project can be found here
This model are designed for a subproject of defect detection. It can detect Multiple equipment components in single image or video stream.
Requirements
- Python 3.8 or later with all [requirements.txt]
- PyTorch 1.7.0 or later with CUDA and torchvision
create virtual environment
bash
python -m venv Defect-Detection
source Defect-Detection/bin/activate
install requirements
bash
cd $project_path
pip install -r requirements.txt
Raw data processing
Data should store in yolov5-format dir tree as follow:
markdown
yolov5_data
sub_yolo_data
images
train
val
labels
train
val
Where images should be .jpg format and labels should be yolo .txt format.
For raw data in folder, way to process data is as follow:
Seperate mixed data
In each data folder,images(.jpg files and .JPG files) and labels(.txt files) are mixed together. We need to seperate them into two folders and rename them.
Code for replacing space with underscore
bash
data_dir="~/Documents/Defect-Detection-yolov5/data/raw/"
cd $data_dir
for f in *; do mv "$f" `echo $f | tr ' ' '_'`; done
Code for seperating images and labels
bash
data_dir="~/Documents/Defect-Detection-yolov5/data/raw/"
touch mvjpg.txt
touch mvxml.txt
find $data_dir -iregex .*\.jpg > mvjpg.txt
find $data_dir -iregex .*\.xml > mvxml.txt
mkdir $data_dir/images
mkdir $data_dir/labels
for i in $(cat mvjpg.txt); do (mv $i $data_dir/images); done
for i in $(cat mvxml.txt); do (mv $i $data_dir/labels); done
rm mvjpg.txt
rm mvxml.txt
Deal with images with wrong label in the dataset
Labeling errors are common in the dataset, so we need to deal with them. For some unknown reason, labels of some images are rotated by 90 degrees(seems randomly clockwise or counterclockwise).
bash
# find the images with wrong label
python data/wrong_size.py
The outout is a txt file with the path of images with wrong label.including rotated images and images with incorrect label for other reasons.then seperate them from the dataset.
bash
data_dir="/Users/wzilai/Documents/Defect-Detection-yolov5/data/raw/"
mkdir $data_dir/bad_examples/images
mkdir $data_dir/bad_examples/labels
for i in $(cat $data_dir/wrong_size.txt); do mv $data_dir/images/$i $data_dir/bad_examples/images; done
for i in $(cat $data_dir/wrong_size.txt); do mv $data_dir/labels/$i $data_dir/bad_examples/labels; done
You can fix the label of these images manully or just use the remaining images to train the model.
Change label format
raw labels are stored in xml format,but yolo model need input in yolo-format. prepare.py can change xml format to yolo-format and separate data into train set and validation set. before run prepare.py, you need to change class name and yolo-format dir name in prepare.py. dir create is not fully-automatic in prepare.py, you need to create dir by yourself for now.
data dir tree
markdown
yolov5_data
sub_yolo_data
images
train
val
labels
train
val
Create yaml file
yaml file is used to record the project information. After run prepare.py, create yaml file for dataset, then change train/test image path and class name in yaml file. train.py will automatically read yaml file, and find labels.
Sample yolov5 yaml file
yaml
path: ./data/dataset
#train/val/test dir are subdirs of path
train: images/train
val: images/val
test: image/test #can be empty if no test set
#Number of classes
nc: 3
#Classes
names:
0: cat
1: dog
2: person
training
track and visualize
yolov5
python
!pip install comet_ml
or
python
!pip install wandb
import wandb
wandb.login()
train
```bash python train.py --img 640 //image size --batch 16 //batch size --epochs 300 //epochs --data withoutddityxtg.yaml //yaml file path --weights yolov5s.pt //pretrained weights --device 0 //gpu id, -1 for cpu, 0,1,2,3,... for multi-gpu --hyp data/hyp.scratch-low.yaml //hyperparameters
```
you can change hyperparameters in hyp.yaml file.
result for 300 epochs:
markdown
Class Images Instances P R mAP50
all 182 179 0.516 0.583 0.541 0.282
cysb_sgz 182 3 0.174 0.667 0.159 0.0843
SF6ylb 182 3 0.556 0.333 0.34 0.238
drq 182 31 0.487 0.516 0.344 0.14
ecjxh 182 6 0.491 0.324 0.456 0.254
drqgd 182 5 0.295 0.6 0.445 0.248
cysb_lqq 182 23 0.475 0.522 0.508 0.208
cysb_qtjdq 182 12 0.356 0.583 0.547 0.221
xldlb 182 15 0.906 0.733 0.778 0.439
ywj 182 2 1 0.977 0.995 0.505
jdyxx 182 6 0.352 0.333 0.283 0.231
bmwh 182 2 0.798 0.5 0.828 0.133
xmbhzc 182 1 0.38 1 0.995 0.796
pzq 182 22 0.317 0.318 0.256 0.115
jyh 182 13 0.923 0.846 0.853 0.472
ywc 182 10 0.086 0.2 0.052 0.015
cysb_cyg 182 25 0.657 0.88 0.814 0.415

Detect
bash
!python detect.py --weights ~/Defect-Detection-yolov5/runs/train/exp21/weights/best.pt --source your_source
markdown
Usage - sources:
--source 0 # webcam
img.jpg # image
vid.mp4 # video
screen # screenshot
path/ # directory
list.txt # list of images
list.streams # list of streams
'path/*.jpg' # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
Example

Owner
- Name: Zilai Wang
- Login: toncula
- Kind: user
- Repositories: 1
- Profile: https://github.com/toncula
Xi'an Jiaotong University
GitHub Events
Total
Last Year
Dependencies
- pytorch/pytorch 2.0.0-cuda11.7-cudnn8-runtime build
- gcr.io/google-appengine/python latest build
- Pillow >=7.1.2
- PyYAML >=5.3.1
- gitpython >=3.1.30
- matplotlib >=3.3
- numpy >=1.22.2
- opencv-python >=4.1.1
- pandas >=1.1.4
- psutil *
- requests >=2.23.0
- scipy >=1.4.1
- seaborn >=0.11.0
- setuptools >=65.5.1
- thop >=0.1.1
- torchvision >=0.8.1
- tqdm >=4.64.0
- ultralytics >=8.0.147
- Flask ==2.3.2
- gunicorn ==19.10.0
- pip ==21.1
- werkzeug >=2.2.3