prath0029 commited on
Commit
9ec6587
·
verified ·
1 Parent(s): 597f120

Upload ZeroDefect YOLOv5 package (weights, ONNX, RKNN, metadata)

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. README.md +119 -3
  3. best.onnx +3 -0
  4. best.pt +3 -0
  5. best.rknn +3 -0
  6. data.yaml +12 -0
  7. infer_onnx.py +164 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ best.rknn filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,119 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - object-detection
5
+ - pytorch
6
+ - yolov5
7
+ - onnx
8
+ - rknn
9
+ - rv1106
10
+ - luckfox
11
+ - aircraft-inspection
12
+ - edge-ai
13
+ metrics:
14
+ - precision
15
+ - recall
16
+ - map
17
+ model-index:
18
+ - name: Zerodefect-1.0
19
+ results:
20
+ - task:
21
+ type: object-detection
22
+ name: Object Detection
23
+ dataset:
24
+ type: custom
25
+ name: Merged Aircraft Skin Defect & Corrosion Dataset
26
+ metrics:
27
+ - type: precision
28
+ value: 0.428
29
+ name: Precision
30
+ - type: recall
31
+ value: 0.358
32
+ name: Recall
33
+ - type: map
34
+ value: 0.319
35
+ name: mAP@0.5
36
+ - type: map
37
+ value: 0.235
38
+ name: mAP@0.5:0.95
39
+ ---
40
+
41
+ # ZeroDefect YOLOv5 Nano Model
42
+ ### Optimized Object Detection for Aircraft Skin Inspections (Edge-Deployable)
43
+
44
+ This repository contains the trained weights, ONNX export, and compiled RKNN model for detection of surface defects on aircraft skin. It is optimized for edge deployment on low-cost hardware like the **Luckfox Pico Max (RV1106 SoC with 0.5 TOPS NPU)**.
45
+
46
+ ## 📊 Classes & Keywords Map
47
+ The model detects the following **7 categories** of defects:
48
+ 1. `crack` — Structural cracks, fatigue lines
49
+ 2. `dent` — Impact dents, surface deformations
50
+ 3. `corrosion` — Rust, chemical wear, oxidation
51
+ 4. `scratch` — Superficial scrapes, paint scratches
52
+ 5. `paint-peel` — Flaking paint, coating degradation
53
+ 6. `missing-head` — Missing rivet heads or fasteners
54
+ 7. `defect` — Generic surface defect anomalies
55
+
56
+ ---
57
+
58
+ ## 📈 Training & Validation Metrics
59
+ * **Model Architecture**: YOLOv5 Nano (`yolov5n.pt` backbone)
60
+ * **Epochs**: 150
61
+ * **Dataset Size**: 22k+ training images, 2.2k+ validation images (unified from 36 COCO sources)
62
+ * **Validation Performance**:
63
+ * **Precision**: `0.428`
64
+ * **Recall**: `0.358`
65
+ * **mAP@0.5**: `0.319`
66
+ * **mAP@0.5:0.95**: `0.235`
67
+
68
+ ### Category mAP@0.5 Highlights:
69
+ * `missing-head` (rivets): **0.526**
70
+ * `dent`: **0.441**
71
+ * `scratch`: **0.355**
72
+ * `paint-peel`: **0.311**
73
+ * `crack`: **0.276**
74
+
75
+ ---
76
+
77
+ ## 📁 Repository Contents
78
+
79
+ * `best.pt` — PyTorch model weights (YOLOv5 format, ~3.8 MB)
80
+ * `best.onnx` — Exported ONNX model graph (opset 12, simplified, ~7.2 MB)
81
+ * `best.rknn` — Compiled RKNN binary optimized for RV1106 NPU (INT8 quantized, ~2.6 MB)
82
+ * `data.yaml` — Dataset metadata configuration
83
+ * `infer_onnx.py` — Simple python script to run inference on custom images using `best.onnx`
84
+
85
+ ---
86
+
87
+ ## 🚀 Quick Start: ONNX Inference (Python)
88
+
89
+ To run inference on a local image using the ONNX model, download `best.onnx` and `infer_onnx.py` and run:
90
+
91
+ ```bash
92
+ pip install onnxruntime opencv-python numpy
93
+ python infer_onnx.py --model best.onnx --image test.jpg --conf 0.25
94
+ ```
95
+
96
+ ---
97
+
98
+ ## 📟 Hardware Deployment (Luckfox Pico Max / RV1106 NPU)
99
+
100
+ The `best.rknn` model is pre-compiled and quantized to **INT8** for direct execution on the RV1106 NPU.
101
+
102
+ 1. **Push model to the board**:
103
+ ```bash
104
+ adb push best.rknn /userdata/
105
+ ```
106
+ 2. **Cross-compile the C++ inference runner**:
107
+ Compile the YOLOv5 demo from `rknn_model_zoo` using the RV1106 GCC toolchain:
108
+ ```bash
109
+ ./build-linux.sh -t rv1106 -a armv7l -d yolov5
110
+ ```
111
+ 3. **Execute on board**:
112
+ ```bash
113
+ ./rknn_yolov5_demo /userdata/best.rknn test.jpg
114
+ ```
115
+
116
+ ---
117
+
118
+ ## 📜 License
119
+ This model is released under the **MIT License**.
best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc2a70634e8df86c0890b4b455bf2fa0aec22c1790ee214659149b45c4b5bf05
3
+ size 7513583
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:add51aa71cf5dc72d4ae72072d1f36f2a0a64a2a0d7ab8f8d14faeac5ffd636d
3
+ size 3778031
best.rknn ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a6682605a433a6f067ce3e104ad2e6a7810879be080f24074033a3fbdc0cfc98
3
+ size 2748405
data.yaml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ path: ./
2
+ train: images/train
3
+ val: images/val
4
+ nc: 7
5
+ names:
6
+ - crack
7
+ - dent
8
+ - corrosion
9
+ - scratch
10
+ - paint-peel
11
+ - missing-head
12
+ - defect
infer_onnx.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import cv2
3
+ import numpy as np
4
+ import onnxruntime as ort
5
+
6
+ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=False, scaleFill=False, scaleup=True, stride=32):
7
+ # Resize and pad image while meeting stride-multiple constraints
8
+ shape = im.shape[:2] # current shape [height, width]
9
+ if isinstance(new_shape, int):
10
+ new_shape = (new_shape, new_shape)
11
+
12
+ # Scale ratio (new / old)
13
+ r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
14
+ if not scaleup: # only scale down, do not scale up (for better val mAP)
15
+ r = min(r, 1.0)
16
+
17
+ # Compute padding
18
+ ratio = r, r # width, height ratios
19
+ new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
20
+ dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
21
+ if auto: # minimum rectangle
22
+ dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
23
+ elif scaleFill: # stretch
24
+ dw, dh = 0.0, 0.0
25
+ new_unpad = new_shape[1], new_shape[0]
26
+ ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
27
+
28
+ dw /= 2 # divide padding into 2 sides
29
+ dh /= 2
30
+
31
+ if shape[::-1] != new_unpad: # resize
32
+ im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
33
+ top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
34
+ left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
35
+ im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
36
+ return im, ratio, (dw, dh)
37
+
38
+ def xywh2xyxy(x):
39
+ # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
40
+ y = np.copy(x)
41
+ y[..., 0] = x[..., 0] - x[..., 2] / 2 # top left x
42
+ y[..., 1] = x[..., 1] - x[..., 3] / 2 # top left y
43
+ y[..., 2] = x[..., 0] + x[..., 2] / 2 # bottom right x
44
+ y[..., 3] = x[..., 1] + x[..., 3] / 2 # bottom right y
45
+ return y
46
+
47
+ def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None):
48
+ # Runs Non-Maximum Suppression (NMS) on inference results
49
+ nc = prediction.shape[2] - 5 # number of classes
50
+ xc = prediction[..., 4] > conf_thres # candidates
51
+
52
+ # Settings
53
+ max_wh = 7680 # maximum box width and height
54
+ max_det = 300 # maximum detections per image
55
+ redundant = True # require redundant detections
56
+ multi_label = nc > 1 # multiple labels per box (adds AMAP of class overlap)
57
+
58
+ output = [np.zeros((0, 6))] * prediction.shape[0]
59
+ for xi, x in enumerate(prediction): # image index, image inference
60
+ x = x[xc[xi]] # confidence filter
61
+
62
+ if not x.shape[0]:
63
+ continue
64
+
65
+ # Compute candidate box coordinates [x1, y1, x2, y2]
66
+ box = xywh2xyxy(x[:, :4])
67
+
68
+ # Confidences & class probabilities
69
+ conf = x[:, 4:5]
70
+ class_prob = x[:, 5:]
71
+ j = np.argmax(class_prob, axis=1, keepdims=True)
72
+ c_prob = np.take_along_axis(class_prob, j, axis=1)
73
+
74
+ # Filter detections by confidence score
75
+ x = np.concatenate((box, c_prob * conf, j.astype(np.float32)), axis=1)
76
+ x = x[x[:, 4] > conf_thres]
77
+
78
+ if not x.shape[0]:
79
+ continue
80
+
81
+ # Sort by confidence
82
+ x = x[x[:, 4].argsort()[::-1]]
83
+
84
+ # Batched NMS
85
+ c = x[:, 5:6] * max_wh # class offset
86
+ boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
87
+
88
+ # Compute NMS using OpenCV
89
+ indices = cv2.dnn.NMSBoxes(
90
+ bboxes=boxes.tolist(),
91
+ scores=scores.tolist(),
92
+ score_threshold=conf_thres,
93
+ nms_threshold=iou_thres
94
+ )
95
+
96
+ if len(indices) > 0:
97
+ if isinstance(indices, np.ndarray):
98
+ indices = indices.flatten()
99
+ x = x[indices[:max_det]]
100
+ output[xi] = x
101
+
102
+ return output
103
+
104
+ def main():
105
+ parser = argparse.ArgumentParser()
106
+ parser.add_argument("--model", type=str, required=True, help="ONNX model path")
107
+ parser.add_argument("--image", type=str, required=True, help="Input image path")
108
+ parser.add_argument("--output", type=str, default="output.jpg", help="Output image path")
109
+ parser.add_argument("--conf", type=float, default=0.25, help="Confidence threshold")
110
+ parser.add_argument("--iou", type=float, default=0.45, help="IoU threshold")
111
+ args = parser.parse_args()
112
+
113
+ # Load image
114
+ img0 = cv2.imread(args.image)
115
+ if img0 is None:
116
+ print(f"Error: Could not read image {args.image}")
117
+ return
118
+
119
+ # Letterbox resize
120
+ img, ratio, (dw, dh) = letterbox(img0, new_shape=(640, 640), auto=False)
121
+
122
+ # Preprocess
123
+ img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
124
+ img = np.ascontiguousarray(img).astype(np.float32) / 255.0 # normalize
125
+ if len(img.shape) == 3:
126
+ img = np.expand_dims(img, 0)
127
+
128
+ # ONNX session
129
+ session = ort.InferenceSession(args.model)
130
+ input_name = session.get_inputs()[0].name
131
+ outputs = session.run(None, {input_name: img})
132
+
133
+ # Run NMS
134
+ detections = non_max_suppression(outputs[0], conf_thres=args.conf, iou_thres=args.iou)[0]
135
+
136
+ # Classes
137
+ classes = ["crack", "dent", "corrosion", "scratch", "paint-peel", "missing-head", "defect"]
138
+ colors = [
139
+ (0, 0, 255), (0, 255, 0), (255, 0, 0),
140
+ (0, 255, 255), (255, 0, 255), (255, 255, 0), (128, 128, 128)
141
+ ]
142
+
143
+ # Scale boxes back to original image size
144
+ if len(detections) > 0:
145
+ detections[:, [0, 2]] -= dw
146
+ detections[:, [1, 3]] -= dh
147
+ detections[:, :4] /= ratio[0]
148
+
149
+ # Draw boxes
150
+ for det in detections:
151
+ x1, y1, x2, y2, conf, cls = det
152
+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
153
+ cls_id = int(cls)
154
+ label = f"{classes[cls_id]} {conf:.2f}"
155
+ color = colors[cls_id % len(colors)]
156
+
157
+ cv2.rectangle(img0, (x1, y1), (x2, y2), color, 2)
158
+ cv2.putText(img0, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
159
+
160
+ cv2.imwrite(args.output, img0)
161
+ print(f"🎉 Saved detection result to {args.output} (found {len(detections)} defects)")
162
+
163
+ if __name__ == "__main__":
164
+ main()