FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ImageRecognitionAI/train_test.py at master · BaseMax/ImageRecognitionAI · GitHub
BaseMax
/
ImageRecognitionAI
Public
Notifications
You must be signed in to change notification settings
Fork
7
Star
15
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ImageRecognitionAI
/
train_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
146 lines (119 loc) · 4.87 KB
Breadcrumbs
ImageRecognitionAI
/
train_test.py
Copy path
File metadata and controls
146 lines (119 loc) · 4.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#-----------------------------------
# TRAINING OUR MODEL
#-----------------------------------
import
h5py
import
numpy
as
np
import
os
import
glob
import
cv2
import
warnings
from
matplotlib
import
pyplot
from
sklearn
.
model_selection
import
train_test_split
,
cross_val_score
from
sklearn
.
model_selection
import
KFold
,
StratifiedKFold
from
sklearn
.
metrics
import
confusion_matrix
,
accuracy_score
,
classification_report
from
sklearn
.
linear_model
import
LogisticRegression
from
sklearn
.
tree
import
DecisionTreeClassifier
from
sklearn
.
ensemble
import
RandomForestClassifier
from
sklearn
.
neighbors
import
KNeighborsClassifier
from
sklearn
.
discriminant_analysis
import
LinearDiscriminantAnalysis
from
sklearn
.
naive_bayes
import
GaussianNB
from
sklearn
.
svm
import
SVC
from
sklearn
.
externals
import
joblib
warnings
.
filterwarnings
(
'ignore'
)
#--------------------
# tunable-parameters
#--------------------
num_trees
=
100
test_size
=
0.10
seed
=
9
test_path
=
"dataset/test"
h5_data
=
'output/data.h5'
h5_labels
=
'output/labels.h5'
scoring
=
"accuracy"
if
not
os
.
path
.
exists
(
test_path
):
os
.
makedirs
(
test_path
)
# create all the machine learning models
models
=
[]
models
.
append
((
'LR'
,
LogisticRegression
(
random_state
=
seed
)))
models
.
append
((
'LDA'
,
LinearDiscriminantAnalysis
()))
models
.
append
((
'KNN'
,
KNeighborsClassifier
()))
models
.
append
((
'CART'
,
DecisionTreeClassifier
(
random_state
=
seed
)))
models
.
append
((
'RF'
,
RandomForestClassifier
(
n_estimators
=
num_trees
,
random_state
=
seed
)))
models
.
append
((
'NB'
,
GaussianNB
()))
models
.
append
((
'SVM'
,
SVC
(
random_state
=
seed
)))
# variables to hold the results and names
results
=
[]
names
=
[]
# import the feature vector and trained labels
h5f_data
=
h5py
.
File
(
h5_data
,
'r'
)
h5f_label
=
h5py
.
File
(
h5_labels
,
'r'
)
global_features_string
=
h5f_data
[
'dataset_1'
]
global_labels_string
=
h5f_label
[
'dataset_1'
]
global_features
=
np
.
array
(
global_features_string
)
global_labels
=
np
.
array
(
global_labels_string
)
h5f_data
.
close
()
h5f_label
.
close
()
# verify the shape of the feature vector and labels
print
(
"[STATUS] features shape: {}"
.
format
(
global_features
.
shape
))
print
(
"[STATUS] labels shape: {}"
.
format
(
global_labels
.
shape
))
print
(
"[STATUS] training started..."
)
# split the training and testing data
(
trainDataGlobal
,
testDataGlobal
,
trainLabelsGlobal
,
testLabelsGlobal
)
=
train_test_split
(
np
.
array
(
global_features
),
np
.
array
(
global_labels
),
test_size
=
test_size
,
random_state
=
seed
)
print
(
"[STATUS] splitted train and test data..."
)
print
(
"Train data : {}"
.
format
(
trainDataGlobal
.
shape
))
print
(
"Test data : {}"
.
format
(
testDataGlobal
.
shape
))
print
(
"Train labels: {}"
.
format
(
trainLabelsGlobal
.
shape
))
print
(
"Test labels : {}"
.
format
(
testLabelsGlobal
.
shape
))
# 10-fold cross validation
for
name
,
model
in
models
:
kfold
=
KFold
(
n_splits
=
10
,
random_state
=
seed
)
cv_results
=
cross_val_score
(
model
,
trainDataGlobal
,
trainLabelsGlobal
,
cv
=
kfold
,
scoring
=
scoring
)
results
.
append
(
cv_results
)
names
.
append
(
name
)
msg
=
"%s: %f (%f)"
%
(
name
,
cv_results
.
mean
(),
cv_results
.
std
())
print
(
msg
)
# boxplot algorithm comparison
fig
=
pyplot
.
figure
()
fig
.
suptitle
(
'Machine Learning algorithm comparison'
)
ax
=
fig
.
add_subplot
(
111
)
pyplot
.
boxplot
(
results
)
ax
.
set_xticklabels
(
names
)
pyplot
.
show
()
#-----------------------------------
# TESTING OUR MODEL
#-----------------------------------
# to visualize results
import
matplotlib
.
pyplot
as
plt
# create the model - Random Forests
clf
=
RandomForestClassifier
(
n_estimators
=
num_trees
,
random_state
=
seed
)
# fit the training data to the model
clf
.
fit
(
trainDataGlobal
,
trainLabelsGlobal
)
# loop through the test images
for
file
in
glob
.
glob
(
test_path
+
"/*.jpg"
):
# read the image
image
=
cv2
.
imread
(
file
)
# resize the image
image
=
cv2
.
resize
(
image
,
fixed_size
)
####################################
# Global Feature extraction
####################################
fv_hu_moments
=
fd_hu_moments
(
image
)
fv_haralick
=
fd_haralick
(
image
)
fv_histogram
=
fd_histogram
(
image
)
###################################
# Concatenate global features
###################################
global_feature
=
np
.
hstack
([
fv_histogram
,
fv_haralick
,
fv_hu_moments
])
# scale features in the range (0-1)
scaler
=
MinMaxScaler
(
feature_range
=
(
0
,
1
))
rescaled_feature
=
scaler
.
fit_transform
(
global_feature
)
# predict label of test image
prediction
=
clf
.
predict
(
rescaled_feature
.
reshape
(
1
,
-
1
))[
0
]
# show predicted label on image
cv2
.
putText
(
image
,
train_labels
[
prediction
], (
20
,
30
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1.0
, (
0
,
255
,
255
),
3
)
# display the output image
plt
.
imshow
(
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2RGB
))
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL