-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
343 lines (282 loc) · 13.9 KB
/
model.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# model.py
# Written By Connor Cozad
#
# Purpose of this file:
# This file builds and trains the neural network with the data processed by assemble.py. This file also validates the
# model, telling the user how accurate it is.
#
# Outline of this file:
# - Reads and augments images of hurricanes and their labels for use in convolutional neural network (CNN)
# - Builds a CNN and trains it on those images and labels
# - Validates the model using k-fold validation
# - Prints MAE and saves two graphs that show additional details about the model's error
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
import gc
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
from keras import models
from keras import layers
from keras import metrics
from keras.callbacks import EarlyStopping
def read_and_prepare_data(validation_mode, k=5, augment=True):
if validation_mode == 'k_fold':
# Read in data from files
images = np.load('images.npy')
labels = np.load('labels.npy')
# Split the image and label datasets into k number of subsets
folded_images = []
folded_labels = []
for i in range(k):
start = int((i / k) * len(images))
end = int(((i + 1) / k) * len(images))
folded_images.append(images[start:end])
folded_labels.append(labels[start:end])
# Generate augmented images for each fold
folded_augmented_images = []
folded_augmented_labels = []
for i in range(k):
if augment:
print('\nAugmenting Fold ' + str(i + 1) + ' of ' + str(k))
augmented_images, augmented_labels = augment_images(folded_images[i], folded_labels[i])
folded_augmented_images.append(augmented_images)
folded_augmented_labels.append(augmented_labels)
# Combine the folds into sets for each iteration of the model and standardize the data
train_images = []
train_labels = []
test_images = []
test_labels = []
for i in range(k):
train_images.append(np.concatenate(folded_images[:i] + folded_images[(i+1):]))
train_labels.append(np.concatenate(folded_labels[:i] + folded_labels[(i+1):]))
if augment:
train_images[i] = np.concatenate(([train_images[i]] + folded_augmented_images[:i] + folded_augmented_images[(i + 1):]))
train_labels[i] = np.concatenate(([train_labels[i]] + folded_augmented_labels[:i] + folded_augmented_labels[(i + 1):]))
test_images.append(folded_images[i])
test_labels.append(folded_labels[i])
train_images[i], test_images[i] = standardize_data(train_images[i], test_images[i])
return train_images, train_labels, test_images, test_labels
def augment_images(images, labels):
# Create generators to augment images
from keras.preprocessing import image
flip_generator = image.ImageDataGenerator(
horizontal_flip=True,
vertical_flip=True
)
rotate_generator = image.ImageDataGenerator(
rotation_range=360,
fill_mode='nearest'
)
# Accumulate augmented images and labels
augmented_images = []
augmented_labels = []
# Loop each images in the set to augment
for i in range(len(images)):
# Reshape image for generator
image = np.reshape(images[i], (1, images[i].shape[0], images[i].shape[1], 1))
label = labels[i]
# Reset the number of augmented images have been created to zero
num_new_images = 0
# Generate 2 new images if the image is of a tropical cyclone between 50 and 75 knots
if 50 < label < 75:
for batch in flip_generator.flow(image, batch_size=1):
gc.collect()
new_image = np.reshape(batch[0], (batch[0].shape[0], batch[0].shape[1], 1))
augmented_images.append(new_image)
augmented_labels.append(label)
num_new_images += 1
if num_new_images == 2:
break
# Generate 6 new images if the image is of a tropical cyclone between 75 and 100 knots
elif 75 < label < 100:
for batch in rotate_generator.flow(image, batch_size=1):
gc.collect()
new_image = np.reshape(batch[0], (batch[0].shape[0], batch[0].shape[1], 1))
augmented_images.append(new_image)
augmented_labels.append(label)
num_new_images += 1
if num_new_images == 6:
break
# Generate 12 new images if the image is of a tropical cyclone greater than or equal to 100 knots
elif 100 <= label:
for batch in rotate_generator.flow(image, batch_size=1):
gc.collect()
new_image = np.reshape(batch[0], (batch[0].shape[0], batch[0].shape[1], 1))
augmented_images.append(new_image)
augmented_labels.append(label)
num_new_images += 1
if num_new_images == 12:
break
print_progress('Augmenting Images', i + 1, len(images))
# Convert lists of images/labels into numpy arrays
augmented_images = np.array(augmented_images)
augmented_labels = np.array(augmented_labels)
return augmented_images, augmented_labels
def build_model():
# Build network architecture
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(50, 50, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation=None))
# Configure model optimization
model.compile(
optimizer='rmsprop',
loss='mse',
metrics=[metrics.MeanAbsoluteError(), metrics.RootMeanSquaredError()])
return model
def train_model(model, train_images, train_labels, test_images, test_labels, show_performance_by_epoch=False):
# Run model and get metrics for each epoch
performance_log = model.fit(
train_images,
train_labels,
callbacks=[EarlyStopping(monitor='val_mean_absolute_error', patience=5, restore_best_weights=True)],
epochs=100,
batch_size=64,
validation_data=(test_images, test_labels))
if show_performance_by_epoch:
performance_by_epoch(performance_log)
return model
def performance_by_epoch(performance_log):
# Get metrics for each epoch after model finishes training
train_loss = performance_log.history['loss']
test_loss = performance_log.history['val_loss']
train_mae = performance_log.history['mean_absolute_error']
test_mae = performance_log.history['val_mean_absolute_error']
epochs = range(1, len(train_loss) + 1)
# Build a dataframe storing epoch metrics
performance_df = pd.DataFrame(columns=['epoch', 'train_or_test', 'loss_or_mae', 'value'])
for i in range(len(train_loss)):
new_row = {'epoch': epochs[i], 'train_or_test': 'train', 'loss_or_mae': 'loss', 'value': train_loss[i]}
performance_df = performance_df.append(new_row, ignore_index=True)
new_row = {'epoch': epochs[i], 'train_or_test': 'test', 'loss_or_mae': 'loss', 'value': test_loss[i]}
performance_df = performance_df.append(new_row, ignore_index=True)
new_row = {'epoch': epochs[i], 'train_or_test': 'train', 'loss_or_mae': 'mae', 'value': train_mae[i]}
performance_df = performance_df.append(new_row, ignore_index=True)
new_row = {'epoch': epochs[i], 'train_or_test': 'test', 'loss_or_mae': 'mae', 'value': test_mae[i]}
performance_df = performance_df.append(new_row, ignore_index=True)
performance_df = performance_df.astype({'epoch': np.int64})
# Plot metrics on graph, fitted with exponential decay curves
lm = sns.lmplot(
x='epoch',
y='value',
data=performance_df,
row='loss_or_mae',
hue='train_or_test', # Note: If epoch = 1, this line causes an error. Make sure epoch >= 2
logx=True,
truncate=False,
sharey=False)
axes = lm.axes
max_mae = performance_df.loc[performance_df.loss_or_mae == 'mae']['value'].max()
min_mae = performance_df.loc[performance_df.loss_or_mae == 'mae']['value'].min()
axes[1, 0].set_ylim(min_mae - min_mae * 0.2, max_mae + max_mae * 0.2)
plt.show()
def generate_predictions(model, test_images, test_labels):
# Run validation data through model and print mean absolute error
raw_predictions = model.predict(test_images)
raw_predictions = raw_predictions.flatten()
# Build a dataframe storing data for each prediction made by the model
processed_predictions = pd.DataFrame(columns=['prediction', 'actual', 'abs_error', 'category'])
for i in range(len(raw_predictions)):
abs_error = abs(raw_predictions[i] - test_labels[i])
new_row = {
'prediction': raw_predictions[i],
'actual': test_labels[i],
'abs_error': abs_error,
'abs_error_squared': abs_error ** 2,
'category': category_of(test_labels[i])}
processed_predictions = processed_predictions.append(new_row, ignore_index=True)
print_progress('Processing Predictions', i + 1, len(raw_predictions))
return processed_predictions
def show_validation_results(predictions, show_plots=True, print_error=True):
print('\n\nRESULTS')
if print_error:
mae = predictions['abs_error'].mean()
print('\nMean Absolute Error: ' + str(round(float(mae), 2)) + ' knots')
rmse = predictions['abs_error_squared'].mean() ** 0.5
print('Root Mean Square Error: ' + str(round(float(rmse), 2)) + ' knots')
if show_plots:
# List of categories in order of ascending strength
categories = ['T. Depression', 'T. Storm', 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5']
# Show bar graph of median absolute error for each category
plt.figure(figsize=(10, 5), dpi=300)
sns.barplot(
x='category',
y='abs_error',
data=predictions,
estimator=np.median,
order=categories)
sns.despine()
plt.xlabel("Hurricane Strength")
plt.ylabel("Absolute Error")
plt.title("Median Absolute Error in Neural Network's Predictions By Category")
plt.savefig('median_abs_error_by_category.png')
print('Graph of median absolute error by category saved as median_abs_error_by_category.png')
plt.clf()
# Show density plot of error for each category
for category in categories:
num_samples_tested = len(predictions.loc[predictions.category == category]['abs_error'])
sns.distplot(
predictions.loc[predictions.category == category]['abs_error'],
label=category + ' (' + str(num_samples_tested) + ' samples tested)',
hist=False,
kde_kws={"shade": True})
sns.despine()
plt.xlabel("Absolute Error")
plt.title("Distribution of Absolute Error By Category")
plt.legend()
plt.xlim(0, None)
plt.ylim(0, None)
plt.savefig('error_dist_by_category.png')
print('Graph of error distribution by category saved as error_dist_by_category.png')
def standardize_data(train_images, test_images):
train_images[train_images < 0] = 0
test_images[test_images < 0] = 0
st_dev = np.std(train_images)
mean = np.mean(train_images)
train_images = np.divide(np.subtract(train_images, mean), st_dev)
test_images = np.divide(np.subtract(test_images, mean), st_dev)
return train_images, test_images
def print_progress(action, progress, total):
percent_progress = round((progress / total) * 100, 1)
print('\r' + action + '... ' + str(percent_progress) + '% (' + str(progress) + ' of ' + str(total) + ')', end='')
def category_of(wind_speed):
if wind_speed <= 33:
return 'T. Depression'
elif wind_speed <= 64:
return 'T. Storm'
elif wind_speed <= 83:
return 'Category 1'
elif wind_speed <= 95:
return 'Category 2'
elif wind_speed <= 113:
return 'Category 3'
elif wind_speed <= 134:
return 'Category 4'
else:
return 'Category 5'
if __name__ == "__main__":
# Specify whether the script should use Keras's ImageDataGenerator to augment the training dataset. Assigning
# this variable to True will improve accuracy, but will also increase execution time.
AUGMENT = True
# Specify how many folds in the k-fold validation process. Can be any integer greater than or equal to 2. Larger
# integers will increase execution time.
NUM_FOLDS = 5
train_images, train_labels, test_images, test_labels = read_and_prepare_data('k_fold', NUM_FOLDS, augment=AUGMENT)
model = build_model()
predictions = pd.DataFrame(columns=['prediction', 'actual', 'abs_error', 'category'])
for i in range(NUM_FOLDS):
print('\n\nTraining Fold ' + str(i + 1) + ' of ' + str(NUM_FOLDS) + '\n')
model = train_model(model, train_images[i], train_labels[i], test_images[i], test_labels[i])
kth_fold_predictions = generate_predictions(model, test_images[i], test_labels[i])
predictions = predictions.append(kth_fold_predictions, ignore_index=True)
show_validation_results(predictions)