-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModelClassificationHardNet4.py
118 lines (89 loc) · 3.46 KB
/
ModelClassificationHardNet4.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
# -*- 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 HardNet import HardNet
from scipy import spatial
from skimage.util import view_as_blocks
from ModelClassificationBase import ModelClassificationBase
class ModelClassificationHardNet4(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,
'HardNet4',
anomaly_algorithm_selection,
visualize
)
# Create HardNet model object
self.hardNet = HardNet()
# Get data, metrics and classify the data
try:
if self.modelData:
self.procDataFromDict()
else:
self.procDataFromFile()
#self.procDataFromFile()
self.dataClassify()
except:
logging.error('An error occured during classification using ' + self.featExtName + ' feature extraction method...')
traceback.print_exc()
pass
## Compute the classification metrics
def computeMetrics(self, processedData, eps = 1e-6):
# Get the data
orgData = processedData.get('Org')
decData = processedData.get('Dec')
labels = processedData.get('Lab')
# Initialize the conversion and metric lists
metrics = []
# Get HardNet features for each image
for imgOrg, imgDec in zip(orgData, decData):
# Convert images to gray
if(imgOrg.shape[2] == 3):
imgOrg = cv.cvtColor(imgOrg, cv.COLOR_BGR2GRAY)
imgDec = cv.cvtColor(imgDec, cv.COLOR_BGR2GRAY)
else:
imgOrg = np.squeeze(imgOrg)
imgDec = np.squeeze(imgDec)
# Split the image into 32x32 images
batchOrg = view_as_blocks(imgOrg, (32, 32))
batchOrg = batchOrg.reshape(batchOrg.shape[0]*batchOrg.shape[1], 32, 32, 1)
batchDec = view_as_blocks(imgDec, (32, 32))
batchDec = batchDec.reshape(batchDec.shape[0]*batchDec.shape[1], 32, 32, 1)
# Get HardNet features
desOrg = self.hardNet.forward(batchOrg)
desDec = self.hardNet.forward(batchDec)
dist = []
# Match descriptors using cosine simillarity scaled between 0-1
for desVecOrg, desVecDec in zip(desOrg, desDec):
dist.append(np.log(1 - np.abs(spatial.distance.cosine(desVecOrg, desVecDec))))
metrics.append(np.array(dist))
# Convert the metrics to np array and normalize them
metrics = self.normalize2DData(np.array(metrics))
return metrics, labels