def chunk_by_image(boxes):
    """
    turn a flat list of boxes into a hierarchy of:

    image
        category
            [boxes]

    :param boxes: list of box detections
    :return: dictionary of boxes chunked by image/category
    """
    chunks = {}
    for b in boxes:

        if b['image_id'] not in chunks:
            chunks[b['image_id']] = {b['category_id']: [b]}
        elif b['category_id'] not in chunks[b['image_id']]:
            chunks[b['image_id']][b['category_id']] = [b]
        else:
            chunks[b['image_id']][b['category_id']].append(b)

    return chunks