mingyang91 commited on
Commit
06a2085
β€’
1 Parent(s): 095f7cc
Files changed (6) hide show
  1. .idea/workspace.xml +90 -15
  2. Dockerfile +2 -1
  3. demo.py +13 -5
  4. evaluator.py +29 -10
  5. tests/coco8.zip +0 -0
  6. yolo_dataset.py +54 -33
.idea/workspace.xml CHANGED
@@ -4,14 +4,13 @@
4
  <option name="autoReloadType" value="SELECTIVE" />
5
  </component>
6
  <component name="ChangeListManager">
7
- <list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="IoU evaluator">
8
- <change afterPath="$PROJECT_DIR$/yolo_model.py" afterDir="false" />
9
  <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
10
  <change beforePath="$PROJECT_DIR$/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/Dockerfile" afterDir="false" />
11
  <change beforePath="$PROJECT_DIR$/demo.py" beforeDir="false" afterPath="$PROJECT_DIR$/demo.py" afterDir="false" />
12
  <change beforePath="$PROJECT_DIR$/evaluator.py" beforeDir="false" afterPath="$PROJECT_DIR$/evaluator.py" afterDir="false" />
 
13
  <change beforePath="$PROJECT_DIR$/yolo_dataset.py" beforeDir="false" afterPath="$PROJECT_DIR$/yolo_dataset.py" afterDir="false" />
14
- <change beforePath="$PROJECT_DIR$/yolo_fire.py" beforeDir="false" />
15
  </list>
16
  <option name="SHOW_DIALOG" value="false" />
17
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -30,6 +29,21 @@
30
  <component name="Git.Settings">
31
  <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
32
  </component>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  <component name="MarkdownSettingsMigration">
34
  <option name="stateVersion" value="1" />
35
  </component>
@@ -47,9 +61,10 @@
47
  </component>
48
  <component name="PropertiesComponent"><![CDATA[{
49
  "keyToString": {
50
- "Python.demo.executor": "Run",
51
- "Python.evaluator.executor": "Run",
52
  "Python.extract.executor": "Run",
 
53
  "Python.yolo_dataset.executor": "Run",
54
  "RunOnceActivity.OpenProjectViewOnStart": "true",
55
  "RunOnceActivity.ShowReadmeOnStart": "true",
@@ -74,7 +89,7 @@
74
  <recent name="$PROJECT_DIR$" />
75
  </key>
76
  </component>
77
- <component name="RunManager" selected="Python.yolo_dataset">
78
  <configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
79
  <module name="detector" />
80
  <option name="ENV_FILES" value="" />
@@ -121,6 +136,30 @@
121
  <option name="INPUT_FILE" value="" />
122
  <method v="2" />
123
  </configuration>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  <configuration name="yolo_dataset" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
125
  <module name="detector" />
126
  <option name="ENV_FILES" value="" />
@@ -150,7 +189,8 @@
150
  <option name="ENV_FILES" value="" />
151
  <option name="INTERPRETER_OPTIONS" value="" />
152
  <option name="PARENT_ENVS" value="true" />
153
- <option name="SDK_HOME" value="" />
 
154
  <option name="WORKING_DIRECTORY" value="" />
155
  <option name="IS_MODULE_SDK" value="false" />
156
  <option name="ADD_CONTENT_ROOTS" value="true" />
@@ -159,11 +199,18 @@
159
  <option name="launchJavascriptDebuger" value="false" />
160
  <method v="2" />
161
  </configuration>
 
 
 
 
 
 
 
162
  <recent_temporary>
163
  <list>
164
- <item itemvalue="Python.yolo_dataset" />
165
- <item itemvalue="Python.evaluator" />
166
  <item itemvalue="Python.demo" />
 
 
167
  </list>
168
  </recent_temporary>
169
  </component>
@@ -187,7 +234,9 @@
187
  <workItem from="1702476484538" duration="9613000" />
188
  <workItem from="1705757850315" duration="14186000" />
189
  <workItem from="1705812482464" duration="711000" />
190
- <workItem from="1705845330999" duration="5355000" />
 
 
191
  </task>
192
  <task id="LOCAL-00001" summary="init commit">
193
  <option name="closed" value="true" />
@@ -365,7 +414,15 @@
365
  <option name="project" value="LOCAL" />
366
  <updated>1705771207294</updated>
367
  </task>
368
- <option name="localTasksCounter" value="23" />
 
 
 
 
 
 
 
 
369
  <servers />
370
  </component>
371
  <component name="TypeScriptGeneratedFilesManager">
@@ -408,12 +465,30 @@
408
  <MESSAGE value="use /run/secrets" />
409
  <MESSAGE value="fix confidence" />
410
  <MESSAGE value="IoU evaluator" />
411
- <option name="LAST_COMMIT_MESSAGE" value="IoU evaluator" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
  </component>
413
  <component name="com.intellij.coverage.CoverageDataManagerImpl">
414
- <SUITE FILE_PATH="coverage/detector$yolo_dataset.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705850662461" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
415
- <SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1702302380580" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
416
- <SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="1705847678258" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
 
417
  <SUITE FILE_PATH="coverage/detector$extract.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705764465837" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
418
  </component>
419
  </project>
 
4
  <option name="autoReloadType" value="SELECTIVE" />
5
  </component>
6
  <component name="ChangeListManager">
7
+ <list default="true" id="d7806539-b6d6-42e7-bb45-1565f5d54891" name="Changes" comment="Update UI">
 
8
  <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
9
  <change beforePath="$PROJECT_DIR$/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/Dockerfile" afterDir="false" />
10
  <change beforePath="$PROJECT_DIR$/demo.py" beforeDir="false" afterPath="$PROJECT_DIR$/demo.py" afterDir="false" />
11
  <change beforePath="$PROJECT_DIR$/evaluator.py" beforeDir="false" afterPath="$PROJECT_DIR$/evaluator.py" afterDir="false" />
12
+ <change beforePath="$PROJECT_DIR$/tests/coco8.zip" beforeDir="false" afterPath="$PROJECT_DIR$/tests/coco8.zip" afterDir="false" />
13
  <change beforePath="$PROJECT_DIR$/yolo_dataset.py" beforeDir="false" afterPath="$PROJECT_DIR$/yolo_dataset.py" afterDir="false" />
 
14
  </list>
15
  <option name="SHOW_DIALOG" value="false" />
16
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
 
29
  <component name="Git.Settings">
30
  <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
31
  </component>
32
+ <component name="HighlightingSettingsPerFile">
33
+ <setting file="file://$USER_HOME$/Library/Caches/JetBrains/PyCharm2023.3/python_stubs/580317742/posix.py" root0="SKIP_INSPECTION" />
34
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/torch/utils/_contextlib.py" root0="SKIP_INSPECTION" />
35
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/data/base.py" root0="SKIP_INSPECTION" />
36
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/data/build.py" root0="SKIP_INSPECTION" />
37
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/data/dataset.py" root0="SKIP_INSPECTION" />
38
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/data/utils.py" root0="SKIP_INSPECTION" />
39
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/engine/model.py" root0="SKIP_INSPECTION" />
40
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/engine/validator.py" root0="SKIP_INSPECTION" />
41
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/models/yolo/detect/val.py" root0="SKIP_INSPECTION" />
42
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/utils/checks.py" root0="SKIP_INSPECTION" />
43
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/utils/metrics.py" root0="SKIP_INSPECTION" />
44
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/yaml/scanner.py" root0="SKIP_INSPECTION" />
45
+ <setting file="file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/zipfile.py" root0="SKIP_INSPECTION" />
46
+ </component>
47
  <component name="MarkdownSettingsMigration">
48
  <option name="stateVersion" value="1" />
49
  </component>
 
61
  </component>
62
  <component name="PropertiesComponent"><![CDATA[{
63
  "keyToString": {
64
+ "Python.demo.executor": "Debug",
65
+ "Python.evaluator.executor": "Debug",
66
  "Python.extract.executor": "Run",
67
+ "Python.streamlit.executor": "Debug",
68
  "Python.yolo_dataset.executor": "Run",
69
  "RunOnceActivity.OpenProjectViewOnStart": "true",
70
  "RunOnceActivity.ShowReadmeOnStart": "true",
 
89
  <recent name="$PROJECT_DIR$" />
90
  </key>
91
  </component>
92
+ <component name="RunManager" selected="Python.streamlit">
93
  <configuration name="demo" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
94
  <module name="detector" />
95
  <option name="ENV_FILES" value="" />
 
136
  <option name="INPUT_FILE" value="" />
137
  <method v="2" />
138
  </configuration>
139
+ <configuration name="streamlit" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
140
+ <module name="detector" />
141
+ <option name="ENV_FILES" value="" />
142
+ <option name="INTERPRETER_OPTIONS" value="" />
143
+ <option name="PARENT_ENVS" value="true" />
144
+ <envs>
145
+ <env name="PYTHONUNBUFFERED" value="1" />
146
+ </envs>
147
+ <option name="SDK_HOME" value="" />
148
+ <option name="SDK_NAME" value="detector" />
149
+ <option name="WORKING_DIRECTORY" value="" />
150
+ <option name="IS_MODULE_SDK" value="false" />
151
+ <option name="ADD_CONTENT_ROOTS" value="true" />
152
+ <option name="ADD_SOURCE_ROOTS" value="true" />
153
+ <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
154
+ <option name="SCRIPT_NAME" value="streamlit" />
155
+ <option name="PARAMETERS" value="run demo.py" />
156
+ <option name="SHOW_COMMAND_LINE" value="false" />
157
+ <option name="EMULATE_TERMINAL" value="false" />
158
+ <option name="MODULE_MODE" value="true" />
159
+ <option name="REDIRECT_INPUT" value="false" />
160
+ <option name="INPUT_FILE" value="" />
161
+ <method v="2" />
162
+ </configuration>
163
  <configuration name="yolo_dataset" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
164
  <module name="detector" />
165
  <option name="ENV_FILES" value="" />
 
189
  <option name="ENV_FILES" value="" />
190
  <option name="INTERPRETER_OPTIONS" value="" />
191
  <option name="PARENT_ENVS" value="true" />
192
+ <option name="SDK_HOME" value="/opt/homebrew/anaconda3/envs/detector/bin/python" />
193
+ <option name="SDK_NAME" value="detector" />
194
  <option name="WORKING_DIRECTORY" value="" />
195
  <option name="IS_MODULE_SDK" value="false" />
196
  <option name="ADD_CONTENT_ROOTS" value="true" />
 
199
  <option name="launchJavascriptDebuger" value="false" />
200
  <method v="2" />
201
  </configuration>
202
+ <list>
203
+ <item itemvalue="FastAPI.detector" />
204
+ <item itemvalue="Python.streamlit" />
205
+ <item itemvalue="Python.demo" />
206
+ <item itemvalue="Python.evaluator" />
207
+ <item itemvalue="Python.yolo_dataset" />
208
+ </list>
209
  <recent_temporary>
210
  <list>
 
 
211
  <item itemvalue="Python.demo" />
212
+ <item itemvalue="Python.evaluator" />
213
+ <item itemvalue="Python.yolo_dataset" />
214
  </list>
215
  </recent_temporary>
216
  </component>
 
234
  <workItem from="1702476484538" duration="9613000" />
235
  <workItem from="1705757850315" duration="14186000" />
236
  <workItem from="1705812482464" duration="711000" />
237
+ <workItem from="1705845330999" duration="11089000" />
238
+ <workItem from="1706104589422" duration="517000" />
239
+ <workItem from="1706105118954" duration="3764000" />
240
  </task>
241
  <task id="LOCAL-00001" summary="init commit">
242
  <option name="closed" value="true" />
 
414
  <option name="project" value="LOCAL" />
415
  <updated>1705771207294</updated>
416
  </task>
417
+ <task id="LOCAL-00023" summary="Update UI">
418
+ <option name="closed" value="true" />
419
+ <created>1705850745341</created>
420
+ <option name="number" value="00023" />
421
+ <option name="presentableId" value="LOCAL-00023" />
422
+ <option name="project" value="LOCAL" />
423
+ <updated>1705850745341</updated>
424
+ </task>
425
+ <option name="localTasksCounter" value="24" />
426
  <servers />
427
  </component>
428
  <component name="TypeScriptGeneratedFilesManager">
 
465
  <MESSAGE value="use /run/secrets" />
466
  <MESSAGE value="fix confidence" />
467
  <MESSAGE value="IoU evaluator" />
468
+ <MESSAGE value="Update UI" />
469
+ <option name="LAST_COMMIT_MESSAGE" value="Update UI" />
470
+ </component>
471
+ <component name="XDebuggerManager">
472
+ <breakpoint-manager>
473
+ <breakpoints>
474
+ <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
475
+ <url>file:///opt/homebrew/anaconda3/envs/detector/lib/python3.10/site-packages/ultralytics/data/utils.py</url>
476
+ <line>311</line>
477
+ <option name="timeStamp" value="9" />
478
+ </line-breakpoint>
479
+ <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
480
+ <url>file://$PROJECT_DIR$/evaluator.py</url>
481
+ <line>77</line>
482
+ <option name="timeStamp" value="17" />
483
+ </line-breakpoint>
484
+ </breakpoints>
485
+ </breakpoint-manager>
486
  </component>
487
  <component name="com.intellij.coverage.CoverageDataManagerImpl">
488
+ <SUITE FILE_PATH="coverage/detector$yolo_dataset.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705852113469" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
489
+ <SUITE FILE_PATH="coverage/detector$evaluator.coverage" NAME="evaluator Coverage Results" MODIFIED="1706107083258" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
490
+ <SUITE FILE_PATH="coverage/detector$demo.coverage" NAME="demo Coverage Results" MODIFIED="1706108414052" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
491
+ <SUITE FILE_PATH="coverage/detector$streamlit.coverage" NAME="streamlit Coverage Results" MODIFIED="1706108547026" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
492
  <SUITE FILE_PATH="coverage/detector$extract.coverage" NAME="yolo_dataset Coverage Results" MODIFIED="1705764465837" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
493
  </component>
494
  </project>
Dockerfile CHANGED
@@ -14,5 +14,6 @@ RUN pip install -r requirements.txt
14
  RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
15
  HF_TOKEN=$(cat /run/secrets/HF_TOKEN) python yolo_model.py \
16
  --weight_files=SHOU-ISD/fire-and-smoke:yolov8n.pt \
17
- --weight_files=SHOU-ISD/yolo-cracks:best.pt
 
18
  ENTRYPOINT ["streamlit", "run", "demo.py"]
 
14
  RUN --mount=type=secret,id=HF_TOKEN,mode=0444,required=true \
15
  HF_TOKEN=$(cat /run/secrets/HF_TOKEN) python yolo_model.py \
16
  --weight_files=SHOU-ISD/fire-and-smoke:yolov8n.pt \
17
+ --weight_files=SHOU-ISD/yolo-cracks:best.pt \
18
+ --weight_files=ultralyticsplus/yolov8s:yolov8s.pt
19
  ENTRYPOINT ["streamlit", "run", "demo.py"]
demo.py CHANGED
@@ -1,4 +1,5 @@
1
  import contextlib
 
2
  import time
3
  from functools import wraps
4
  from io import StringIO
@@ -13,6 +14,7 @@ from yolo_model import YoloModel
13
 
14
  fire_and_smoke = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
15
  crack = YoloModel("SHOU-ISD/yolo-cracks", "best.pt")
 
16
 
17
 
18
  def main():
@@ -22,13 +24,14 @@ def main():
22
  layout="centered")
23
 
24
  model = None
25
- model_choice = None
26
  with st.sidebar:
27
- model_choice = st.radio("Select Model", ["Fire&Smoke", "Crack"])
28
  if model_choice == "Fire&Smoke":
29
  model = fire_and_smoke
30
  elif model_choice == "Crack":
31
  model = crack
 
 
32
 
33
  st.title(f"{model_choice} Detection:")
34
 
@@ -48,9 +51,14 @@ def evaluate(model: YoloModel):
48
  # Slider for changing confidence
49
  confidence = st.slider('Confidence Threshold', 0, 100, 30)
50
  yolo_dataset = YoloDataset.from_zip_file(ZipFile(buffer))
51
- capture_output(evaluator.evaluate)(model=model.model,
52
- coco_gt=yolo_dataset.to_coco(), loader=yolo_dataset.load_image,
53
- confidence_threshold=confidence / 100.0)
 
 
 
 
 
54
 
55
 
56
  def detect(model: YoloModel):
 
1
  import contextlib
2
+ import os
3
  import time
4
  from functools import wraps
5
  from io import StringIO
 
14
 
15
  fire_and_smoke = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
16
  crack = YoloModel("SHOU-ISD/yolo-cracks", "best.pt")
17
+ coco = YoloModel("ultralyticsplus/yolov8s", "yolov8s.pt")
18
 
19
 
20
  def main():
 
24
  layout="centered")
25
 
26
  model = None
 
27
  with st.sidebar:
28
+ model_choice = st.radio("Select Model", ["Fire&Smoke", "Crack", "Coco"])
29
  if model_choice == "Fire&Smoke":
30
  model = fire_and_smoke
31
  elif model_choice == "Crack":
32
  model = crack
33
+ elif model_choice == "Coco":
34
+ model = coco
35
 
36
  st.title(f"{model_choice} Detection:")
37
 
 
51
  # Slider for changing confidence
52
  confidence = st.slider('Confidence Threshold', 0, 100, 30)
53
  yolo_dataset = YoloDataset.from_zip_file(ZipFile(buffer))
54
+ metrics_res = capture_output(evaluator.evaluate)(model=model.model,
55
+ dataset=yolo_dataset,
56
+ confidence_threshold=confidence / 100.0)
57
+ with metrics_res as metrics:
58
+ st.json(metrics.speed)
59
+ st.json(metrics.result_dict)
60
+ for pic in os.listdir(metrics.val.save_dir):
61
+ st.image(os.path.join(metrics.val.save_dir, pic), use_column_width=True)
62
 
63
 
64
  def detect(model: YoloModel):
evaluator.py CHANGED
@@ -1,17 +1,18 @@
 
1
  from typing import Callable
2
 
3
  from PIL.Image import Image
4
  from coco_eval import CocoEvaluator
5
- from pycocotools.coco import COCO
6
  from tqdm import tqdm
7
 
8
- from yolo_dataset import YoloDataset
9
  from yolo_model import YoloModel
10
 
11
  image_loader = Callable[[str], Image]
12
 
13
 
14
- def evaluate(model: YoloModel, coco_gt: COCO, loader: image_loader, confidence_threshold=0.6):
 
15
  # initialize evaluator with ground truth (gt)
16
  evaluator = CocoEvaluator(coco_gt=coco_gt, iou_types=["bbox"])
17
 
@@ -19,7 +20,7 @@ def evaluate(model: YoloModel, coco_gt: COCO, loader: image_loader, confidence_t
19
  for image_id, annotations in tqdm(coco_gt.imgToAnns.items()):
20
  # get the inputs
21
  image = coco_gt.imgs[image_id]
22
- results = model.model(source=loader(image["file_name"]))
23
  for result in results:
24
  coco_anns = yolo_boxes_to_coco_annotations(image_id, result.boxes,
25
  confidence_threshold=confidence_threshold)
@@ -33,6 +34,23 @@ def evaluate(model: YoloModel, coco_gt: COCO, loader: image_loader, confidence_t
33
  evaluator.synchronize_between_processes()
34
  evaluator.accumulate()
35
  evaluator.summarize()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
 
38
  def yolo_boxes_to_coco_annotations(image_id: int, yolo_boxes, confidence_threshold=0.6):
@@ -53,13 +71,14 @@ def main():
53
  coco_gt = yolo_dataset.to_coco()
54
  model = YoloModel("ultralyticsplus/yolov8s", "yolov8s.pt")
55
  # model = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
56
- evaluate(model=model, coco_gt=coco_gt, loader=yolo_dataset.load_image, confidence_threshold=0.1)
57
  # Validate the model
58
- metrics = model.model.val() # no arguments needed, dataset and settings remembered
59
- print(metrics.box.map) # map50-95
60
- print(metrics.box.map50) # map50
61
- print(metrics.box.map75) # map75
62
- print(metrics.box.maps) # a list contains map50-95 of each category
 
63
 
64
 
65
  if __name__ == '__main__':
 
1
+ import shutil
2
  from typing import Callable
3
 
4
  from PIL.Image import Image
5
  from coco_eval import CocoEvaluator
 
6
  from tqdm import tqdm
7
 
8
+ from yolo_dataset import YoloDataset, MaterialYoloDataset
9
  from yolo_model import YoloModel
10
 
11
  image_loader = Callable[[str], Image]
12
 
13
 
14
+ def evaluate(model: YoloModel, dataset: YoloDataset, confidence_threshold=0.6):
15
+ coco_gt = dataset.to_coco()
16
  # initialize evaluator with ground truth (gt)
17
  evaluator = CocoEvaluator(coco_gt=coco_gt, iou_types=["bbox"])
18
 
 
20
  for image_id, annotations in tqdm(coco_gt.imgToAnns.items()):
21
  # get the inputs
22
  image = coco_gt.imgs[image_id]
23
+ results = model.model(source=dataset.load_image(image["file_name"]))
24
  for result in results:
25
  coco_anns = yolo_boxes_to_coco_annotations(image_id, result.boxes,
26
  confidence_threshold=confidence_threshold)
 
34
  evaluator.synchronize_between_processes()
35
  evaluator.accumulate()
36
  evaluator.summarize()
37
+ return Metrics(model=model, material=dataset.to_material())
38
+
39
+
40
+ class Metrics:
41
+ def __init__(self, model: YoloModel, material: MaterialYoloDataset):
42
+ self.model = model
43
+ self.material = material
44
+ self.val = None
45
+
46
+ def __enter__(self):
47
+ if self.val is None:
48
+ self.val = self.model.model.val(data=self.material.yaml)
49
+ return self.val
50
+
51
+ def __exit__(self, exc_type, exc_val, exc_tb):
52
+ self.material.__exit__(exc_type, exc_val, exc_tb)
53
+ shutil.rmtree(self.val.save_dir)
54
 
55
 
56
  def yolo_boxes_to_coco_annotations(image_id: int, yolo_boxes, confidence_threshold=0.6):
 
71
  coco_gt = yolo_dataset.to_coco()
72
  model = YoloModel("ultralyticsplus/yolov8s", "yolov8s.pt")
73
  # model = YoloModel("SHOU-ISD/fire-and-smoke", "yolov8n.pt")
74
+ # evaluate(model=model, coco_gt=coco_gt, loader=yolo_dataset.load_image, confidence_threshold=0.1)
75
  # Validate the model
76
+ with yolo_dataset.to_material() as material:
77
+ metrics = model.model.val(data=material.yaml)
78
+ print(metrics.box.map) # map50-95
79
+ print(metrics.box.map50) # map50
80
+ print(metrics.box.map75) # map75
81
+ print(metrics.box.maps) # a list contains map50-95 of each category
82
 
83
 
84
  if __name__ == '__main__':
tests/coco8.zip CHANGED
Binary files a/tests/coco8.zip and b/tests/coco8.zip differ
 
yolo_dataset.py CHANGED
@@ -12,7 +12,6 @@
12
  # β”œβ”€β”€ 1.txt
13
  # β”œβ”€β”€ 2.txt
14
  # └── ...
15
- import os
16
  import pathlib
17
  import shutil
18
  from datetime import datetime
@@ -20,7 +19,6 @@ from typing import Optional
20
  from zipfile import ZipFile
21
 
22
  from PIL import Image
23
- from pycocotools import coco
24
  from pycocotools.coco import COCO
25
 
26
  yolo_label = {
@@ -126,8 +124,9 @@ class YoloDataset:
126
  namelist = zip_file.namelist()
127
  root_name = namelist[0]
128
  namelist = list(filter(lambda x: not zip_file.getinfo(x).is_dir(), namelist))
129
- if 'classes.txt' in namelist:
130
- classes = zip_file.read('classes.txt').decode('utf-8').split('\n')
 
131
  else:
132
  classes = []
133
  images = list(filter(lambda x: x.startswith(root_name + 'images'), namelist))
@@ -192,7 +191,7 @@ class YoloDataset:
192
  "supercategory": None,
193
  })
194
 
195
- coco_ds = coco.COCO()
196
  coco_ds.dataset = {
197
  "images": images,
198
  "annotations": annotations,
@@ -201,36 +200,58 @@ class YoloDataset:
201
  coco_ds.createIndex()
202
  return coco_ds
203
 
204
- def to_yaml(self):
205
- """
206
- :return: Yaml format in
207
- <code>
208
- path: ../datasets/coco128 # dataset root dir
209
- val: images/train2017 # val images (relative to 'path') 128 images
210
-
211
- # Classes
212
- names:
213
- 0: person
214
- </code>
215
- """
216
- try:
217
- # recursively create dir
218
- pathlib.Path(f'datasets/{self._zip_file.filename}').mkdir(parents=True, exist_ok=True)
219
- self._zip_file.extractall(f'datasets/{self._zip_file.filename}')
220
- with open(f'datasets/{self._zip_file.filename}/data.yaml', 'w') as f:
221
- f.write(f'path: ../datasets/{self._zip_file.filename}\n')
222
- f.write(f'val: images/train2017\n')
223
- f.write(f'\n')
224
- f.write(f'# Classes\n')
225
- f.write(f'names:\n')
226
- for i in range(len(self._classes)):
227
- f.write(f' {i}: {self._classes[i]}\n')
228
- finally:
229
- shutil.rmtree(f'datasets/{self._zip_file.filename}')
230
 
 
 
 
231
 
232
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  dataset = YoloDataset.from_path('tests/coco8.zip')
234
  coco = dataset.to_coco()
235
  print(coco)
236
- dataset.to_yaml()
 
 
 
 
 
12
  # β”œβ”€β”€ 1.txt
13
  # β”œβ”€β”€ 2.txt
14
  # └── ...
 
15
  import pathlib
16
  import shutil
17
  from datetime import datetime
 
19
  from zipfile import ZipFile
20
 
21
  from PIL import Image
 
22
  from pycocotools.coco import COCO
23
 
24
  yolo_label = {
 
124
  namelist = zip_file.namelist()
125
  root_name = namelist[0]
126
  namelist = list(filter(lambda x: not zip_file.getinfo(x).is_dir(), namelist))
127
+ cls_filename = root_name + 'classes.txt'
128
+ if cls_filename in namelist:
129
+ classes = zip_file.read(cls_filename).decode('utf-8').split('\n')
130
  else:
131
  classes = []
132
  images = list(filter(lambda x: x.startswith(root_name + 'images'), namelist))
 
191
  "supercategory": None,
192
  })
193
 
194
+ coco_ds = COCO()
195
  coco_ds.dataset = {
196
  "images": images,
197
  "annotations": annotations,
 
200
  coco_ds.createIndex()
201
  return coco_ds
202
 
203
+ def to_material(self):
204
+ return MaterialYoloDataset(self)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
+ @property
207
+ def zip_file(self):
208
+ return self._zip_file
209
 
210
+ @property
211
+ def classes(self):
212
+ return self._classes
213
+
214
+
215
+ class MaterialYoloDataset:
216
+ def __init__(self, dataset: YoloDataset):
217
+ print(dataset.to_coco().cats)
218
+ self._classes = dataset.classes
219
+ self._zip_file = dataset.zip_file
220
+ self._root = self._zip_file.namelist()[0][:-1]
221
+
222
+ def __enter__(self):
223
+ # recursively create dir
224
+ dataset_path = pathlib.Path(f'./datasets/')
225
+ dataset_path.mkdir(parents=True, exist_ok=True)
226
+ self._zip_file.extractall(f'./datasets/')
227
+ with open(f'./datasets/{self._root}/data.yaml', 'w+') as f:
228
+ f.write(f'path: {dataset_path.absolute()}/{self._root}/\n')
229
+ f.write(f'train: images/train\n')
230
+ f.write(f'val: images/val\n')
231
+ f.write(f'\n')
232
+ f.write(f'# Classes\n')
233
+ f.write(f'names:\n')
234
+ for i in range(len(self._classes)):
235
+ f.write(f' {i}: {self._classes[i]}\n')
236
+ else:
237
+ for i in range(0, 2):
238
+ f.write(f' {i}: unknown\n')
239
+ return self
240
+
241
+ def __exit__(self, exc_type, exc_val, exc_tb):
242
+ shutil.rmtree(f'./datasets/{self._root}')
243
+
244
+ @property
245
+ def yaml(self):
246
+ return pathlib.Path(f'./datasets/{self._root}/data.yaml').absolute()
247
+
248
+
249
+ def main():
250
  dataset = YoloDataset.from_path('tests/coco8.zip')
251
  coco = dataset.to_coco()
252
  print(coco)
253
+ print(dataset.to_material().yaml)
254
+
255
+
256
+ if __name__ == '__main__':
257
+ main()