-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
99 lines (92 loc) · 5.42 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
import tensorflow as tf
def model(input,num_classes):
batch_size=input.shape[0]
with tf.variable_scope("conv1") as scope:
weights = tf.get_variable('weight',
shape=[3, 3, 3, 64],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[64],
dtype=tf.float32,
initializer=tf.constant_initializer(0))
conv = tf.nn.conv2d(input, weights, strides=[1, 2, 2, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv1_out = tf.nn.relu(pre_activation,scope.name)
with tf.variable_scope('pooling1') as scope:
pool1_out = tf.nn.max_pool(conv1_out, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='SAME', name='pooling')
with tf.variable_scope("conv2") as scope:
weights = tf.get_variable('weight',
shape=[3, 3, 64, 128],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[128],
dtype=tf.float32,
initializer=tf.constant_initializer(0))
conv = tf.nn.conv2d(pool1_out, weights, strides=[1, 2, 2, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv2_out = tf.nn.relu(pre_activation,scope.name)
with tf.variable_scope('pooling2') as scope:
pool2_out = tf.nn.max_pool(conv2_out, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='SAME', name='pooling')
with tf.variable_scope("conv3") as scope:
weights = tf.get_variable('weight',
shape=[3, 3, 128, 512],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[512],
dtype=tf.float32,
initializer=tf.constant_initializer(0))
conv = tf.nn.conv2d(pool2_out, weights, strides=[1, 2, 2, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv3_out = tf.nn.relu(pre_activation,scope.name)
with tf.variable_scope('pooling2') as scope:
pool3_out = tf.nn.max_pool(conv3_out, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='SAME', name='pooling')
reshape = tf.reshape(pool3_out, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
with tf.variable_scope('fc1') as scope:
weights = tf.get_variable('weights',
shape=[dim, 1000],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[1000],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
fc1_out = tf.nn.sigmoid(tf.matmul(reshape, weights) + biases, name='local4')
with tf.variable_scope('fc2') as scope:
weights = tf.get_variable('weights',
shape=[1000, 500],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[500],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
fc2_out = tf.nn.sigmoid(tf.matmul(fc1_out, weights) + biases, name='local4')
with tf.variable_scope('fc3') as scope:
weights = tf.get_variable('weights',
shape=[500, 100],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[100],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
fc3_out = tf.nn.sigmoid(tf.matmul(fc2_out, weights) + biases, name='local4')
with tf.variable_scope('fc4') as scope:
weights = tf.get_variable('weights',
shape=[100, num_classes],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable('biases',
shape=[num_classes],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
fc4_out = tf.nn.sigmoid(tf.matmul(fc3_out, weights) + biases, name='local4')
output=fc4_out
return output