-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN_cifar10.py
57 lines (42 loc) · 2.21 KB
/
CNN_cifar10.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
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras import layers, models, Sequential
class CNNCifar10_Model(object):
def __init__(self):
self.model = Sequential()
self.model.add(layers.Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(32,32,3)))
self.model.add(layers.BatchNormalization())
self.model.add(layers.Conv2D(32, (3,3), padding='same', activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.MaxPooling2D(pool_size=(2,2)))
self.model.add(layers.Dropout(0.3))
self.model.add(layers.Conv2D(64, (3,3), padding='same', activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.Conv2D(64, (3,3), padding='same', activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.MaxPooling2D(pool_size=(2,2)))
self.model.add(layers.Dropout(0.5))
self.model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.MaxPooling2D(pool_size=(2,2)))
self.model.add(layers.Dropout(0.5))
self.model.add(layers.Flatten())
self.model.add(layers.Dense(128, activation='relu'))
self.model.add(layers.BatchNormalization())
self.model.add(layers.Dropout(0.5))
self.model.add(layers.Dense(10, activation='softmax')) # num_classes = 10
self.model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
def train(self, x_train, y_train, epochs):
self.model.fit(x_train, y_train, epochs=epochs)
def evaluate(self, x_test, y_test):
return self.model.evaluate(x_test, y_test)
def get_history(self):
return self.model.history.history
def get_weights(self):
return self.model.get_weights()
def set_weights(self, weights):
self.model.set_weights(weights)
def get_layers(self):
return self.model.layers