Post processsing using DLC utils key error

Hi , I am trying to do some post processing on DLC output files. I am using a pre trained model first to do analysis and then run it on sample videos.

I can read the h5 files but when i try to access a certain keypoint I get key error and I was wondering why ? I am not running it on jupyternotebook but natively on my local system.

import pandas as pd
import numpy as np

DLCscorer = 'DLC_resnet50_t1Nov16shuffle1_60000'
df = pd.read_hdf('/home/sandbox/DLC_resnet50_refJan14shuffle1_60000.h5')
print(df.head())
bpt = 'Nose'
xvalues=df[DLCscorer][bpt]['x'].values
print(xvalues)

The output of it is as follows

scorer    DLC_resnet50_refJan14shuffle1_60000                                                                        ...                                                                                     
bodyparts                                Nose                                Eye                           Nearknee  ... Offhindfetlock Offhindfoot                            Ischium                       
coords                                      x           y likelihood           x           y likelihood           x  ...     likelihood           x           y likelihood           x           y likelihood
0                                  400.960236  615.656250        1.0  377.561615  580.536743   0.999997  357.805176  ...       1.000000   87.803474  786.541382   0.999997  147.820618  652.951965        1.0
1                                  421.998047  619.109070        1.0  400.585327  582.936035   1.000000  368.237335  ...       1.000000   94.383957  784.617737   0.999979  166.802475  652.178528        1.0
2                                  442.585999  622.709961        1.0  422.380890  586.786743   1.000000  377.525513  ...       1.000000  117.975861  776.955078   0.999987  183.814301  652.417480        1.0
3                                  462.066406  628.464050        1.0  443.044891  589.996887   1.000000  381.022491  ...       0.999998  142.195984  768.622437   0.999980  203.175476  650.075745        1.0
4                                  480.481415  629.203491        1.0  463.040466  589.852600   1.000000  387.465637  ...       0.999853  216.780777  787.220520   0.999686  225.965851  641.960449        1.0

[5 rows x 66 columns]
Traceback (most recent call last):
  File "/home//.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2898, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DLC_resnet50_t1Nov16shuffle1_60000'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "utils.py", line 8, in <module>
    xsnout=df[DLCscorer][bpt]['x'].values
  File "/home/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 2905, in __getitem__
    return self._getitem_multilevel(key)
  File "/home/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 2955, in _getitem_multilevel
    loc = self.columns.get_loc(key)
  File "/home/.local/lib/python3.6/site-packages/pandas/core/indexes/multi.py", line 2704, in get_loc
    loc = self._get_level_indexer(key, level=0)
  File "/home/.local/lib/python3.6/site-packages/pandas/core/indexes/multi.py", line 2969, in _get_level_indexer
    code = self._get_loc_single_level_index(level_index, key)
  File "/home/.local/lib/python3.6/site-packages/pandas/core/indexes/multi.py", line 2637, in _get_loc_single_level_index
    return level_index.get_loc(key)
  File "/home/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2900, in get_loc
    raise KeyError(key) from err
KeyError: 'DLC_resnet50_t1Nov16shuffle1_60000'

I am wondering what is the reason for such an error ?

Hey, your DLCscorer looks wrong (rather, it is different from the one written to your data file).
DLC_resnet50_refJan14shuffle1_60000 should be the correct one.
Alternatively, note that you could directly get the desired column as follows:
df.xs([bpt, 'x'], level=['bodyparts', 'coords'], axis=1)

1 Like