megadetector 10.0.13__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.
- megadetector/__init__.py +0 -0
- megadetector/api/__init__.py +0 -0
- megadetector/api/batch_processing/integration/digiKam/setup.py +6 -0
- megadetector/api/batch_processing/integration/digiKam/xmp_integration.py +465 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/config_template.py +5 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/push_annotations_to_emammal.py +125 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/select_images_for_testing.py +55 -0
- megadetector/classification/__init__.py +0 -0
- megadetector/classification/aggregate_classifier_probs.py +108 -0
- megadetector/classification/analyze_failed_images.py +227 -0
- megadetector/classification/cache_batchapi_outputs.py +198 -0
- megadetector/classification/create_classification_dataset.py +626 -0
- megadetector/classification/crop_detections.py +516 -0
- megadetector/classification/csv_to_json.py +226 -0
- megadetector/classification/detect_and_crop.py +853 -0
- megadetector/classification/efficientnet/__init__.py +9 -0
- megadetector/classification/efficientnet/model.py +415 -0
- megadetector/classification/efficientnet/utils.py +608 -0
- megadetector/classification/evaluate_model.py +520 -0
- megadetector/classification/identify_mislabeled_candidates.py +152 -0
- megadetector/classification/json_to_azcopy_list.py +63 -0
- megadetector/classification/json_validator.py +696 -0
- megadetector/classification/map_classification_categories.py +276 -0
- megadetector/classification/merge_classification_detection_output.py +509 -0
- megadetector/classification/prepare_classification_script.py +194 -0
- megadetector/classification/prepare_classification_script_mc.py +228 -0
- megadetector/classification/run_classifier.py +287 -0
- megadetector/classification/save_mislabeled.py +110 -0
- megadetector/classification/train_classifier.py +827 -0
- megadetector/classification/train_classifier_tf.py +725 -0
- megadetector/classification/train_utils.py +323 -0
- megadetector/data_management/__init__.py +0 -0
- megadetector/data_management/animl_to_md.py +161 -0
- megadetector/data_management/annotations/__init__.py +0 -0
- megadetector/data_management/annotations/annotation_constants.py +33 -0
- megadetector/data_management/camtrap_dp_to_coco.py +270 -0
- megadetector/data_management/cct_json_utils.py +566 -0
- megadetector/data_management/cct_to_md.py +184 -0
- megadetector/data_management/cct_to_wi.py +293 -0
- megadetector/data_management/coco_to_labelme.py +284 -0
- megadetector/data_management/coco_to_yolo.py +702 -0
- megadetector/data_management/databases/__init__.py +0 -0
- megadetector/data_management/databases/add_width_and_height_to_db.py +107 -0
- megadetector/data_management/databases/combine_coco_camera_traps_files.py +210 -0
- megadetector/data_management/databases/integrity_check_json_db.py +528 -0
- megadetector/data_management/databases/subset_json_db.py +195 -0
- megadetector/data_management/generate_crops_from_cct.py +200 -0
- megadetector/data_management/get_image_sizes.py +164 -0
- megadetector/data_management/labelme_to_coco.py +559 -0
- megadetector/data_management/labelme_to_yolo.py +349 -0
- megadetector/data_management/lila/__init__.py +0 -0
- megadetector/data_management/lila/create_lila_blank_set.py +556 -0
- megadetector/data_management/lila/create_lila_test_set.py +187 -0
- megadetector/data_management/lila/create_links_to_md_results_files.py +106 -0
- megadetector/data_management/lila/download_lila_subset.py +182 -0
- megadetector/data_management/lila/generate_lila_per_image_labels.py +777 -0
- megadetector/data_management/lila/get_lila_annotation_counts.py +174 -0
- megadetector/data_management/lila/get_lila_image_counts.py +112 -0
- megadetector/data_management/lila/lila_common.py +319 -0
- megadetector/data_management/lila/test_lila_metadata_urls.py +164 -0
- megadetector/data_management/mewc_to_md.py +344 -0
- megadetector/data_management/ocr_tools.py +873 -0
- megadetector/data_management/read_exif.py +964 -0
- megadetector/data_management/remap_coco_categories.py +195 -0
- megadetector/data_management/remove_exif.py +156 -0
- megadetector/data_management/rename_images.py +194 -0
- megadetector/data_management/resize_coco_dataset.py +663 -0
- megadetector/data_management/speciesnet_to_md.py +41 -0
- megadetector/data_management/wi_download_csv_to_coco.py +247 -0
- megadetector/data_management/yolo_output_to_md_output.py +594 -0
- megadetector/data_management/yolo_to_coco.py +876 -0
- megadetector/data_management/zamba_to_md.py +188 -0
- megadetector/detection/__init__.py +0 -0
- megadetector/detection/change_detection.py +840 -0
- megadetector/detection/process_video.py +479 -0
- megadetector/detection/pytorch_detector.py +1451 -0
- megadetector/detection/run_detector.py +1267 -0
- megadetector/detection/run_detector_batch.py +2159 -0
- megadetector/detection/run_inference_with_yolov5_val.py +1314 -0
- megadetector/detection/run_md_and_speciesnet.py +1494 -0
- megadetector/detection/run_tiled_inference.py +1038 -0
- megadetector/detection/tf_detector.py +209 -0
- megadetector/detection/video_utils.py +1379 -0
- megadetector/postprocessing/__init__.py +0 -0
- megadetector/postprocessing/add_max_conf.py +72 -0
- megadetector/postprocessing/categorize_detections_by_size.py +166 -0
- megadetector/postprocessing/classification_postprocessing.py +1752 -0
- megadetector/postprocessing/combine_batch_outputs.py +249 -0
- megadetector/postprocessing/compare_batch_results.py +2110 -0
- megadetector/postprocessing/convert_output_format.py +403 -0
- megadetector/postprocessing/create_crop_folder.py +629 -0
- megadetector/postprocessing/detector_calibration.py +570 -0
- megadetector/postprocessing/generate_csv_report.py +522 -0
- megadetector/postprocessing/load_api_results.py +223 -0
- megadetector/postprocessing/md_to_coco.py +428 -0
- megadetector/postprocessing/md_to_labelme.py +351 -0
- megadetector/postprocessing/md_to_wi.py +41 -0
- megadetector/postprocessing/merge_detections.py +392 -0
- megadetector/postprocessing/postprocess_batch_results.py +2077 -0
- megadetector/postprocessing/remap_detection_categories.py +226 -0
- megadetector/postprocessing/render_detection_confusion_matrix.py +677 -0
- megadetector/postprocessing/repeat_detection_elimination/find_repeat_detections.py +206 -0
- megadetector/postprocessing/repeat_detection_elimination/remove_repeat_detections.py +82 -0
- megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py +1665 -0
- megadetector/postprocessing/separate_detections_into_folders.py +795 -0
- megadetector/postprocessing/subset_json_detector_output.py +964 -0
- megadetector/postprocessing/top_folders_to_bottom.py +238 -0
- megadetector/postprocessing/validate_batch_results.py +332 -0
- megadetector/taxonomy_mapping/__init__.py +0 -0
- megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py +491 -0
- megadetector/taxonomy_mapping/map_new_lila_datasets.py +213 -0
- megadetector/taxonomy_mapping/prepare_lila_taxonomy_release.py +165 -0
- megadetector/taxonomy_mapping/preview_lila_taxonomy.py +543 -0
- megadetector/taxonomy_mapping/retrieve_sample_image.py +71 -0
- megadetector/taxonomy_mapping/simple_image_download.py +224 -0
- megadetector/taxonomy_mapping/species_lookup.py +1008 -0
- megadetector/taxonomy_mapping/taxonomy_csv_checker.py +159 -0
- megadetector/taxonomy_mapping/taxonomy_graph.py +346 -0
- megadetector/taxonomy_mapping/validate_lila_category_mappings.py +83 -0
- megadetector/tests/__init__.py +0 -0
- megadetector/tests/test_nms_synthetic.py +335 -0
- megadetector/utils/__init__.py +0 -0
- megadetector/utils/ct_utils.py +1857 -0
- megadetector/utils/directory_listing.py +199 -0
- megadetector/utils/extract_frames_from_video.py +307 -0
- megadetector/utils/gpu_test.py +125 -0
- megadetector/utils/md_tests.py +2072 -0
- megadetector/utils/path_utils.py +2832 -0
- megadetector/utils/process_utils.py +172 -0
- megadetector/utils/split_locations_into_train_val.py +237 -0
- megadetector/utils/string_utils.py +234 -0
- megadetector/utils/url_utils.py +825 -0
- megadetector/utils/wi_platform_utils.py +968 -0
- megadetector/utils/wi_taxonomy_utils.py +1759 -0
- megadetector/utils/write_html_image_list.py +239 -0
- megadetector/visualization/__init__.py +0 -0
- megadetector/visualization/plot_utils.py +309 -0
- megadetector/visualization/render_images_with_thumbnails.py +243 -0
- megadetector/visualization/visualization_utils.py +1940 -0
- megadetector/visualization/visualize_db.py +630 -0
- megadetector/visualization/visualize_detector_output.py +479 -0
- megadetector/visualization/visualize_video_output.py +705 -0
- megadetector-10.0.13.dist-info/METADATA +134 -0
- megadetector-10.0.13.dist-info/RECORD +147 -0
- megadetector-10.0.13.dist-info/WHEEL +5 -0
- megadetector-10.0.13.dist-info/licenses/LICENSE +19 -0
- megadetector-10.0.13.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
remap_detection_categories.py
|
|
4
|
+
|
|
5
|
+
Given a MegaDetector results file, remap the category IDs according to a specified
|
|
6
|
+
dictionary, writing the results to a new file.
|
|
7
|
+
|
|
8
|
+
Currently only supports remapping detection categories, not classification categories.
|
|
9
|
+
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
#%% Constants and imports
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
import json
|
|
16
|
+
import argparse
|
|
17
|
+
|
|
18
|
+
from tqdm import tqdm
|
|
19
|
+
|
|
20
|
+
from megadetector.utils.ct_utils import invert_dictionary
|
|
21
|
+
from megadetector.utils.ct_utils import write_json
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#%% Main function
|
|
25
|
+
|
|
26
|
+
def remap_detection_categories(input_file,
|
|
27
|
+
output_file,
|
|
28
|
+
target_category_map,
|
|
29
|
+
extra_category_handling='error',
|
|
30
|
+
overwrite=False):
|
|
31
|
+
"""
|
|
32
|
+
Given a MegaDetector results file [input_file], remap the category IDs according to the dictionary
|
|
33
|
+
[target_category_map], writing the results to [output_file]. The remapped dictionary needs to have
|
|
34
|
+
the same category names as the input file's detection_categories dictionary.
|
|
35
|
+
|
|
36
|
+
Typically used to map, e.g., a variety of species to the class "mammal" or the class "animal".
|
|
37
|
+
|
|
38
|
+
Currently only supports remapping detection categories, not classification categories.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
input_file (str): the MD .json results file to remap
|
|
42
|
+
output_file (str): the remapped .json file to write
|
|
43
|
+
target_category_map (dict): the category mapping that should be used in the output file.
|
|
44
|
+
This can also be a MD results file, in which case we'll use that file's
|
|
45
|
+
detection_categories dictionary.
|
|
46
|
+
extra_category_handling (str, optional): specifies what we should do if categories are present
|
|
47
|
+
in the source file that are not present in the target mapping:
|
|
48
|
+
|
|
49
|
+
* 'error' == Error in this case.
|
|
50
|
+
* 'drop_if_unused' == Don't include these in the output file's category mappings if they are
|
|
51
|
+
unused, error if they are.
|
|
52
|
+
* 'remap' == Remap to unused category IDs. This is reserved for future use, not currently
|
|
53
|
+
implemented.
|
|
54
|
+
overwrite (bool, optional): whether to overwrite [output_file] if it exists; if this is True and
|
|
55
|
+
[output_file] exists, this function is a no-op
|
|
56
|
+
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
if os.path.exists(output_file) and (not overwrite):
|
|
60
|
+
print('File {} exists, bypassing remapping'.format(output_file))
|
|
61
|
+
return
|
|
62
|
+
|
|
63
|
+
assert os.path.isfile(input_file), \
|
|
64
|
+
'File {} does not exist'.format(input_file)
|
|
65
|
+
|
|
66
|
+
# If "target_category_map" is passed as a filename, load the "detection_categories"
|
|
67
|
+
# dict.
|
|
68
|
+
if isinstance(target_category_map,str):
|
|
69
|
+
target_categories_file = target_category_map
|
|
70
|
+
with open(target_categories_file,'r') as f:
|
|
71
|
+
d = json.load(f)
|
|
72
|
+
target_category_map = d['detection_categories']
|
|
73
|
+
assert isinstance(target_category_map,dict)
|
|
74
|
+
|
|
75
|
+
with open(input_file,'r') as f:
|
|
76
|
+
input_data = json.load(f)
|
|
77
|
+
|
|
78
|
+
input_images = input_data['images']
|
|
79
|
+
input_categories = input_data['detection_categories']
|
|
80
|
+
|
|
81
|
+
# Figure out which categories are actually used
|
|
82
|
+
used_category_ids = set()
|
|
83
|
+
for im in input_images:
|
|
84
|
+
|
|
85
|
+
if 'detections' not in im or im['detections'] is None:
|
|
86
|
+
continue
|
|
87
|
+
|
|
88
|
+
for det in im['detections']:
|
|
89
|
+
used_category_ids.add(det['category'])
|
|
90
|
+
used_category_names = [input_categories[cid] for cid in used_category_ids]
|
|
91
|
+
|
|
92
|
+
input_names_set = set(input_categories.values())
|
|
93
|
+
output_names_set = set(target_category_map.values())
|
|
94
|
+
|
|
95
|
+
# category_name = list(input_names_set)[0]
|
|
96
|
+
for category_name in input_names_set:
|
|
97
|
+
if category_name in output_names_set:
|
|
98
|
+
continue
|
|
99
|
+
if extra_category_handling == 'error':
|
|
100
|
+
raise ValueError('Category {} present in source but not in target'.format(category_name))
|
|
101
|
+
elif extra_category_handling == 'drop_if_unused':
|
|
102
|
+
if category_name in used_category_names:
|
|
103
|
+
raise ValueError('Category {} present (and used) in source but not in target'.format(
|
|
104
|
+
category_name))
|
|
105
|
+
else:
|
|
106
|
+
print('Category {} is unused and not present in the target mapping, ignoring'.format(
|
|
107
|
+
category_name))
|
|
108
|
+
continue
|
|
109
|
+
elif extra_category_handling == 'remap':
|
|
110
|
+
raise NotImplementedError('Remapping of extra category IDs not yet implemented')
|
|
111
|
+
else:
|
|
112
|
+
raise ValueError('Unrecognized extra category handling scheme {}'.format(
|
|
113
|
+
extra_category_handling))
|
|
114
|
+
|
|
115
|
+
output_category_name_to_output_category_id = invert_dictionary(target_category_map)
|
|
116
|
+
|
|
117
|
+
input_category_id_to_output_category_id = {}
|
|
118
|
+
for input_category_id in input_categories.keys():
|
|
119
|
+
category_name = input_categories[input_category_id]
|
|
120
|
+
if category_name not in output_category_name_to_output_category_id:
|
|
121
|
+
assert category_name not in used_category_names
|
|
122
|
+
else:
|
|
123
|
+
output_category_id = output_category_name_to_output_category_id[category_name]
|
|
124
|
+
input_category_id_to_output_category_id[input_category_id] = output_category_id
|
|
125
|
+
|
|
126
|
+
# im = input_images[0]
|
|
127
|
+
for im in tqdm(input_images):
|
|
128
|
+
|
|
129
|
+
if 'detections' not in im or im['detections'] is None:
|
|
130
|
+
continue
|
|
131
|
+
|
|
132
|
+
# det = im['detections'][0]
|
|
133
|
+
for det in im['detections']:
|
|
134
|
+
det['category'] = input_category_id_to_output_category_id[det['category']]
|
|
135
|
+
|
|
136
|
+
# ...for each image
|
|
137
|
+
|
|
138
|
+
input_data['detection_categories'] = target_category_map
|
|
139
|
+
|
|
140
|
+
write_json(output_file,input_data)
|
|
141
|
+
|
|
142
|
+
print('Saved remapped results to {}'.format(output_file))
|
|
143
|
+
|
|
144
|
+
# ...def remap_detection_categories(...)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
#%% Interactive driver
|
|
148
|
+
|
|
149
|
+
if False:
|
|
150
|
+
|
|
151
|
+
pass
|
|
152
|
+
|
|
153
|
+
#%%
|
|
154
|
+
|
|
155
|
+
target_categories_file = 'all-classes_usgs-only_yolov5x6.json'
|
|
156
|
+
target_category_map = target_categories_file
|
|
157
|
+
input_file = 'x.json'
|
|
158
|
+
|
|
159
|
+
output_file = input_file.replace('.json','_remapped.json')
|
|
160
|
+
assert output_file != input_file
|
|
161
|
+
overwrite = True
|
|
162
|
+
|
|
163
|
+
extra_category_handling = 'drop_if_unused'
|
|
164
|
+
|
|
165
|
+
remap_detection_categories(input_file=input_file,
|
|
166
|
+
output_file=output_file,
|
|
167
|
+
target_category_map=target_category_map,
|
|
168
|
+
extra_category_handling=extra_category_handling,
|
|
169
|
+
overwrite=overwrite)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
#%% Command-line driver
|
|
173
|
+
|
|
174
|
+
def main():
|
|
175
|
+
"""
|
|
176
|
+
Command-line interface for remapping detection categories in a MegaDetector results file
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
parser = argparse.ArgumentParser(
|
|
180
|
+
description='Remap detection categories in a MegaDetector .json results file'
|
|
181
|
+
)
|
|
182
|
+
parser.add_argument(
|
|
183
|
+
'input_file',
|
|
184
|
+
type=str,
|
|
185
|
+
help='Path to the MegaDetector .json results file to remap'
|
|
186
|
+
)
|
|
187
|
+
parser.add_argument(
|
|
188
|
+
'output_file',
|
|
189
|
+
type=str,
|
|
190
|
+
help='Path to save the remapped .json results file'
|
|
191
|
+
)
|
|
192
|
+
parser.add_argument(
|
|
193
|
+
'target_category_map_file',
|
|
194
|
+
type=str,
|
|
195
|
+
help="Path to a MegaDetector .json results file from which to take the target 'detection_categories' mapping"
|
|
196
|
+
)
|
|
197
|
+
parser.add_argument(
|
|
198
|
+
'--extra_category_handling',
|
|
199
|
+
type=str,
|
|
200
|
+
default='error',
|
|
201
|
+
choices=['error', 'drop_if_unused'],
|
|
202
|
+
help="How to handle source categories not in target map (default: 'error')"
|
|
203
|
+
)
|
|
204
|
+
parser.add_argument(
|
|
205
|
+
'--overwrite',
|
|
206
|
+
type=str,
|
|
207
|
+
default='false',
|
|
208
|
+
choices=['true', 'false'],
|
|
209
|
+
help="Overwrite output file if it exists (default: 'false')."
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
args = parser.parse_args()
|
|
213
|
+
|
|
214
|
+
overwrite_bool = (args.overwrite.lower() == 'true')
|
|
215
|
+
|
|
216
|
+
print('Starting category remapping...')
|
|
217
|
+
|
|
218
|
+
remap_detection_categories(
|
|
219
|
+
input_file=args.input_file,
|
|
220
|
+
output_file=args.output_file,
|
|
221
|
+
target_category_map=args.target_category_map_file, # Pass filename directly
|
|
222
|
+
extra_category_handling=args.extra_category_handling,
|
|
223
|
+
overwrite=overwrite_bool)
|
|
224
|
+
|
|
225
|
+
if __name__ == '__main__':
|
|
226
|
+
main()
|