forked from MaybeShewill-CV/lanenet-lane-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcn_decoder.py
120 lines (103 loc) · 4.86 KB
/
fcn_decoder.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 18-1-29 下午2:38
# @Author : Luo Yao
# @Site : http://icode.baidu.com/repos/baidu/personal-code/Luoyao
# @File : dilation_decoder.py
# @IDE: PyCharm Community Edition
"""
实现一个全卷积网络解码类
"""
import tensorflow as tf
from encoder_decoder_model import cnn_basenet
from encoder_decoder_model import vgg_encoder
from encoder_decoder_model import dense_encoder
class FCNDecoder(cnn_basenet.CNNBaseModel):
"""
实现一个全卷积解码类
"""
def __init__(self):
"""
"""
super(FCNDecoder, self).__init__()
def decode(self, input_tensor_dict, decode_layer_list, name):
"""
解码特征信息反卷积还原
:param input_tensor_dict:
:param decode_layer_list: 需要解码的层名称需要由深到浅顺序写
eg. ['pool5', 'pool4', 'pool3']
:param name:
:return:
"""
ret = dict()
with tf.variable_scope(name):
# score stage 1
input_tensor = input_tensor_dict[decode_layer_list[0]]['data']
score = self.conv2d(inputdata=input_tensor, out_channel=64,
kernel_size=1, use_bias=False, name='score_origin')
decode_layer_list = decode_layer_list[1:]
for i in range(len(decode_layer_list)):
deconv = self.deconv2d(inputdata=score, out_channel=64, kernel_size=4,
stride=2, use_bias=False, name='deconv_{:d}'.format(i + 1))
input_tensor = input_tensor_dict[decode_layer_list[i]]['data']
score = self.conv2d(inputdata=input_tensor, out_channel=64,
kernel_size=1, use_bias=False, name='score_{:d}'.format(i + 1))
fused = tf.add(deconv, score, name='fuse_{:d}'.format(i + 1))
score = fused
deconv_final = self.deconv2d(inputdata=score, out_channel=64, kernel_size=16,
stride=8, use_bias=False, name='deconv_final')
score_final = self.conv2d(inputdata=deconv_final, out_channel=2,
kernel_size=1, use_bias=False, name='score_final')
ret['logits'] = score_final
ret['deconv'] = deconv_final
# # score stage 1
# input_tensor = input_tensor_dict['pool5']
#
# score_1 = self.conv2d(inputdata=input_tensor, out_channel=2,
# kernel_size=1, use_bias=False, name='score_1')
#
# # decode stage 1
# deconv_1 = self.deconv2d(inputdata=score_1, out_channel=2, kernel_size=4,
# stride=2, use_bias=False, name='deconv_1')
#
# # score stage 2
# score_2 = self.conv2d(inputdata=input_tensor_dict['pool4'], out_channel=2,
# kernel_size=1, use_bias=False, name='score_2')
#
# # fuse stage 1
# fuse_1 = tf.add(deconv_1, score_2, name='fuse_1')
#
# # decode stage 2
# deconv_2 = self.deconv2d(inputdata=fuse_1, out_channel=2, kernel_size=4,
# stride=2, use_bias=False, name='deconv_2')
#
# # score stage 3
# score_3 = self.conv2d(inputdata=input_tensor_dict['pool3'], out_channel=2,
# kernel_size=1, use_bias=False, name='score_3')
#
# # fuse stage 2
# fuse_2 = tf.add(deconv_2, score_3, name='fuse_2')
#
# # decode stage 3
# deconv_3 = self.deconv2d(inputdata=fuse_2, out_channel=2, kernel_size=16,
# stride=8, use_bias=False, name='deconv_3')
#
# # score stage 4
# score_4 = self.conv2d(inputdata=deconv_3, out_channel=2,
# kernel_size=1, use_bias=False, name='score_4')
#
# ret['logits'] = score_4
return ret
if __name__ == '__main__':
vgg_encoder = vgg_encoder.VGG16Encoder(phase=tf.constant('train', tf.string))
dense_encoder = dense_encoder.DenseEncoder(l=40, growthrate=12,
with_bc=True, phase='train', n=5)
decoder = FCNDecoder()
in_tensor = tf.placeholder(dtype=tf.float32, shape=[None, 256, 512, 3],
name='input')
vgg_encode_ret = vgg_encoder.encode(in_tensor, name='vgg_encoder')
dense_encode_ret = dense_encoder.encode(in_tensor, name='dense_encoder')
decode_ret = decoder.decode(vgg_encode_ret, name='decoder',
decode_layer_list=['pool5',
'pool4',
'pool3'])