forked from abzargar/COVID-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
172 lines (164 loc) · 6.25 KB
/
utils.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import cv2
import glob
import numpy as np
import scipy.io as sio
from scipy.stats import skew
from scipy.stats import kurtosis
import pywt
from skimage.feature import greycomatrix
import scipy.io as sio
import numpy as np
import seaborn as sn
import pandas as pd
from sklearn import preprocessing
from sklearn.decomposition import KernelPCA
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
from keras.utils import to_categorical
from sklearn import metrics
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
import os
from sklearn.preprocessing import MinMaxScaler
from keras.callbacks import EarlyStopping
from sklearn.decomposition import KernelPCA
from tensorflow.keras.utils import plot_model
from IPython.display import Image
import itertools
import os
from sklearn.metrics import roc_curve, auc
kl = keras.layers
# =============================================================================
# im2double
# =============================================================================
def im2double(img):
""" convert image to double format """
min_val = np.min(img.ravel())
max_val = np.max(img.ravel())
out = (img.astype('float') - min_val) / (max_val - min_val)
return out
# =============================================================================
# compute_14_features
# =============================================================================
def compute_14_features(region):
""" Compute 14 features """
temp_array=region.reshape(-1)
all_pixels=temp_array[temp_array!=0]
# Area
Area = np.sum(all_pixels)
# mean
density = np.mean(all_pixels)
# Std
std_Density = np.std(all_pixels)
# skewness
Skewness = skew(all_pixels)
# kurtosis
Kurtosis = kurtosis(all_pixels)
# Energy
ENERGY =np.sum(np.square(all_pixels))
# Entropy
value,counts = np.unique(all_pixels, return_counts=True)
p = counts / np.sum(counts)
p = p[p!=0]
ENTROPY =-np.sum( p*np.log2(p));
# Maximum
MAX = np.max(all_pixels)
# Mean Absolute Deviation
sum_deviation= np.sum(np.abs(all_pixels-np.mean(all_pixels)))
mean_absolute_deviation = sum_deviation/len(all_pixels)
# Median
MEDIAN = np.median(all_pixels)
# Minimum
MIN = np.min(all_pixels)
# Range
RANGE = np.max(all_pixels)-np.min(all_pixels)
# Root Mean Square
RMS = np.sqrt(np.mean(np.square(all_pixels)))
# Uniformity
UNIFORMITY = np.sum(np.square(p))
features = np.array([Area, density, std_Density,
Skewness, Kurtosis,ENERGY, ENTROPY,
MAX, mean_absolute_deviation, MEDIAN, MIN, RANGE, RMS, UNIFORMITY])
return features
# =============================================================================
# GLDM
# =============================================================================
def GLDM(img, distance):
""" GLDM in four directions """
pro1=np.zeros(img.shape,dtype=np.float32)
pro2=np.zeros(img.shape,dtype=np.float32)
pro3=np.zeros(img.shape,dtype=np.float32)
pro4=np.zeros(img.shape,dtype=np.float32)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if((j+distance)<img.shape[1]):
pro1[i,j]=np.abs(img[i,j]-img[i,(j+distance)])
if((i-distance)>0)&((j+distance)<img.shape[1]):
pro2[i,j]=np.abs(img[i,j]-img[(i-distance),(j+distance)])
if((i+distance)<img.shape[0]):
pro3[i,j]=np.abs(img[i,j]-img[(i+distance),j])
if((i-distance)>0)&((j-distance)>0):
pro4[i,j]=np.abs(img[i,j]-img[(i-distance),(j-distance)])
n=256;
cnt, bin_edges=np.histogram(pro1[pro1!=0], bins=np.arange(n)/(n-1), density=False)
Out1 = cnt.cumsum()
cnt, bin_edges=np.histogram(pro2[pro2!=0], bins=np.arange(n)/(n-1), density=False)
Out2 = cnt.cumsum()
cnt, bin_edges=np.histogram(pro3[pro3!=0], bins=np.arange(n)/(n-1), density=False)
Out3 = cnt.cumsum()
cnt, bin_edges=np.histogram(pro4[pro4!=0], bins=np.arange(n)/(n-1), density=False)
Out4 = cnt.cumsum()
return Out1,Out2,Out3,Out4
# =============================================================================
# show model
# =============================================================================
def show_model(model):
print(plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png'))
return Image(filename='model.png')
# =============================================================================
# build model
# =============================================================================
def build_model(feature_size, n_classes):
""" Build a small model for multi-label classification """
inp = kl.Input((feature_size,))
x = kl.Dense(128, activation='sigmoid')(inp)
x=kl.Dropout(0.2)(x)
x = kl.Dense(32, activation='sigmoid')(x)
x=kl.Dropout(0.2)(x)
out = kl.Dense(n_classes, activation='sigmoid')(x)
model = keras.Model(inputs=inp, outputs=out)
model.summary()
return model
# =============================================================================
# plot confusion matrix
# =============================================================================
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')