-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
133 lines (88 loc) · 5.8 KB
/
model.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
import tensorflow as tf
import tensorflow.compat.v1 as tf1
from utils.model import *
class ImageSearchModel(object):
def __init__(self ,
learning_rate ,
image_size ,
number_of_classes = 10 ):
'''
Define CNN Model
:param learning_rate : learning_rate
:param image_size : tuple , (height , width) of an image
:param number_of_classes : integer , number of classes in dataset
'''
#tf.reset_default_graph()
tf.compat.v1.reset_default_graph()
#ops.reset_default_graph()
self.inputs , self.targets , self.dropout_rate = model_inputs(image_size)
normalized_images = tf1.layers.batch_normalization(self.inputs)
#conv block 1
conv_block_1 , self.conv_1_features = conv_block(inputs = normalized_images,
number_of_filters = 64 ,
kernel_size = (3,3) ,
strides=(1,1) ,
padding = 'SAME' ,
activation = tf.nn.relu ,
max_pool = True ,
batch_norm = True)
#conv block 2
conv_block_2 , self.conv_2_features = conv_block(inputs = conv_block_1 ,
number_of_filters = 128 ,
kernel_size = (3,3) ,
strides=(1,1) ,
padding = 'SAME' ,
activation = tf.nn.relu ,
max_pool = True ,
batch_norm = True)
#conv block 3
conv_block_3 , self.conv_3_features = conv_block(inputs = conv_block_2 ,
number_of_filters = 256 ,
kernel_size = (5,5) ,
strides=(1,1) ,
padding = 'SAME' ,
activation = tf.nn.relu ,
max_pool = True ,
batch_norm = True)
#conv block 4
conv_block_4 , self.conv_4_features = conv_block(inputs = conv_block_3 ,
number_of_filters = 512 ,
kernel_size = (5,5) ,
strides=(1,1) ,
padding = 'SAME' ,
activation = tf.nn.relu ,
max_pool = True ,
batch_norm = True)
#flattening
flat_layer = tf1.layers.flatten(conv_block_4)
# Dense block 1
dense_block_1 , dense_1_features = dense_block(inputs = flat_layer ,
units = 128 ,
activation = tf.nn.relu ,
dropout_rate = self.dropout_rate ,
batch_norm = True )
# Dense block 2
dense_block_2 , self.dense_2_features = dense_block(inputs = dense_block_1 ,
units = 256 ,
activation = tf.nn.relu ,
dropout_rate = self.dropout_rate ,
batch_norm = True )
# Dense block 3
dense_block_3 , self.dense_3_features = dense_block(inputs = dense_block_2 ,
units = 512 ,
activation = tf.nn.relu ,
dropout_rate = self.dropout_rate ,
batch_norm = True )
# Dense block 4
dense_block_4 , self.dense_4_features = dense_block(inputs = dense_block_3 ,
units = 1024 ,
activation = tf.nn.relu ,
dropout_rate = self.dropout_rate ,
batch_norm = True )
logits = tf1.layers.dense(inputs = dense_block_4 ,
units = number_of_classes ,
activation = None )
self.predictions = tf.nn.softmax(logits)
self.loss , self.opt = opt_loss(logits = logits ,
targets = self.targets ,
learning_rate = learning_rate)