MCPcopy Index your code
hub / github.com/DeepLabCut/DeepLabCut / calc_map_from_obj

Function calc_map_from_obj

deeplabcut/benchmark/metrics.py:139–197  ·  view source on GitHub ↗

Calculate mean average precision (mAP) based on predictions.

(
    eval_results_obj,
    h5_file,
    metadata_file,
    oks_sigma=0.1,
    margin=0,
    symmetric_kpts=None,
    drop_kpts=None,
)

Source from the content-addressed store, hash-verified

137
138
139def calc_map_from_obj(
140 eval_results_obj,
141 h5_file,
142 metadata_file,
143 oks_sigma=0.1,
144 margin=0,
145 symmetric_kpts=None,
146 drop_kpts=None,
147):
148 """Calculate mean average precision (mAP) based on predictions."""
149 df = pd.read_hdf(h5_file)
150 try:
151 df.drop("single", level="individuals", axis=1, inplace=True)
152 except KeyError:
153 pass
154 n_animals = len(df.columns.get_level_values("individuals").unique())
155 kpts = list(df.columns.get_level_values("bodyparts").unique())
156
157 test_indices = _load_test_indices(metadata_file)
158 df_test = df.iloc[test_indices]
159 test_images = load_test_images(h5_file, metadata_file)
160 missing_images = set(test_images) - set(eval_results_obj.keys())
161 if len(missing_images) > 0:
162 raise ValueError(
163 f"Failed to compute the test mAP: there are test images missing from theprediction object: {missing_images}"
164 )
165
166 ground_truth = df_test.to_numpy().reshape((len(test_images), n_animals, -1, 2))
167 temp = np.ones((*ground_truth.shape[:3], 3))
168 temp[..., :2] = ground_truth
169 assemblies_gt_test = {
170 test_images[i]: assembly for i, assembly in inferenceutils._parse_ground_truth_data(temp).items()
171 }
172
173 # TODO(stes): remove/rewrite
174 if drop_kpts is not None:
175 temp = {}
176 for k, v in assemblies_gt_test.items():
177 lst = []
178 for a in v:
179 arr = np.delete(a.data[:, :3], drop_kpts, axis=0)
180 a = inferenceutils.Assembly.from_array(arr)
181 lst.append(a)
182 temp[k] = lst
183 assemblies_gt_test = temp
184 for ind in sorted(drop_kpts, reverse=True):
185 kpts.pop(ind)
186
187 assemblies_pred = conv_obj_to_assemblies(eval_results_obj, kpts)
188 with deeplabcut.benchmark.utils.DisableOutput():
189 oks = inferenceutils.evaluate_assembly(
190 assemblies_pred,
191 assemblies_gt_test,
192 oks_sigma,
193 margin=margin,
194 symmetric_kpts=symmetric_kpts,
195 greedy_matching=True,
196 )

Callers

nothing calls this directly

Calls 7

_load_test_indicesFunction · 0.85
load_test_imagesFunction · 0.85
conv_obj_to_assembliesFunction · 0.85
uniqueMethod · 0.80
keysMethod · 0.80
itemsMethod · 0.80
from_arrayMethod · 0.80

Tested by

no test coverage detected