-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_keras.py
132 lines (82 loc) · 4.86 KB
/
cnn_keras.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
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dropout
from keras import Sequential
import keras
class LeNet5:
def __init__(self, input_size=(32, 32, 3), classes=2, learning_rate=1e-3):
self.model = Sequential()
self.model.add(Conv2D(6, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Conv2D(16, (5, 5), activation='relu'))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Flatten())
self.model.add(Dense(120, activation='relu'))
self.model.add(Dense(84, activation='relu'))
self.model.add(Dense(classes, activation='softmax'))
self.model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.SGD(lr=learning_rate),
metrics=['accuracy'])
def fit(self, data, targets, batch_size=None):
self.model.fit(data, targets, batch_size=batch_size, epochs=1, verbose=0)
def accuracy(self, data, targets):
return self.model.test_on_batch(data, targets)[1]
def loss(self, data, targets):
return self.model.test_on_batch(data, targets)[0]
class AlexNet:
def __init__(self, input_size=(227, 227, 3), classes=1000, learning_rate=1e-3):
self.model = Sequential()
self.model.add(Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
self.model.add(Conv2D(256, (5, 5), strides=(1, 1), padding=2, activation='relu'))
self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
self.model.add(Conv2D(384, (3, 3), strides=(1, 1), padding=1, activation='relu'))
self.model.add(Conv2D(384, (3, 3), strides=(1, 1), padding=1, activation='relu'))
self.model.add(Conv2D(256, (3, 3), strides=(1, 1), padding=1, activation='relu'))
self.model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
self.model.add(Flatten())
self.model.add(Dropout(rate=0.5))
self.model.add(Dense(4096, activation='relu'))
self.model.add(Dropout(rate=0.5))
self.model.add(Dense(4096, activation='relu'))
self.model.add(Dense(classes, activation='relu'))
self.model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop(lr=learning_rate, decay=0.0005),
metrics=['accuracy'])
def fit(self, data, targets, batch_size=None):
self.model.fit(data, targets, batch_size=batch_size, epochs=1, verbose=0)
def accuracy(self, data, targets):
return self.model.test_on_batch(data, targets)[1]
def loss(self, data, targets):
return self.model.test_on_batch(data, targets)[0]
class VGG19:
def __init__(self, input_size=(224, 224, 3), classes=1000, learning_rate=1e-3):
self.model = Sequential()
self.model.add(Conv2D(64, (3, 3), activation='relu', input_shape=input_size))
self.model.add(Conv2D(64, (3, 3), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Conv2D(128, (3, 3), activation='relu', input_shape=input_size))
self.model.add(Conv2D(128, (3, 3), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Conv2D(256, (3, 3), activation='relu', input_shape=input_size))
self.model.add(Conv2D(256, (3, 3), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Conv2D(512, (3, 3), activation='relu', input_shape=input_size))
self.model.add(Conv2D(512, (3, 3), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Conv2D(512, (3, 3), activation='relu', input_shape=input_size))
self.model.add(Conv2D(512, (3, 3), activation='relu', input_shape=input_size))
self.model.add(MaxPooling2D(pool_size=(2, 2)))
self.model.add(Dense(4096, activation='relu'))
self.model.add(Dense(4096, activation='relu'))
self.model.add(Dense(classes, activation='relu'))
self.model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop(lr=learning_rate, decay=0.0005),
metrics=['accuracy'])
def fit(self, data, targets, batch_size=None):
self.model.fit(data, targets, batch_size=batch_size, epochs=1, verbose=0)
def accuracy(self, data, targets):
return self.model.test_on_batch(data, targets)[1]
def loss(self, data, targets):
return self.model.test_on_batch(data, targets)[0]