megadetector 5.0.10__py3-none-any.whl → 5.0.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of megadetector might be problematic. Click here for more details.

Files changed (226) hide show
  1. {api → megadetector/api}/batch_processing/api_core/batch_service/score.py +2 -2
  2. {api → megadetector/api}/synchronous/api_core/animal_detection_api/api_backend.py +1 -1
  3. {api → megadetector/api}/synchronous/api_core/animal_detection_api/api_frontend.py +1 -1
  4. {classification → megadetector/classification}/analyze_failed_images.py +3 -3
  5. {classification → megadetector/classification}/cache_batchapi_outputs.py +1 -1
  6. {classification → megadetector/classification}/create_classification_dataset.py +1 -1
  7. {classification → megadetector/classification}/crop_detections.py +1 -1
  8. {classification → megadetector/classification}/detect_and_crop.py +5 -5
  9. {classification → megadetector/classification}/evaluate_model.py +1 -1
  10. {classification → megadetector/classification}/json_to_azcopy_list.py +2 -2
  11. {classification → megadetector/classification}/json_validator.py +13 -9
  12. {classification → megadetector/classification}/map_classification_categories.py +1 -1
  13. {classification → megadetector/classification}/merge_classification_detection_output.py +1 -1
  14. {classification → megadetector/classification}/run_classifier.py +2 -1
  15. {classification → megadetector/classification}/train_classifier.py +8 -6
  16. {classification → megadetector/classification}/train_classifier_tf.py +10 -9
  17. {classification → megadetector/classification}/train_utils.py +3 -2
  18. {data_management → megadetector/data_management}/camtrap_dp_to_coco.py +4 -3
  19. {data_management → megadetector/data_management}/cct_json_utils.py +2 -2
  20. {data_management → megadetector/data_management}/cct_to_md.py +1 -1
  21. {data_management → megadetector/data_management}/coco_to_labelme.py +1 -1
  22. {data_management → megadetector/data_management}/coco_to_yolo.py +1 -1
  23. {data_management → megadetector/data_management}/databases/integrity_check_json_db.py +2 -2
  24. {data_management → megadetector/data_management}/get_image_sizes.py +4 -3
  25. {data_management → megadetector/data_management}/importers/auckland_doc_test_to_json.py +6 -5
  26. {data_management → megadetector/data_management}/importers/auckland_doc_to_json.py +4 -3
  27. {data_management → megadetector/data_management}/importers/awc_to_json.py +6 -4
  28. {data_management → megadetector/data_management}/importers/bellevue_to_json.py +3 -3
  29. {data_management → megadetector/data_management}/importers/cacophony-thermal-importer.py +4 -4
  30. {data_management → megadetector/data_management}/importers/carrizo_shrubfree_2018.py +5 -4
  31. {data_management → megadetector/data_management}/importers/carrizo_trail_cam_2017.py +8 -6
  32. {data_management → megadetector/data_management}/importers/cct_field_adjustments.py +2 -1
  33. {data_management → megadetector/data_management}/importers/channel_islands_to_cct.py +2 -2
  34. {data_management → megadetector/data_management}/importers/ena24_to_json.py +6 -5
  35. {data_management → megadetector/data_management}/importers/filenames_to_json.py +2 -1
  36. {data_management → megadetector/data_management}/importers/helena_to_cct.py +7 -6
  37. {data_management → megadetector/data_management}/importers/idaho-camera-traps.py +6 -6
  38. {data_management → megadetector/data_management}/importers/idfg_iwildcam_lila_prep.py +4 -4
  39. {data_management → megadetector/data_management}/importers/jb_csv_to_json.py +1 -1
  40. {data_management → megadetector/data_management}/importers/missouri_to_json.py +4 -3
  41. {data_management → megadetector/data_management}/importers/noaa_seals_2019.py +2 -2
  42. {data_management → megadetector/data_management}/importers/pc_to_json.py +5 -5
  43. {data_management → megadetector/data_management}/importers/prepare-noaa-fish-data-for-lila.py +3 -3
  44. {data_management → megadetector/data_management}/importers/prepare_zsl_imerit.py +3 -3
  45. {data_management → megadetector/data_management}/importers/rspb_to_json.py +2 -2
  46. {data_management → megadetector/data_management}/importers/save_the_elephants_survey_A.py +4 -4
  47. {data_management → megadetector/data_management}/importers/save_the_elephants_survey_B.py +6 -9
  48. {data_management → megadetector/data_management}/importers/snapshot_safari_importer.py +4 -4
  49. {data_management → megadetector/data_management}/importers/snapshot_safari_importer_reprise.py +2 -2
  50. {data_management → megadetector/data_management}/importers/snapshot_serengeti_lila.py +4 -4
  51. {data_management → megadetector/data_management}/importers/timelapse_csv_set_to_json.py +3 -3
  52. {data_management → megadetector/data_management}/importers/ubc_to_json.py +3 -3
  53. {data_management → megadetector/data_management}/importers/umn_to_json.py +2 -2
  54. {data_management → megadetector/data_management}/importers/wellington_to_json.py +3 -3
  55. {data_management → megadetector/data_management}/importers/wi_to_json.py +3 -2
  56. {data_management → megadetector/data_management}/labelme_to_coco.py +6 -7
  57. {data_management → megadetector/data_management}/labelme_to_yolo.py +2 -2
  58. {data_management → megadetector/data_management}/lila/add_locations_to_island_camera_traps.py +4 -4
  59. {data_management → megadetector/data_management}/lila/create_lila_blank_set.py +10 -9
  60. {data_management → megadetector/data_management}/lila/create_lila_test_set.py +3 -2
  61. {data_management → megadetector/data_management}/lila/create_links_to_md_results_files.py +1 -1
  62. {data_management → megadetector/data_management}/lila/download_lila_subset.py +5 -4
  63. {data_management → megadetector/data_management}/lila/generate_lila_per_image_labels.py +6 -5
  64. {data_management → megadetector/data_management}/lila/get_lila_annotation_counts.py +2 -2
  65. {data_management → megadetector/data_management}/lila/get_lila_image_counts.py +2 -1
  66. {data_management → megadetector/data_management}/lila/lila_common.py +5 -5
  67. {data_management → megadetector/data_management}/lila/test_lila_metadata_urls.py +2 -2
  68. {data_management → megadetector/data_management}/ocr_tools.py +6 -6
  69. {data_management → megadetector/data_management}/read_exif.py +2 -2
  70. {data_management → megadetector/data_management}/remap_coco_categories.py +1 -1
  71. {data_management → megadetector/data_management}/remove_exif.py +1 -1
  72. {data_management → megadetector/data_management}/resize_coco_dataset.py +4 -4
  73. {data_management → megadetector/data_management}/wi_download_csv_to_coco.py +3 -3
  74. {data_management → megadetector/data_management}/yolo_output_to_md_output.py +5 -5
  75. {data_management → megadetector/data_management}/yolo_to_coco.py +9 -9
  76. {detection → megadetector/detection}/process_video.py +9 -10
  77. {detection → megadetector/detection}/pytorch_detector.py +12 -8
  78. {detection → megadetector/detection}/run_detector.py +6 -6
  79. {detection → megadetector/detection}/run_detector_batch.py +12 -12
  80. {detection → megadetector/detection}/run_inference_with_yolov5_val.py +12 -12
  81. {detection → megadetector/detection}/run_tiled_inference.py +8 -9
  82. {detection → megadetector/detection}/tf_detector.py +3 -2
  83. {detection → megadetector/detection}/video_utils.py +2 -2
  84. {api/batch_processing → megadetector}/postprocessing/add_max_conf.py +1 -1
  85. {api/batch_processing → megadetector}/postprocessing/categorize_detections_by_size.py +1 -1
  86. {api/batch_processing → megadetector}/postprocessing/combine_api_outputs.py +1 -1
  87. {api/batch_processing → megadetector}/postprocessing/compare_batch_results.py +5 -5
  88. {api/batch_processing → megadetector}/postprocessing/convert_output_format.py +4 -5
  89. {api/batch_processing → megadetector}/postprocessing/load_api_results.py +1 -1
  90. {api/batch_processing → megadetector}/postprocessing/md_to_coco.py +3 -3
  91. {api/batch_processing → megadetector}/postprocessing/md_to_labelme.py +3 -3
  92. {api/batch_processing → megadetector}/postprocessing/merge_detections.py +1 -1
  93. {api/batch_processing → megadetector}/postprocessing/postprocess_batch_results.py +19 -21
  94. {api/batch_processing → megadetector}/postprocessing/remap_detection_categories.py +1 -1
  95. {api/batch_processing → megadetector}/postprocessing/render_detection_confusion_matrix.py +5 -6
  96. {api/batch_processing → megadetector}/postprocessing/repeat_detection_elimination/find_repeat_detections.py +3 -3
  97. {api/batch_processing → megadetector}/postprocessing/repeat_detection_elimination/remove_repeat_detections.py +3 -2
  98. {api/batch_processing → megadetector}/postprocessing/repeat_detection_elimination/repeat_detections_core.py +11 -11
  99. {api/batch_processing → megadetector}/postprocessing/separate_detections_into_folders.py +3 -4
  100. {api/batch_processing → megadetector}/postprocessing/subset_json_detector_output.py +2 -2
  101. {api/batch_processing → megadetector}/postprocessing/top_folders_to_bottom.py +1 -1
  102. {taxonomy_mapping → megadetector/taxonomy_mapping}/map_lila_taxonomy_to_wi_taxonomy.py +2 -2
  103. {taxonomy_mapping → megadetector/taxonomy_mapping}/map_new_lila_datasets.py +2 -6
  104. {taxonomy_mapping → megadetector/taxonomy_mapping}/preview_lila_taxonomy.py +6 -7
  105. {taxonomy_mapping → megadetector/taxonomy_mapping}/retrieve_sample_image.py +1 -1
  106. {taxonomy_mapping → megadetector/taxonomy_mapping}/simple_image_download.py +2 -1
  107. {taxonomy_mapping → megadetector/taxonomy_mapping}/species_lookup.py +1 -1
  108. {taxonomy_mapping → megadetector/taxonomy_mapping}/taxonomy_csv_checker.py +1 -1
  109. {taxonomy_mapping → megadetector/taxonomy_mapping}/validate_lila_category_mappings.py +1 -1
  110. {md_utils → megadetector/utils}/azure_utils.py +7 -3
  111. {md_utils → megadetector/utils}/directory_listing.py +1 -1
  112. {md_utils → megadetector/utils}/md_tests.py +29 -29
  113. {md_utils → megadetector/utils}/split_locations_into_train_val.py +1 -1
  114. {md_utils → megadetector/utils}/write_html_image_list.py +1 -1
  115. {md_visualization → megadetector/visualization}/render_images_with_thumbnails.py +3 -3
  116. {md_visualization → megadetector/visualization}/visualization_utils.py +6 -7
  117. {md_visualization → megadetector/visualization}/visualize_db.py +3 -4
  118. {md_visualization → megadetector/visualization}/visualize_detector_output.py +9 -10
  119. {megadetector-5.0.10.dist-info → megadetector-5.0.12.dist-info}/LICENSE +0 -0
  120. {megadetector-5.0.10.dist-info → megadetector-5.0.12.dist-info}/METADATA +12 -11
  121. megadetector-5.0.12.dist-info/RECORD +199 -0
  122. megadetector-5.0.12.dist-info/top_level.txt +1 -0
  123. api/batch_processing/data_preparation/manage_local_batch.py +0 -2391
  124. api/batch_processing/data_preparation/manage_video_batch.py +0 -327
  125. api/synchronous/api_core/animal_detection_api/data_management/annotations/annotation_constants.py +0 -47
  126. api/synchronous/api_core/animal_detection_api/detection/detector_training/copy_checkpoints.py +0 -43
  127. api/synchronous/api_core/animal_detection_api/detection/process_video.py +0 -543
  128. api/synchronous/api_core/animal_detection_api/detection/pytorch_detector.py +0 -304
  129. api/synchronous/api_core/animal_detection_api/detection/run_detector.py +0 -627
  130. api/synchronous/api_core/animal_detection_api/detection/run_detector_batch.py +0 -1029
  131. api/synchronous/api_core/animal_detection_api/detection/run_inference_with_yolov5_val.py +0 -581
  132. api/synchronous/api_core/animal_detection_api/detection/run_tiled_inference.py +0 -754
  133. api/synchronous/api_core/animal_detection_api/detection/tf_detector.py +0 -165
  134. api/synchronous/api_core/animal_detection_api/detection/video_utils.py +0 -495
  135. api/synchronous/api_core/animal_detection_api/md_utils/azure_utils.py +0 -174
  136. api/synchronous/api_core/animal_detection_api/md_utils/ct_utils.py +0 -262
  137. api/synchronous/api_core/animal_detection_api/md_utils/directory_listing.py +0 -251
  138. api/synchronous/api_core/animal_detection_api/md_utils/matlab_porting_tools.py +0 -97
  139. api/synchronous/api_core/animal_detection_api/md_utils/path_utils.py +0 -416
  140. api/synchronous/api_core/animal_detection_api/md_utils/process_utils.py +0 -110
  141. api/synchronous/api_core/animal_detection_api/md_utils/sas_blob_utils.py +0 -509
  142. api/synchronous/api_core/animal_detection_api/md_utils/string_utils.py +0 -59
  143. api/synchronous/api_core/animal_detection_api/md_utils/url_utils.py +0 -144
  144. api/synchronous/api_core/animal_detection_api/md_utils/write_html_image_list.py +0 -226
  145. api/synchronous/api_core/animal_detection_api/md_visualization/visualization_utils.py +0 -841
  146. detection/detector_training/model_main_tf2.py +0 -114
  147. docs/source/conf.py +0 -43
  148. megadetector-5.0.10.dist-info/RECORD +0 -224
  149. megadetector-5.0.10.dist-info/top_level.txt +0 -8
  150. {api → megadetector/api}/__init__.py +0 -0
  151. {api → megadetector/api}/batch_processing/__init__.py +0 -0
  152. {api → megadetector/api}/batch_processing/api_core/__init__.py +0 -0
  153. {api → megadetector/api}/batch_processing/api_core/batch_service/__init__.py +0 -0
  154. {api → megadetector/api}/batch_processing/api_core/server.py +0 -0
  155. {api → megadetector/api}/batch_processing/api_core/server_api_config.py +0 -0
  156. {api → megadetector/api}/batch_processing/api_core/server_app_config.py +0 -0
  157. {api → megadetector/api}/batch_processing/api_core/server_batch_job_manager.py +0 -0
  158. {api → megadetector/api}/batch_processing/api_core/server_job_status_table.py +0 -0
  159. {api → megadetector/api}/batch_processing/api_core/server_orchestration.py +0 -0
  160. {api → megadetector/api}/batch_processing/api_core/server_utils.py +0 -0
  161. {api → megadetector/api}/batch_processing/api_core_support/__init__.py +0 -0
  162. {api → megadetector/api}/batch_processing/api_core_support/aggregate_results_manually.py +0 -0
  163. {api → megadetector/api}/batch_processing/api_support/__init__.py +0 -0
  164. {api → megadetector/api}/batch_processing/api_support/summarize_daily_activity.py +0 -0
  165. {api → megadetector/api}/batch_processing/data_preparation/__init__.py +0 -0
  166. {api → megadetector/api}/batch_processing/integration/digiKam/setup.py +0 -0
  167. {api → megadetector/api}/batch_processing/integration/digiKam/xmp_integration.py +0 -0
  168. {api → megadetector/api}/batch_processing/integration/eMammal/test_scripts/config_template.py +0 -0
  169. {api → megadetector/api}/batch_processing/integration/eMammal/test_scripts/push_annotations_to_emammal.py +0 -0
  170. {api → megadetector/api}/batch_processing/integration/eMammal/test_scripts/select_images_for_testing.py +0 -0
  171. {api/batch_processing/postprocessing → megadetector/api/synchronous}/__init__.py +0 -0
  172. {api/synchronous → megadetector/api/synchronous/api_core/animal_detection_api}/__init__.py +0 -0
  173. {api → megadetector/api}/synchronous/api_core/animal_detection_api/config.py +0 -0
  174. {api/synchronous/api_core/animal_detection_api → megadetector/api/synchronous/api_core/tests}/__init__.py +0 -0
  175. {api → megadetector/api}/synchronous/api_core/tests/load_test.py +0 -0
  176. {api/synchronous/api_core/tests → megadetector/classification}/__init__.py +0 -0
  177. {classification → megadetector/classification}/aggregate_classifier_probs.py +0 -0
  178. {classification → megadetector/classification}/csv_to_json.py +0 -0
  179. {classification → megadetector/classification}/efficientnet/__init__.py +0 -0
  180. {classification → megadetector/classification}/efficientnet/model.py +0 -0
  181. {classification → megadetector/classification}/efficientnet/utils.py +0 -0
  182. {classification → megadetector/classification}/identify_mislabeled_candidates.py +0 -0
  183. {classification → megadetector/classification}/prepare_classification_script.py +0 -0
  184. {classification → megadetector/classification}/prepare_classification_script_mc.py +0 -0
  185. {classification → megadetector/classification}/save_mislabeled.py +0 -0
  186. {classification → megadetector/data_management}/__init__.py +0 -0
  187. {data_management → megadetector/data_management/annotations}/__init__.py +0 -0
  188. {data_management → megadetector/data_management}/annotations/annotation_constants.py +0 -0
  189. {data_management → megadetector/data_management}/cct_to_wi.py +0 -0
  190. {data_management/annotations → megadetector/data_management/databases}/__init__.py +0 -0
  191. {data_management → megadetector/data_management}/databases/add_width_and_height_to_db.py +0 -0
  192. {data_management → megadetector/data_management}/databases/combine_coco_camera_traps_files.py +0 -0
  193. {data_management → megadetector/data_management}/databases/subset_json_db.py +0 -0
  194. {data_management → megadetector/data_management}/generate_crops_from_cct.py +0 -0
  195. {data_management → megadetector/data_management}/importers/add_nacti_sizes.py +0 -0
  196. {data_management → megadetector/data_management}/importers/add_timestamps_to_icct.py +0 -0
  197. {data_management → megadetector/data_management}/importers/animl_results_to_md_results.py +0 -0
  198. {data_management → megadetector/data_management}/importers/eMammal/copy_and_unzip_emammal.py +0 -0
  199. {data_management → megadetector/data_management}/importers/eMammal/eMammal_helpers.py +0 -0
  200. {data_management → megadetector/data_management}/importers/eMammal/make_eMammal_json.py +0 -0
  201. {data_management → megadetector/data_management}/importers/mcgill_to_json.py +0 -0
  202. {data_management → megadetector/data_management}/importers/nacti_fieldname_adjustments.py +0 -0
  203. {data_management → megadetector/data_management}/importers/plot_wni_giraffes.py +0 -0
  204. {data_management → megadetector/data_management}/importers/snapshotserengeti/make_full_SS_json.py +0 -0
  205. {data_management → megadetector/data_management}/importers/snapshotserengeti/make_per_season_SS_json.py +0 -0
  206. {data_management → megadetector/data_management}/importers/sulross_get_exif.py +0 -0
  207. {data_management → megadetector/data_management}/importers/zamba_results_to_md_results.py +0 -0
  208. {data_management/databases → megadetector/data_management/lila}/__init__.py +0 -0
  209. {data_management → megadetector/data_management}/lila/add_locations_to_nacti.py +0 -0
  210. {data_management/lila → megadetector/detection}/__init__.py +0 -0
  211. {detection → megadetector/detection/detector_training}/__init__.py +0 -0
  212. {api/synchronous/api_core/animal_detection_api → megadetector}/detection/detector_training/model_main_tf2.py +0 -0
  213. {detection/detector_training → megadetector/postprocessing}/__init__.py +0 -0
  214. {md_utils → megadetector/taxonomy_mapping}/__init__.py +0 -0
  215. {taxonomy_mapping → megadetector/taxonomy_mapping}/prepare_lila_taxonomy_release.py +0 -0
  216. {taxonomy_mapping → megadetector/taxonomy_mapping}/taxonomy_graph.py +0 -0
  217. {md_visualization → megadetector/utils}/__init__.py +0 -0
  218. {md_utils → megadetector/utils}/ct_utils.py +0 -0
  219. {md_utils → megadetector/utils}/path_utils.py +0 -0
  220. {md_utils → megadetector/utils}/process_utils.py +0 -0
  221. {md_utils → megadetector/utils}/sas_blob_utils.py +0 -0
  222. {md_utils → megadetector/utils}/string_utils.py +0 -0
  223. {md_utils → megadetector/utils}/url_utils.py +0 -0
  224. {taxonomy_mapping → megadetector/visualization}/__init__.py +0 -0
  225. {md_visualization → megadetector/visualization}/plot_utils.py +0 -0
  226. {megadetector-5.0.10.dist-info → megadetector-5.0.12.dist-info}/WHEEL +0 -0
@@ -1,581 +0,0 @@
1
- ########
2
- #
3
- # run_inference_with_yolov5_val.py
4
- #
5
- # Runs a folder of images through MegaDetector (or another YOLOv5 model) with YOLOv5's
6
- # val.py, converting the output to the standard MD format. The main goal is to leverage
7
- # YOLO's test-time augmentation tools.
8
- #
9
- # YOLOv5's val.py uses each file's base name as a unique identifier, which doesn't work
10
- # when you have typical camera trap images like:
11
- #
12
- # a/b/c/RECONYX0001.JPG
13
- # d/e/f/RECONYX0001.JPG
14
- #
15
- # ...so this script jumps through a bunch of hoops to put a symlinks in a flat
16
- # folder, run YOLOv5 on that folder, and map the results back to the real files.
17
- #
18
- # Currently requires the user to supply the path where a working YOLOv5 install lives,
19
- # and assumes that the current conda environment is all set up for YOLOv5.
20
- #
21
- # By default, this script uses symlinks to format the input images in a way that YOLOv5's
22
- # val.py likes. This requires admin privileges on Windows... actually technically this only
23
- # requires permissions to create symbolic links, but I've never seen a case where someone has
24
- # that permission and *doesn't* have admin privileges. If you are running this script on
25
- # Windows and you don't have admin privileges, use --no_use_symlinks.
26
- #
27
- # TODO:
28
- #
29
- # * Multiple GPU support
30
- #
31
- # * Checkpointing
32
- #
33
- # * Support alternative class names at the command line (currently defaults to MD classes,
34
- # though other class names can be supplied programmatically)
35
- #
36
- ########
37
-
38
- #%% Imports
39
-
40
- import os
41
- import uuid
42
- import glob
43
- import tempfile
44
- import shutil
45
- import json
46
-
47
- from tqdm import tqdm
48
-
49
- from md_utils import path_utils
50
- from md_utils import process_utils
51
- from data_management import yolo_output_to_md_output
52
-
53
-
54
- #%% Options class
55
-
56
- class YoloInferenceOptions:
57
-
58
- ## Required ##
59
-
60
- input_folder = None
61
- model_filename = None
62
- yolo_working_folder = None
63
- output_file = None
64
-
65
- ## Optional ##
66
-
67
- image_size = 1280 * 1.3
68
- conf_thres = '0.001'
69
- batch_size = 1
70
- device_string = '0'
71
- augment = True
72
-
73
- symlink_folder = None
74
- use_symlinks = True
75
-
76
- yolo_results_folder = None
77
-
78
- remove_symlink_folder = True
79
- remove_yolo_results_folder = True
80
-
81
- # These are deliberately offset from the standard MD categories; YOLOv5
82
- # needs categories IDs to start at 0.
83
- yolo_category_id_to_name = {0:'animal',1:'person',2:'vehicle'}
84
-
85
- # 'error','skip','overwrite'
86
- overwrite_handling = 'skip'
87
-
88
-
89
- #%% Main function
90
-
91
- def run_inference_with_yolo_val(options):
92
-
93
- ##%% Path handling
94
-
95
- assert os.path.isdir(options.input_folder) or os.path.isfile(options.input_folder), \
96
- 'Could not find input {}'.format(options.input_folder)
97
- assert os.path.isdir(options.yolo_working_folder), \
98
- 'Could not find working folder {}'.format(options.yolo_working_folder)
99
- assert os.path.isfile(options.model_filename), \
100
- 'Could not find model file {}'.format(options.model_filename)
101
-
102
- if os.path.exists(options.output_file):
103
- if options.overwrite_handling == 'skip':
104
- print('Warning: output file {} exists, skipping'.format(options.output_file))
105
- return
106
- elif options.overwrite_handling == 'overwrite':
107
- print('Warning: output file {} exists, overwriting'.format(options.output_file))
108
- elif options.overwrite_handling == 'error':
109
- raise ValueError('Output file {} exists'.format(options.output_file))
110
- else:
111
- raise ValueError('Unknown output handling method {}'.format(options.overwrite_handling))
112
-
113
- os.makedirs(os.path.dirname(options.output_file),exist_ok=True)
114
-
115
- temporary_folder = None
116
- symlink_folder_is_temp_folder = False
117
- yolo_folder_is_temp_folder = False
118
-
119
- job_id = str(uuid.uuid1())
120
-
121
- def get_job_temporary_folder(tf):
122
- if tf is not None:
123
- return tf
124
- tempdir_base = tempfile.gettempdir()
125
- tf = os.path.join(tempdir_base,'md_to_yolo','md_to_yolo_' + job_id)
126
- os.makedirs(tf,exist_ok=True)
127
- return tf
128
-
129
- symlink_folder = options.symlink_folder
130
- yolo_results_folder = options.yolo_results_folder
131
-
132
- if symlink_folder is None:
133
- temporary_folder = get_job_temporary_folder(temporary_folder)
134
- symlink_folder = os.path.join(temporary_folder,'symlinks')
135
- symlink_folder_is_temp_folder = True
136
-
137
- if yolo_results_folder is None:
138
- temporary_folder = get_job_temporary_folder(temporary_folder)
139
- yolo_results_folder = os.path.join(temporary_folder,'yolo_results')
140
- yolo_folder_is_temp_folder = True
141
-
142
- # Attach a GUID to the symlink folder, regardless of whether we created it
143
- symlink_folder_inner = os.path.join(symlink_folder,job_id)
144
-
145
- os.makedirs(symlink_folder_inner,exist_ok=True)
146
- os.makedirs(yolo_results_folder,exist_ok=True)
147
-
148
-
149
- ##%% Enumerate images
150
-
151
- if os.path.isdir(options.input_folder):
152
- image_files_absolute = path_utils.find_images(options.input_folder,recursive=True)
153
- else:
154
- assert os.path.isfile(options.input_folder)
155
- with open(options.input_folder,'r') as f:
156
- image_files_absolute = json.load(f)
157
- assert isinstance(image_files_absolute,list)
158
- for fn in image_files_absolute:
159
- assert os.path.isfile(fn), 'Could not find image file {}'.format(fn)
160
-
161
-
162
- ##%% Create symlinks to give a unique ID to each image
163
-
164
- image_id_to_file = {}
165
- image_id_to_error = {}
166
-
167
- if options.use_symlinks:
168
- print('Creating {} symlinks in {}'.format(len(image_files_absolute),symlink_folder_inner))
169
- else:
170
- print('Symlinks disabled, copying {} images to {}'.format(len(image_files_absolute),symlink_folder_inner))
171
-
172
- # i_image = 0; image_fn = image_files_absolute[i_image]
173
- for i_image,image_fn in tqdm(enumerate(image_files_absolute),total=len(image_files_absolute)):
174
-
175
- ext = os.path.splitext(image_fn)[1]
176
-
177
- image_id = str(i_image).zfill(10)
178
- image_id_to_file[image_id] = image_fn
179
- symlink_name = image_id + ext
180
- symlink_full_path = os.path.join(symlink_folder_inner,symlink_name)
181
-
182
- try:
183
- if options.use_symlinks:
184
- path_utils.safe_create_link(image_fn,symlink_full_path)
185
- else:
186
- shutil.copyfile(image_fn,symlink_full_path)
187
- except Exception as e:
188
- image_id_to_error[image_id] = str(e)
189
- print('Warning: error copying/creating link for input file {}: {}'.format(
190
- image_fn,str(e)))
191
- continue
192
-
193
- # ...for each image
194
-
195
-
196
- ##%% Create the dataset file
197
-
198
- # Category IDs need to be continuous integers starting at 0
199
- category_ids = sorted(list(options.yolo_category_id_to_name.keys()))
200
- assert category_ids[0] == 0
201
- assert len(category_ids) == 1 + category_ids[-1]
202
-
203
- dataset_file = os.path.join(yolo_results_folder,'dataset.yaml')
204
-
205
- with open(dataset_file,'w') as f:
206
- f.write('path: {}\n'.format(symlink_folder_inner))
207
- f.write('train: .\n')
208
- f.write('val: .\n')
209
- f.write('test: .\n')
210
- f.write('\n')
211
- f.write('nc: {}\n'.format(len(options.yolo_category_id_to_name)))
212
- f.write('\n')
213
- f.write('names:\n')
214
- for category_id in category_ids:
215
- assert isinstance(category_id,int)
216
- f.write(' {}: {}\n'.format(category_id,
217
- options.yolo_category_id_to_name[category_id]))
218
-
219
-
220
- ##%% Prepare YOLOv5 command
221
-
222
- image_size_string = str(round(options.image_size))
223
- cmd = 'python val.py --data "{}"'.format(dataset_file)
224
- cmd += ' --weights "{}"'.format(options.model_filename)
225
- cmd += ' --batch-size {} --imgsz {} --conf-thres {} --task test'.format(
226
- options.batch_size,image_size_string,options.conf_thres)
227
- cmd += ' --device "{}" --save-json'.format(options.device_string)
228
- cmd += ' --project "{}" --name "{}" --exist-ok'.format(yolo_results_folder,'yolo_results')
229
-
230
- if options.augment:
231
- cmd += ' --augment'
232
-
233
-
234
- ##%% Run YOLOv5 command
235
-
236
- current_dir = os.getcwd()
237
- os.chdir(options.yolo_working_folder)
238
- execution_result = process_utils.execute_and_print(cmd)
239
- assert execution_result['status'] == 0, 'Error running YOLOv5'
240
- yolo_console_output = execution_result['output']
241
-
242
- yolo_read_failures = []
243
- for line in yolo_console_output:
244
- if 'cannot identify image file' in line:
245
- tokens = line.split('cannot identify image file')
246
- image_name = tokens[-1].strip()
247
- assert image_name[0] == "'" and image_name [-1] == "'"
248
- image_name = image_name[1:-1]
249
- yolo_read_failures.append(image_name)
250
-
251
- # image_file = yolo_read_failures[0]
252
- for image_file in yolo_read_failures:
253
- image_id = os.path.splitext(os.path.basename(image_file))[0]
254
- assert image_id in image_id_to_file
255
- if image_id not in image_id_to_error:
256
- image_id_to_error[image_id] = 'YOLOv5 read failure'
257
-
258
- os.chdir(current_dir)
259
-
260
-
261
- ##%% Convert results to MD format
262
-
263
- json_files = glob.glob(yolo_results_folder+ '/yolo_results/*.json')
264
- assert len(json_files) == 1
265
- yolo_json_file = json_files[0]
266
-
267
- image_id_to_relative_path = {}
268
- for image_id in image_id_to_file:
269
- fn = image_id_to_file[image_id]
270
- if os.path.isdir(options.input_folder):
271
- assert options.input_folder in fn
272
- relative_path = os.path.relpath(fn,options.input_folder)
273
- else:
274
- assert os.path.isfile(options.input_folder)
275
- # We'll use the absolute path as a relative path, and pass '/'
276
- # as the base path in this case.
277
- relative_path = fn
278
- image_id_to_relative_path[image_id] = relative_path
279
-
280
- if os.path.isdir(options.input_folder):
281
- image_base = options.input_folder
282
- else:
283
- assert os.path.isfile(options.input_folder)
284
- image_base = '/'
285
-
286
- yolo_output_to_md_output.yolo_json_output_to_md_output(
287
- yolo_json_file=yolo_json_file,
288
- image_folder=image_base,
289
- output_file=options.output_file,
290
- yolo_category_id_to_name=options.yolo_category_id_to_name,
291
- detector_name=os.path.basename(options.model_filename),
292
- image_id_to_relative_path=image_id_to_relative_path,
293
- image_id_to_error=image_id_to_error)
294
-
295
-
296
- ##%% Clean up
297
-
298
- if options.remove_symlink_folder:
299
- shutil.rmtree(symlink_folder)
300
- elif symlink_folder_is_temp_folder:
301
- print('Warning: using temporary symlink folder {}, but not removing it'.format(
302
- symlink_folder))
303
-
304
- if options.remove_yolo_results_folder:
305
- shutil.rmtree(yolo_results_folder)
306
- elif yolo_folder_is_temp_folder:
307
- print('Warning: using temporary YOLO results folder {}, but not removing it'.format(
308
- yolo_results_folder))
309
-
310
- # ...def run_inference_with_yolo_val()
311
-
312
-
313
- #%% Command-line driver
314
-
315
- import argparse,sys
316
- from md_utils.ct_utils import args_to_object
317
-
318
- def main():
319
-
320
- options = YoloInferenceOptions()
321
-
322
- parser = argparse.ArgumentParser()
323
- parser.add_argument(
324
- 'model_filename',type=str,
325
- help='model file name')
326
- parser.add_argument(
327
- 'input_folder',type=str,
328
- help='folder on which to recursively run the model, or a .json list of filenames')
329
- parser.add_argument(
330
- 'output_file',type=str,
331
- help='.json file where output will be written')
332
- parser.add_argument(
333
- 'yolo_working_folder',type=str,
334
- help='folder in which to execute val.py')
335
-
336
- parser.add_argument(
337
- '--image_size', default=options.image_size, type=int,
338
- help='image size for model execution (default {})'.format(
339
- options.image_size))
340
- parser.add_argument(
341
- '--conf_thres', default=options.conf_thres, type=float,
342
- help='confidence threshold for including detections in the output file (default {})'.format(
343
- options.conf_thres))
344
- parser.add_argument(
345
- '--batch_size', default=options.batch_size, type=int,
346
- help='inference batch size (default {})'.format(options.batch_size))
347
- parser.add_argument(
348
- '--device_string', default=options.device_string, type=str,
349
- help='CUDA device specifier, e.g. "0" or "cpu" (default {})'.format(options.device_string))
350
- parser.add_argument(
351
- '--overwrite_handling', default=options.overwrite_handling, type=str,
352
- help='action to take if the output file exists (skip, error, overwrite) (default {})'.format(
353
- options.overwrite_handling) )
354
-
355
- parser.add_argument(
356
- '--symlink_folder', type=str,
357
- help='temporary folder for symlinks (defaults to a folder in the system temp dir)')
358
- parser.add_argument(
359
- '--yolo_results_folder', type=str,
360
- help='temporary folder for YOLO intermediate output (defaults to a folder in the system temp dir)')
361
- parser.add_argument(
362
- '--no_use_symlinks', action='store_true',
363
- help='copy files instead of creating symlinks when preparing the yolo input folder')
364
- parser.add_argument(
365
- '--no_remove_symlink_folder', action='store_true',
366
- help='don\'t remove the temporary folder full of symlinks')
367
- parser.add_argument(
368
- '--no_remove_yolo_results_folder', action='store_true',
369
- help='don\'t remove the temporary folder full of YOLO intermediate files')
370
-
371
- if options.augment:
372
- default_augment_enabled = 1
373
- else:
374
- default_augment_enabled = 0
375
- parser.add_argument(
376
- '--augment_enabled', default=default_augment_enabled, type=int,
377
- help='enable/disable augmentation (default {})'.format(default_augment_enabled))
378
-
379
- if len(sys.argv[1:]) == 0:
380
- parser.print_help()
381
- parser.exit()
382
-
383
- args = parser.parse_args()
384
-
385
- args_to_object(args, options)
386
- options.remove_symlink_folder = (not options.no_remove_symlink_folder)
387
- options.remove_yolo_results_folder = (not options.no_remove_yolo_results_folder)
388
- options.use_symlinks = (not options.no_use_symlinks)
389
- options.augment = (options.augment_enabled > 0)
390
-
391
- print(options.__dict__)
392
-
393
- run_inference_with_yolo_val(options)
394
-
395
-
396
- if __name__ == '__main__':
397
- main()
398
-
399
-
400
- #%% Scrap
401
-
402
- if False:
403
-
404
- #%% Run from a set of options
405
-
406
- options = YoloInferenceOptions()
407
-
408
- args = {'augment': 1, 'batch_size': 1, 'conf_thres': 0.005, 'device_string': '1', 'image_size': 1664.0, 'input_folder': '/home/user/postprocessing/usgs-kissel/usgs-kissel-2023-09-11-aug-v5a.0.0/chunk031.json', 'model_filename': '/home/user/models/camera_traps/megadetector/md_v5.0.0/md_v5a.0.0.pt', 'output_file': '/home/user/postprocessing/usgs-kissel/usgs-kissel-2023-09-11-aug-v5a.0.0/chunk031_results.json', 'overwrite_handling': 'skip', 'symlink_folder': '/home/user/postprocessing/usgs-kissel/usgs-kissel-2023-09-11-aug-v5a.0.0/symlinks/symlinks_031', 'yolo_results_folder': '/home/user/postprocessing/usgs-kissel/usgs-kissel-2023-09-11-aug-v5a.0.0/yolo_results/yolo_results_031', 'yolo_working_folder': '/home/user/git/yolov5', 'remove_symlink_folder': True, 'remove_yolo_results_folder': True, 'use_symlinks': False, 'augment': True}
409
-
410
- for k in args:
411
- setattr(options, k, args[k])
412
-
413
-
414
- #%% Test driver (folder)
415
-
416
- project_name = 'KRU-test-corrupted'
417
- input_folder = os.path.expanduser(f'~/data/{project_name}')
418
- output_folder = os.path.expanduser(f'~/tmp/{project_name}')
419
- model_filename = os.path.expanduser('~/models/camera_traps/megadetector/md_v5.0.0/md_v5a.0.0.pt')
420
- yolo_working_folder = os.path.expanduser('~/git/yolov5')
421
- model_name = os.path.splitext(os.path.basename(model_filename))[0]
422
-
423
- symlink_folder = os.path.join(output_folder,'symlinks')
424
- yolo_results_folder = os.path.join(output_folder,'yolo_results')
425
-
426
- output_file = os.path.join(output_folder,'{}_{}-md_format.json'.format(
427
- project_name,model_name))
428
-
429
- options = YoloInferenceOptions()
430
-
431
- options.yolo_working_folder = yolo_working_folder
432
-
433
- options.output_file = output_file
434
-
435
- options.augment = False
436
- options.conf_thres = '0.001'
437
- options.batch_size = 1
438
- options.device_string = '0'
439
-
440
- if options.augment:
441
- options.image_size = round(1280 * 1.3)
442
- else:
443
- options.image_size = 1280
444
-
445
- options.input_folder = input_folder
446
- options.model_filename = model_filename
447
-
448
- options.yolo_results_folder = yolo_results_folder # os.path.join(output_folder + 'yolo_results')
449
- options.symlink_folder = symlink_folder # os.path.join(output_folder,'symlinks')
450
- options.use_symlinks = False
451
-
452
- options.remove_temporary_symlink_folder = False
453
- options.remove_yolo_results_file = False
454
-
455
- cmd = f'python run_inference_with_yolov5_val.py {model_filename} {input_folder} {output_file} {yolo_working_folder} ' + \
456
- f' --image_size {options.image_size} --conf_thres {options.conf_thres} --batch_size {options.batch_size} ' + \
457
- f' --symlink_folder {options.symlink_folder} --yolo_results_folder {options.yolo_results_folder} ' + \
458
- ' --no_remove_symlink_folder --no_remove_yolo_results_folder'
459
-
460
- if not options.use_symlinks:
461
- cmd += ' --no_use_symlinks'
462
- if not options.augment:
463
- cmd += ' --augment_enabled 0'
464
-
465
- print(cmd)
466
- execute_in_python = False
467
- if execute_in_python:
468
- run_inference_with_yolo_val(options)
469
- else:
470
- import clipboard; clipboard.copy(cmd)
471
-
472
-
473
- #%% Preview results
474
-
475
- postprocessing_output_folder = os.path.join(output_folder,'yolo-val-preview')
476
- md_json_file = options.output_file
477
-
478
- from api.batch_processing.postprocessing.postprocess_batch_results import (
479
- PostProcessingOptions, process_batch_results)
480
-
481
- with open(md_json_file,'r') as f:
482
- d = json.load(f)
483
-
484
- base_task_name = os.path.basename(md_json_file)
485
-
486
- pp_options = PostProcessingOptions()
487
- pp_options.image_base_dir = input_folder
488
- pp_options.include_almost_detections = True
489
- pp_options.num_images_to_sample = None
490
- pp_options.confidence_threshold = 0.1
491
- pp_options.almost_detection_confidence_threshold = pp_options.confidence_threshold - 0.025
492
- pp_options.ground_truth_json_file = None
493
- pp_options.separate_detections_by_category = True
494
- # pp_options.sample_seed = 0
495
-
496
- pp_options.parallelize_rendering = True
497
- pp_options.parallelize_rendering_n_cores = 16
498
- pp_options.parallelize_rendering_with_threads = False
499
-
500
- output_base = os.path.join(postprocessing_output_folder,
501
- base_task_name + '_{:.3f}'.format(pp_options.confidence_threshold))
502
-
503
- os.makedirs(output_base, exist_ok=True)
504
- print('Processing to {}'.format(output_base))
505
-
506
- pp_options.api_output_file = md_json_file
507
- pp_options.output_dir = output_base
508
- ppresults = process_batch_results(pp_options)
509
- html_output_file = ppresults.output_html_file
510
-
511
- path_utils.open_file(html_output_file)
512
-
513
- # ...for each prediction file
514
-
515
-
516
- #%% Compare results
517
-
518
- import itertools
519
-
520
- from api.batch_processing.postprocessing.compare_batch_results import (
521
- BatchComparisonOptions,PairwiseBatchComparisonOptions,compare_batch_results)
522
-
523
- options = BatchComparisonOptions()
524
-
525
- organization_name = ''
526
- project_name = ''
527
-
528
- options.job_name = f'{organization_name}-comparison'
529
- options.output_folder = os.path.join(output_folder,'model_comparison')
530
- options.image_folder = input_folder
531
-
532
- options.pairwise_options = []
533
-
534
- filenames = [
535
- f'/home/user/tmp/{project_name}/{project_name}_md_v5a.0.0-md_format.json',
536
- f'/home/user/postprocessing/{organization_name}/{organization_name}-2023-04-06-v5a.0.0/combined_api_outputs/{organization_name}-2023-04-06-v5a.0.0_detections.json',
537
- f'/home/user/postprocessing/{organization_name}/{organization_name}-2023-04-06-v5b.0.0/combined_api_outputs/{organization_name}-2023-04-06-v5b.0.0_detections.json'
538
- ]
539
-
540
- descriptions = ['YOLO w/augment','MDv5a','MDv5b']
541
-
542
- if False:
543
- results = []
544
-
545
- for fn in filenames:
546
- with open(fn,'r') as f:
547
- d = json.load(f)
548
- results.append(d)
549
-
550
- detection_thresholds = [0.1,0.1,0.1]
551
-
552
- assert len(detection_thresholds) == len(filenames)
553
-
554
- rendering_thresholds = [(x*0.6666) for x in detection_thresholds]
555
-
556
- # Choose all pairwise combinations of the files in [filenames]
557
- for i, j in itertools.combinations(list(range(0,len(filenames))),2):
558
-
559
- pairwise_options = PairwiseBatchComparisonOptions()
560
-
561
- pairwise_options.results_filename_a = filenames[i]
562
- pairwise_options.results_filename_b = filenames[j]
563
-
564
- pairwise_options.results_description_a = descriptions[i]
565
- pairwise_options.results_description_b = descriptions[j]
566
-
567
- pairwise_options.rendering_confidence_threshold_a = rendering_thresholds[i]
568
- pairwise_options.rendering_confidence_threshold_b = rendering_thresholds[j]
569
-
570
- pairwise_options.detection_thresholds_a = {'animal':detection_thresholds[i],
571
- 'person':detection_thresholds[i],
572
- 'vehicle':detection_thresholds[i]}
573
- pairwise_options.detection_thresholds_b = {'animal':detection_thresholds[j],
574
- 'person':detection_thresholds[j],
575
- 'vehicle':detection_thresholds[j]}
576
- options.pairwise_options.append(pairwise_options)
577
-
578
- results = compare_batch_results(options)
579
-
580
- from md_utils.path_utils import open_file
581
- open_file(results.html_output_file)