-
Notifications
You must be signed in to change notification settings - Fork 47
/
model.py
108 lines (87 loc) · 5.46 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
import tensorflow as tf
from utils.model import *
class ImageSearchModel(object):
def __init__(self,
learning_rate,
image_size,
number_of_classes=10):
'''
Defines 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 a dataset.
'''
tf.reset_default_graph()
#model inputs
self.inputs, self.targets, self.dropout_rate = model_inputs(image_size)
normalized_images = tf.layers.batch_normalization(self.inputs)
#conv_1 block
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_2 block
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_3 block
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_4 block
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 = tf.layers.flatten(conv_block_4)
#1st dense block
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)
#2nd dense block
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)
#3rd dense block
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)
#4th dense block
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)
#output layer
logits = tf.layers.dense(inputs=dense_block_4,
units=number_of_classes,
activation=None)
self.predictions = tf.nn.softmax(logits)
self.loss, self.optimizer = opt_loss(logits=logits,
targets=self.targets,
learning_rate=learning_rate)