-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResNet.py
154 lines (123 loc) · 6.28 KB
/
ResNet.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 12 01:09:17 2016
@author: stephen
"""
from __future__ import print_function
from keras.models import Model
from keras.layers import Input, Dense, merge, Activation
from keras.utils import np_utils
import numpy as np
import keras
from keras.callbacks import ReduceLROnPlateau
np.random.seed(813306)
def build_resnet(input_shape, n_feature_maps, nb_classes):
print ('build conv_x')
x = Input(shape=(input_shape))
conv_x = keras.layers.normalization.BatchNormalization()(x)
conv_x = keras.layers.Conv2D(n_feature_maps, 8, 1, border_mode='same')(conv_x)
conv_x = keras.layers.normalization.BatchNormalization()(conv_x)
conv_x = Activation('relu')(conv_x)
print ('build conv_y')
conv_y = keras.layers.Conv2D(n_feature_maps, 5, 1, border_mode='same')(conv_x)
conv_y = keras.layers.normalization.BatchNormalization()(conv_y)
conv_y = Activation('relu')(conv_y)
print ('build conv_z')
conv_z = keras.layers.Conv2D(n_feature_maps, 3, 1, border_mode='same')(conv_y)
conv_z = keras.layers.normalization.BatchNormalization()(conv_z)
is_expand_channels = not (input_shape[-1] == n_feature_maps)
if is_expand_channels:
shortcut_y = keras.layers.Conv2D(n_feature_maps, 1, 1,border_mode='same')(x)
shortcut_y = keras.layers.normalization.BatchNormalization()(shortcut_y)
else:
shortcut_y = keras.layers.normalization.BatchNormalization()(x)
print ('Merging skip connection')
y = merge([shortcut_y, conv_z], mode='sum')
y = Activation('relu')(y)
print ('build conv_x')
x1 = y
conv_x = keras.layers.Conv2D(n_feature_maps*2, 8, 1, border_mode='same')(x1)
conv_x = keras.layers.normalization.BatchNormalization()(conv_x)
conv_x = Activation('relu')(conv_x)
print ('build conv_y')
conv_y = keras.layers.Conv2D(n_feature_maps*2, 5, 1, border_mode='same')(conv_x)
conv_y = keras.layers.normalization.BatchNormalization()(conv_y)
conv_y = Activation('relu')(conv_y)
print ('build conv_z')
conv_z = keras.layers.Conv2D(n_feature_maps*2, 3, 1, border_mode='same')(conv_y)
conv_z = keras.layers.normalization.BatchNormalization()(conv_z)
is_expand_channels = not (input_shape[-1] == n_feature_maps*2)
if is_expand_channels:
shortcut_y = keras.layers.Conv2D(n_feature_maps*2, 1, 1,border_mode='same')(x1)
shortcut_y = keras.layers.normalization.BatchNormalization()(shortcut_y)
else:
shortcut_y = keras.layers.normalization.BatchNormalization()(x1)
print ('Merging skip connection')
y = merge([shortcut_y, conv_z], mode='sum')
y = Activation('relu')(y)
print ('build conv_x')
x1 = y
conv_x = keras.layers.Conv2D(n_feature_maps*2, 8, 1, border_mode='same')(x1)
conv_x = keras.layers.normalization.BatchNormalization()(conv_x)
conv_x = Activation('relu')(conv_x)
print ('build conv_y')
conv_y = keras.layers.Conv2D(n_feature_maps*2, 5, 1, border_mode='same')(conv_x)
conv_y = keras.layers.normalization.BatchNormalization()(conv_y)
conv_y = Activation('relu')(conv_y)
print ('build conv_z')
conv_z = keras.layers.Conv2D(n_feature_maps*2, 3, 1, border_mode='same')(conv_y)
conv_z = keras.layers.normalization.BatchNormalization()(conv_z)
is_expand_channels = not (input_shape[-1] == n_feature_maps*2)
if is_expand_channels:
shortcut_y = keras.layers.Conv2D(n_feature_maps*2, 1, 1,border_mode='same')(x1)
shortcut_y = keras.layers.normalization.BatchNormalization()(shortcut_y)
else:
shortcut_y = keras.layers.normalization.BatchNormalization()(x1)
print ('Merging skip connection')
y = merge([shortcut_y, conv_z], mode='sum')
y = Activation('relu')(y)
full = keras.layers.pooling.GlobalAveragePooling2D()(y)
out = Dense(nb_classes, activation='softmax')(full)
print (' -- model was built.')
return x, out
def readucr(filename):
data = np.loadtxt(filename, delimiter = ',')
Y = data[:,0]
X = data[:,1:]
return X, Y
nb_epochs = 1500
#flist = ['Adiac', 'Beef', 'CBF', 'ChlorineConcentration', 'CinC_ECG_torso', 'Coffee', 'Cricket_X', 'Cricket_Y', 'Cricket_Z',
#'DiatomSizeReduction', 'ECGFiveDays', 'FaceAll', 'FaceFour', 'FacesUCR', '50words', 'FISH', 'Gun_Point', 'Haptics',
#'InlineSkate', 'ItalyPowerDemand', 'Lighting2', 'Lighting7', 'MALLAT', 'MedicalImages', 'MoteStrain', 'NonInvasiveFatalECG_Thorax1',
#'NonInvasiveFatalECG_Thorax2', 'OliveOil', 'OSULeaf', 'SonyAIBORobotSurface', 'SonyAIBORobotSurfaceII', 'StarLightCurves', 'SwedishLeaf', 'Symbols',
#'synthetic_control', 'Trace', 'TwoLeadECG', 'Two_Patterns', 'uWaveGestureLibrary_X', 'uWaveGestureLibrary_Y', 'uWaveGestureLibrary_Z', 'wafer', 'WordsSynonyms', 'yoga']
flist = ['Adiac']
for each in flist:
fname = each
x_train, y_train = readucr(fname+'/'+fname+'_TRAIN')
x_test, y_test = readucr(fname+'/'+fname+'_TEST')
nb_classes = len(np.unique(y_test))
batch_size = min(x_train.shape[0]/10, 16)
y_train = (y_train - y_train.min())/(y_train.max()-y_train.min())*(nb_classes-1)
y_test = (y_test - y_test.min())/(y_test.max()-y_test.min())*(nb_classes-1)
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
x_train_mean = x_train.mean()
x_train_std = x_train.std()
x_train = (x_train - x_train_mean)/(x_train_std)
x_test = (x_test - x_train_mean)/(x_train_std)
x_train = x_train.reshape(x_train.shape + (1,1,))
x_test = x_test.reshape(x_test.shape + (1,1,))
x , y = build_resnet(x_train.shape[1:], 64, nb_classes)
model = Model(input=x, output=y)
optimizer = keras.optimizers.Adam()
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.5,
patience=50, min_lr=0.0001)
hist = model.fit(x_train, Y_train, batch_size=batch_size, nb_epoch=nb_epochs,
verbose=1, validation_data=(x_test, Y_test), callbacks = [reduce_lr])
log = pd.DataFrame(hist.history)
print log.loc[log[‘loss'].idxmin]['loss’], log.loc[log[‘loss'].idxmin][‘val_acc’]