-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoriginal_model-64.py
242 lines (185 loc) · 7.46 KB
/
original_model-64.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
230
231
232
233
234
235
236
237
238
239
240
241
242
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'
import numpy as np
np.random.seed(2016)
import os
import glob
import cv2
import datetime
import pandas as pd
import time
import warnings
warnings.filterwarnings("ignore")
from sklearn.cross_validation import KFold
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping
from keras.utils import np_utils
from sklearn.metrics import log_loss
from keras import __version__ as keras_version
def get_im_cv2(path):
img = cv2.imread(path)
resized = cv2.resize(img, (64, 64), cv2.INTER_LINEAR)
return resized
def load_train():
X_train = []
X_train_id = []
y_train = []
start_time = time.time()
print('Read train images')
folders = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']
for fld in folders:
index = folders.index(fld)
print('Load folder {} (Index: {})'.format(fld, index))
path = os.path.join('.', 'train', fld, '*.jpg')
print(path)
files = glob.glob(path)
for fl in files:
flbase = os.path.basename(fl)
img = get_im_cv2(fl)
X_train.append(img)
X_train_id.append(flbase)
y_train.append(index)
print('Read train data time: {} seconds'.format(round(time.time() - start_time, 2)))
return X_train, y_train, X_train_id
def load_test():
path = os.path.join('.', 'test-small', '*.jpg')
files = sorted(glob.glob(path))
X_test = []
X_test_id = []
for fl in files:
flbase = os.path.basename(fl)
img = get_im_cv2(fl)
X_test.append(img)
X_test_id.append(flbase)
return X_test, X_test_id
def create_submission(predictions, test_id, info):
result1 = pd.DataFrame(predictions, columns=['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT'])
result1.loc[:, 'image'] = pd.Series(test_id, index=result1.index)
now = datetime.datetime.now()
sub_file = 'submission_' + info + '_' + str(now.strftime("%Y-%m-%d-%H-%M")) + '.csv'
result1.to_csv(sub_file, index=False)
def read_and_normalize_train_data():
train_data, train_target, train_id = load_train()
print('Convert to numpy...')
train_data = np.array(train_data, dtype=np.uint8)
train_target = np.array(train_target, dtype=np.uint8)
print('Reshape...')
train_data = train_data.transpose((0, 3, 1, 2))
print('Convert to float...')
train_data = train_data.astype('float32')
train_data = train_data / 255
train_target = np_utils.to_categorical(train_target, 8)
print('Train shape:', train_data.shape)
print(train_data.shape[0], 'train samples')
return train_data, train_target, train_id
def read_and_normalize_test_data():
start_time = time.time()
test_data, test_id = load_test()
test_data = np.array(test_data, dtype=np.uint8)
test_data = test_data.transpose((0, 3, 1, 2))
test_data = test_data.astype('float32')
test_data = test_data / 255
print('Test shape:', test_data.shape)
# print(test_data.shape[0], 'test samples')
print('Read and process test data time: {} seconds'.format(round(time.time() - start_time, 2)))
return test_data, test_id
def dict_to_list(d):
ret = []
for i in d.items():
ret.append(i[1])
return ret
def merge_several_folds_mean(data, nfolds):
a = np.array(data[0])
for i in range(1, nfolds):
a += np.array(data[i])
a /= nfolds
return a.tolist()
def create_model():
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, 64, 64), dim_ordering='th'))
model.add(Convolution2D(4, 3, 3, activation='relu', dim_ordering='th'))
model.add(ZeroPadding2D((1, 1), dim_ordering='th'))
model.add(Convolution2D(4, 3, 3, activation='relu', dim_ordering='th'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), dim_ordering='th'))
model.add(ZeroPadding2D((1, 1), dim_ordering='th'))
model.add(Convolution2D(8, 3, 3, activation='relu', dim_ordering='th'))
model.add(ZeroPadding2D((1, 1), dim_ordering='th'))
model.add(Convolution2D(8, 3, 3, activation='relu', dim_ordering='th'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), dim_ordering='th'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(8, activation='softmax'))
sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
return model
def get_validation_predictions(train_data, predictions_valid):
pv = []
for i in range(len(train_data)):
pv.append(predictions_valid[i])
return pv
def run_cross_validation_create_models(nfolds=10):
# input image dimensions
batch_size = 16
nb_epoch = 30
random_state = 51
train_data, train_target, train_id = read_and_normalize_train_data()
yfull_train = dict()
kf = KFold(len(train_id), n_folds=nfolds, shuffle=True, random_state=random_state)
num_fold = 0
sum_score = 0
models = []
for train_index, test_index in kf:
model = create_model()
X_train = train_data[train_index]
Y_train = train_target[train_index]
X_valid = train_data[test_index]
Y_valid = train_target[test_index]
num_fold += 1
#print('Start KFold number {} from {}'.format(num_fold, nfolds))
#print('Split train: ', len(X_train), len(Y_train))
#print('Split valid: ', len(X_valid), len(Y_valid))
callbacks = [
EarlyStopping(monitor='val_loss', patience=3, verbose=0),
]
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
callbacks=callbacks)
predictions_valid = model.predict(X_valid.astype('float32'), batch_size=batch_size, verbose=0)
score = log_loss(Y_valid, predictions_valid)
print('Score log_loss: ', score)
sum_score += score*len(test_index)
# Store valid predictions
for i in range(len(test_index)):
yfull_train[test_index[i]] = predictions_valid[i]
models.append(model)
score = sum_score/len(train_data)
print("Log_loss train independent avg: ", score)
info_string = 'loss_' + str(score) + '_folds_' + str(nfolds) + '_ep_' + str(nb_epoch)
return info_string, models
def run_cross_validation_process_test(info_string, models):
batch_size = 16
num_fold = 0
yfull_test = []
test_id = []
nfolds = len(models)
for i in range(nfolds):
model = models[i]
num_fold += 1
print('Start KFold number {} from {}'.format(num_fold, nfolds))
test_data, test_id = read_and_normalize_test_data()
test_prediction = model.predict(test_data, batch_size=batch_size, verbose=2)
yfull_test.append(test_prediction)
test_res = merge_several_folds_mean(yfull_test, nfolds)
info_string = 'loss_' + info_string \
+ '_folds_' + str(nfolds)
create_submission(test_res, test_id, info_string)
if __name__ == '__main__':
print('Keras version: {}'.format(keras_version))
num_folds = 3
info_string, models = run_cross_validation_create_models(num_folds)
run_cross_validation_process_test(info_string, models)