BlueprintParse-R50: FP32 ONNX (5 sanitary fixture classes), inference + export code, cards
Browse files- LICENSE +202 -0
- MODEL_CARD.ko.md +115 -0
- NOTICE +13 -0
- README.md +131 -0
- blueprintparse.py +160 -0
- bp_fp32.onnx +3 -0
- export_onnx.py +130 -0
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright [yyyy] [name of copyright owner]
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
MODEL_CARD.ko.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BlueprintParse-R50 (한국어 카드)
|
| 2 |
+
|
| 3 |
+
🇺🇸 English card: [README.md](./README.md)
|
| 4 |
+
|
| 5 |
+
> **먼저 읽을 것**
|
| 6 |
+
> - **위생기구 5종 전용이다.** 가스레인지·변기·세면대·싱크대·욕조만 찾는다. 벽·공간·치수·문자와 그 밖의 객체는 찾지도 주장하지도 않는다.
|
| 7 |
+
> - **수량 자동 산출은 COUNT_NO_GO다.** 학습에 없던 novel 도면 53장에서 도면 단위 정확 일치가 **0.566**(95% CI 0.434–0.698)으로, 게이트 0.90에 크게 못 미쳤다.
|
| 8 |
+
> - **개수는 사람이 항목마다 확인하는 초안까지만 쓴다.**
|
| 9 |
+
|
| 10 |
+
한국 건축 평면도 이미지에서 위 5종을 찾는 Faster R-CNN(ResNet-50 FPN v2) 검출기다. FP32 ONNX 그래프 하나로 내보냈고, onnxruntime으로 CPU와 GPU에서 모두 돈다. 클래스·점수·박스를 내고, 박스는 원본 이미지 픽셀 좌표로 되돌려 준다.
|
| 11 |
+
|
| 12 |
+
## 0. 쓰는 법
|
| 13 |
+
|
| 14 |
+
```bash
|
| 15 |
+
pip install onnxruntime pillow numpy huggingface_hub # CUDA 는 onnxruntime-gpu
|
| 16 |
+
huggingface-cli download ThakiCloud/BlueprintParse-R50 --local-dir BlueprintParse-R50
|
| 17 |
+
cd BlueprintParse-R50
|
| 18 |
+
python blueprintparse.py --selftest # 합성 도면으로 그래프가 뜨고 도는지 확인
|
| 19 |
+
python blueprintparse.py floor_plan.png --threads 16
|
| 20 |
+
python blueprintparse.py floor_plan.png --providers CUDAExecutionProvider,CPUExecutionProvider
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
```python
|
| 24 |
+
from blueprintparse import BlueprintParse
|
| 25 |
+
bp = BlueprintParse(threads=16) # ONNX sha256 을 대조한다
|
| 26 |
+
out = bp(open("floor_plan.png", "rb").read())
|
| 27 |
+
print(out["counts_draft"]) # 초안일 뿐이다
|
| 28 |
+
for d in out["detections"]:
|
| 29 |
+
print(d["class"], d["score"], d["box_xyxy"])
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
전처리는 학습 때와 같다. 회색조로 바꾸고, 긴 변이 2048px(모델 메타데이터 값)을 넘으면 줄이고, 0~1로 나눈 뒤 3채널로 복제한다. 점수 0.5 이상은 `detections`, 0.3 이상 0.5 미만은 `uncertain_candidates`로 간다. `review_required`는 항상 참이다.
|
| 33 |
+
|
| 34 |
+
ONNX 입출력: 입력 `image` [3,H,W] float32(H·W 동적), 출력 `boxes` [N,4](입력 텐서 좌표 xyxy)·`labels` [N] int64(1..5)·`scores` [N]. opset 17. 메타데이터에 `class_ids`·`max_side`·`precision`·`opset`·원본 체크포인트 sha256이 있다.
|
| 35 |
+
|
| 36 |
+
## 1. 파일
|
| 37 |
+
|
| 38 |
+
| 파일 | 내용 |
|
| 39 |
+
|---|---|
|
| 40 |
+
| `bp_fp32.onnx` | FP32 ONNX 검출기, sha256 `5ed19b8bccc6d5abfdeb5dd26f153c28379115b100a9a4ce479479e10bffb773` |
|
| 41 |
+
| `blueprintparse.py` | 추론(전처리·박스 좌표 복원·개수 초안)과 합성 셀프테스트 |
|
| 42 |
+
| `export_onnx.py` | 직접 학습한 torchvision 체크포인트에서 같은 ONNX를 다시 만들고, ONNX와 PyTorch 출력을 대조 |
|
| 43 |
+
|
| 44 |
+
학습 데이터의 도면·크롭·라벨·오버레이·예측 샘플은 이 저장소에 없다.
|
| 45 |
+
|
| 46 |
+
## 2. 결과
|
| 47 |
+
|
| 48 |
+
수치는 전부 우리가 직접 돌려 내부 실험 원장에 남긴 값이다. 공식 test 분할은 공개한 이 ONNX 파일로 한 번만 채점했다.
|
| 49 |
+
|
| 50 |
+
### 검출
|
| 51 |
+
|
| 52 |
+
| 분할 | 장 수 | mAP50-95 | AP50 |
|
| 53 |
+
|---|---|---|---|
|
| 54 |
+
| 공식 test | 1,014 | **0.926** | **0.9805** |
|
| 55 |
+
| novel(학습과 근접중복 제거) | 53 | 0.7875 | 0.922 |
|
| 56 |
+
|
| 57 |
+
test AR@100은 0.9447이다. ONNX FP32와 PyTorch 학습 체크포인트의 차이는 test −0.0009 / 0.0, novel −0.0026 / −0.0005다.
|
| 58 |
+
|
| 59 |
+
클래스별 AP50:
|
| 60 |
+
|
| 61 |
+
| 클래스 | test (1,014) | novel (53) |
|
| 62 |
+
|---|---|---|
|
| 63 |
+
| 가스레인지 | 0.9883 | 0.9637 |
|
| 64 |
+
| 변기 | 0.9793 | 0.9508 |
|
| 65 |
+
| 세면대 | 0.9886 | 0.9489 |
|
| 66 |
+
| 싱크대 | 0.9677 | 0.7896 |
|
| 67 |
+
| 욕조 | 0.9784 | 0.9572 |
|
| 68 |
+
|
| 69 |
+
학습 도면과 생김새가 다른 도면에 쓸 생각이라면 test가 아니라 novel 값(mAP50-95 0.7875)으로 계획을 세운다. 싱크대가 가장 많이 떨어진다.
|
| 70 |
+
|
| 71 |
+
### 개수 세기(수량 자동 산출이 COUNT_NO_GO인 이유)
|
| 72 |
+
|
| 73 |
+
novel 도면 53장, 정답 객체 400개, 점수 임계값 0.8(dev 분할에서 고름):
|
| 74 |
+
|
| 75 |
+
| 지표 | 값 | 게이트 |
|
| 76 |
+
|---|---|---|
|
| 77 |
+
| 도면 단위 정확 일치(5종 개수 모두 맞음) | 0.566 (0.434–0.698) | ≥ 0.90 |
|
| 78 |
+
| 클래스 칸별 평균 절대 오차 | 0.1925 | ≤ 0.05 |
|
| 79 |
+
| 오류 플래그 재현율 | 0.478 | ≥ 0.80 |
|
| 80 |
+
| 플래그가 안 붙은 도면의 정확 일치 | 0.684 | ≥ 0.95 |
|
| 81 |
+
|
| 82 |
+
완전 자동 게이트도, '플래그 없는 도면만 자동' 게이트도 떨어졌다. 신뢰구간 상한도 문턱 아래라 표본 크기 탓으로 돌릴 수 없다. 그래서 개수는 사람이 확인할 초안으로만 낸다.
|
| 83 |
+
|
| 84 |
+
### 지연(이미지 1장 p50, ms)
|
| 85 |
+
|
| 86 |
+
합성 1448×2048 입력. 열 순서: ONNX FP32 CUDA / ONNX FP16 CUDA / ONNX FP32 CPU / INT8 정적 CPU / INT8 동적 CPU.
|
| 87 |
+
|
| 88 |
+
| 장비 | FP32 CUDA | FP16 CUDA | FP32 CPU | INT8 정적 CPU | INT8 동적 CPU |
|
| 89 |
+
|---|---|---|---|---|---|
|
| 90 |
+
| B200 (CPU 32코어) | 26.1 | 24.69 | 786.1 | 1925.01 | 2831.59 |
|
| 91 |
+
| H200 (CPU 16코어) | 27.33 | 22.23 | 1204.46 | 2590.13 | 3607.11 |
|
| 92 |
+
| H100 (CPU 10코어, 대리 가중치) | 30.12 | 25.2 | 1953.59 | 2496.96 | 4100.12 |
|
| 93 |
+
|
| 94 |
+
공개하는 그래프는 FP32 하나다. 사전등록한 선택 규칙에서 INT8 동적은 dev mAP50-95가 0.0735 떨어졌고, INT8 정적은 정확도는 지켰지만 CPU에서 FP32보다 느렸다. FP16은 정확도는 지켰지만 B200에서 5%만 빨라서(규칙은 10% 이상) 채택하지 않았다. H100 값은 같은 구조의 대리 가중치로 쟀다. 실도면(B200, ONNX FP32 CUDA)에서 전처리까지 넣은 E2E는 p50 135 ms, 추론만 22.8 ms였고, CPU 32스레드에서는 889 ms였다. 도면당 검출 28개까지는 검출 수와 지연이 관계없었다.
|
| 95 |
+
|
| 96 |
+
## 3. 한계
|
| 97 |
+
|
| 98 |
+
- 5종만 본다(맨 위 참고). 검출 0건은 없다는 증거가 아니다.
|
| 99 |
+
- 수량 자동 산출은 COUNT_NO_GO다. 초안과 사람 확인까지만 쓴다.
|
| 100 |
+
- 공개 데이터셋 하나로만 평가했다. 고객 도면은 평가하지 않았다.
|
| 101 |
+
- 평가한 도면 중 가장 빽빽한 것이 객체 28개였다. 기구가 수백 개인 도면은 재지 않았다.
|
| 102 |
+
- 정답은 데이터셋 라벨 박스다. 라벨 품질 감사는 하지 않았다.
|
| 103 |
+
- novel 분할이 53장뿐이라 신뢰구간이 넓다.
|
| 104 |
+
|
| 105 |
+
## 4. 학습 데이터
|
| 106 |
+
|
| 107 |
+
출처: **AI 허브(AI-Hub) — 건축 도면 데이터(OBJ)(71465)**. 분할은 train 7,900 / dev 199 / test 1,014이고, novel 53장은 따로 뽑았다.
|
| 108 |
+
|
| 109 |
+
데이터 재배포 불가, 신청자 본인 내부 이용. 신청자 본인이 자기 환경에서만 학습에 썼다. 이 저장소에는 학습된 가중치와 코드만 있고, 도면·크롭·라벨 파일·서브셋은 없다. 다시 학습하려면 AI 허브에 본인 계정으로 데이터를 신청해야 한다.
|
| 110 |
+
|
| 111 |
+
초기화: torchvision `fasterrcnn_resnet50_fpn_v2` COCO 사전학습 가중치.
|
| 112 |
+
|
| 113 |
+
## 5. 라이선스
|
| 114 |
+
|
| 115 |
+
가중치와 코드는 Apache-2.0이다. 필수 데이터 출처 표기는 `NOTICE`에 있다. 실행 의존성은 모두 MIT·BSD·Apache-2.0 계열이다.
|
NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BlueprintParse-R50
|
| 2 |
+
Copyright 2026 ThakiCloud
|
| 3 |
+
|
| 4 |
+
Model weights and code in this repository are released under the Apache License, Version 2.0 (see LICENSE).
|
| 5 |
+
|
| 6 |
+
Training data attribution (required):
|
| 7 |
+
AI 허브(AI-Hub) — 건축 도면 데이터(OBJ)(71465)
|
| 8 |
+
The dataset itself is NOT included and is NOT redistributable. Only weights trained on it are published.
|
| 9 |
+
|
| 10 |
+
Third-party components:
|
| 11 |
+
- Architecture: torchvision fasterrcnn_resnet50_fpn_v2 (BSD-3-Clause). Training was initialized from the
|
| 12 |
+
torchvision COCO-pretrained weights for this architecture (weights="DEFAULT").
|
| 13 |
+
- Runtime dependencies (not bundled): onnxruntime (MIT), onnx (Apache-2.0), numpy (BSD-3-Clause), pillow (MIT-CMU).
|
README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- ko
|
| 5 |
+
- en
|
| 6 |
+
library_name: onnx
|
| 7 |
+
pipeline_tag: object-detection
|
| 8 |
+
tags:
|
| 9 |
+
- object-detection
|
| 10 |
+
- faster-rcnn
|
| 11 |
+
- floor-plan
|
| 12 |
+
- architecture
|
| 13 |
+
- onnx
|
| 14 |
+
- korean
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# BlueprintParse-R50
|
| 18 |
+
|
| 19 |
+
🇰🇷 Korean card: [MODEL_CARD.ko.md](./MODEL_CARD.ko.md)
|
| 20 |
+
|
| 21 |
+
> **Read this first**
|
| 22 |
+
> - **Five sanitary fixture classes only:** gas range, toilet, washbasin, kitchen sink, bathtub. Walls, rooms, dimensions, text and every other object are neither detected nor claimed.
|
| 23 |
+
> - **Automatic quantity takeoff: COUNT_NO_GO.** On 53 novel drawings, per-drawing exact count match was **0.566** (95% CI 0.434–0.698), far below our 0.90 gate.
|
| 24 |
+
> - **Use the counts only as a draft that a person checks item by item.**
|
| 25 |
+
|
| 26 |
+
Faster R-CNN (ResNet-50 FPN v2) detector for Korean architectural floor-plan images, exported to a single FP32 ONNX graph that runs on CPU or GPU with onnxruntime. It returns boxes, class and score for the five fixture classes, with boxes mapped back to original image pixels.
|
| 27 |
+
|
| 28 |
+
## 0. How to use
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
pip install onnxruntime pillow numpy huggingface_hub # onnxruntime-gpu for CUDA
|
| 32 |
+
huggingface-cli download ThakiCloud/BlueprintParse-R50 --local-dir BlueprintParse-R50
|
| 33 |
+
cd BlueprintParse-R50
|
| 34 |
+
python blueprintparse.py --selftest # synthetic drawing, checks the graph loads and runs
|
| 35 |
+
python blueprintparse.py floor_plan.png --threads 16
|
| 36 |
+
python blueprintparse.py floor_plan.png --providers CUDAExecutionProvider,CPUExecutionProvider
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
from blueprintparse import BlueprintParse
|
| 41 |
+
bp = BlueprintParse(threads=16) # verifies the ONNX sha256
|
| 42 |
+
out = bp(open("floor_plan.png", "rb").read())
|
| 43 |
+
print(out["counts_draft"]) # {"gas_range": 1, "toilet": 2, ...} draft only
|
| 44 |
+
for d in out["detections"]:
|
| 45 |
+
print(d["class"], d["score"], d["box_xyxy"])
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
Pre-processing (same as training): grayscale, shrink so the long side is at most 2048 px (read from model metadata), scale to 0..1, replicate to 3 channels. Detections with score ≥ 0.5 are returned; scores in [0.3, 0.5) go to `uncertain_candidates`. `review_required` is always true.
|
| 49 |
+
|
| 50 |
+
ONNX interface: input `image` [3,H,W] float32 (H, W dynamic); outputs `boxes` [N,4] (input-tensor xyxy), `labels` [N] int64 (1..5), `scores` [N]. Opset 17. Metadata holds `class_ids`, `max_side`, `precision`, `opset` and the source checkpoint sha256.
|
| 51 |
+
|
| 52 |
+
## 1. Files
|
| 53 |
+
|
| 54 |
+
| File | What it is |
|
| 55 |
+
|---|---|
|
| 56 |
+
| `bp_fp32.onnx` | FP32 ONNX detector, sha256 `5ed19b8bccc6d5abfdeb5dd26f153c28379115b100a9a4ce479479e10bffb773` |
|
| 57 |
+
| `blueprintparse.py` | inference (pre-processing, box mapping, counts draft) + synthetic self-test |
|
| 58 |
+
| `export_onnx.py` | reproduce the ONNX from your own torchvision checkpoint, and check ONNX vs PyTorch |
|
| 59 |
+
|
| 60 |
+
No drawings, crops, labels, overlays or sample predictions from the training data are in this repository.
|
| 61 |
+
|
| 62 |
+
## 2. Results
|
| 63 |
+
|
| 64 |
+
All numbers come from our own runs, recorded in an internal experiment ledger. The official test split was scored once with the published ONNX file.
|
| 65 |
+
|
| 66 |
+
### Detection
|
| 67 |
+
|
| 68 |
+
| Split | Images | mAP50-95 | AP50 |
|
| 69 |
+
|---|---|---|---|
|
| 70 |
+
| Official test split | 1,014 | **0.926** | **0.9805** |
|
| 71 |
+
| Novel drawings (near-duplicates of training removed) | 53 | 0.7875 | 0.922 |
|
| 72 |
+
|
| 73 |
+
AR@100 on the test split: 0.9447. ONNX FP32 vs the PyTorch training checkpoint: −0.0009 / 0.0 (test), −0.0026 / −0.0005 (novel).
|
| 74 |
+
|
| 75 |
+
Per-class AP50:
|
| 76 |
+
|
| 77 |
+
| Class | Test (1,014) | Novel (53) |
|
| 78 |
+
|---|---|---|
|
| 79 |
+
| gas_range | 0.9883 | 0.9637 |
|
| 80 |
+
| toilet | 0.9793 | 0.9508 |
|
| 81 |
+
| washbasin | 0.9886 | 0.9489 |
|
| 82 |
+
| kitchen_sink | 0.9677 | 0.7896 |
|
| 83 |
+
| bathtub | 0.9784 | 0.9572 |
|
| 84 |
+
|
| 85 |
+
The drop from test to novel (0.926 → 0.7875 mAP50-95) is the number to plan with for drawings that do not look like the training set. Kitchen sinks drop the most.
|
| 86 |
+
|
| 87 |
+
### Counting (why takeoff is COUNT_NO_GO)
|
| 88 |
+
|
| 89 |
+
Novel 53 drawings, 400 ground-truth objects, score threshold 0.8 (chosen on the dev split):
|
| 90 |
+
|
| 91 |
+
| Metric | Value | Gate |
|
| 92 |
+
|---|---|---|
|
| 93 |
+
| Per-drawing exact count match (all 5 classes right) | 0.566 (0.434–0.698) | ≥ 0.90 |
|
| 94 |
+
| Mean absolute error per class cell | 0.1925 | ≤ 0.05 |
|
| 95 |
+
| Error-flag recall | 0.478 | ≥ 0.80 |
|
| 96 |
+
| Exact match on drawings the model did not flag | 0.684 | ≥ 0.95 |
|
| 97 |
+
|
| 98 |
+
Both the fully automatic gate and the "auto only for unflagged drawings" gate failed, and the upper confidence bounds are still below the thresholds. Counts are therefore a draft for human review.
|
| 99 |
+
|
| 100 |
+
### Latency (p50 per image, ms)
|
| 101 |
+
|
| 102 |
+
Synthetic 1448×2048 input. Columns: ONNX FP32 on CUDA / ONNX FP16 on CUDA / ONNX FP32 on CPU / INT8 static on CPU / INT8 dynamic on CPU.
|
| 103 |
+
|
| 104 |
+
| Hardware | FP32 CUDA | FP16 CUDA | FP32 CPU | INT8 static CPU | INT8 dyn CPU |
|
| 105 |
+
|---|---|---|---|---|---|
|
| 106 |
+
| B200 (CPU 32 cores) | 26.1 | 24.69 | 786.1 | 1925.01 | 2831.59 |
|
| 107 |
+
| H200 (CPU 16 cores) | 27.33 | 22.23 | 1204.46 | 2590.13 | 3607.11 |
|
| 108 |
+
| H100 (CPU 10 cores, proxy weights) | 30.12 | 25.2 | 1953.59 | 2496.96 | 4100.12 |
|
| 109 |
+
|
| 110 |
+
Only the FP32 graph is published. Under our pre-registered selection rule, INT8 dynamic lost 0.0735 dev mAP50-95, INT8 static kept accuracy but was slower than FP32 on CPU, and FP16 kept accuracy but was only 5% faster on B200 (the rule required ≥10%). H100 timings used stand-in weights of the same architecture. On real drawings (B200, ONNX FP32 CUDA) end-to-end time including pre-processing was 135 ms p50 with 22.8 ms inference, and 889 ms on 32 CPU threads; latency did not depend on the number of detections up to 28 per drawing.
|
| 111 |
+
|
| 112 |
+
## 3. Limitations
|
| 113 |
+
|
| 114 |
+
- Five classes only (see top). Zero detections is not proof of absence.
|
| 115 |
+
- COUNT_NO_GO for automatic quantity takeoff; draft + human check only.
|
| 116 |
+
- Evaluated on one public dataset. No customer drawings have been evaluated.
|
| 117 |
+
- The densest evaluated drawings had 28 objects; drawings with hundreds of fixtures were not measured.
|
| 118 |
+
- Ground truth is the dataset's label boxes; label quality was not audited.
|
| 119 |
+
- The novel split is small (53 drawings), so its confidence intervals are wide.
|
| 120 |
+
|
| 121 |
+
## 4. Training data
|
| 122 |
+
|
| 123 |
+
Trained on **AI 허브(AI-Hub) — 건축 도면 데이터(OBJ)(71465)** (Korean architectural drawing dataset, object annotations, AI-Hub dataset 71465). Split: 7,900 train / 199 dev / 1,014 test, with a 53-drawing novel subset.
|
| 124 |
+
|
| 125 |
+
데이터 재배포 불가, 신청자 본인 내부 이용. The dataset is not redistributable: it was used only by the applicant, inside the applicant's own environment. This repository contains trained weights and code only; no drawings, crops, label files or derived subsets. To retrain, apply for the dataset on AI-Hub with your own account.
|
| 126 |
+
|
| 127 |
+
Initialization: torchvision `fasterrcnn_resnet50_fpn_v2` COCO-pretrained weights.
|
| 128 |
+
|
| 129 |
+
## 5. License
|
| 130 |
+
|
| 131 |
+
Apache-2.0 for the weights and code. See `NOTICE` for the required dataset attribution. Runtime dependencies are permissively licensed (MIT / BSD / Apache-2.0).
|
blueprintparse.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""BlueprintParse-R50 — detect five sanitary fixture classes in Korean architectural floor-plan images (ONNX, CPU/GPU).
|
| 3 |
+
|
| 4 |
+
Classes (model label ids 1..5): gas_range, toilet, washbasin, kitchen_sink, bathtub. Nothing else is detected or claimed.
|
| 5 |
+
Per-drawing counts are a DRAFT for human review: automatic quantity takeoff did not pass our gate (COUNT_NO_GO).
|
| 6 |
+
|
| 7 |
+
Pre-processing = training: grayscale, shrink so the long side <= max_side (2048, read from model metadata),
|
| 8 |
+
scale to 0..1, replicate to 3 channels. Boxes are mapped back to original pixel coordinates.
|
| 9 |
+
|
| 10 |
+
Usage:
|
| 11 |
+
python blueprintparse.py plan.png # JSON to stdout
|
| 12 |
+
python blueprintparse.py plan.png --providers CUDAExecutionProvider,CPUExecutionProvider
|
| 13 |
+
python blueprintparse.py --selftest # synthetic drawing, no data needed
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import hashlib
|
| 19 |
+
import io
|
| 20 |
+
import json
|
| 21 |
+
import sys
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
HERE = Path(__file__).resolve().parent
|
| 25 |
+
MODEL = HERE / "bp_fp32.onnx"
|
| 26 |
+
MODEL_SHA256 = "5ed19b8bccc6d5abfdeb5dd26f153c28379115b100a9a4ce479479e10bffb773"
|
| 27 |
+
INPUT_NAME, OUTPUT_NAMES = "image", ("boxes", "labels", "scores")
|
| 28 |
+
CLASS_EN = {"객체_가스레인지": "gas_range", "객체_변기": "toilet", "객체_세면대": "washbasin",
|
| 29 |
+
"객체_싱크대": "kitchen_sink", "객체_욕조": "bathtub"}
|
| 30 |
+
SCORE_THRESHOLD, CANDIDATE_FLOOR, REVIEW_THRESHOLD = 0.5, 0.3, 0.8
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def sha256_file(p) -> str:
|
| 34 |
+
h = hashlib.sha256()
|
| 35 |
+
with open(p, "rb") as f:
|
| 36 |
+
for b in iter(lambda: f.read(1 << 22), b""):
|
| 37 |
+
h.update(b)
|
| 38 |
+
return h.hexdigest()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def preprocess(data: bytes, max_side: int):
|
| 42 |
+
"""-> (x [3,h,w] float32, scale, (W, H))."""
|
| 43 |
+
import numpy as np
|
| 44 |
+
from PIL import Image
|
| 45 |
+
with Image.open(io.BytesIO(data)) as im:
|
| 46 |
+
im = im.convert("L"); W, H = im.size; s = min(1.0, max_side / max(W, H))
|
| 47 |
+
small = im.resize((max(1, round(W * s)), max(1, round(H * s))), Image.BILINEAR) if s < 1.0 else im.copy()
|
| 48 |
+
a = np.asarray(small, dtype=np.uint8).astype(np.float32) / 255.0
|
| 49 |
+
return np.repeat(a[None], 3, axis=0), s, (W, H)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def to_original(box, scale: float, size) -> list:
|
| 53 |
+
W, H = size
|
| 54 |
+
x0, y0, x1, y1 = (float(v) / scale for v in box)
|
| 55 |
+
return [round(max(0., min(W, x0)), 2), round(max(0., min(H, y0)), 2),
|
| 56 |
+
round(max(0., min(W, x1)), 2), round(max(0., min(H, y1)), 2)]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class BlueprintParse:
|
| 60 |
+
def __init__(self, model_path=MODEL, providers=None, threads: int | None = None, verify_sha: bool = True):
|
| 61 |
+
import onnxruntime as ort
|
| 62 |
+
self.path = Path(model_path)
|
| 63 |
+
if verify_sha and self.path.resolve() == MODEL.resolve() and sha256_file(self.path) != MODEL_SHA256:
|
| 64 |
+
raise ValueError("bp_fp32.onnx sha256 mismatch")
|
| 65 |
+
so = ort.SessionOptions()
|
| 66 |
+
if threads:
|
| 67 |
+
so.intra_op_num_threads = int(threads)
|
| 68 |
+
self.sess = ort.InferenceSession(str(self.path), so, providers=providers or ["CPUExecutionProvider"])
|
| 69 |
+
meta = {}
|
| 70 |
+
for k, v in self.sess.get_modelmeta().custom_metadata_map.items():
|
| 71 |
+
try:
|
| 72 |
+
meta[k] = json.loads(v)
|
| 73 |
+
except (ValueError, TypeError):
|
| 74 |
+
meta[k] = v
|
| 75 |
+
self.meta = meta
|
| 76 |
+
cid = meta.get("class_ids") or {c: i + 1 for i, c in enumerate(CLASS_EN)}
|
| 77 |
+
self.names = {int(v): k for k, v in cid.items()}
|
| 78 |
+
self.max_side = int(meta.get("max_side", 2048))
|
| 79 |
+
|
| 80 |
+
def raw(self, x):
|
| 81 |
+
import numpy as np
|
| 82 |
+
b, l, s = self.sess.run(list(OUTPUT_NAMES), {INPUT_NAME: np.ascontiguousarray(x, dtype=np.float32)})
|
| 83 |
+
return np.asarray(b), np.asarray(l), np.asarray(s)
|
| 84 |
+
|
| 85 |
+
def __call__(self, data: bytes, score_threshold: float = SCORE_THRESHOLD, candidate_floor: float = CANDIDATE_FLOOR) -> dict:
|
| 86 |
+
x, scale, size = preprocess(data, self.max_side)
|
| 87 |
+
dets, cands = [], []
|
| 88 |
+
for b, l, s in zip(*self.raw(x)):
|
| 89 |
+
s = float(s)
|
| 90 |
+
if s < candidate_floor:
|
| 91 |
+
continue
|
| 92 |
+
name = self.names.get(int(l), f"unknown:{int(l)}")
|
| 93 |
+
d = {"class": CLASS_EN.get(name, "out_of_scope"), "score": round(s, 4), "box_xyxy": to_original(b, scale, size)}
|
| 94 |
+
(dets if s >= score_threshold else cands).append(d)
|
| 95 |
+
dets.sort(key=lambda d: -d["score"]); cands.sort(key=lambda d: -d["score"])
|
| 96 |
+
counts = {c: 0 for c in CLASS_EN.values()}
|
| 97 |
+
for d in dets:
|
| 98 |
+
if d["class"] in counts:
|
| 99 |
+
counts[d["class"]] += 1
|
| 100 |
+
review = bool(cands) or any(d["score"] < REVIEW_THRESHOLD for d in dets) or not dets
|
| 101 |
+
return {"input": {"sha256": hashlib.sha256(data).hexdigest(), "width": size[0], "height": size[1],
|
| 102 |
+
"resize_scale": round(scale, 6)},
|
| 103 |
+
"counts_draft": counts, "detections": dets, "uncertain_candidates": cands,
|
| 104 |
+
"thresholds": {"score": score_threshold, "candidate_floor": candidate_floor},
|
| 105 |
+
"review_required": True, "low_confidence_present": review,
|
| 106 |
+
"note": "Counts are a draft for human review (automatic takeoff COUNT_NO_GO). Zero detections is not proof of absence."}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def synthetic_drawing(h: int = 1448, w: int = 2048, seed: int = 0) -> bytes:
|
| 110 |
+
"""Deterministic synthetic line drawing (white background, black walls / boxes / rings). Not dataset data."""
|
| 111 |
+
import numpy as np
|
| 112 |
+
from PIL import Image
|
| 113 |
+
rng = np.random.default_rng(seed)
|
| 114 |
+
a = np.ones((h, w), dtype=np.float32)
|
| 115 |
+
for _ in range(60):
|
| 116 |
+
if rng.random() < .5:
|
| 117 |
+
y = int(rng.integers(0, h)); x0, x1 = sorted(rng.integers(0, w, 2)); a[max(0, y - 2):y + 2, x0:x1] = 0
|
| 118 |
+
else:
|
| 119 |
+
x = int(rng.integers(0, w)); y0, y1 = sorted(rng.integers(0, h, 2)); a[y0:y1, max(0, x - 2):x + 2] = 0
|
| 120 |
+
for _ in range(25):
|
| 121 |
+
cx, cy = int(rng.integers(40, w - 40)), int(rng.integers(40, h - 40)); r = int(rng.integers(10, 35))
|
| 122 |
+
a[cy - r:cy + r, cx - r] = a[cy - r:cy + r, cx + r - 1] = 0
|
| 123 |
+
a[cy - r, cx - r:cx + r] = a[cy + r - 1, cx - r:cx + r] = 0
|
| 124 |
+
yy, xx = np.ogrid[-r:r, -r:r]; ring = np.abs(np.sqrt(yy ** 2 + xx ** 2) - r * .6) < 1.2
|
| 125 |
+
a[cy - r:cy + r, cx - r:cx + r][ring] = 0
|
| 126 |
+
buf = io.BytesIO(); Image.fromarray((a * 255).astype("uint8")).save(buf, format="PNG")
|
| 127 |
+
return buf.getvalue()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def selftest(threads=None) -> dict:
|
| 131 |
+
import onnxruntime as ort
|
| 132 |
+
m = BlueprintParse(threads=threads)
|
| 133 |
+
out = m(synthetic_drawing())
|
| 134 |
+
assert out["review_required"] is True and set(out["counts_draft"]) == set(CLASS_EN.values())
|
| 135 |
+
return {"ok": True, "onnxruntime": ort.__version__, "inputs": [i.name for i in m.sess.get_inputs()],
|
| 136 |
+
"outputs": [o.name for o in m.sess.get_outputs()], "max_side": m.max_side,
|
| 137 |
+
"class_names_en": [CLASS_EN.get(m.names[k], "?") for k in sorted(m.names)],
|
| 138 |
+
"metadata_precision": m.meta.get("precision"), "n_detections": len(out["detections"]),
|
| 139 |
+
"n_candidates": len(out["uncertain_candidates"])}
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def main(argv=None) -> int:
|
| 143 |
+
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
| 144 |
+
ap.add_argument("image", nargs="?")
|
| 145 |
+
ap.add_argument("--model", default=str(MODEL))
|
| 146 |
+
ap.add_argument("--providers", default="CPUExecutionProvider")
|
| 147 |
+
ap.add_argument("--threads", type=int, default=None)
|
| 148 |
+
ap.add_argument("--selftest", action="store_true")
|
| 149 |
+
a = ap.parse_args(argv)
|
| 150 |
+
if a.selftest:
|
| 151 |
+
print(json.dumps(selftest(a.threads), ensure_ascii=False, indent=1)); return 0
|
| 152 |
+
if not a.image:
|
| 153 |
+
ap.error("image path required (or --selftest)")
|
| 154 |
+
m = BlueprintParse(a.model, providers=[p for p in a.providers.split(",") if p], threads=a.threads)
|
| 155 |
+
print(json.dumps(m(Path(a.image).read_bytes()), ensure_ascii=False, indent=1))
|
| 156 |
+
return 0
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
sys.exit(main())
|
bp_fp32.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5ed19b8bccc6d5abfdeb5dd26f153c28379115b100a9a4ce479479e10bffb773
|
| 3 |
+
size 173265653
|
export_onnx.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reproduce bp_fp32.onnx from a torchvision checkpoint you trained yourself.
|
| 3 |
+
|
| 4 |
+
The published ONNX was exported from a checkpoint saved as
|
| 5 |
+
{"state_dict": ..., "class_ids": {"객체_가스레인지": 1, "객체_변기": 2, "객체_세면대": 3, "객체_싱크대": 4, "객체_욕조": 5},
|
| 6 |
+
"arch": "r50fpn2", "max_side": 2048}
|
| 7 |
+
Architecture: torchvision fasterrcnn_resnet50_fpn_v2(num_classes=6, min_size=max_side, max_size=max_side,
|
| 8 |
+
box_detections_per_img=300), initialized for training from the torchvision COCO weights (weights="DEFAULT").
|
| 9 |
+
|
| 10 |
+
The ONNX graph takes one pre-processed image `image` [3,H,W] float32 0..1 (H, W dynamic) and returns
|
| 11 |
+
`boxes` [N,4] (input-tensor xyxy), `labels` [N] int64 (1..5), `scores` [N]. Opset 17. Metadata keys:
|
| 12 |
+
class_ids, arch, max_side, checkpoint_sha256, precision, opset.
|
| 13 |
+
|
| 14 |
+
python export_onnx.py --ckpt my_checkpoint.pt --out bp_fp32.onnx [--device cuda]
|
| 15 |
+
python export_onnx.py --check bp_fp32.onnx --ckpt my_checkpoint.pt # ONNX vs torch on synthetic drawings
|
| 16 |
+
"""
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import argparse
|
| 20 |
+
import hashlib
|
| 21 |
+
import json
|
| 22 |
+
import sys
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
|
| 25 |
+
OPSET, INPUT_NAME, OUTPUT_NAMES = 17, "image", ("boxes", "labels", "scores")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def sha256_file(p) -> str:
|
| 29 |
+
h = hashlib.sha256()
|
| 30 |
+
with open(p, "rb") as f:
|
| 31 |
+
for b in iter(lambda: f.read(1 << 22), b""):
|
| 32 |
+
h.update(b)
|
| 33 |
+
return h.hexdigest()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def build_model(ncls: int, max_side: int):
|
| 37 |
+
from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2
|
| 38 |
+
return fasterrcnn_resnet50_fpn_v2(weights=None, weights_backbone=None, num_classes=ncls + 1,
|
| 39 |
+
min_size=max_side, max_size=max_side, box_detections_per_img=300)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def load_checkpoint(path):
|
| 43 |
+
import torch
|
| 44 |
+
ck = torch.load(str(path), map_location="cpu", weights_only=False) # your own file; contains plain dict/str/int + tensors
|
| 45 |
+
cid = {str(k): int(v) for k, v in ck["class_ids"].items()}
|
| 46 |
+
max_side = int(ck.get("max_side", 2048))
|
| 47 |
+
m = build_model(len(cid), max_side)
|
| 48 |
+
m.load_state_dict(ck["state_dict"])
|
| 49 |
+
return m.eval(), {"class_ids": cid, "arch": "r50fpn2", "max_side": max_side, "checkpoint_sha256": sha256_file(path)}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _single(model):
|
| 53 |
+
import torch
|
| 54 |
+
|
| 55 |
+
class W(torch.nn.Module):
|
| 56 |
+
def __init__(self, m):
|
| 57 |
+
super().__init__(); self.m = m
|
| 58 |
+
|
| 59 |
+
def forward(self, image):
|
| 60 |
+
o = self.m([image])[0]
|
| 61 |
+
return o["boxes"], o["labels"], o["scores"]
|
| 62 |
+
return W(model).eval()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def stamp_metadata(path, meta: dict) -> None:
|
| 66 |
+
import onnx
|
| 67 |
+
m = onnx.load(str(path))
|
| 68 |
+
old = {p.key: p.value for p in m.metadata_props}
|
| 69 |
+
old.update({k: v if isinstance(v, str) else json.dumps(v, ensure_ascii=False) for k, v in meta.items()})
|
| 70 |
+
del m.metadata_props[:]
|
| 71 |
+
for k, v in old.items():
|
| 72 |
+
e = m.metadata_props.add(); e.key, e.value = k, v
|
| 73 |
+
onnx.save(m, str(path))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def export(ckpt, out, device="cpu", sample_hw=(1448, 2048)) -> dict:
|
| 77 |
+
import onnx
|
| 78 |
+
import torch
|
| 79 |
+
from blueprintparse import synthetic_drawing, preprocess
|
| 80 |
+
model, meta = load_checkpoint(ckpt)
|
| 81 |
+
w = _single(model).to(device)
|
| 82 |
+
x, _, _ = preprocess(synthetic_drawing(*sample_hw), meta["max_side"])
|
| 83 |
+
x = torch.from_numpy(x).to(device)
|
| 84 |
+
kw = dict(opset_version=OPSET, input_names=[INPUT_NAME], output_names=list(OUTPUT_NAMES),
|
| 85 |
+
dynamic_axes={INPUT_NAME: {1: "H", 2: "W"}, "boxes": {0: "N"}, "labels": {0: "N"}, "scores": {0: "N"}},
|
| 86 |
+
do_constant_folding=True)
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
try:
|
| 89 |
+
torch.onnx.export(w, (x,), str(out), dynamo=False, **kw)
|
| 90 |
+
except TypeError:
|
| 91 |
+
torch.onnx.export(w, (x,), str(out), **kw)
|
| 92 |
+
stamp_metadata(out, {**meta, "precision": "fp32", "opset": OPSET})
|
| 93 |
+
onnx.checker.check_model(str(out))
|
| 94 |
+
return {"out": str(out), "sha256": sha256_file(out), "bytes": Path(out).stat().st_size}
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def check(onnx_path, ckpt, n=3) -> dict:
|
| 98 |
+
"""Greedy box match (IoU >= 0.5, same label) of ONNX vs torch on n synthetic drawings, scores >= 0.3."""
|
| 99 |
+
import numpy as np
|
| 100 |
+
import torch
|
| 101 |
+
from blueprintparse import BlueprintParse, preprocess, synthetic_drawing
|
| 102 |
+
model, meta = load_checkpoint(ckpt)
|
| 103 |
+
bp = BlueprintParse(onnx_path, verify_sha=False)
|
| 104 |
+
|
| 105 |
+
def iou(a, b):
|
| 106 |
+
ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1]))
|
| 107 |
+
u = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - ix * iy
|
| 108 |
+
return ix * iy / u if u > 0 else 0.0
|
| 109 |
+
tot = {"n_ref": 0, "matched": 0, "max_abs_score": 0.0}
|
| 110 |
+
for seed in range(n):
|
| 111 |
+
x, _, _ = preprocess(synthetic_drawing(seed=seed), meta["max_side"])
|
| 112 |
+
with torch.no_grad():
|
| 113 |
+
o = model([torch.from_numpy(x)])[0]
|
| 114 |
+
ref = [(b, int(l), float(s)) for b, l, s in zip(o["boxes"].numpy(), o["labels"].numpy(), o["scores"].numpy()) if s >= 0.3]
|
| 115 |
+
got = [(b, int(l), float(s)) for b, l, s in zip(*bp.raw(x)) if s >= 0.3]
|
| 116 |
+
used = set(); tot["n_ref"] += len(ref)
|
| 117 |
+
for b, l, s in ref:
|
| 118 |
+
j = max((k for k in range(len(got)) if k not in used and got[k][1] == l), key=lambda k: iou(b, got[k][0]), default=None)
|
| 119 |
+
if j is not None and iou(b, got[j][0]) >= 0.5:
|
| 120 |
+
used.add(j); tot["matched"] += 1; tot["max_abs_score"] = max(tot["max_abs_score"], abs(s - got[j][2]))
|
| 121 |
+
return tot
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
ap = argparse.ArgumentParser()
|
| 126 |
+
ap.add_argument("--ckpt", required=True); ap.add_argument("--out", default="bp_fp32.onnx")
|
| 127 |
+
ap.add_argument("--device", default="cpu"); ap.add_argument("--check")
|
| 128 |
+
a = ap.parse_args()
|
| 129 |
+
print(json.dumps(check(a.check, a.ckpt) if a.check else export(a.ckpt, a.out, a.device), indent=1))
|
| 130 |
+
sys.exit(0)
|