-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModelClassificationSIFT.py
132 lines (100 loc) · 3.74 KB
/
ModelClassificationSIFT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 12 17:23:05 2022
@author: Simon Bilik
This class is used for the classification of the evaluated model
"""
import logging
import traceback
import cv2 as cv
import numpy as np
from sklearn.decomposition import PCA
from ModelClassificationBase import ModelClassificationBase
class ModelClassificationSIFT(ModelClassificationBase):
## Constructor
def __init__(
self,
modelDataPath,
experimentPath,
modelSel,
layerSel,
labelInfo,
imageDim,
modelData,
anomaly_algorithm_selection = ["Robust covariance", "One-Class SVM", "Isolation Forest", "Local Outlier Factor"],
visualize = True
):
# Call the parent
ModelClassificationBase.__init__(
self,
modelDataPath,
experimentPath,
modelSel,
layerSel,
labelInfo,
imageDim,
modelData,
'SIFT',
anomaly_algorithm_selection,
visualize
)
# Get data, metrics and classify the data
try:
if self.modelData:
self.procDataFromDict()
else:
self.procDataFromFile()
self.dataClassify()
except:
logging.error('An error occured during classification using ' + self.featExtName + ' feature extraction method...')
traceback.print_exc()
pass
## Preprocess the images
def imgPreprocess(self, img):
# Map the image to UINT8
if(img.shape[2] == 3):
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
else:
gray = np.squeeze(img)
imgP = cv.normalize(gray, None, 0, 255, cv.NORM_MINMAX, cv.CV_8U)
return imgP
## Compute the classification metrics
def computeMetrics(self, processedData):
# Get the data
imgData = np.subtract(processedData.get('Org'), processedData.get('Dec'))
#imgData = processedData.get('Dec')
labels = processedData.get('Lab')
# Initialize the metric array
metricArray = []
nFeatures = 5
# Create SIFT detector object
sift = cv.SIFT_create(nfeatures = nFeatures)
# Loop through the images in imgData
for i in range(np.size(labels)):
# Initialize the metric arrays and safety counter
valSizeSIFT = []
valRespSIFT = []
counter = 0
# Get an image, preprocess it, convert it to UINT8 and to grayscale
imgP = self.imgPreprocess(imgData[i][:][:])
# Get the SIFT features
kpSIFT, _ = sift.detectAndCompute(imgP, None)
for i in range(5):
try:
# Append the features to the array
valSizeSIFT.append(kpSIFT[i].size)
valRespSIFT.append(kpSIFT[i].response)
except:
# Not enough feature points, fill the missing values with zeros
#logging.warning('Missing ' + f'{float(nFeatures - counter):}' + ' feature points, appending zeros to fix the lenght.')
valSizeSIFT.append(0)
valRespSIFT.append(0)
# Convert the lists into the np arrays
metricArray.append(valSizeSIFT + valRespSIFT)
# Get metrics np array
metrics = self.normalize2DData(np.array(metricArray))
# Reduce the dimensionality of metrics
if metrics.shape[1] > 50:
pca_red = PCA(n_components = 50)
metrics = pca_red.fit_transform(metrics)
return metrics, labels