-
Notifications
You must be signed in to change notification settings - Fork 28
/
train.py
229 lines (168 loc) · 7.3 KB
/
train.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import cv2
import numpy as np
import pylab as plt
from glob import glob
import argparse
import os
import progressbar
import pickle as pkl
from numpy.lib import stride_tricks
from skimage import feature
from sklearn import metrics
from sklearn.model_selection import train_test_split
import time
import mahotas as mt
def check_args(args):
if not os.path.exists(args.image_dir):
raise ValueError("Image directory does not exist")
if not os.path.exists(args.label_dir):
raise ValueError("Label directory does not exist")
if args.classifier != "SVM" and args.classifier != "RF" and args.classifier != "GBC":
raise ValueError("Classifier must be either SVM, RF or GBC")
if args.output_model.split('.')[-1] != "p":
raise ValueError("Model extension must be .p")
return args
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--image_dir" , help="Path to images", required=True)
parser.add_argument("-l", "--label_dir", help="Path to labels", required=True)
parser.add_argument("-c", "--classifier", help="Classification model to use", required = True)
parser.add_argument("-o", "--output_model", help="Path to save model. Must end in .p", required = True)
args = parser.parse_args()
return check_args(args)
def read_data(image_dir, label_dir):
print ('[INFO] Reading image data.')
filelist = glob(os.path.join(image_dir, '*.jpg'))
image_list = []
label_list = []
for file in filelist:
image_list.append(cv2.imread(file, 1))
label_list.append(cv2.imread(os.path.join(label_dir, os.path.basename(file).split('.')[0]+'.png'), 0))
return image_list, label_list
def subsample(features, labels, low, high, sample_size):
idx = np.random.randint(low, high, sample_size)
return features[idx], labels[idx]
def subsample_idx(low, high, sample_size):
return np.random.randint(low,high,sample_size)
def calc_haralick(roi):
feature_vec = []
texture_features = mt.features.haralick(roi)
mean_ht = texture_features.mean(axis=0)
[feature_vec.append(i) for i in mean_ht[0:9]]
return np.array(feature_vec)
def harlick_features(img, h_neigh, ss_idx):
print ('[INFO] Computing haralick features.')
size = h_neigh
shape = (img.shape[0] - size + 1, img.shape[1] - size + 1, size, size)
strides = 2 * img.strides
patches = stride_tricks.as_strided(img, shape=shape, strides=strides)
patches = patches.reshape(-1, size, size)
if len(ss_idx) == 0 :
bar = progressbar.ProgressBar(maxval=len(patches), \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
else:
bar = progressbar.ProgressBar(maxval=len(ss_idx), \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
h_features = []
if len(ss_idx) == 0:
for i, p in enumerate(patches):
bar.update(i+1)
h_features.append(calc_haralick(p))
else:
for i, p in enumerate(patches[ss_idx]):
bar.update(i+1)
h_features.append(calc_haralick(p))
#h_features = [calc_haralick(p) for p in patches[ss_idx]]
return np.array(h_features)
def create_binary_pattern(img, p, r):
print ('[INFO] Computing local binary pattern features.')
lbp = feature.local_binary_pattern(img, p, r)
return (lbp-np.min(lbp))/(np.max(lbp)-np.min(lbp)) * 255
def create_features(img, img_gray, label, train=True):
lbp_radius = 24 # local binary pattern neighbourhood
h_neigh = 11 # haralick neighbourhood
num_examples = 1000 # number of examples per image to use for training model
lbp_points = lbp_radius*8
h_ind = int((h_neigh - 1)/ 2)
feature_img = np.zeros((img.shape[0],img.shape[1],4))
feature_img[:,:,:3] = img
img = None
feature_img[:,:,3] = create_binary_pattern(img_gray, lbp_points, lbp_radius)
feature_img = feature_img[h_ind:-h_ind, h_ind:-h_ind]
features = feature_img.reshape(feature_img.shape[0]*feature_img.shape[1], feature_img.shape[2])
if train == True:
ss_idx = subsample_idx(0, features.shape[0], num_examples)
features = features[ss_idx]
else:
ss_idx = []
h_features = harlick_features(img_gray, h_neigh, ss_idx)
features = np.hstack((features, h_features))
if train == True:
label = label[h_ind:-h_ind, h_ind:-h_ind]
labels = label.reshape(label.shape[0]*label.shape[1], 1)
labels = labels[ss_idx]
else:
labels = None
return features, labels
def create_training_dataset(image_list, label_list):
print ('[INFO] Creating training dataset on %d image(s).' %len(image_list))
X = []
y = []
for i, img in enumerate(image_list):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
features, labels = create_features(img, img_gray, label_list[i])
X.append(features)
y.append(labels)
X = np.array(X)
X = X.reshape(X.shape[0]*X.shape[1], X.shape[2])
y = np.array(y)
y = y.reshape(y.shape[0]*y.shape[1], y.shape[2]).ravel()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print ('[INFO] Feature vector size:', X_train.shape)
return X_train, X_test, y_train, y_test
def train_model(X, y, classifier):
if classifier == "SVM":
from sklearn.svm import SVC
print ('[INFO] Training Support Vector Machine model.')
model = SVC()
model.fit(X, y)
elif classifier == "RF":
from sklearn.ensemble import RandomForestClassifier
print ('[INFO] Training Random Forest model.')
model = RandomForestClassifier(n_estimators=250, max_depth=12, random_state=42)
model.fit(X, y)
elif classifier == "GBC":
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
model.fit(X, y)
print ('[INFO] Model training complete.')
print ('[INFO] Training Accuracy: %.2f' %model.score(X, y))
return model
def test_model(X, y, model):
pred = model.predict(X)
precision = metrics.precision_score(y, pred, average='weighted', labels=np.unique(pred))
recall = metrics.recall_score(y, pred, average='weighted', labels=np.unique(pred))
f1 = metrics.f1_score(y, pred, average='weighted', labels=np.unique(pred))
accuracy = metrics.accuracy_score(y, pred)
print ('--------------------------------')
print ('[RESULTS] Accuracy: %.2f' %accuracy)
print ('[RESULTS] Precision: %.2f' %precision)
print ('[RESULTS] Recall: %.2f' %recall)
print ('[RESULTS] F1: %.2f' %f1)
print ('--------------------------------')
def main(image_dir, label_dir, classifier, output_model):
start = time.time()
image_list, label_list = read_data(image_dir, label_dir)
X_train, X_test, y_train, y_test = create_training_dataset(image_list, label_list)
model = train_model(X_train, y_train, classifier)
test_model(X_test, y_test, model)
pkl.dump(model, open(output_model, "wb"))
print ('Processing time:',time.time()-start)
if __name__ == "__main__":
args = parse_args()
image_dir = args.image_dir
label_dir = args.label_dir
classifier = args.classifier
output_model = args.output_model
main(image_dir, label_dir, classifier, output_model)