This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdensemapnet.py
executable file
·140 lines (116 loc) · 4.74 KB
/
densemapnet.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
136
137
138
139
140
'''DenseMapNet - a tiny network for fast disparity estimation
from stereo images
DenseMapNet class is where the actual model is built
Atienza, R. "Fast Disparity Estimation using Dense Networks".
International Conference on Robotics and Automation,
Brisbane, Australia, 2018.
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import keras
from keras.layers import Dense, Dropout
from keras.layers import Input, Conv2D, Conv2DTranspose
from keras.layers import ZeroPadding2D, BatchNormalization, Activation
from keras.layers import UpSampling2D
from keras.optimizers import RMSprop
from keras.callbacks import ModelCheckpoint, LambdaCallback
from keras.models import load_model, Model
from keras.layers.pooling import MaxPooling2D
from keras.utils import plot_model
import numpy as np
from utils import Settings
class DenseMapNet(object):
def __init__(self, settings):
self.settings =settings
self.xdim = self.settings.xdim
self.ydim = self.settings.ydim
self.channels = self.settings.channels
self.model = None
def build_model(self, lr=1e-3):
dropout = 0.2
shape=(None, self.ydim, self.xdim, self.channels)
left = Input(batch_shape=shape)
right = Input(batch_shape=shape)
# left image as reference
x = Conv2D(filters=16, kernel_size=5, padding='same')(left)
xleft = Conv2D(filters=1,
kernel_size=5,
padding='same',
dilation_rate=2)(left)
# left and right images for disparity estimation
xin = keras.layers.concatenate([left, right])
xin = Conv2D(filters=32, kernel_size=5, padding='same')(xin)
# image reduced by 8
x8 = MaxPooling2D(8)(xin)
x8 = BatchNormalization()(x8)
x8 = Activation('relu', name='downsampled_stereo')(x8)
dilation_rate = 1
y = x8
# correspondence network
# parallel cnn at increasing dilation rate
for i in range(4):
a = Conv2D(filters=32,
kernel_size=5,
padding='same',
dilation_rate=dilation_rate)(x8)
a = Dropout(dropout)(a)
y = keras.layers.concatenate([a, y])
dilation_rate += 1
dilation_rate = 1
x = MaxPooling2D(8)(x)
# disparity network
# dense interconnection inspired by DenseNet
for i in range(4):
x = keras.layers.concatenate([x, y])
y = BatchNormalization()(x)
y = Activation('relu')(y)
y = Conv2D(filters=64,
kernel_size=1,
padding='same')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv2D(filters=16,
kernel_size=5,
padding='same',
dilation_rate=dilation_rate)(y)
y = Dropout(dropout)(y)
dilation_rate += 1
# disparity estimate scaled back to original image size
x = keras.layers.concatenate([x, y], name='upsampled_disparity')
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters=32, kernel_size=1, padding='same')(x)
x = UpSampling2D(8)(x)
if not self.settings.nopadding:
x = ZeroPadding2D(padding=(2, 0))(x)
# left image skip connection to disparity estimate
x = keras.layers.concatenate([x, xleft])
y = BatchNormalization()(x)
y = Activation('relu')(y)
y = Conv2D(filters=16, kernel_size=5, padding='same')(y)
x = keras.layers.concatenate([x, y])
y = BatchNormalization()(x)
y = Activation('relu')(y)
y = Conv2DTranspose(filters=1, kernel_size=9, padding='same')(y)
# prediction
if self.settings.otanh:
yout = Activation('tanh', name='disparity_output')(y)
else:
yout = Activation('sigmoid', name='disparity_output')(y)
# densemapnet model
self.model = Model([left, right],yout)
if self.settings.model_weights:
print("Loading checkpoint model weights %s...."
% self.settings.model_weights)
self.model.load_weights(self.settings.model_weights)
if self.settings.otanh:
self.model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=lr))
else:
self.model.compile(loss='mse',
optimizer=RMSprop(lr=lr))
print("DenseMapNet Model:")
self.model.summary()
plot_model(self.model, to_file='densemapnet.png', show_shapes=True)
return self.model