From b25b9be4eb0d633225c3705083de87c57514936b Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Tue, 16 Mar 2021 21:06:27 +1030 Subject: [PATCH] Initial Commit --- fast_depth_functional.py | 63 ++++++++++++++++++++++++++++++ main.py | 83 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 fast_depth_functional.py create mode 100644 main.py diff --git a/fast_depth_functional.py b/fast_depth_functional.py new file mode 100644 index 0000000..eee428b --- /dev/null +++ b/fast_depth_functional.py @@ -0,0 +1,63 @@ +import tensorflow.keras as keras + +''' +Functional version of fastdepth model. Note that this doesn't work at the moment +''' + + +def _depthwise_conv_block(inputs, + pointwise_conv_filters, + depth_multiplier=1, + strides=(1, 1), + block_id=1): + channel_axis = 1 if keras.backend.image_data_format() == 'channels_first' else -1 + pointwise_conv_filters = int(pointwise_conv_filters) + + if strides == (1, 1): + x = inputs + else: + x = keras.layers.ZeroPadding2D(((0, 1), (0, 1)), name='conv_pad_%d' % block_id)( + inputs) + x = keras.layers.DepthwiseConv2D((3, 3), + padding='same' if strides == (1, 1) else 'valid', + depth_multiplier=depth_multiplier, + strides=strides, + use_bias=False, + name='conv_dw_%d' % block_id)( + x) + x = keras.layers.BatchNormalization( + axis=channel_axis, name='conv_dw_%d_bn' % block_id)( + x) + x = keras.layers.ReLU(6., name='conv_dw_%d_relu' % block_id)(x) + + x = keras.layers.Conv2D( + pointwise_conv_filters, (1, 1), + padding='same', + use_bias=False, + strides=(1, 1), + name='conv_pw_%d' % block_id)( + x) + x = keras.layers.BatchNormalization( + axis=channel_axis, name='conv_pw_%d_bn' % block_id)( + x) + return keras.layers.ReLU(6., name='conv_pw_%d_relu' % block_id)(x) + + +def make_fastdepth_functional(): + # This doesn't work, at least right now... + mobilenet = keras.applications.MobileNet(include_top=False) + input = keras.layers.Input(shape=(224, 224, 3)) + x = mobilenet(input) + x = _depthwise_conv_block(x, 512, block_id=14) + x = _depthwise_conv_block(x, 256, block_id=15) + x = keras.layers.Add()([x, mobilenet.get_layer('conv_pw_5_relu').output]) + x = _depthwise_conv_block(x, 128, block_id=16) + x = keras.layers.Add()([x, mobilenet.get_layer('conv_pw_3_relu').output]) + x = _depthwise_conv_block(x, 64, block_id=17) + x = keras.layers.Add()([x, mobilenet.get_layer('conv_pw_1_relu').output]) + x = _depthwise_conv_block(x, 32, block_id=18) + + x = keras.layers.Conv2D(1, 1)(x) + x = keras.layers.BatchNormalization()(x) + x = keras.layers.ReLU()(x) + return keras.Model(input, x, name="fast_depth") diff --git a/main.py b/main.py new file mode 100644 index 0000000..051da5b --- /dev/null +++ b/main.py @@ -0,0 +1,83 @@ +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/