-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_train.py
158 lines (118 loc) · 5.28 KB
/
model_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
import numpy as np
import cv2
import os
import re
from keras.models import Sequential
from keras.layers import Conv2D, Dense, MaxPool2D, Flatten, Dropout
from keras.callbacks import EarlyStopping, ModelCheckpoint
from sklearn.model_selection import train_test_split
from skimage import feature
#################### set parameters ######################
path = '../path/to/data'
wx = 13
wy = 2
mode = 0 # 1: CNN / 0: HOG
#################### create dataset ######################
def create_data(path,wx,wy,mode):
X = []
y = []
files = os.listdir(path)
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
files.sort(key=alphanum_key)
file = files[0::2]
gt_files = files[1::2]
Xp, Xn, yp, yn = ([] for i in range(4))
for (f,g) in zip(file,gt_files):
fname = os.path.join(roots,f)
gtname = os.path.join(roots,g)
img = cv2.imread(fname).astype(np.float32)/255
gt = cv2.imread(gtname).astype(np.float32)/255
h, w, d = img.shape
# padding to borders of image
pimg = cv2.copyMakeBorder(img,wx,wx,wx,wx,cv2.BORDER_REFLECT101)
pgt = cv2.copyMakeBorder(gt,wy,wy,wy,wy,cv2.BORDER_REFLECT101)
# collect positive samples
cnt = 0
for i in range(wx,w+wx):
for j in range(wx,h+wx):
if (pgt[j-wx+wy,i-wx+wy,0]==0): # positive sample (black pixel)
patch = pimg[j-wx:j+wx+1,i-wx:i+wx+1,:]
if mode: # CNN feature extraction
label = pgt[j-wx:j-wx+2*wy+1,i-wx:i-wx+2*wy+1,0]
label = np.reshape(label,((2*wy+1)**2,),order='F')
Xp.append(patch)
yp.append(label)
else: # HOG feature extraction
hg = feature.hog(patch[:,:,0], orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")
label = 0
Xp.append(hg)
yp.append(label)
cnt += 1
# collect random negative samples
for i in np.random.choice(range(wx,w+wx),int(np.sqrt(2*cnt))):
for j in np.random.choice(range(wx,h+wx),int(np.sqrt(2*cnt))):
if (pgt[j-wx+wy,i-wx+wy,0]==255): # negative sample (white pixel)
patch = pimg[j-wx:j+wx+1,i-wx:i+wx+1,:]
if mode: # CNN feature extraction
label = pgt[j-wx:j-wx+2*wy+1,i-wx:i-wx+2*wy+1,0]
label = np.reshape(label,((2*wy+1)**2,),order='F')
Xn.append(patch)
yn.append(label)
else: # HOG feature extraction
hg = feature.hog(patch[:,:,0], orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")
label = 1
Xn.append(hg)
yn.append(label)
X = np.vstack((Xp,Xn))
y = np.concatenate((yp,yn),axis=0)
return np.array(X), np.array(y)
X, y = create_data(path,wx,wy,mode)
print(np.shape(X))
print(np.shape(y))
################### train model ########################
def train_model(mode):
model = Sequential()
if mode:
# conv1
model.add(Conv2D(128,kernel_size=(7,7),padding='same',activation='relu',input_shape=(2*wx+1,2*wx+1,3)))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
# conv2
model.add(Conv2D(256,kernel_size=(5,5),padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
# dense1
model.add(Dense(1000,activation='relu'))
model.add(Dropout(0.5))
# dense2
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
# ouput layer (multi-label classification)
model.add(Dense((2*wy+1)**2,activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
else:
model.add(Dense(512, activation='relu', input_dim=np.shape(X)[1]))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# train/validation split
Xtrn, Xtst, ytrn, ytst = train_test_split(X,y,test_size=0.2,shuffle=True,random_state=42)
early_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='auto')
mdl_save = ModelCheckpoint('mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='auto')
model = train_model(mode)
model.fit(Xtrn,ytrn,batch_size=128,epochs=10,shuffle=True,verbose=2,
validation_data=(Xtst,ytst),callbacks=[early_stop,mdl_save])
### save model
file = os.path.expanduser('~') + '../path/to/directory/model.json'
try:
os.remove(file)
except OSError:
pass
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
print("Model saved to disk")