Prediction Interface#
- class glasses_detector.components.pred_interface.PredInterface[source]#
Bases:
ABC
Interface for handling image-based predictions.
This interface provides a common API for handling predictions (e.g. saving, loading, etc.) for all models. It also provides a common API for making predictions on images, directories, and files.
Any class that inherits from this interface must implement the
predict()
method. This method should take a single image path or a list of image paths and return a single prediction or a list of predictions, respectively.- static save(pred: Default | dict[str, Default], filepath: FilePath)[source]#
Saves a prediction to a file.
This method saves a prediction or a dictionary of named predictions to the provided file. The type of the file will be inferred automatically from the extension and will be saved accordingly.
# Extension
Format
.txt
For a single prediction, it is flattened and saved as a single line separated by spaces. For a dictionary of predictions, each row contains the name of the prediction followed by the flattened prediction values separated by spaces (no header!).
.csv
For a single prediction, it is flattened and saved as a single line separated by commas. For a dictionary of predictions, each row contains the name of the prediction followed by the flattened prediction values separated by commas (no header!).
.json
For a single prediction, it is saved as a single JSON object. For a dictionary of predictions, each prediction is saved as a separate JSON object with the name of the prediction as the key.
.yml
,.yaml
For a single prediction, it is saved as a single YAML object. For a dictionary of predictions, each prediction is saved as a separate YAML object with the name of the prediction as the key.
.pkl
For a single prediction, it is saved as a single pickle object. For a dictionary of predictions, each prediction is saved as a separate pickle object with the name of the prediction as the key.
.npy
,.npz
For a single prediction, it is saved as a single numpy array or scalar. For a dictionary of predictions, it is flattened to a 2D matrix where each row contains the name of the prediction followed by the flattened prediction values. For
.npy
,numpy.save()
is used and for.npz
,numpy.savez_compressed()
is used..dat
For a single prediction, it is saved as a single numpy array or scalar using
numpy.ndarray.tofile()
. For a dictionary of predictions, they are first flattened to a 2D matrix before saving..jpg
,.jpeg
,.png
,.bmp
,.pgm
,.webp
For a single prediction, it is saved as an image. For a dictionary of predictions, each prediction is saved as a separate image with the name of the prediction as the file name. In the case of multiple predictions, all images are saved under directory called
filepath
, just without an extension.
- abstract predict(image: FilePath, **kwargs) Default [source]#
- abstract predict(image: Collection[FilePath], **kwargs) list[Default]
Generates a prediction for the given image(-s).
Takes a path to an image or a list of paths to images and returns a prediction or a list of predictions, respectively.
- Parameters:
image (FilePath | Collection[FilePath]) – The path to an image or a list of paths to images to generate predictions for.
**kwargs – Additional keyword arguments to pass to the prediction method.
- Returns:
The prediction or a list of predictions for the given image(-s).
- Return type:
- process_file(input_path: FilePath, output_path: FilePath | None = None, ext: str | None = None, show: bool = False, **pred_kwargs) Default | None [source]#
- process_file(input_path: Collection[FilePath], output_path: Collection[FilePath] | None = None, ext: str | None = None, show: bool = False, **pred_kwargs) list[Default | None]
Processes a single image or a list of images.
Takes a path to the image or a list of paths to images, generates the prediction(-s), and returns them, based on how
predict()
behaves. If the output path is specified, the prediction(-s) will be saved to the given path(-s) based on the extension of the output path. The following cases are considered:If
output_path
isNone
, no predictions are saved. If there are multiple output paths (one for each input path) and some of the entries areNone
, then only the outputs for the corresponding predictions are not be saved.If the output path is a single file, then the predictions are saved to that file. If there are multiple input paths, then the corresponding predictions are aggregated to a single file.
If
output_path
is a directory, then the prediction(-s) are saved to that directory. For each input path, a corresponding file is created in the specified output directory with the same name as the input. The extension, if not provided asext
, is set to.jpg
for images and.txt
for other predictions.If
output_path
is a list of output paths, then the predictions are saved to the corresponding output paths. If the number of input paths and output paths do not match, then the number of predictions are be truncated or expanded withNone
to match the number of input paths and a warning is raised. all the output paths are interpreted as files.
For more details on how each file type is saved, regardless if it is a single prediction or the aggregated predictions, see
save()
.NB: aggregation of multiple images to a single file is different from that of
process_dir()
- here, the full paths are used as sample identifiers, unlike just the names of the images.Tip
If multiple images are provided (as a list of input paths), they are likely to be loaded into a single batch for a faster prediction (see
predict()
for more details), thus more memory is required than if they were processed individually. For this reason, consider not to pass too many images at once (e.g., <200).Note
If some input path does not lead to a valid image file, e.g., does not exist, its prediction is set to
None
. Also, if at least one prediction fails, then all predictions are set toNone
. In both cases, a warning is is raised and the files or the lines in the aggregated file are skipped (not saved).- Parameters:
input_path (FilePath | Collection[FilePath]) – The path to an image or a list of paths to images to generate predictions for.
output_path (FilePath | Collection[FilePath] | None, optional) – The path to save the prediction(-s) to. If
None
, no predictions are saved. If a single file, the predictions are aggregated (if multiple) and saved to that file. If a directory, the predictions are saved to that directory with the names copied from inputs. Defaults toNone
.ext (str | None, optional) – The extension to use for the output file(-s). Only used when
output_path
is a directory. IfNone
, the extension is set to".jpg"
for images and".txt"
for other predictions (depends on what is returned bypredict()
returns) For available options, refer tosave()
. Defaults toNone
.show (bool, optional) – Whether to show the predictions. Images will be shown using
PIL.Image.Image.show()
and other predictions will be printed to stdout. Defaults toFalse
.**pred_kwargs – Additional keyword arguments to pass to
predict()
.
- Returns:
The prediction or a list of predictions for the given image(-s). Any failed predictions will be set to
None
.- Return type:
- process_dir(input_path: FilePath, output_path: FilePath | None = None, ext: str | None = None, batch_size: int = 1, show: bool = False, pbar: bool | str | tqdm = True, update_total: bool = True, **pred_kwargs) dict[str, Default | None] | None [source]#
Processes a directory of images.
Takes a path to a directory of images, optionally sub-groups to batches, generates the predictions for every image and returns them if
output_path
isNone
or saves them to a specified file or as files to a specified directory. The following cases are considered:If
output_path
isNone
, the predictions are returned as a dictionary of predictions where the keys are the names of the images and the values are the corresponding predictions.If
output_path
is a single file, the predictions are aggregated to a single file.If
output_path
is a directory, the predictions are saved to that directory. For each input path, a corresponding file is created in the specified output directory with the same name as the input. The extension, if not provided asext
, is set automatically as explained inprocess_file()
.
For more details on how each file type is saved, regardless if it is a single prediction or the aggregated predictions, see
save()
.NB: aggregation of images to a single file/dictionary is different from that of
process_file()
(when multiple file paths are passed) - here, only the names of the images are used as keys, unlike the full paths.Tip
For very large directories, consider specifying
output_path
as a directory because aggregating the predictions to a single file or waiting for them to be returned might consume too much memory and lead to errors.Note
Any files in the input directory that are not valid images or those for which the prediction fails for any reason are are simply skipped and a warning is raised - for more details, see
process_file()
.- Parameters:
input_path (FilePath) – The path to a directory of images to generate predictions for.
output_path (FilePath | None, optional) – The path to save the prediction(-s) to. If
None
, the predictions are returned as a dictionary, if a single file, the predictions are aggregated to a single file, and if a directory, the predictions are saved to that directory with the names copied from inputs. Defaults toNone
.ext (str | None, optional) – The extension to use for the output file(-s). Only used when
output_path
is a directory. The extension should include a leading dot, e.g.,".txt"
,".npy"
,".jpg"
etc (seesave()
). IfNone
, the behavior followsprocess_file()
. Defaults toNone
.batch_size (int, optional) – The batch size to use when processing the images. This groups the files in the specified directory to batches of size
batch_size
before processing them. In some cases, larger batch sizes can speed up the processing at the cost of more memory usage. Defaults to1
.show (bool, optional) – Whether to show the predictions. Images will be shown using
PIL.Image.Image.show()
and other predictions will be printed to stdout. It is not recommended to set this toTrue
as it might spam your stdout. Defaults toFalse
.pbar (bool | str | tqdm, optional) – Whether to show a progress bar. If
True
, a progress bar with no description is shown. Ifstr
, a progress bar with the given description is shown. If an instance oftqdm
, it is used as is. Defaults toTrue
.update_total (bool, optional) – Whether to update the total number of files in the progress bar. This is only relevant if
pbar
is an instance oftqdm
. For example, if the number of total files is already known and captured bytqdm.tqdm.total
, then there is no need to update it. Defaults toTrue
.**pred_kwargs – Additional keyword arguments to pass to
predict()
.
- Returns:
The dictionary of predictions if
output_path
isNone
orNone
ifoutput_path
is specified.- Return type: