-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotato_Peal.py
308 lines (239 loc) · 10.8 KB
/
Potato_Peal.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import json
import pprint
import numpy as np
import random
from tensorflow.python import keras as ks
def load_data(devset_size=320, dims=(75, 75)):
with open("train_processed.json") as f:
data = json.load(f)
random.shuffle(data)
num_data = len(data)
x_train = np.empty((num_data - devset_size, dims[0], dims[1], 2))
y_train = np.empty((num_data - devset_size, 2))
x_test = np.empty((devset_size, dims[0], dims[1], 2))
y_test = np.empty((devset_size, 2))
i = 0
for item in data[0:-devset_size]:
band1 = np.asarray(item["band_1"]).reshape((dims[0], dims[1]))
band2 = np.asarray(item["band_2"]).reshape((dims[0], dims[1]))
# band3 = np.asarray(item["band_3"]).reshape((dims[0],dims[1]))
x_train[i] = np.dstack((band1, band2))
#y_train[i] = item['is_iceberg'] == 1
if item['is_iceberg'] == 1:
y_train[i] = [1, 0]
else:
y_train[i] = [0, 1]
i += 1
i = 0
for item in data[num_data - devset_size:]:
band1 = np.asarray(item["band_1"]).reshape((dims[0], dims[1]))
band2 = np.asarray(item["band_2"]).reshape((dims[0], dims[1]))
# band3 = np.asarray(item["band_3"]).reshape((dims[0],dims[1]))
x_test[i] = np.dstack((band1, band2))
#y_test[i] = item['is_iceberg'] == 1
if item['is_iceberg'] == 1:
y_test[i] = [1, 0]
else:
y_test[i] = [0, 1]
i += 1
return x_train, y_train, x_test, y_test
def load_test_data(dims=(75,75)):
with open("test_processed.json") as f:
data = json.load(f)
num_data = len(data)
images = np.empty((num_data, dims[0], dims[1], 2))
id = []
i = 0
for item in data:
band1 = np.asarray(item["band_1"]).reshape((dims[0], dims[1]))
band2 = np.asarray(item["band_2"]).reshape((dims[0], dims[1]))
# band3 = np.asarray(item["band_3"]).reshape((dims[0],dims[1]))
images[i] = np.dstack((band1, band2))
id.append(item["id"])
i += 1
return images, id
def identity_block(X, f, filters, stage, block, seed=None):
"""
Implementation of the identity block. A three layer residual block
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
stage -- integer, used to name the layers, depending on their position in the network
block -- string/character, used to name the layers, depending on their position in the network
Returns:
X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
"""
# defining name basis
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
# Retrieve Filters
F1, F2, F3 = filters
# Save the input value. You'll need this later to add back to the main path.
X_shortcut = X
# First component of main path
X = ks.layers.Conv2D(filters=F1,
kernel_size=(1, 1),
strides=(1, 1),
padding='valid',
name=conv_name_base + '2a',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
X = ks.layers.Activation('relu')(X)
# Second component of main path
X = ks.layers.Conv2D(filters=F2,
kernel_size=(f, f),
strides=(1, 1),
padding='same',
name=conv_name_base + '2b',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
X = ks.layers.Activation('relu')(X)
# Third component of main path
X = ks.layers.Conv2D(filters=F3,
kernel_size=(1, 1),
strides=(1, 1),
padding='valid',
name=conv_name_base + '2c',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2c')(X)
# Final step: Add shortcut value to main path, and pass it through a RELU activation
X = ks.layers.Add()([X, X_shortcut])
X = ks.layers.Activation('relu')(X)
return X
def convolutional_block(X, f, filters, stage, block, s=2, seed=None):
"""
Implementation of the convolutional block.
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
stage -- integer, used to name the layers, depending on their position in the network
block -- string/character, used to name the layers, depending on their position in the network
s -- Integer, specifying the stride to be used
Returns:
X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
"""
# defining name basis
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
# Retrieve Filters
F1, F2, F3 = filters
# Save the input value
X_shortcut = X
# First component of main path
X = ks.layers.Conv2D(filters=F1,
kernel_size=(1, 1),
strides=(s, s),
padding='valid',
name=conv_name_base + '2a',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
X = ks.layers.Activation('relu')(X)
# Second component of main path
X = ks.layers.Conv2D(filters=F2,
kernel_size=(f, f),
strides=(1, 1),
padding='same',
name=conv_name_base + '2b',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
X = ks.layers.Activation('relu')(X)
# Third component of main path
X = ks.layers.Conv2D(filters=F3,
kernel_size=(1, 1),
strides=(1, 1),
padding='valid',
name=conv_name_base + '2c',
kernel_initializer=ks.initializers.glorot_uniform(seed))(X)
X = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '2c')(X)
# Shortcut path
X_shortcut = ks.layers.Conv2D(filters=F3,
kernel_size=(1, 1),
strides=(s, s),
padding='valid',
name=conv_name_base + '1',
kernel_initializer=ks.initializers.glorot_uniform())(X_shortcut)
X_shortcut = ks.layers.BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut)
# Final step: Add shortcut value to main path, and pass it through a RELU activation
X = ks.layers.Add()([X, X_shortcut])
X = ks.layers.Activation('relu')(X)
return X
def potato_peal(input_shape=(75, 75, 2), classes=2):
"""
The potato peal residual network. A small network with
Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in Keras
"""
# Define the input as a tensor with shape input_shape
X_input = ks.layers.Input(input_shape)
# Zero-Padding
X = ks.layers.ZeroPadding2D((3, 3))(X_input)
# Stage 1
X = ks.layers.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', kernel_initializer=ks.initializers.glorot_uniform())(
X)
X = ks.layers.BatchNormalization(axis=3, name='bn_conv1')(X)
X = ks.layers.Activation('relu')(X)
X = ks.layers.MaxPooling2D((3, 3), strides=(2, 2))(X)
# Stage 2
X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block='a', s=1)
X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')
# Stage 3
X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2)
X = identity_block(X, 3, [128, 128, 512], stage=3, block='b')
X = identity_block(X, 3, [128, 128, 512], stage=3, block='c')
X = identity_block(X, 3, [128, 128, 512], stage=3, block='d')
# Stage 4
X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2)
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')
X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')
# Stage 5
X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2)
X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')
# Avgpool
X = ks.layers.AveragePooling2D(pool_size=(2, 2), padding='same')(X)
# output layer
X = ks.layers.Flatten()(X)
X = ks.layers.Dense(classes, activation='softmax', name='fc' + str(classes),
kernel_initializer=ks.initializers.glorot_uniform())(X)
# Create model
model = ks.models.Model(inputs=X_input, outputs=X, name='Potato_peal')
return model
if __name__ == "__main__":
print("loading the data")
x_train, y_train, x_test, y_test = load_data()
print("done!")
print("building model")
peal = potato_peal(input_shape=(75, 75, 2), classes=2)
peal.compile(optimizer='adam', loss="categorical_crossentropy", metrics=['accuracy'])
print("done!")
tests = []
for i in range(1):
peal.fit(x_train, y_train, 32, 1)
scores = peal.evaluate(x_test, y_test, verbose=1)
tests.append(scores)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
pprint.pprint(tests)
print("saving model")
peal.save("models/potato_peal")
print("clearing variable")
x_train, y_train, x_test, y_test = None, None, None, None
print("loading test data")
images, id = load_test_data()
print("predicting")
predictions = peal.predict(images)
print("creating csv")
with open("peal.csv", 'w') as f:
string = 'id,is_iceberg\n'
f.write(string)
for i in range(len(predictions)):
string = id[i] + "," + str(predictions[i][0]) + "\n"
f.write(string)