hawk_eye.inference.find_targets¶
Contains logic for finding targets in images.
-
hawk_eye.inference.find_targets.
create_batches
(image_tensor: torch.Tensor, coords: List[Tuple[int, int]], batch_size: int)[source]¶ Creates batches of images based on the supplied params. The whole image is tiled first, the batches are generated.
- Parameters
image – The opencv opened image.
tile_size – The height, width of the tiles to create.
overlap – The amount of overlap between tiles.
batch_size – The number of images to have per batch.
- Returns
Yields the image batch and the top left coordinate of the tile in the space of the original image.
-
hawk_eye.inference.find_targets.
find_all_targets
(images: List[pathlib.Path], clf_timestamp: str = '2020-09-05T15.51.57', det_timestamp: str = '2020-10-10T14.02.09', visualization_dir: pathlib.Path = None, save_json_data: bool = False) → None[source]¶ Entrypoint function if running this script as main.
- Parameters
images – A list of all the images to inference.
clf_timestamp – The classification model to load.
det_timestamp – The detection model to load.
visualization_dir – Where to save the visualizations, if any.
-
hawk_eye.inference.find_targets.
find_targets
(image: PIL.Image.Image, clf_model: torch.nn.modules.module.Module, det_model: torch.nn.modules.module.Module, clf_confidence: float = 0.9) → None[source]¶ Tile up image, classify them, then perform object detection where it’s needed.
- Parameters
image – The input image to inference.
clf_model – The loaded classification model.
det_model – The loaded detection model.
-
hawk_eye.inference.find_targets.
globalize_boxes
(results: List[third_party.detectron2.postprocess.BoundingBox], img_size: int) → List[hawk_eye.inference.inference_types.Target][source]¶ Take the normalized detections on a _tile_ and gloabalize them to pixel space of the original large image.
- Parameters
results – A list of the detections for the tiles.
img_size – The size of the tile whihc is needed to unnormalize the detections.
- Returns
A list of the globalized boxes
-
hawk_eye.inference.find_targets.
load_models
(clf_timestamp: str = '2020-09-05T15.51.57', det_timestamp: str = '2020-10-10T14.02.09') → Tuple[torch.nn.modules.module.Module, torch.nn.modules.module.Module][source]¶ Loads the given time stamps for the classification and detector models.
- Parameters
clf_timestamp – Which classification model to load.
det_timestamp – Which detection model to load.
- Returns
Returns both models.
-
hawk_eye.inference.find_targets.
normalize
(img: numpy.ndarray, mean: Tuple[float, float, float] = (0.485, 0.456, 0.406), std: Tuple[float, float, float] = (0.229, 0.224, 0.225), max_pixel_value: float = 255.0) → numpy.ndarray[source]¶ Normalize images based on ImageNet values. This is identical to albumentations normalization used for training. :param img: The image to normalize. :param mean: The mean of each channel. :param std: The standard deviation of each channel. :param max_pixel_value: The max value a pixel can have.
- Returns
A normalized image as a numpy array.
-
hawk_eye.inference.find_targets.
save_target_meta
(filename_meta: pathlib.Path, filename_image: str, targets: List[hawk_eye.inference.inference_types.Target]) → None[source]¶ Save target metadata to a file.
-
hawk_eye.inference.find_targets.
tile_image
(image: PIL.Image.Image, tile_size: Tuple[int, int], overlap: int) → Tuple[torch.Tensor, List[Tuple[int, int]]][source]¶ Take in an image and tile it into smaller tiles for inference.
- Parameters
image – The input image to tile.
tile_size – The (width, height) of the tiles.
overlap – The overlap between adjacent tiles (height, width).
- Returns
A tensor of the tiles and a list of the (x, y) offset for the tiles. The offets are needed to keep track of which tiles have targets.
Examples:
>>> tiles, coords = tile_image(Image.new("RGB", (1000, 1000)), (512, 512), 50) >>> tiles.shape[0] 9 >>> len(coords) 9 >>> tiles.shape[-2:] torch.Size([512, 512])
-
hawk_eye.inference.find_targets.
visualize_image
(image_name: str, image: numpy.ndarray, visualization_dir: pathlib.Path, targets: List[hawk_eye.inference.inference_types.Target], clf_tiles: List[Tuple[int, int]]) → None[source]¶ Function used to draw boxes and information onto image for visualizing the output of inference.
- Parameters
image_name – The original image name used for saving the visualization.
image – The image array.
visualization_dir – Where to save the visualizations.
targets – A list of the targets that were found during inference.
clf_tiles – Which tiles were classified as having targets in them.