pyvisim 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyvisim/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """
2
+ PyVisim: A Python library for image similarity analysis using Image Encoders and Neural Networks.
3
+ """
4
+
5
+ __all__ = ['datasets', 'encoders', 'features', 'eval']
@@ -0,0 +1,51 @@
1
+ import abc
2
+ import logging
3
+
4
+ import numpy as np
5
+
6
+ class SimilarityMetric(abc.ABC):
7
+ """
8
+ Abstract base for all similarity encoders.
9
+
10
+ All concrete similarity metric classes must inherit from this class.
11
+ """
12
+ _logger = logging.getLogger('Similarity_Metrics')
13
+ @abc.abstractmethod
14
+ def similarity_score(self, image1: np.ndarray, image2: np.ndarray) -> float:
15
+ """
16
+ Compute a similarity score between two images.
17
+
18
+ :param image1: First image
19
+ :param image2: Second image
20
+ :return: A similarity score
21
+ """
22
+ pass
23
+
24
+ class FeatureExtractorBase(abc.ABC):
25
+ """
26
+ Abstract interface for extracting features from images.
27
+
28
+ A feature extractor transforms an image (NumPy array) into a
29
+ set of feature vectors (NumPy array).
30
+ """
31
+ _logger = logging.getLogger("Feature_Extractor")
32
+ def __init__(self):
33
+ pass
34
+
35
+ @abc.abstractmethod
36
+ def __call__(self, image: np.ndarray) -> np.ndarray:
37
+ """
38
+ Extracts features from an image.
39
+
40
+ :param image: Input image (NumPy array).
41
+ :return: Feature descriptors (NumPy array).
42
+ """
43
+ raise NotImplementedError
44
+
45
+ @property
46
+ @abc.abstractmethod
47
+ def output_dim(self) -> int:
48
+ """
49
+ The dimensionality (D) of each feature vector, i.e., shape[1] of the output.
50
+ """
51
+ raise NotImplementedError
pyvisim/_config.py ADDED
@@ -0,0 +1,56 @@
1
+ import logging
2
+ import logging.config
3
+ import pathlib
4
+
5
+ import yaml
6
+ import torch
7
+
8
+ # -Config for the dataset- #
9
+ ROOT = pathlib.Path(__file__).parent.parent
10
+ LOG_FILE_PATH = ROOT / "res/logs/log_msgs.log"
11
+
12
+ # - Device - #
13
+ ENFORCE_CUDA = True
14
+
15
+ def get_device():
16
+ """Get device (if available)"""
17
+ global ENFORCE_CUDA
18
+ if ENFORCE_CUDA:
19
+ if not torch.cuda.is_available():
20
+ raise RuntimeError("CUDA is not available. Please check your computer's configuration.")
21
+ return torch.device("cuda")
22
+ return torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
+
24
+ DEVICE = get_device()
25
+ print(f"Device used: {DEVICE}")
26
+
27
+ # -Paths for the Excavator dataset- #
28
+ TRAIN_IMG_DATA_PATH_EXCAVATOR= rf"{ROOT}/excavator_dataset_w_masks/train"
29
+ TRAIN_MASK_DATA_PATH_EXCAVATOR = rf"{ROOT}/excavator_dataset_w_masks/train_annot"
30
+ TEST_IMG_DATA_PATH_EXCAVATOR = rf"{ROOT}/excavator_dataset_w_masks/test"
31
+ TEST_MASK_DATA_PATH_EXCAVATOR = rf"{ROOT}/excavator_dataset_w_masks/test_annot"
32
+ VALID_IMG_DATA_PATH_EXCAVATOR = None
33
+ VALID_MASK_DATA_PATH_EXCAVATOR = None
34
+
35
+ # -Paths for the Flower dataset- #
36
+ IMG_DATA_PATH_FLOWER = rf"{ROOT}/oxford_flower_dataset/images"
37
+ LABELS_PATH_FLOWER = rf"{ROOT}/oxford_flower_dataset/imagelabels.mat"
38
+ SETID_PATH_FLOWER = rf"{ROOT}/oxford_flower_dataset/setid.mat"
39
+
40
+
41
+ # - Logging - #
42
+ def setup_logging(default_path=rf"{ROOT}/res/logging_config.yaml", default_level=logging.INFO):
43
+ """Setup logging configuration"""
44
+ try:
45
+ with open(default_path, 'rt') as f:
46
+ config = yaml.safe_load(f.read())
47
+ try:
48
+ config["handlers"]["file_handler"]["filename"] = str(LOG_FILE_PATH)
49
+ except Exception as e:
50
+ print(f"Error in Logging Configuration: {e}. Cannot set output path for log file.")
51
+ logging.config.dictConfig(config)
52
+ except Exception as e:
53
+ print(f"Error in Logging Configuration: {e}")
54
+ logging.basicConfig(level=default_level, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
55
+
56
+
pyvisim/_errors.py ADDED
@@ -0,0 +1,10 @@
1
+ """
2
+ Includes exceptions for the package.
3
+ """
4
+
5
+ class InvalidImageError(Exception):
6
+ """
7
+ Raised when an image is not provided.
8
+ """
9
+ def __init__(self, message: str = "Input is not a valid image."):
10
+ super().__init__(message)