cv-tda 0.0.1__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.
Files changed (49) hide show
  1. cv_tda-0.0.1.dist-info/METADATA +41 -0
  2. cv_tda-0.0.1.dist-info/RECORD +49 -0
  3. cv_tda-0.0.1.dist-info/WHEEL +5 -0
  4. cv_tda-0.0.1.dist-info/top_level.txt +1 -0
  5. cvtda/classification/NNClassifier.py +173 -0
  6. cvtda/classification/__init__.py +6 -0
  7. cvtda/classification/classify.py +209 -0
  8. cvtda/classification/estimate_quality.py +47 -0
  9. cvtda/classification/information_value.py +98 -0
  10. cvtda/dumping/__init__.py +9 -0
  11. cvtda/dumping/base.py +34 -0
  12. cvtda/dumping/devnull.py +18 -0
  13. cvtda/dumping/dump_name_concat.py +4 -0
  14. cvtda/dumping/numpy.py +36 -0
  15. cvtda/face_recognition/BaseLearner.py +67 -0
  16. cvtda/face_recognition/DiagramsLearner.py +33 -0
  17. cvtda/face_recognition/NNLearner.py +193 -0
  18. cvtda/face_recognition/SimpleTopologicalLearner.py +14 -0
  19. cvtda/face_recognition/__init__.py +4 -0
  20. cvtda/face_recognition/learn.py +99 -0
  21. cvtda/logging/__init__.py +8 -0
  22. cvtda/logging/base.py +39 -0
  23. cvtda/logging/cli.py +33 -0
  24. cvtda/logging/devnull.py +27 -0
  25. cvtda/neural_network/Dataset.py +101 -0
  26. cvtda/neural_network/NNBase.py +54 -0
  27. cvtda/neural_network/__init__.py +2 -0
  28. cvtda/topology/DiagramVectorizer.py +180 -0
  29. cvtda/topology/FeatureExtractor.py +89 -0
  30. cvtda/topology/FiltrationsExtractor.py +182 -0
  31. cvtda/topology/GeometryExtractor.py +163 -0
  32. cvtda/topology/GreyscaleExtractor.py +37 -0
  33. cvtda/topology/PointCloudsExtractor.py +37 -0
  34. cvtda/topology/__init__.py +7 -0
  35. cvtda/topology/interface/Extractor.py +123 -0
  36. cvtda/topology/interface/TopologicalExtractor.py +80 -0
  37. cvtda/topology/interface/__init__.py +2 -0
  38. cvtda/topology/utils/__init__.py +3 -0
  39. cvtda/topology/utils/hstack.py +30 -0
  40. cvtda/topology/utils/process_iter.py +7 -0
  41. cvtda/topology/utils/process_iter_dump.py +22 -0
  42. cvtda/utils/__init__.py +7 -0
  43. cvtda/utils/duplicates.py +105 -0
  44. cvtda/utils/image2pointcloud.py +20 -0
  45. cvtda/utils/rgb2gray.py +15 -0
  46. cvtda/utils/rgb2hsv.py +15 -0
  47. cvtda/utils/sequence2features.py +47 -0
  48. cvtda/utils/set_random_seed.py +13 -0
  49. cvtda/utils/spread_points.py +12 -0
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.4
2
+ Name: cv-tda
3
+ Version: 0.0.1
4
+ Summary: A Library to Solve Various Computer Vision Problems Using Topological Data Analysis
5
+ Author-email: Aleksandr Abramov <ggttpo100ajiex@yandex.ru>
6
+ License-Expression: MIT
7
+ Project-URL: Repository, https://github.com/TTPO100AJIEX/CVTDA
8
+ Keywords: topological data analysis,computer vision,algebraic topology,image analysis,classification,face recognition,topological features
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Topic :: Scientific/Engineering
15
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
16
+ Requires-Python: ==3.10
17
+ Description-Content-Type: text/markdown
18
+ Requires-Dist: giotto-tda==0.6.2
19
+ Requires-Dist: gudhi>=3.10.1
20
+ Requires-Dist: joblib>=1.4.2
21
+ Requires-Dist: scikit-learn==1.6.1
22
+ Requires-Dist: scikit-image>=0.25.0
23
+ Requires-Dist: tqdm>=4.67.1
24
+ Provides-Extra: classification
25
+ Requires-Dist: catboost>=1.2.7; extra == "classification"
26
+ Requires-Dist: xgboost>=3.0.0; extra == "classification"
27
+ Requires-Dist: matplotlib>=3.10.1; extra == "classification"
28
+ Requires-Dist: pandas>=2.2.3; extra == "classification"
29
+ Requires-Dist: torch>=2.5.1; extra == "classification"
30
+ Requires-Dist: torchvision>=0.20.1; extra == "classification"
31
+ Requires-Dist: torchph==0.0.0; extra == "classification"
32
+ Provides-Extra: face-recognition
33
+ Requires-Dist: pytorch-metric-learning==2.8.1; extra == "face-recognition"
34
+ Requires-Dist: matplotlib>=3.10.1; extra == "face-recognition"
35
+ Requires-Dist: torch>=2.5.1; extra == "face-recognition"
36
+ Requires-Dist: torchvision>=0.20.1; extra == "face-recognition"
37
+ Requires-Dist: torchph==0.0.0; extra == "face-recognition"
38
+
39
+ # Computer Vision With Topological Data Analysis
40
+
41
+ TO BE DONE
@@ -0,0 +1,49 @@
1
+ cvtda/classification/NNClassifier.py,sha256=g5fVYlReUlvBuXosA4PyiQQVWJJW8PTKf9nQhf3BBtk,6720
2
+ cvtda/classification/__init__.py,sha256=mc2dFRTlig6EqHV-H_kX0-FEhRj-8bw5_pqYpiLw50o,311
3
+ cvtda/classification/classify.py,sha256=DgExRUwCbcnXkoJmDPja8UcaW3zWJ8ORAGkrYgjMM80,7398
4
+ cvtda/classification/estimate_quality.py,sha256=NFEW8XzJq0LGtXJqz28KOHddrdBdch8NT0mocyXmxMA,2014
5
+ cvtda/classification/information_value.py,sha256=hVGQ04Gjg-_k5KisoIyRW3xcs7XYmilt0v1hVTniO24,3155
6
+ cvtda/dumping/__init__.py,sha256=qcl6A653jUs8AD8cW5_88LIgBFzvf_G1AQT42EXGJzM,313
7
+ cvtda/dumping/base.py,sha256=hE7d0s6jw9pEspTNRj7nb14u248cRyPQ2BYyb-oFW-A,901
8
+ cvtda/dumping/devnull.py,sha256=bBoOsGG6ZM_lE7mBfmoHwjr6UbCJCyJSltjhoj2C_N8,474
9
+ cvtda/dumping/dump_name_concat.py,sha256=HE19P7nyHPAwtwSEJYjzR_5z6VpL_BQsbknDnOVp7f8,163
10
+ cvtda/dumping/numpy.py,sha256=Tqhj9tA8N-CxbIZRCmGGhXxYf94fDQSEml_WafaD8gU,1236
11
+ cvtda/face_recognition/BaseLearner.py,sha256=GQCfioy-U4ZGfl3X8sWsLFdazMveVvZbh86Q3F50ANw,2240
12
+ cvtda/face_recognition/DiagramsLearner.py,sha256=M0mM1lqbEmTKs-BRH3tC3qCLXRGNZewNZCkHH0f3MK0,1274
13
+ cvtda/face_recognition/NNLearner.py,sha256=Cz-DD32cCY7vSf44A0llU_exAcwv7lpxCqLW5N33FNA,7415
14
+ cvtda/face_recognition/SimpleTopologicalLearner.py,sha256=0UY7NGk5Ued8Yk4G8nTZ4CQRpdosS6eW9tHy4MSWy30,472
15
+ cvtda/face_recognition/__init__.py,sha256=GQQNlRFAmwWpI0tMgygc4JPmPtk4a34kJEwclfPt-20,170
16
+ cvtda/face_recognition/learn.py,sha256=MyUhH6kEmror_tkHsC25Dn_Y9ueuAaA7yoMWh9GakE8,3736
17
+ cvtda/logging/__init__.py,sha256=ux-wNn70zudCY2cPVQNgwopuHEiVF0f34mPJjwsJobo,255
18
+ cvtda/logging/base.py,sha256=NOgkdQOoodtkE9_KeIR6Rs02lEYTL7MvnPTw6ZS7GH8,876
19
+ cvtda/logging/cli.py,sha256=GJpH4C_6eYyLMgxbIciw7Pwoolee6YWupS-n6NWpn64,730
20
+ cvtda/logging/devnull.py,sha256=ffPc6r72PKsPl6dGWlY7EPXtE7otcibNu493xPC9Qtw,567
21
+ cvtda/neural_network/Dataset.py,sha256=GU3Df3FGvC1pYCRjkBOQOOnY9rpxR-2f0FLtkLAdGOw,3867
22
+ cvtda/neural_network/NNBase.py,sha256=X3g4Wz_jQ9tPlJzgGB0kFYePUZ1rVITZJGvFF-0w_K0,1893
23
+ cvtda/neural_network/__init__.py,sha256=G2TcRIy8rToCb21_QCBxUb8tf7akgPHNv3yKZPzd2ow,58
24
+ cvtda/topology/DiagramVectorizer.py,sha256=0iGOlT3m58UVaSqA461XfU4gI0L5zWWNLimSttUDoCc,7263
25
+ cvtda/topology/FeatureExtractor.py,sha256=wm4t-2r_RD6XHyI-tseZXsaUlhQT5sDVmaFfbSWh3GU,4174
26
+ cvtda/topology/FiltrationsExtractor.py,sha256=mlHcq3j5e1MCejkMfXGGrmtP5_WPEuwnGGZMOyxieBA,8645
27
+ cvtda/topology/GeometryExtractor.py,sha256=DB9I8fvqjm6S_GHKjy7svVSEiht_8AgoAuCXBQYyrhg,7196
28
+ cvtda/topology/GreyscaleExtractor.py,sha256=Jels1MrcMwrESw1RJGwt9hldWs8lEJJfHiGhLSG7HpQ,1137
29
+ cvtda/topology/PointCloudsExtractor.py,sha256=jCZQg6wY2s3apL4glKTv38-Isle3tymxzErf8WVGkJA,1217
30
+ cvtda/topology/__init__.py,sha256=CasgExAIf60ct_xjaGKl0KLgKwfQeQ7VpVI7-4UIu-M,367
31
+ cvtda/topology/interface/Extractor.py,sha256=U1bWMXJWd0IUaXuY7dP3DTnvvmE1k7Lp3AiKgFw6WBI,5563
32
+ cvtda/topology/interface/TopologicalExtractor.py,sha256=Y7SQjIZMrllXMFY99g7T2CePSPS26f-USQyWBOPyiZ0,3134
33
+ cvtda/topology/interface/__init__.py,sha256=DkrHDmH8TfBjJoJPP0nS0aaHd5lB97QEqyY7nVZZOak,90
34
+ cvtda/topology/utils/__init__.py,sha256=TPe68BivyFWikxsW5czh-x6wpLavey-B6GOtQz2-uIM,118
35
+ cvtda/topology/utils/hstack.py,sha256=uS7nVI9hgXSyoLZ9p5-ph2DwqeE7IwIgkI9X51dBK_c,958
36
+ cvtda/topology/utils/process_iter.py,sha256=iefLtoi2COVGugAJpo1HS-BK5291hEQ2HC7i8aiQB0Q,273
37
+ cvtda/topology/utils/process_iter_dump.py,sha256=vqZSHZVCXC2C-TupWuUfcjPkrpgnCUbHpUPou7Wj6pE,589
38
+ cvtda/utils/__init__.py,sha256=pw2Mj7nPP_jElKl8wlKBK5izzWsGOwayDjavJ-RXKQ4,298
39
+ cvtda/utils/duplicates.py,sha256=rQcbgidXYDMt4qT5LomZne6A4-7vhoIqd_cukZwPAWA,4210
40
+ cvtda/utils/image2pointcloud.py,sha256=6knvuPKq-cnvLk3qkuU5wp_uTRYLlRRDSXY7pyFDRRI,641
41
+ cvtda/utils/rgb2gray.py,sha256=d-UtVTpICPgtK-Szzn56dKaqUdzt_gw51zvotQN4YX0,376
42
+ cvtda/utils/rgb2hsv.py,sha256=g6GrjXAyCLXLhUZdjuKBPznJWed0akSxLm67ZNtBvUY,373
43
+ cvtda/utils/sequence2features.py,sha256=OwEbcjecBeYL6qrKJs_UWUwUEPUQnsS5w_dUuYInkhA,2055
44
+ cvtda/utils/set_random_seed.py,sha256=6Mw-Q61tt7oiK66AKjTBEssoufVVbSJ1Nn4Rj_OygE4,304
45
+ cvtda/utils/spread_points.py,sha256=V0h5PX2D2zRmK5ELbMbTGZB_J0YCCT2QI_phdIyv4Dk,460
46
+ cv_tda-0.0.1.dist-info/METADATA,sha256=cdkNEEuIm2GWzYMkNUgavPI8qly73MphNE7ZKcFKD9g,1916
47
+ cv_tda-0.0.1.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
48
+ cv_tda-0.0.1.dist-info/top_level.txt,sha256=ldYBdWRCCGsaK5oUc85sXvwy7FCVzsI3GWSSQMK4N0o,6
49
+ cv_tda-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (78.1.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ cvtda
@@ -0,0 +1,173 @@
1
+ import typing
2
+
3
+ import numpy
4
+ import torch
5
+ import sklearn.base
6
+ import sklearn.metrics
7
+ import torch.utils.data
8
+
9
+ import cvtda.utils
10
+ import cvtda.logging
11
+ import cvtda.neural_network
12
+
13
+
14
+ class NNClassifier(sklearn.base.ClassifierMixin):
15
+ def __init__(
16
+ self,
17
+
18
+ n_jobs: int = -1,
19
+ random_state: int = 42,
20
+
21
+ device: torch.device = torch.device("cuda"),
22
+ batch_size: int = 256,
23
+ learning_rate: float = 1e-3,
24
+ n_epochs: int = 100,
25
+
26
+ skip_diagrams: bool = False,
27
+ skip_images: bool = False,
28
+ skip_features: bool = False
29
+ ):
30
+ self.n_jobs_ = n_jobs
31
+ self.random_state_ = random_state
32
+
33
+ self.device_ = device
34
+ self.batch_size_ = batch_size
35
+ self.learning_rate_ = learning_rate
36
+ self.n_epochs_ = n_epochs
37
+
38
+ self.skip_diagrams_ = skip_diagrams
39
+ self.skip_images_ = skip_images
40
+ self.skip_features_ = skip_features
41
+
42
+
43
+ def fit(self, train: cvtda.neural_network.Dataset, val: typing.Optional[cvtda.neural_network.Dataset]):
44
+ cvtda.utils.set_random_seed(self.random_state_)
45
+ train_dl = torch.utils.data.DataLoader(
46
+ torch.utils.data.TensorDataset(train.images, train.features, train.labels, torch.arange(len(train))),
47
+ batch_size = self.batch_size_,
48
+ shuffle = True
49
+ )
50
+ self.init_(len(torch.unique(train.labels)), *next(iter(train_dl)), train)
51
+
52
+ for epoch in range(self.n_epochs_):
53
+ sum_loss = 0
54
+
55
+ self.model_list_.train()
56
+ for images, features, labels, diagram_idxs in cvtda.logging.logger().pbar(train_dl, desc = f"Epoch {epoch}"):
57
+ self.optimizer_.zero_grad()
58
+ pred = self.forward_(images, features, diagram_idxs, train)
59
+ loss = torch.nn.functional.cross_entropy(pred, labels.to(self.device_), reduction = 'mean')
60
+ loss.backward()
61
+ self.optimizer_.step()
62
+ sum_loss += loss.item()
63
+ postfix = { 'loss': sum_loss, 'lr': self.optimizer_.param_groups[0]['lr'] }
64
+ self.scheduler_.step()
65
+
66
+ if val is not None:
67
+ val_proba = self.predict_proba_(val)
68
+ val_pred = numpy.argmax(val_proba, axis = 1)
69
+ postfix['val_acc'] = sklearn.metrics.accuracy_score(val.labels, val_pred)
70
+
71
+ print(f"Epoch {epoch}:", postfix)
72
+
73
+ self.fitted_ = True
74
+ return self
75
+
76
+ def predict_proba(self, dataset: cvtda.neural_network.Dataset) -> numpy.ndarray:
77
+ assert self.fitted_ is True, 'fit() must be called before predict_proba()'
78
+ cvtda.utils.set_random_seed(self.random_state_)
79
+ return self.predict_proba_(dataset)
80
+
81
+
82
+ def init_(
83
+ self,
84
+ num_classes: int,
85
+ images: torch.Tensor,
86
+ features: torch.Tensor,
87
+ labels: torch.Tensor,
88
+ diagram_idxs: torch.Tensor,
89
+ dataset: cvtda.neural_network.Dataset
90
+ ):
91
+ diagrams = [] if self.skip_diagrams_ else dataset.get_diagrams(diagram_idxs)
92
+
93
+ is_no_topology = (self.skip_diagrams_ and self.skip_features_ and not self.skip_images_)
94
+ print('Topology: ', not is_no_topology)
95
+ print('Images: ', images.shape)
96
+ print('Features: ', features.shape)
97
+
98
+ if is_no_topology:
99
+ images_output = num_classes
100
+ else:
101
+ images_output = 1024
102
+
103
+ self.nn_base_ = cvtda.neural_network.NNBase(
104
+ num_diagrams = len(diagrams) // 2,
105
+ skip_diagrams = self.skip_diagrams_,
106
+ skip_images = self.skip_images_,
107
+ skip_features = self.skip_features_,
108
+ images_n_channels = images.shape[1],
109
+ images_output = images_output
110
+ ).to(self.device_).train()
111
+
112
+ self.model_ = torch.nn.Sequential(
113
+ torch.nn.Dropout(0.4), torch.nn.LazyLinear(256), torch.nn.BatchNorm1d(256), torch.nn.GELU(),
114
+ torch.nn.Dropout(0.3), torch.nn.Linear(256, 128), torch.nn.BatchNorm1d(128), torch.nn.GELU(),
115
+ torch.nn.Dropout(0.2), torch.nn.Linear(128, 64), torch.nn.BatchNorm1d(64), torch.nn.GELU(),
116
+ torch.nn.Dropout(0.1), torch.nn.Linear(64, 32), torch.nn.BatchNorm1d(32), torch.nn.GELU(),
117
+ torch.nn.Linear(32, num_classes)
118
+ ).to(self.device_).train()
119
+
120
+ if is_no_topology:
121
+ self.model_ = torch.nn.Identity()
122
+
123
+ self.model_list_ = torch.nn.ModuleList([ self.nn_base_, self.model_ ])
124
+
125
+ self.optimizer_ = torch.optim.AdamW(
126
+ params = self.model_list_.parameters(),
127
+ lr = self.learning_rate_ * 100
128
+ )
129
+
130
+ def lr_scheduler_lambda(epoch):
131
+ if epoch < self.n_epochs_ // 10:
132
+ return 1
133
+ if epoch < self.n_epochs_ // 4:
134
+ return 0.1
135
+ if epoch < self.n_epochs_ // 2:
136
+ return 0.01
137
+ if epoch < 3 * self.n_epochs_ // 4:
138
+ return 0.001
139
+ return 0.0001
140
+ self.scheduler_ = torch.optim.lr_scheduler.LambdaLR(self.optimizer_, lr_scheduler_lambda)
141
+
142
+ self.forward_(images, features, diagram_idxs, dataset)
143
+ if not is_no_topology:
144
+ cvtda.logging.logger().print(f'Input to LazyLinear: {self.model_[1].in_features}')
145
+ cvtda.logging.logger().print(f'Parameters: {sum(p.numel() for p in self.model_list_.parameters())}')
146
+
147
+ def forward_(
148
+ self,
149
+ images: torch.Tensor,
150
+ features: torch.Tensor,
151
+ diagram_idxs: torch.Tensor,
152
+ dataset: cvtda.neural_network.Dataset
153
+ ) -> torch.Tensor:
154
+ images = images.to(self.device_)
155
+ features = features.to(self.device_)
156
+ if self.skip_diagrams_:
157
+ return self.model_(self.nn_base_(images, features))
158
+ return self.model_(self.nn_base_(images, features, *dataset.get_diagrams(diagram_idxs)))
159
+
160
+ def predict_proba_(self, dataset: cvtda.neural_network.Dataset) -> numpy.ndarray:
161
+ dl = torch.utils.data.DataLoader(
162
+ torch.utils.data.TensorDataset(dataset.images, dataset.features, torch.arange(len(dataset))),
163
+ batch_size = self.batch_size_,
164
+ shuffle = False
165
+ )
166
+
167
+ y_pred_proba = [ ]
168
+ self.model_list_.eval()
169
+ with torch.no_grad():
170
+ for images, features, diagram_idxs in dl:
171
+ y_pred_proba.append(self.forward_(images, features, diagram_idxs, dataset))
172
+ y_pred_proba = torch.vstack(y_pred_proba)
173
+ return torch.nn.functional.softmax(y_pred_proba, dim = 1).cpu().numpy()
@@ -0,0 +1,6 @@
1
+ from .classify import classify
2
+ from .estimate_quality import estimate_quality
3
+ from .information_value import calculate_binary_information_value
4
+ from .information_value import calculate_information_value
5
+ from .information_value import InformationValueFeatureSelector
6
+ from .NNClassifier import NNClassifier
@@ -0,0 +1,209 @@
1
+ import os
2
+ import typing
3
+
4
+ import numpy
5
+ import torch
6
+ import pandas
7
+ import xgboost
8
+ import catboost
9
+ import sklearn.base
10
+ import sklearn.ensemble
11
+ import sklearn.neighbors
12
+ import matplotlib.pyplot as plt
13
+
14
+ import cvtda.dumping
15
+ import cvtda.neural_network
16
+ from .NNClassifier import NNClassifier
17
+ from .estimate_quality import estimate_quality
18
+
19
+ def classify(
20
+ train_images: numpy.ndarray,
21
+ train_features: numpy.ndarray,
22
+ train_labels: numpy.ndarray,
23
+ train_diagrams: typing.Optional[typing.List[numpy.ndarray]],
24
+
25
+ test_images: numpy.ndarray,
26
+ test_features: numpy.ndarray,
27
+ test_labels: numpy.ndarray,
28
+ test_diagrams: typing.Optional[typing.List[numpy.ndarray]],
29
+
30
+ label_names: typing.Optional[typing.List[str]] = None,
31
+ confusion_matrix_include_values: bool = True,
32
+
33
+ n_jobs: int = -1,
34
+ random_state: int = 42,
35
+ dump_name: typing.Optional[str] = None,
36
+ only_get_from_dump: bool = False,
37
+
38
+ knn_neighbours: int = 50,
39
+
40
+ random_forest_estimators: int = 100,
41
+
42
+ nn_device: torch.device = torch.device('cuda'),
43
+ nn_batch_size: int = 256,
44
+ nn_learning_rate: float = 1e-3,
45
+ nn_epochs: int = 100,
46
+
47
+ grad_boost_max_iter: int = 20,
48
+ grad_boost_max_depth: int = 4,
49
+ grad_boost_max_features: float = 0.1,
50
+
51
+ xgboost_n_classifiers: int = 25,
52
+ xgboost_max_depth: int = 4,
53
+ xgboost_device: str = 'gpu',
54
+
55
+ catboost_iterations: int = 600,
56
+ catboost_depth: int = 4,
57
+ catboost_device: str = 'GPU',
58
+
59
+ without_diagrams: bool = False
60
+ ):
61
+ if (train_images is not None) and (not only_get_from_dump):
62
+ nn_train = cvtda.neural_network.Dataset(
63
+ train_images, train_diagrams, train_features, train_labels, n_jobs = n_jobs, device = nn_device
64
+ )
65
+ nn_test = cvtda.neural_network.Dataset(
66
+ test_images, test_diagrams, test_features, test_labels, n_jobs = n_jobs, device = nn_device
67
+ )
68
+
69
+ def classify_one(classifier: sklearn.base.ClassifierMixin, name: str, display_name: str, ax: plt.Axes):
70
+ if without_diagrams and name == 'NNClassifier_diagrams':
71
+ print(f'Skipping {name} - {classifier}')
72
+ return {}
73
+
74
+ print(f'Trying {name} - {classifier}')
75
+
76
+ dumper = cvtda.dumping.dumper()
77
+ model_dump_name = cvtda.dumping.dump_name_concat(dump_name, name)
78
+ if only_get_from_dump or dumper.has_dump(model_dump_name):
79
+ y_pred_proba = dumper.get_dump(model_dump_name)
80
+ else:
81
+ if type(classifier) == NNClassifier:
82
+ classifier.fit(nn_train, nn_test)
83
+ y_pred_proba = classifier.predict_proba(nn_test)
84
+ else:
85
+ classifier.fit(train_features, train_labels)
86
+ y_pred_proba = classifier.predict_proba(test_features)
87
+
88
+ if model_dump_name is not None:
89
+ dumper.save_dump(y_pred_proba, model_dump_name)
90
+
91
+ ax.set_title(display_name)
92
+ result = {
93
+ 'classifier': display_name,
94
+ **estimate_quality(
95
+ y_pred_proba, test_labels, ax,
96
+ label_names = label_names, confusion_matrix_include_values = confusion_matrix_include_values
97
+ )
98
+ }
99
+ print(result)
100
+ return result
101
+
102
+ classifiers = [
103
+ sklearn.neighbors.KNeighborsClassifier(
104
+ n_jobs = n_jobs,
105
+ n_neighbors = knn_neighbours
106
+ ),
107
+ sklearn.ensemble.RandomForestClassifier(
108
+ n_estimators = random_forest_estimators,
109
+ random_state = random_state,
110
+ n_jobs = n_jobs
111
+ ),
112
+ sklearn.ensemble.HistGradientBoostingClassifier(
113
+ random_state = random_state,
114
+ max_iter = grad_boost_max_iter,
115
+ max_depth = grad_boost_max_depth,
116
+ max_features = grad_boost_max_features,
117
+ verbose = 2
118
+ ),
119
+ catboost.CatBoostClassifier(
120
+ iterations = catboost_iterations,
121
+ depth = catboost_depth,
122
+ random_seed = random_state,
123
+ loss_function = 'MultiClass',
124
+ devices = '0-3',
125
+ task_type = catboost_device,
126
+ verbose = True
127
+ ),
128
+ xgboost.XGBClassifier(
129
+ n_jobs = n_jobs,
130
+ n_estimators = xgboost_n_classifiers,
131
+ max_depth = xgboost_max_depth,
132
+ device = xgboost_device
133
+ ),
134
+ NNClassifier(
135
+ random_state = random_state,
136
+ device = nn_device,
137
+ batch_size = nn_batch_size,
138
+ learning_rate = nn_learning_rate,
139
+ n_epochs = nn_epochs * 2,
140
+ skip_diagrams = True,
141
+ skip_images = True,
142
+ skip_features = False,
143
+ ),
144
+ NNClassifier(
145
+ random_state = random_state,
146
+ device = nn_device,
147
+ batch_size = nn_batch_size,
148
+ learning_rate = nn_learning_rate,
149
+ n_epochs = nn_epochs // 5,
150
+ skip_diagrams = False,
151
+ skip_images = True,
152
+ skip_features = True,
153
+ ),
154
+ NNClassifier(
155
+ random_state = random_state,
156
+ device = nn_device,
157
+ batch_size = nn_batch_size,
158
+ learning_rate = nn_learning_rate,
159
+ n_epochs = nn_epochs,
160
+ skip_diagrams = True,
161
+ skip_images = False,
162
+ skip_features = True,
163
+ ),
164
+ NNClassifier(
165
+ random_state = random_state,
166
+ device = nn_device,
167
+ batch_size = nn_batch_size,
168
+ learning_rate = nn_learning_rate,
169
+ n_epochs = nn_epochs,
170
+ skip_diagrams = True,
171
+ skip_images = False,
172
+ skip_features = False,
173
+ )
174
+ ]
175
+ names = [
176
+ 'KNeighborsClassifier',
177
+ 'RandomForestClassifier',
178
+ 'HistGradientBoostingClassifier',
179
+ 'CatBoostClassifier',
180
+ 'XGBClassifier',
181
+ 'NNClassifier_features',
182
+ 'NNClassifier_diagrams',
183
+ 'NNClassifier_images',
184
+ 'NNClassifier_features_images'
185
+ ]
186
+ display_names = [
187
+ 'Метод k ближайших соседей',
188
+ 'Случайный лес',
189
+ 'Град. бустинг на основе гистограмм',
190
+ 'CatBoost',
191
+ 'XGBoost',
192
+ 'Нейронная сеть для тополог. признаков',
193
+ 'Обучаемая векторизация диаграмм',
194
+ 'ResNet50 – базовая модель',
195
+ 'Комбинированная нейронная сеть'
196
+ ]
197
+
198
+ figure, axes = plt.subplots(3, 3, figsize = (15, 15))
199
+ df = pandas.DataFrame([ classify_one(*args) for args in zip(classifiers, names, display_names, axes.flat) ])
200
+ figure.tight_layout()
201
+
202
+ dumper = cvtda.dumping.dumper()
203
+ if (dump_name is not None) and isinstance(dumper, cvtda.dumping.NumpyDumper):
204
+ file = dumper.get_file_name_(cvtda.dumping.dump_name_concat(dump_name, "confusion_matrixes"))
205
+ os.makedirs(os.path.dirname(file), exist_ok = True)
206
+ figure.savefig(file[:-4] + ".svg")
207
+ figure.savefig(file[:-4] + ".png")
208
+ df.to_csv(dumper.get_file_name_(cvtda.dumping.dump_name_concat(dump_name, "quality_metrics.csv"))[:-4])
209
+ return df
@@ -0,0 +1,47 @@
1
+ import typing
2
+
3
+ import numpy
4
+ import sklearn.metrics
5
+ import matplotlib.pyplot as plt
6
+
7
+ def estimate_quality(
8
+ y_pred_proba: numpy.ndarray,
9
+ y_true: numpy.ndarray,
10
+ ax: typing.Optional[plt.Axes] = None,
11
+ label_names: typing.Optional[typing.List[str]] = None,
12
+ confusion_matrix_include_values: bool = True
13
+ ) -> dict:
14
+ if label_names is None:
15
+ label_names = list(range(y_pred_proba.shape[1]))
16
+
17
+ y_pred = numpy.argmax(y_pred_proba, axis = 1)
18
+ if ax:
19
+ sklearn.metrics.ConfusionMatrixDisplay.from_predictions(
20
+ y_true, y_pred, ax = ax, colorbar = False,
21
+ display_labels = label_names, include_values = confusion_matrix_include_values
22
+ )
23
+ ax.set_xticks(ax.get_xticks(), labels = ax.get_xticklabels(), rotation = 45, ha = "right", rotation_mode = "anchor")
24
+ ax.set_xlabel(None)
25
+ ax.set_ylabel(None)
26
+
27
+ numeric_labels = list(range(y_pred_proba.shape[1]))
28
+ auc_roc_input = y_pred_proba
29
+ multi_class = 'ovo'
30
+ average = 'macro'
31
+
32
+ if y_pred_proba.shape[1] == 2:
33
+ auc_roc_input = y_pred_proba[:, 1]
34
+ multi_class = 'raise'
35
+ average = 'binary'
36
+
37
+ metrics = {
38
+ 'Accuracy': sklearn.metrics.accuracy_score (y_true, y_pred),
39
+ 'AUC-ROC': sklearn.metrics.roc_auc_score (y_true, auc_roc_input, multi_class = multi_class, labels = numeric_labels),
40
+ 'Precision': sklearn.metrics.precision_score (y_true, y_pred, average = average, zero_division = 0),
41
+ 'Recall': sklearn.metrics.recall_score (y_true, y_pred, average = average),
42
+ 'F1-score': sklearn.metrics.f1_score (y_true, y_pred, average = average)
43
+ }
44
+ for k in [ 2, 3, 5, 7, 9, 15, 30 ]:
45
+ if y_pred_proba.shape[1] > k:
46
+ metrics[f'TOP-{k} Accuracy'] = sklearn.metrics.top_k_accuracy_score(y_true, y_pred_proba, k = k, labels = numeric_labels)
47
+ return metrics
@@ -0,0 +1,98 @@
1
+ import numpy
2
+ import pandas
3
+ import joblib
4
+ import sklearn.base
5
+ import matplotlib.pyplot as plt
6
+
7
+ import cvtda.logging
8
+
9
+
10
+ def calculate_binary_information_value(feature: numpy.ndarray, target: numpy.ndarray, bins: int) -> float:
11
+ quantiles = numpy.linspace(0, 1, bins + 1)
12
+ bins = numpy.unique(numpy.quantile(feature, quantiles))
13
+ bins[0] -= 0.0001
14
+ bins[-1] += 0.0001
15
+
16
+ feature = numpy.digitize(feature, bins, right = True)
17
+ n, events = [ ], [ ]
18
+ for bin in range(1, len(bins)):
19
+ mask = (feature == bin)
20
+ n.append(mask.sum())
21
+ events.append((target * mask).sum())
22
+ n = numpy.array(n)
23
+ events = numpy.array(events)
24
+
25
+ non_events = n - events
26
+ events_prc = numpy.maximum(events, 0.5) / events.sum()
27
+ non_events_prc = numpy.maximum(non_events, 0.5) / non_events.sum()
28
+
29
+ woe = numpy.log(events_prc / non_events_prc)
30
+ iv = woe * (events_prc - non_events_prc)
31
+ return iv.sum()
32
+
33
+
34
+ def calculate_information_value_one_feature(
35
+ feature: numpy.ndarray,
36
+ y_true: numpy.ndarray,
37
+ bins: int
38
+ ) -> dict:
39
+ IVs = []
40
+ for class_idx in range(numpy.max(y_true)):
41
+ target = (y_true == class_idx).astype(int)
42
+ IVs.append(calculate_binary_information_value(feature, target, bins))
43
+ return numpy.mean(IVs)
44
+
45
+
46
+ def calculate_information_value(
47
+ features: numpy.ndarray,
48
+ y_true: numpy.ndarray,
49
+ bins: int = 10,
50
+ n_jobs: int = -1
51
+ ) -> pandas.DataFrame:
52
+ iv_generator = joblib.Parallel(return_as = 'generator', n_jobs = n_jobs)(
53
+ joblib.delayed(calculate_information_value_one_feature)(
54
+ features[:, idx],
55
+ y_true,
56
+ bins
57
+ ) for idx in range(features.shape[1])
58
+ )
59
+ pbar = cvtda.logging.logger().pbar(iv_generator, total = features.shape[1], desc = 'information values')
60
+ return numpy.array(list(pbar))
61
+
62
+
63
+ class InformationValueFeatureSelector(sklearn.base.TransformerMixin):
64
+ def __init__(
65
+ self,
66
+ n_jobs: int = -1,
67
+ verbose: bool = True,
68
+
69
+ bins: int = 10,
70
+ threshold: float = 0.5
71
+ ):
72
+ self.fitted_ = False
73
+ self.n_jobs_ = n_jobs
74
+
75
+ self.bins_ = bins
76
+ self.threshold_ = threshold
77
+
78
+ def fit(self, features: numpy.ndarray, target: numpy.ndarray):
79
+ cvtda.logging.logger().print('Fitting the information value feature selector')
80
+ self.IV_ = calculate_information_value(
81
+ features,
82
+ target,
83
+ bins = self.bins_,
84
+ n_jobs = self.n_jobs_
85
+ )
86
+ self.good_features_idx_ = numpy.where(self.IV_ > self.threshold_)[0]
87
+
88
+ cvtda.logging.logger().print('Fitting complete')
89
+ self.fitted_ = True
90
+ return self
91
+
92
+ def transform(self, features: numpy.ndarray) -> numpy.ndarray:
93
+ assert self.fitted_ is True, 'fit() must be called before transform()'
94
+ return features[:, self.good_features_idx_]
95
+
96
+ def hist(self, bins: int = 50):
97
+ assert self.fitted_ is True, 'fit() must be called before hist()'
98
+ return plt.hist(self.IV_, bins = bins)
@@ -0,0 +1,9 @@
1
+ from .base import BaseDumper
2
+ from .numpy import NumpyDumper
3
+ from .devnull import DevNullDumper
4
+ from .dump_name_concat import dump_name_concat
5
+
6
+ def dumper() -> BaseDumper:
7
+ if BaseDumper.current_dumper is None:
8
+ BaseDumper.current_dumper = NumpyDumper("./")
9
+ return BaseDumper.current_dumper
cvtda/dumping/base.py ADDED
@@ -0,0 +1,34 @@
1
+ import abc
2
+ import typing
3
+
4
+ T = typing.TypeVar("T")
5
+
6
+ class BaseDumper(abc.ABC, typing.Generic[T]):
7
+ current_dumper = None
8
+
9
+ def __enter__(self):
10
+ self.__previous = BaseDumper.current_dumper
11
+ BaseDumper.current_dumper = self
12
+
13
+ def __exit__(self, *args):
14
+ BaseDumper.current_dumper = self.__previous
15
+
16
+ @abc.abstractmethod
17
+ def execute(self, function: typing.Callable[[typing.Any], T], name: str, *function_args) -> T:
18
+ pass
19
+
20
+ @abc.abstractmethod
21
+ def save_dump(self, data: T, name: str):
22
+ pass
23
+
24
+ @abc.abstractmethod
25
+ def has_dump(self, name: str) -> bool:
26
+ pass
27
+
28
+ def get_dump(self, name: str) -> T:
29
+ assert self.has_dump(name), f"There is no dump at {name}"
30
+ return self.get_dump_impl_(name)
31
+
32
+ @abc.abstractmethod
33
+ def get_dump_impl_(self, name: str) -> T:
34
+ pass