-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModelClassificationHardNet1.py
111 lines (86 loc) · 3.13 KB
/
ModelClassificationHardNet1.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
# -*- 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 sklearn.utils import gen_batches
from ModelClassificationBase import ModelClassificationBase
class ModelClassificationHardNet1(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
):
# Disable tf2 behavior for tf1
#tf1.disable_v2_behavior()
# Call the parent
ModelClassificationBase.__init__(
self,
modelDataPath,
experimentPath,
modelSel,
layerSel,
labelInfo,
imageDim,
modelData,
'HardNet1',
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.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
diffData = np.subtract(processedData.get('Org'), processedData.get('Dec'))
labels = processedData.get('Lab')
# Initialize the conversion and metric lists
gsData = []
# Convert the data to grayscale and to required shape (batch, height, width, channel)
if(diffData.shape[3] == 3):
for img in diffData:
gsData.append(cv.resize(cv.cvtColor(img, cv.COLOR_BGR2GRAY), (32, 32), interpolation = cv.INTER_AREA))
else:
for img in diffData:
gsData.append(cv.resize(np.squeeze(img), (32, 32), interpolation = cv.INTER_AREA))
gsData = np.array(gsData)
gsData = np.expand_dims(gsData, axis=3)
# Generate batches
batches = gen_batches(gsData.shape[0], 20)
fsRun = True
# Get HardNet features
for batch in batches:
temp = self.hardNet.forward(gsData[batch])
if fsRun:
metrics = temp
fsRun = False
else:
metrics = np.concatenate((metrics, temp), axis=0)
# Convert the metrics to np array and normalize them
metrics = self.normalize2DData(metrics)
return metrics, labels