-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
189 lines (161 loc) · 7.41 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
from scipy.misc import imread, imsave, imresize
import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from keras.utils import to_categorical
from keras.layers import Conv2D, BatchNormalization, Input, Activation
from keras.layers import LeakyReLU, Lambda, Reshape, Concatenate, Add, Dense, Flatten, Dropout
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, LearningRateScheduler, Callback
from keras.optimizers import SGD, Adam
from keras import regularizers
import keras.backend as K
from keras.models import Model, load_model
import pickle
import numpy as np
from sklearn.externals import joblib
import cv2
import sys, random, time
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.resnet50 import ResNet50, preprocess_input
class LogCallback(Callback):
def __init__(self):
super(Callback, self).__init__()
def on_epoch_end(self, epoch, logs={}):
with open(os.path.join(MODELDIR, 'log.txt'), 'a') as file:
file.write(str(epoch) + ',' + str(logs['loss']) +',' + str(logs['acc']) + ',')
file.write(str(logs['val_loss']) + ',' + str(logs['val_acc']) + '\n')
model_id = sys.argv[1]
if not (os.path.exists("models/%s/" % model_id)):
os.makedirs("models/%s/" % model_id)
MODELDIR = os.path.join("models", model_id)
MODELFILE = 'model.h5'
CLASSES = 8
# sv = ModelCheckpoint(os.path.join(MODELDIR, MODELFILE), save_best_only=True, save_weights_only=True)
sv = ModelCheckpoint(os.path.join(MODELDIR, MODELFILE),
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto',
period=1)
es = EarlyStopping(patience=20)
def EqualizeHistogram(img, in_path=None):
if in_path != None:
img = cv2.imread(in_path,0)
equ = np.array(cv2.equalizeHist(img))
# print(equ.shape)
return equ
def CLAHE(img, in_path = None, tileGridsize=(8,8)):
if in_path != None:
img = cv2.imread(in_path,0)
clahe1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=tileGridsize)
cl1 = clahe1.apply(img)
return cl1
def create_base_model( w = 'imagenet', trainable = False):
model = ResNet50(weights=w, include_top=False, input_shape=(224, 224, 3))
if(not trainable):
for layer in model.layers:
layer.trainable = False
return model
def rebase_base_model(model):
# for layer in model.layers[:self.num_fixed_layers]:
# layer.trainable = False
for layer in model.layers:
layer.trainable = True
return model
def add_custom_layers(base_model):
x = base_model.output
x = Flatten()(x)
# x = Dropout(0.2)(x)
# x = Dense(1024, activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(0.005))(x)
x = Dense(2048, activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(0.005))(x)
x = Dense(2048, activation='relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(0.005))(x)
y = Dense(CLASSES, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=y)
return model
def create_model_ResNetV2():
INPUT_SIZE = 256
xi = Input([INPUT_SIZE, INPUT_SIZE, 3 ])
x = Reshape([INPUT_SIZE, INPUT_SIZE, 3])(xi)
ir = InceptionResNetV2(
include_top=False,
weights= 'imagenet',
input_tensor=x,
input_shape=(INPUT_SIZE, INPUT_SIZE, 3),
pooling='avg')
p = Dense(512, kernel_initializer='glorot_normal', activation='relu')(ir.output)
p = Dense(1024, kernel_initializer='glorot_normal', activation='relu')(ir.output)
p = Dense(CLASSES,activation='softmax')(p)
# ============ Model
model = Model(ir.input, p)
model.compile('adam', 'categorical_crossentropy', metrics= ['acc'])
# model.summary()
return model
def generator(batch_size, valid = False):
DATA_ROOT_PATH = 'data/npy'
while True:
if(valid):
with open('data/npy/X_valid.npy', 'rb') as f:
X = joblib.load(f)
with open('data/npy/y_valid.npy', 'rb') as f:
y = joblib.load(f)
# with open(os.path.join(DATA_ROOT_PATH, 'X_11' + '.npy'), 'rb') as file:
# X = joblib.load(file)
# with open(os.path.join(DATA_ROOT_PATH, 'y_11' + '.npy'), 'rb') as file:
# y = joblib.load(file)
# X = np.array(X)
# y = to_categorical(y, num_classes=CLASSES)
for j in range((len(X) // batch_size) - 1):
yield X[j * batch_size: (j+1) * batch_size], y[ j * batch_size: (j+1) * batch_size]
else:
for i in range(0,19):
with open(os.path.join(DATA_ROOT_PATH, 'X_' + str(i) + '.npy'), 'rb') as file:
X = joblib.load(file)
with open(os.path.join(DATA_ROOT_PATH, 'y_' + str(i) + '.npy'), 'rb') as file:
y = joblib.load(file)
for j in range( (len(X) // batch_size) - 1):
yield X[j * batch_size: (j+1) * batch_size], y[ j * batch_size: (j+1) * batch_size]
def x_gen(batch_size, valid = False):
if(valid):
with open('data/pickles/labels_valid.pkl', 'rb') as f:
traindata = pickle.load(f)
else:
with open('data/pickles/labels_train.pkl', 'rb') as f:
traindata = pickle.load(f)
X, y = [], []
while True:
for k, v in traindata.items():
# img = cv2.imread('data/images/' + k,0)
# resized_image = cv2.resize(img, (256, 256))
# img = EqualizeHistogram(resized_image)
# img = CLAHE(img)
# img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
img = imread('data/images/' + k, mode ='RGB')
img = img / 255
X.append(imresize(img ,size=(224,224)))
y.append(v[0])
if(len(X) == batch_size):
y = to_categorical(y, num_classes=CLASSES)
X = np.array(X)
yield X,y
X, y = [], []
def train():
if(os.path.exists(os.path.join(MODELDIR, MODELFILE))):
model = load_model(os.path.join(MODELDIR, MODELFILE))
else:
model = create_base_model(w = 'imagenet', trainable = False)
model = add_custom_layers(model)
adam = Adam(lr=0.005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# base_model = create_base_model()
# model = add_custom_layers(base_model)
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# train_history = model.fit_generator( x_gen(32, valid = False), validation_data = x_gen(32, valid = True),steps_per_epoch= 200, validation_steps = 14, epochs= 50, callbacks=[es,sv])
train_history = model.fit_generator( generator(32), validation_data = generator(32, valid = True), steps_per_epoch= 100, validation_steps = 84, epochs= 500, callbacks=[sv, LogCallback()])
# DATA_ROOT_PATH = 'data/npy'
# with open(os.path.join(DATA_ROOT_PATH, 'X_11' + '.npy'), 'rb') as file:
# X = joblib.load(file)
# with open(os.path.join(DATA_ROOT_PATH, 'y_11' + '.npy'), 'rb') as file:
# y = joblib.load(file)
# print(np.array(X).shape, np.array(y).shape)
# exit()
train()