-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_xception_224_submission.py
More file actions
261 lines (201 loc) · 7.45 KB
/
project_xception_224_submission.py
File metadata and controls
261 lines (201 loc) · 7.45 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
import gc
import keras as k
import tensorflow as tf
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Activation, BatchNormalization
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.applications.inception_v3 import InceptionV3
from keras.applications.resnet50 import ResNet50
from keras.applications.xception import Xception
from keras.layers import Input
from keras import backend as K
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
import cv2
from tqdm import tqdm
from heamy.dataset import Dataset
from sklearn.model_selection import train_test_split
from sklearn.metrics import fbeta_score, precision_score
from skimage import io,transform
import time
import os
import fnmatch
thresh_2_val = np.load('xception_224_thresh_2_val.npy')
train_mean = np.load('xception_224_train_mean.npy')
base_model = Xception(weights='imagenet', include_top=False)
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(1024)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(17, activation='sigmoid')(x)
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
df_test = pd.read_csv('sample_submission_v2.csv')
model.load_weights('xception_224_weights_2')
x_test = []
x_test = np.zeros((30000,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[:30000], miniters=1000):
img = cv2.imread('test-jpg/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_0 = model.predict(x_test,batch_size=64)
y_pred_0 = np.array(y_test_0>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((30000,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[:30000], miniters=1000):
img = cv2.imread('test-jpg-1/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_1 = model.predict(x_test,batch_size=64)
y_pred_1 = np.array(y_test_1>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((30000,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[:30000], miniters=1000):
img = cv2.imread('test-jpg-2/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_2 = model.predict(x_test,batch_size=64)
y_pred_2 = np.array(y_test_2>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((30000,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[:30000], miniters=1000):
img = cv2.imread('test-jpg-3/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_3 = model.predict(x_test,batch_size=64)
y_pred_3 = np.array(y_test_3>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((30000,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[:30000], miniters=1000):
img = cv2.imread('test-jpg-4/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_4 = model.predict(x_test,batch_size=64)
y_pred_4 = np.array(y_test_4>np.array(thresh_2_val))
y_pred_0= np.array(y_pred_0,np.uint)
y_pred_1= np.array(y_pred_1,np.uint)
y_pred_2= np.array(y_pred_2,np.uint)
y_pred_3= np.array(y_pred_3,np.uint)
y_pred_4= np.array(y_pred_4,np.uint)
x_test = []
x_test = np.zeros((31191,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[30000:], miniters=1000):
img = cv2.imread('test-jpg/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_0_1 = model.predict(x_test,batch_size=64)
y_pred_0_1 = np.array(y_test_0_1>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((31191,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[30000:], miniters=1000):
img = cv2.imread('test-jpg-1/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_1_1 = model.predict(x_test,batch_size=64)
y_pred_1_1 = np.array(y_test_1_1>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((31191,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[30000:], miniters=1000):
img = cv2.imread('test-jpg-2/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_2_1 = model.predict(x_test,batch_size=64)
y_pred_2_1 = np.array(y_test_2_1>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((31191,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[30000:], miniters=1000):
img = cv2.imread('test-jpg-3/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_3_1 = model.predict(x_test,batch_size=64)
y_pred_3_1 = np.array(y_test_3_1>np.array(thresh_2_val))
x_test = []
x_test = np.zeros((31191,224,224,3), np.float32)
i = 0
for f, tags in tqdm(df_test.values[30000:], miniters=1000):
img = cv2.imread('test-jpg-4/{}.jpg'.format(f))
x_test[i,:,:,:] = np.array(cv2.resize(img, (224, 224)),np.float32)/255.#139 minimum size for inception
i+=1
print(x_test.shape)
x_test -= train_mean
y_test_4_1 = model.predict(x_test,batch_size=64)
y_pred_4_1 = np.array(y_test_4_1>np.array(thresh_2_val))
y_pred_0_1= np.array(y_pred_0_1,np.uint)
y_pred_1_1= np.array(y_pred_1_1,np.uint)
y_pred_2_1= np.array(y_pred_2_1,np.uint)
y_pred_3_1= np.array(y_pred_3_1,np.uint)
y_pred_4_1= np.array(y_pred_4_1,np.uint)
y_pred_0 = np.concatenate([y_pred_0,y_pred_0_1])
y_pred_1 = np.concatenate([y_pred_1,y_pred_1_1])
y_pred_2 = np.concatenate([y_pred_2,y_pred_2_1])
y_pred_3 = np.concatenate([y_pred_3,y_pred_3_1])
y_pred_4 = np.concatenate([y_pred_4,y_pred_4_1])
y_sum = y_pred_0 + y_pred_1 + y_pred_2 + y_pred_3 + y_pred_4
labels = ['blow_down',
'bare_ground',
'conventional_mine',
'blooming',
'artisinal_mine',
'selective_logging',
'slash_burn',
'cultivation',
'habitation',
'road',
'agriculture',
'water',
'primary',
'partly_cloudy',
'cloudy',
'clear',
'haze',]
y_pred = (y_sum>=3)
labels_np = np.array(labels)
preds = [' '.join(labels_np[np.array(y_pred[i,:],bool)]) for i in range(y_pred.shape[0])]
subm = pd.DataFrame()
subm['image_name'] = df_test.values[:,0]
subm['tags'] = preds
subm.to_csv('submission_xception_224_1.csv', index=False)
#test set score:0.92646
y_pred = y_pred_0
labels_np = np.array(labels)
preds = [' '.join(labels_np[np.array(y_pred[i,:],bool)]) for i in range(y_pred.shape[0])]
subm = pd.DataFrame()
subm['image_name'] = df_test.values[:,0]
subm['tags'] = preds
subm.to_csv('submission_xception_224_2.csv', index=False)
#test set score:0.92516