kwcoco._helpers module

These items were split out of coco_dataset.py which is becoming too big

These are helper data structures used to do things like auto-increment ids, recycle ids, do renaming, extend sortedcontainers etc…

class kwcoco._helpers._NextId(parent)[source]

Bases: object

Helper class to tracks unused ids for new items

_update_unused(key)[source]

Scans for what the next safe id can be for key

get(key)[source]

Get the next safe item id for key

class kwcoco._helpers._ID_Remapper(reuse=False)[source]

Bases: object

Helper to recycle ids for unions.

For each dataset we create a mapping between each old id and a new id. If possible and reuse=True we allow the new id to match the old id. After each dataset is finished we mark all those ids as used and subsequent new-ids cannot be chosen from that pool.

Parameters:

reuse (bool) – if True we are allowed to reuse ids as long as they haven’t been used before.

Example

>>> video_trackids = [[1, 1, 3, 3, 200, 4], [204, 1, 2, 3, 3, 4, 5, 9]]
>>> self = _ID_Remapper(reuse=True)
>>> for tids in video_trackids:
>>>     new_tids = [self.remap(old_tid) for old_tid in tids]
>>>     self.block_seen()
>>>     print('new_tids = {!r}'.format(new_tids))
new_tids = [1, 1, 3, 3, 200, 4]
new_tids = [204, 205, 2, 206, 206, 207, 5, 9]
>>> #
>>> self = _ID_Remapper(reuse=False)
>>> for tids in video_trackids:
>>>     new_tids = [self.remap(old_tid) for old_tid in tids]
>>>     self.block_seen()
>>>     print('new_tids = {!r}'.format(new_tids))
new_tids = [0, 0, 1, 1, 2, 3]
new_tids = [4, 5, 6, 7, 7, 8, 9, 10]
remap(old_id)[source]

Convert a old-id into a new-id. If self.reuse is True then we will return the same id if it hasn’t been blocked yet.

block_seen()[source]

Mark all seen ids as unable to be used. Any ids sent to remap will now generate new ids.

next_id()[source]

Generate a new id that hasnt been used yet

class kwcoco._helpers.UniqueNameRemapper[source]

Bases: object

helper to ensure names will be unique by appending suffixes

Example

>>> from kwcoco.coco_dataset import *  # NOQA
>>> self = UniqueNameRemapper()
>>> assert self.remap('foo') == 'foo'
>>> assert self.remap('foo') == 'foo_v001'
>>> assert self.remap('foo') == 'foo_v002'
>>> assert self.remap('foo_v001') == 'foo_v003'
>>> assert 'foo' in self
remap(name)[source]
kwcoco._helpers._lut_image_frame_index(imgs, gid)[source]
kwcoco._helpers._lut_frame_index(imgs, gid)
kwcoco._helpers._lut_annot_frame_index(imgs, anns, aid)[source]
class kwcoco._helpers.SortedSet(iterable=None, key=None)[source]

Bases: SortedSet

Initialize sorted set instance.

Optional iterable argument provides an initial iterable of values to initialize the sorted set.

Optional key argument defines a callable that, like the key argument to Python’s sorted function, extracts a comparison key from each value. The default, none, compares values directly.

Runtime complexity: O(n*log(n))

>>> ss = SortedSet([3, 1, 2, 5, 4])
>>> ss
SortedSet([1, 2, 3, 4, 5])
>>> from operator import neg
>>> ss = SortedSet([3, 1, 2, 5, 4], neg)
>>> ss
SortedSet([5, 4, 3, 2, 1], key=<built-in function neg>)
Parameters:
  • iterable – initial values (optional)

  • key – function used to extract comparison key (optional)

_abc_impl = <_abc._abc_data object>
kwcoco._helpers.SortedSetQuiet

alias of SortedSet

kwcoco._helpers._delitems(items, remove_idxs, thresh=750)[source]
Parameters:
  • items (List) – list which will be modified

  • remove_idxs (List[int]) – integers to remove (MUST BE UNIQUE)

kwcoco._helpers._load_and_postprocess(data, loader, postprocess, **loadkw)[source]
kwcoco._helpers._image_corruption_check(fpath, only_shape=False)[source]