84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
import tensorflow as tf
|
|
from tensorflow import keras
|
|
|
|
import tensorflow_datasets as tfds
|
|
|
|
|
|
class DecodeConv(keras.layers.Layer):
|
|
def __init__(self, out_filters, **kwargs):
|
|
super().__init__(**kwargs)
|
|
# Should be depthwise followed by batchnorm and relu.
|
|
self.depthwise = keras.layers.DepthwiseConv2D(5)
|
|
self.batch_norm = keras.layers.BatchNormalization()
|
|
self.relu = keras.layers.ReLU(6.)
|
|
self.pointwise = keras.layers.Conv2D(out_filters, 1)
|
|
self.pointwise_bn = keras.layers.BatchNormalization()
|
|
self.pointwise_rl = keras.layers.ReLU(6.)
|
|
|
|
def call(self, inputs, **kwargs):
|
|
inputs = self.depthwise(inputs, **kwargs)
|
|
inputs = self.batch_norm(inputs, **kwargs)
|
|
inputs = self.relu(inputs, **kwargs)
|
|
inputs = self.pointwise(inputs, **kwargs)
|
|
inputs = self.pointwise_bn(inputs, **kwargs)
|
|
return self.pointwise_rl(inputs, **kwargs)
|
|
|
|
|
|
class FastDepth(keras.Model):
|
|
def get_config(self):
|
|
# TODO: What to put here?
|
|
pass
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.mobile_net = keras.applications.MobileNet(include_top=False)
|
|
|
|
# TODO: Try keras.layers.SeparableConv2D as well, should do the same thing if relu is used as activation
|
|
# It probably doesn't, since
|
|
self.decode_conv1 = DecodeConv(512)
|
|
self.decode_conv2 = DecodeConv(256)
|
|
self.decode_conv3 = DecodeConv(128)
|
|
self.decode_conv4 = DecodeConv(64)
|
|
self.decode_conv5 = DecodeConv(32)
|
|
|
|
self.final_pointwise = keras.layers.Conv2D(1, 1)
|
|
self.final_pointwise_bn = keras.layers.BatchNormalization()
|
|
self.final_pointwise_relu = keras.layers.ReLU()
|
|
|
|
def call(self, inputs, is_training=False, **kwargs):
|
|
# Go through mobilenet, then each decode layer, including skip connections using:
|
|
# keras.layers.Add()
|
|
inputs = self.mobile_net(inputs, is_training=is_training, **kwargs)
|
|
|
|
# FastDepth Additive Decoder
|
|
inputs = self.decode_conv1(inputs, is_training=is_training, **kwargs)
|
|
inputs = self.decode_conv2(inputs, is_training=is_training, **kwargs)
|
|
inputs = inputs + self.mobile_net.get_layer('conv_pw_5_relu').output
|
|
inputs = self.decode_conv3(inputs, is_training=is_training, **kwargs)
|
|
inputs = inputs + self.mobile_net.get_layer('conv_pw_3_relu').output
|
|
inputs = self.decode_conv4(inputs, is_training=is_training, **kwargs)
|
|
inputs = inputs + self.mobile_net.get_layer('conv_pw_1_relu').output
|
|
inputs = self.decode_conv5(inputs, is_training=is_training, **kwargs)
|
|
|
|
inputs = self.final_pointwise(inputs, is_training=is_training, **kwargs)
|
|
inputs = self.final_pointwise_bn(inputs, is_training=is_training, **kwargs)
|
|
return self.final_pointwise_relu(inputs, is_training=is_training, **kwargs)
|
|
|
|
|
|
def load_nyu():
|
|
builder = tfds.builder('nyu_depth_v2')
|
|
builder.download_and_prepare(download_dir='../nyu')
|
|
return builder.as_dataset(split='train', shuffle_files=True)
|
|
|
|
|
|
def print_hi(name):
|
|
# Use a breakpoint in the code line below to debug your script.
|
|
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
|
|
|
|
|
|
# Press the green button in the gutter to run the script.
|
|
if __name__ == '__main__':
|
|
load_nyu()
|
|
|
|
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|