-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
147 lines (139 loc) · 5.07 KB
/
CNN.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
import os
import tensorflow as tf
from tensorflow.keras import layers, initializers, regularizers
def CNN0(num_classes, l2_rate,
filter1, filter2, fc1, dropout_rate):
cnn = tf.keras.Sequential()
cnn.add(layers.Conv2D(
filters=filter1,
kernel_size=(3, 3),
activation='relu',
))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(
filters=filter2,
kernel_size=(3, 3),
activation='relu',
))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
cnn.add(layers.Dropout(dropout_rate))
cnn.add(layers.Dense(fc1,
activation='relu',
))
cnn.add(layers.Dense(num_classes))
return cnn
def CNN(num_classes, l2_rate,
filter1, filter2, fc1, dropout_rate):
cnn = tf.keras.Sequential()
cnn.add(layers.Conv2D(
filters=filter1,
kernel_size=(3, 3),
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(
filters=filter2,
kernel_size=(3, 3),
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
cnn.add(layers.Dropout(dropout_rate))
cnn.add(layers.Dense(fc1,
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
cnn.add(layers.Dense(num_classes))
if num_classes != 1:
cnn.add(layers.Softmax())
if dropout_rate != 0: # c
cnn.add(layers.Activation("sigmoid"))
return cnn
def CNN_c(num_classes, l2_rate,
filter1, filter2, fc1, dropout_rate):
cnn = tf.keras.Sequential()
cnn.add(layers.Conv2D(
filters=filter1,
kernel_size=(3, 3),
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
# model.add(layers.BatchNormalization())
# model.add(layers.Activation('relu'))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Conv2D(
filters=filter2,
kernel_size=(3, 3),
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
# model.add(layers.BatchNormalization())
# model.add(layers.Activation('relu'))
cnn.add(layers.MaxPooling2D((2, 2)))
cnn.add(layers.Flatten())
cnn.add(layers.Dropout(dropout_rate))
cnn.add(layers.Dense(fc1,
activation='relu',
kernel_initializer=initializers.he_normal(), # c
kernel_regularizer=regularizers.l2(l2_rate), # c
))
cnn.add(layers.Dropout(dropout_rate))
cnn.add(layers.Dense(num_classes,
activation='sigmoid'))
return cnn
# class CNN(tf.keras.Model):
# def __init__(self, num_classes, l2_rate,
# filter1, filter2, fc1, dropout_rate):
# super().__init__()
# self.num_classes = num_classes
# self.l2_rate = l2_rate
# self.filter1 = filter1
# self.filter2 = filter2
# self.fc1 = fc1
# self.dropout_rate = dropout_rate
# self.conv1 = layers.Conv2D(
# filters=self.filter1,
# kernel_size=(3, 3),
# activation='relu',
# # kernel_initializer=initializers.he_normal(),
# # kernel_regularizer=regularizers.l2(l2_rate),
# )
# self.pool1 = layers.MaxPooling2D((2, 2))
# self.conv2 = layers.Conv2D(
# filters=self.filter2,
# kernel_size=(3, 3),
# activation='relu',
# # kernel_initializer=initializers.he_normal(),
# # kernel_regularizer=regularizers.l2(l2_rate),
# )
# self.pool2 = layers.MaxPooling2D((2, 2))
# self.flatten = layers.Flatten()
# self.dropout = layers.Dropout(self.dropout_rate)
# self.dense1 = layers.Dense(self.fc1,
# activation='relu',
# # kernel_initializer=initializers.he_normal(),
# # kernel_regularizer=regularizers.l2(l2_rate),
# )
# self.dense2 = layers.Dense(self.num_classes)
# self.dense3 = layers.Activation('softmax')
# def call(self, x, training=False):
# x = tf.reshape(x, [-1, 28, 28, 1])
# conv1 = self.conv1(x)
# pool1 = self.pool1(conv1)
# conv2 = self.conv2(pool1)
# pool2 = self.pool2(conv2)
# flatten = self.flatten(pool2)
# dropout = self.dropout(flatten)
# dense1 = self.dense1(dropout)
# output = self.dense2(dense1)
# if self.num_classes != 1:
# output = self.dense3(output)
# return output