-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKerasBaseLineImplementation.py
136 lines (113 loc) · 5.1 KB
/
KerasBaseLineImplementation.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
import pandas as pd
import numpy as np
import random
import cv2 # Used to manipulated the images
from sklearn.cross_validation import train_test_split
np.random.seed(1337) # The seed I used - pick your own or comment out for a random seed. A constant seed allows for better comparisons though
import matplotlib as plt
import keras.preprocessing.image as prep
# # Import Keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten, Activation
from keras.layers.convolutional import Convolution2D as Conv2D, MaxPooling2D
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
train = pd.read_json("train.json")
test = pd.read_json("test.json")
X_band_1=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in train["band_1"]])
X_band_2=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in train["band_2"]])
target_train=train['is_iceberg']
X_train = np.concatenate([X_band_1[:, :, :, np.newaxis], X_band_2[:, :, :, np.newaxis],((X_band_1+X_band_2)/2)[:, :, :, np.newaxis]], axis=-1)
X_train_cv, X_valid, y_train_cv, y_valid = train_test_split(X_train, target_train, random_state=1, train_size=0.75)
# image generator generating image tensors from the data
gen = prep.ImageDataGenerator(horizontal_flip=True,
vertical_flip=True,
width_shift_range=2,
height_shift_range=2,
channel_shift_range=0,
zoom_range=0.2,
rotation_range=10)
gen_op = gen.flow(x=X_train_cv, y=y_train_cv,batch_size=32, seed=10)
gen_val = gen.flow(x=X_valid, y=y_valid,batch_size=32, seed=10)
def getModel():
#Building the model
gmodel=Sequential()
#Conv Layer 1
gmodel.add(Conv2D(64, kernel_size=(3, 3),activation='relu', input_shape=(75, 75, 3)))
# gmodel.add(BatchNormalization())
gmodel.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
gmodel.add(Dropout(0.2))
#Conv Layer 2
gmodel.add(Conv2D(128, kernel_size=(3, 3), activation='relu' ))
# gmodel.add(BatchNormalization())
gmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
gmodel.add(Dropout(0.2))
#Conv Layer 3
gmodel.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
# gmodel.add(BatchNormalization())
gmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
gmodel.add(Dropout(0.2))
#Conv Layer 4
gmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
# gmodel.add(BatchNormalization())
gmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
gmodel.add(Dropout(0.2))
#Flatten the data for upcoming dense layers
gmodel.add(Flatten())
#Dense Layers
gmodel.add(Dense(512))
gmodel.add(Activation('relu'))
gmodel.add(Dropout(0.2))
#Dense Layer 2
gmodel.add(Dense(256))
gmodel.add(Activation('relu'))
gmodel.add(Dropout(0.2))
#Sigmoid Layer
gmodel.add(Dense(1))
gmodel.add(Activation('sigmoid'))
mypotim=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
gmodel.compile(loss='binary_crossentropy',
optimizer=mypotim,
metrics=['accuracy'])
gmodel.summary()
return gmodel
def get_callbacks(filepath):
es = EarlyStopping('val_loss', patience=10, mode="min")
msave = ModelCheckpoint(filepath, save_best_only=True)
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=7,
verbose=0,
epsilon=1e-4,
mode='min')
tnb = TensorBoard(
log_dir="logs/run_b",
histogram_freq=2,
write_graph=True,
write_images=True
)
return [es, msave, reduce_lr_loss, tnb]
file_path = ".model_weights.hdf5"
callbacks = get_callbacks(filepath=file_path)
gmodel=getModel()
gmodel.fit_generator(generator=gen_op,
epochs=20,
callbacks=callbacks,
validation_data=(X_valid,y_valid))
gmodel.load_weights(filepath=file_path)
score = gmodel.evaluate(X_valid, y_valid, verbose=1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
X_band_test_1=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in test["band_1"]])
X_band_test_2=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in test["band_2"]])
X_test = np.concatenate([X_band_test_1[:, :, :, np.newaxis]
, X_band_test_2[:, :, :, np.newaxis]
, ((X_band_test_1+X_band_test_2)/2)[:, :, :, np.newaxis]], axis=-1)
predicted_test=gmodel.predict_classes(X_test)
submission = pd.DataFrame()
submission['id']=test['id']
submission['is_iceberg']=predicted_test.reshape((predicted_test.shape[0]))
submission.to_csv('subkeras.csv', index=False)
from keras.utils import plot_model
plot_model(gmodel, to_file='model1.png')