aliffatulmf commited on
Commit
ae84cff
·
1 Parent(s): 31781f0

feat: add SmokeGuard models for HuggingFace

Browse files
README.md CHANGED
@@ -1,3 +1,61 @@
1
  ---
2
  license: mit
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ tags:
4
+ - yolov5
5
+ - smoking-detection
6
+ - object-detection
7
+ - onnx
8
+ - pytorch
9
  ---
10
+
11
+ # SmokeGuard Models
12
+
13
+ YOLOv5 models untuk deteksi aktivitas merokok secara real-time.
14
+
15
+ ## Models
16
+
17
+ | Model | Format | Size | Description |
18
+ |-------|--------|------|-------------|
19
+ | `best_checkpoint.pt` | PyTorch | ~40 MB | Model training terbaik |
20
+ | `last_checkpoint.pt` | PyTorch | ~40 MB | Checkpoint terakhir |
21
+ | `best_checkpoint.onnx` | ONNX (FP32) | ~80 MB | Export ONNX untuk inference |
22
+ | `best_checkpoint_int8.onnx` | ONNX (INT8) | ~20 MB | Quantized untuk CPU inference |
23
+
24
+ ## Usage
25
+
26
+ ### PyTorch
27
+
28
+ ```python
29
+ import torch
30
+
31
+ model = torch.hub.load("ultralytics/yolov5", "custom", path="best_checkpoint.pt")
32
+ results = model("image.jpg")
33
+ results.show()
34
+ ```
35
+
36
+ ### ONNX Runtime
37
+
38
+ ```python
39
+ import onnxruntime as ort
40
+ import numpy as np
41
+
42
+ session = ort.InferenceSession("best_checkpoint_int8.onnx")
43
+ input_name = session.get_inputs()[0].name
44
+ input_data = np.random.randn(1, 3, 640, 640).astype(np.float32)
45
+ output = session.run(None, {input_name: input_data})
46
+ ```
47
+
48
+ ## Quantization
49
+
50
+ Untuk quantization ONNX model, lihat notebook `quantisize.ipynb`.
51
+
52
+ ## Citation
53
+
54
+ ```bibtex
55
+ @article{IPI4527801,
56
+ title = "IMPLEMENTASI METODE YOLOv5 PADA SISTEM PENDETEKSI ROKOK DI AREA BEBAS ASAP ROKOK",
57
+ journal = "Institut Penelitian Matematika, Komputer, Keperawatan, Pendidikan dan Ekonomi (IPM2KPE)",
58
+ year = "2024",
59
+ author = "Fathoni, Aliffatul Majid; Zuliarso, Eri"
60
+ }
61
+ ```
best_checkpoint.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea3eb0d895c7ad9777f7cb991f4d9f634fd0bea0dc3a672b5a18ed70848b0a56
3
+ size 83890343
best_checkpoint.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f6f8c39a73f4ff7328482f18a7a48dd1deb2925446d9c92ab3b658ca32f784f6
3
+ size 42234324
best_checkpoint_int8.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0b19100820040a6927830526f2f448984d78c518585ae0e773fe8bb6348866b0
3
+ size 21512155
last_checkpoint.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f18948251bdef9e5ff5774d7f8e15fe0d2ac3fd7466a8bdb2b945051bc384467
3
+ size 42234324
quantisize.ipynb ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "d42d2f51",
6
+ "metadata": {},
7
+ "source": [
8
+ "# ONNX Quantization\n",
9
+ "\n",
10
+ "Quantization ONNX model ke INT8 dengan preprocessing."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "411d7be9",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "import os\n",
21
+ "import numpy as np\n",
22
+ "import onnx\n",
23
+ "from pathlib import Path\n",
24
+ "from onnxruntime.quantization import quantize_static, QuantType, CalibrationDataReader"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "b87dc761",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "def get_size_mb(path):\n",
35
+ " return os.path.getsize(path) / (1024**2)"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": null,
41
+ "id": "a3af9a45",
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "ONNX_MODEL = \"/content/drive/MyDrive/models/best_checkpoint.onnx\"\n",
46
+ "ONNX_PREPROCESSED = str(Path(ONNX_MODEL).with_suffix('')) + \"_preprocessed.onnx\"\n",
47
+ "ONNX_INT8 = str(Path(ONNX_MODEL).with_suffix('')) + \"_int8.onnx\"\n",
48
+ "\n",
49
+ "if not os.path.exists(ONNX_MODEL):\n",
50
+ " raise FileNotFoundError(f\"Model not found: {ONNX_MODEL}\")\n",
51
+ "\n",
52
+ "print(f\"Input: {Path(ONNX_MODEL).name} ({get_size_mb(ONNX_MODEL):.2f} MB)\")"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "markdown",
57
+ "id": "bde9da41",
58
+ "metadata": {},
59
+ "source": [
60
+ "## 1. Preprocessing"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "execution_count": null,
66
+ "id": "f89cfbb8",
67
+ "metadata": {},
68
+ "outputs": [],
69
+ "source": [
70
+ "!python -m onnxruntime.quantization.preprocess --input {ONNX_MODEL} --output {ONNX_PREPROCESSED}\n",
71
+ "\n",
72
+ "print(f\"Preprocessed: {get_size_mb(ONNX_PREPROCESSED):.2f} MB\")"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "markdown",
77
+ "id": "433bb1f9",
78
+ "metadata": {},
79
+ "source": [
80
+ "## 2. Calibration Data Reader"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": null,
86
+ "id": "ec3cb386",
87
+ "metadata": {},
88
+ "outputs": [],
89
+ "source": [
90
+ "class CalibrationReader(CalibrationDataReader):\n",
91
+ " def __init__(self, model_path, num_samples=10):\n",
92
+ " model = onnx.load(model_path)\n",
93
+ " input_shape = tuple([d.dim_value for d in model.graph.input[0].type.tensor_type.shape.dim])\n",
94
+ " self.data = [np.random.randn(*input_shape).astype(np.float32) for _ in range(num_samples)]\n",
95
+ " self.input_name = model.graph.input[0].name\n",
96
+ " self.enum_index = 0\n",
97
+ " \n",
98
+ " def get_next(self):\n",
99
+ " if self.enum_index >= len(self.data):\n",
100
+ " return None\n",
101
+ " input_dict = {self.input_name: self.data[self.enum_index]}\n",
102
+ " self.enum_index += 1\n",
103
+ " return input_dict"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "markdown",
108
+ "id": "d1080068",
109
+ "metadata": {},
110
+ "source": [
111
+ "## 3. INT8 Quantization"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "66501947",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "calibration_reader = CalibrationReader(ONNX_PREPROCESSED)\n",
122
+ "\n",
123
+ "quantize_static(\n",
124
+ " model_input=ONNX_PREPROCESSED,\n",
125
+ " model_output=ONNX_INT8,\n",
126
+ " calibration_data_reader=calibration_reader,\n",
127
+ " weight_type=QuantType.QUInt8,\n",
128
+ " optimize_model=False\n",
129
+ ")\n",
130
+ "\n",
131
+ "print(f\"INT8: {get_size_mb(ONNX_INT8):.2f} MB ({(1 - get_size_mb(ONNX_INT8)/get_size_mb(ONNX_MODEL)) * 100:.1f}% reduction)\")"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "markdown",
136
+ "id": "0a20622e",
137
+ "metadata": {},
138
+ "source": [
139
+ "## 4. Summary"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": null,
145
+ "id": "05d66ea4",
146
+ "metadata": {},
147
+ "outputs": [],
148
+ "source": [
149
+ "print(\"=\" * 50)\n",
150
+ "print(f\"{'Model':<15} {'Size (MB)':<15} {'Reduksi':<10}\")\n",
151
+ "print(\"-\" * 50)\n",
152
+ "print(f\"{'Original':<15} {get_size_mb(ONNX_MODEL):.2f}{'':<8} baseline\")\n",
153
+ "print(f\"{'Preprocessed':<15} {get_size_mb(ONNX_PREPROCESSED):.2f}{'':<8} {(1-get_size_mb(ONNX_PREPROCESSED)/get_size_mb(ONNX_MODEL))*100:.1f}%\")\n",
154
+ "print(f\"{'INT8':<15} {get_size_mb(ONNX_INT8):.2f}{'':<8} {(1-get_size_mb(ONNX_INT8)/get_size_mb(ONNX_MODEL))*100:.1f}%\")\n",
155
+ "print(\"=\" * 50)"
156
+ ]
157
+ }
158
+ ],
159
+ "metadata": {
160
+ "kernelspec": {
161
+ "display_name": "Python 3",
162
+ "language": "python",
163
+ "name": "python3"
164
+ },
165
+ "language_info": {
166
+ "name": "python",
167
+ "version": "3.11.15"
168
+ }
169
+ },
170
+ "nbformat": 4,
171
+ "nbformat_minor": 5
172
+ }