-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcnn.py
135 lines (121 loc) · 6.07 KB
/
mcnn.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
from ops import *
import tensorflow as tf
from tensorflow.contrib import slim
def multi_column_cnn(inputs, scope='mcnn'):
with tf.variable_scope(scope):
with tf.variable_scope('large'):
large_column = slim.conv2d(inputs, 16, [9, 9], padding='SAME', scope='conv1')
large_column = slim.conv2d(large_column, 32, [7, 7], padding='SAME', scope='conv2')
large_column = slim.max_pool2d(large_column, [2, 2], 2, scope='pool1')
large_column = slim.conv2d(large_column, 16, [7, 7], padding='SAME', scope='conv3')
large_column = slim.max_pool2d(large_column, [2, 2], 2, scope='pool2')
large_column = slim.conv2d(large_column, 8, [7, 7], padding='SAME', scope='conv4')
with tf.variable_scope('medium'):
medium_column = slim.conv2d(inputs, 20, [7, 7], padding='SAME', scope='conv1')
medium_column = slim.conv2d(medium_column, 40, [5, 5], padding='SAME', scope='conv2')
medium_column = slim.max_pool2d(medium_column, [2, 2], 2, scope='pool1')
medium_column = slim.conv2d(medium_column, 20, [5, 5], padding='SAME', scope='conv3')
medium_column = slim.max_pool2d(medium_column, [2, 2], 2, scope='pool2')
medium_column = slim.conv2d(medium_column, 10, [5, 5], padding='SAME', scope='conv4')
with tf.variable_scope('small'):
small_column = slim.conv2d(inputs, 24, [5, 5], padding='SAME', scope='conv1')
small_column = slim.conv2d(small_column, 48, [3, 3], padding='SAME', scope='conv2')
small_column = slim.max_pool2d(small_column, [2, 2], 2, scope='pool1')
small_column = slim.conv2d(small_column, 24, [3, 3], padding='SAME', scope='conv3')
small_column = slim.max_pool2d(small_column, [2, 2], 2, scope='pool2')
small_column = slim.conv2d(small_column, 12, [3, 3], padding='SAME', scope='conv4')
net = tf.concat([large_column, medium_column, small_column], axis=3)
dmp = slim.conv2d(net, 1, [1, 1], padding='SAME', scope='dmp_conv1')
return dmp
# network test
'''
import numpy as np
if __name__ == "__main__":
x = tf.placeholder(tf.float32, [1, 700, 800, 1])
net = build(x)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
d_map = sess.run(net,feed_dict={x:255*np.ones(shape=(1,700,800,1), dtype=np.float32)})
prediction = np.asarray(d_map)
print(prediction.shape)
prediction = np.squeeze(prediction, axis=0)
prediction = np.squeeze(prediction, axis=2)
'''
'''
def net9(x):
# w x h x 3 -> w x h x 16
net = conv(x, 16, kernel=9, stride=1, padding='same', scope='conv_1_9x9')
net = relu(net)
# w x h x 16 -> w/2 x h/2 x 16
#net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_9x9')
# w/2 x h/2 x 16 -> w/2 x h/2 x 32
net = conv(net,32, kernel=7, stride=1, padding='same', scope='conv_2_9x9')
net = relu(net)
# w/2 x h/2 x 32 -> w/4 x h/4 x 32
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_2_9x9')
# w/4 x h/4 x 32 -> w/4 x h/4 x 16
net = conv(net, 16, kernel=7, stride=1, padding='same',scope='conv_3_9x9')
net = relu(net)
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_9x9')
# w/4 x h/4 x 32 -> w/4 x h/4 x 8
net = conv(net, 8, kernel=7, stride=1, padding='same',scope='conv_4_9x9')
net = relu(net)
return net
def net7(x):
# w x h x 3 -> w x h x 20
net = conv(x, 20, kernel=7, stride=1, padding='same', scope='conv_1_7x7')
net = relu(net)
# w x h x 20 -> w/2 x h/2 x 20
#net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_7x7')
# w/2 x h/2 x 20 -> w/2 x h/2 x 40
net = conv(net, 40, kernel=5, stride=1, padding='same', scope='conv_2_7x7')
net = relu(net)
# w/2 x h/2 x 40 -> w/4 x h/4 x 40
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_2_7x7')
# w/4 x h/4 x 40 -> w/4 x h/4 x 20
net = conv(net, 20, kernel=5, stride=1, padding='same', scope='conv_3_7x7')
net = relu(net)
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_7x7')
# w/4 x h/4 x 20 -> w/4 x h/4 x 10
net = conv(net, 10, kernel=5, stride=1, padding='same', scope='conv_4_7x7')
net = relu(net)
return net
def net5(x):
# w x h x 3 -> w x h x 24
net = conv(x, 24, kernel=5, stride=1, padding='same', scope='conv_1_5x5')
net = relu(net)
# w x h x 24 -> w/2 x h/2 x 24
# net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_5x5')
# w/2 x h/2 x 24 -> w/2 x h/2 x 48
net = conv(net, 48, kernel=3, stride=1, padding='same', scope='conv_2_5x5')
net = relu(net)
# w/2 x h/2 x 48 -> w/4 x h/4 x 48
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_2_5x5')
# w/4 x h/4 x 48 -> w/4 x h/4 x 24
net = conv(net, 24, kernel=3, stride=1, padding='same', scope='conv_3_5x5')
net = relu(net)
net = pool(net, kh=2, kw=2, stride=2, padding='same', scope='pool_1_5x5')
# w/4 x h/4 x 24 -> w/4 x h/4 x 12
net = conv(net, 12, kernel=3, stride=1, padding='same', scope='conv_4_5x5')
net = relu(net)
return net
def merge_net(net5,net7,net9):
net = tf.concat([net5,net7,net9],axis=3)
net = conv(net,1,kernel=1,stride=1,padding='same',scope='conv_merge')
return net
def build(input_tensor, norm = False):
"""
Builds the entire multi column cnn with 3 shallow nets with different input kernels and one fusing layer.
:param input_tensor: Input tensor image to the network.
:return: estimated density map tensor.
"""
tf.summary.image('input', input_tensor, 1)
if norm:
input_tensor = tf.cast(input_tensor, tf.float32) * (1. / 255) - 0.5
net_1_output = net9(input_tensor) # For column 1 with large receptive fields
net_2_output = net7(input_tensor) # For column 2 with medium receptive fields
net_3_output = net5(input_tensor) # For column 3 with small receptive fields
full_net = merge_net(net_1_output, net_2_output, net_3_output) # Fusing all the column output features
return full_net
'''