Skip to content

instance

COCOInstanceCache

Bases: InstanceSegmentationCache

Cache handler that stores data in MS-COCO format. This is currently only used for instance segmentation models, but could in principle be extended to support semantic/panoptic segmentation in the future. This method is convenient for instance detection because it allows intermediate results to be easily inspected using standard annotation tools.

Source code in src/tcd_pipeline/cache/instance.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class COCOInstanceCache(InstanceSegmentationCache):
    """
    Cache handler that stores data in MS-COCO format. This is currently only
    used for instance segmentation models, but could in principle be extended
    to support semantic/panoptic segmentation in the future. This method is
    convenient for instance detection because it allows intermediate results
    to be easily inspected using standard annotation tools.
    """

    def save(self, instances: List[ProcessedInstance], bbox: box):
        """
        Save cached results in MS-COCO format.

        Args:
            instances (List[ProcessedInstance]: a list of instances to cache
            bbox: tile bounding box in global image coordinates
        """
        file_name = f"{self.tile_count}{self.cache_suffix}.json"
        output_path = os.path.join(self.cache_folder, file_name)

        metadata = {
            "bbox": bbox.bounds,
            "image": self.image_path,
            "tile_id": self.tile_count,
        }

        dump_instances_coco(
            output_path,
            instances,
            self.image_path,
            categories=self.classes,
            metadata=metadata,
        )

        self.tile_count += 1
        self.write_tile_meta(self.tile_count, bbox, output_path)

    def _load_file(self, cache_file: str) -> dict:
        """Load cached results from MS-COCO format

        Args:
            cache_file (str): Cache filename

        Returns:
            list[ProcessedInstance]: instances

        """

        out = {}
        out["instances"] = []

        with open(cache_file, "r") as fp:
            annotations = json.load(fp)

            out["bbox"] = box(*annotations["metadata"]["bbox"])
            out["image"] = annotations["metadata"]["image"]
            out["tile_id"] = annotations["metadata"]["tile_id"]

            for annotation in annotations["annotations"]:
                out["instances"].append(ProcessedInstance.from_coco_dict(annotation))
        return out

save(instances, bbox)

Save cached results in MS-COCO format.

Parameters:

Name Type Description Default
instances List[ProcessedInstance]

a list of instances to cache

required
bbox box

tile bounding box in global image coordinates

required
Source code in src/tcd_pipeline/cache/instance.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def save(self, instances: List[ProcessedInstance], bbox: box):
    """
    Save cached results in MS-COCO format.

    Args:
        instances (List[ProcessedInstance]: a list of instances to cache
        bbox: tile bounding box in global image coordinates
    """
    file_name = f"{self.tile_count}{self.cache_suffix}.json"
    output_path = os.path.join(self.cache_folder, file_name)

    metadata = {
        "bbox": bbox.bounds,
        "image": self.image_path,
        "tile_id": self.tile_count,
    }

    dump_instances_coco(
        output_path,
        instances,
        self.image_path,
        categories=self.classes,
        metadata=metadata,
    )

    self.tile_count += 1
    self.write_tile_meta(self.tile_count, bbox, output_path)

InstanceSegmentationCache

Bases: ResultsCache

A cache format that can store instance segmentation results. The input to, and output from, the cache is a list of ProcessedInstances.

Source code in src/tcd_pipeline/cache/instance.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class InstanceSegmentationCache(ResultsCache):
    """
    A cache format that can store instance segmentation results. The input
    to, and output from, the cache is a list of ProcessedInstances.
    """

    @property
    def results(self) -> List[dict]:
        """
        A list of results that were stored in the cache folder. Each entry
        in the list is a dictionary containing the following keys:

        - bbox: Bbox
        - image: str
        - instances : List[ProcessedInstance]

        """
        return self._results

results: List[dict] property

A list of results that were stored in the cache folder. Each entry in the list is a dictionary containing the following keys:

  • bbox: Bbox
  • image: str
  • instances : List[ProcessedInstance]

PickleInstanceCache

Bases: InstanceSegmentationCache

Source code in src/tcd_pipeline/cache/instance.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class PickleInstanceCache(InstanceSegmentationCache):
    def save(self, instances: List[ProcessedInstance], bbox: box):
        output = {"instances": instances, "bbox": bbox, "image": self.image_path}

        file_name = f"{self.tile_count}_{self.cache_suffix}.pkl"
        output_path = os.path.join(self.cache_folder, file_name)

        with open(output_path, "wb") as fp:
            pickle.dump(output, fp)

        self.tile_count += 1
        self.write_tile_meta(self.tile_count, bbox, output_path)

    def _load_file(self, cache_file: str) -> dict:
        """Load pickled cache results

        Args:
            cache_file (str): Cache filename

        Returns:
            dict: dictionary containing "instances" and "bbox"

        """

        with open(cache_file, "rb") as fp:
            annotations = pickle.load(fp)

        return annotations