From 98edeb29f4b587920d8fcfd073a6eb5702d33f33 Mon Sep 17 00:00:00 2001 From: PyExtreme Date: Mon, 16 Sep 2019 17:12:59 +0530 Subject: [PATCH 1/3] Add Docstring CorrelationCost --- tensorflow_addons/layers/optical_flow.py | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/layers/optical_flow.py b/tensorflow_addons/layers/optical_flow.py index c2c843548a..b7b06d0086 100644 --- a/tensorflow_addons/layers/optical_flow.py +++ b/tensorflow_addons/layers/optical_flow.py @@ -27,7 +27,7 @@ @tf.function -def correlation_cost(input_a, +def _correlation_cost(input_a, input_b, kernel_size, max_displacement, @@ -82,8 +82,8 @@ def correlation_cost(input_a, A `Tensor` of the format specified by `data_format`. """ - with tf.name_scope(name or "correlation_cost"): - op_call = _correlation_cost_op_so.correlation_cost + with tf.name_scope(name or "_correlation_cost"): + op_call = _correlation_cost_op_so._correlation_cost if data_format == "channels_last": op_data_format = "NHWC" @@ -141,6 +141,33 @@ def _correlation_cost_grad(op, grad_output): @keras_utils.register_keras_custom_object class CorrelationCost(tf.keras.layers.Layer): + """ + This layer implements the correlation operation from + FlowNet Learning Optical Flow with Convolutional Networks (Fischer et al.) r + + Following are the parameters it takes: + input_a: A `Tensor` of the format specified by `data_format`. + + input_b: A `Tensor` of the format specified by `data_format`. + + kernel_size: An integer specifying the height and width of the + patch used to compute the per-patch costs. + + max_displacement: An integer specifying the maximum search radius + for each position. + + stride_1: An integer specifying the stride length in the input. + + stride_2: An integer specifying the stride length in the patch. + + pad: An integer specifying the paddings in height and width. + + data_format: Specifies the data format. + Possible values are: + "NHWC" float [batch, height, width, channels] + "NCHW" float [batch, channels, height, width] + Defaults to `"NHWC"`. + """ def __init__(self, kernel_size, max_displacement, stride_1, stride_2, pad, data_format, **kwargs): self.kernel_size = kernel_size @@ -169,7 +196,7 @@ def call(self, inputs): input_a = tf.convert_to_tensor(inputs[0]) input_b = tf.convert_to_tensor(inputs[1]) - return correlation_cost( + return _correlation_cost( input_a, input_b, kernel_size=self.kernel_size, From 0f52bc586ee256379357d8f82bf3892f661e4278 Mon Sep 17 00:00:00 2001 From: PyExtreme Date: Mon, 16 Sep 2019 22:11:03 +0530 Subject: [PATCH 2/3] Add CorrelationCost Documentation --- tensorflow_addons/layers/optical_flow.py | 51 +++++++++++------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/tensorflow_addons/layers/optical_flow.py b/tensorflow_addons/layers/optical_flow.py index b7b06d0086..ff969f8d57 100644 --- a/tensorflow_addons/layers/optical_flow.py +++ b/tensorflow_addons/layers/optical_flow.py @@ -28,14 +28,14 @@ @tf.function def _correlation_cost(input_a, - input_b, - kernel_size, - max_displacement, - stride_1, - stride_2, - pad, - data_format='channels_last', - name=None): + input_b, + kernel_size, + max_displacement, + stride_1, + stride_2, + pad, + data_format='channels_last', + name=None): """Correlation Cost Volume computation. "FlowNet: Learning Optical Flow with Convolutional Networks" @@ -141,33 +141,30 @@ def _correlation_cost_grad(op, grad_output): @keras_utils.register_keras_custom_object class CorrelationCost(tf.keras.layers.Layer): - """ - This layer implements the correlation operation from - FlowNet Learning Optical Flow with Convolutional Networks (Fischer et al.) r - - Following are the parameters it takes: - input_a: A `Tensor` of the format specified by `data_format`. + """This layer implements the correlation operation from FlowNet Learning + Optical Flow with Convolutional Networks (Fischer et al.) r. - input_b: A `Tensor` of the format specified by `data_format`. + Args: - kernel_size: An integer specifying the height and width of the - patch used to compute the per-patch costs. + kernel_size: An integer specifying the height and width of the + patch used to compute the per-patch costs. - max_displacement: An integer specifying the maximum search radius - for each position. + max_displacement: An integer specifying the maximum search radius + for each position. - stride_1: An integer specifying the stride length in the input. + stride_1: An integer specifying the stride length in the input. - stride_2: An integer specifying the stride length in the patch. + stride_2: An integer specifying the stride length in the patch. - pad: An integer specifying the paddings in height and width. + pad: An integer specifying the paddings in height and width. - data_format: Specifies the data format. - Possible values are: - "NHWC" float [batch, height, width, channels] - "NCHW" float [batch, channels, height, width] - Defaults to `"NHWC"`. + data_format: Specifies the data format. + Possible values are: + "channels_last" float [batch, height, width, channels] + "channels_first" float [batch, channels, height, width] + Defaults to `"channels_last"`. """ + def __init__(self, kernel_size, max_displacement, stride_1, stride_2, pad, data_format, **kwargs): self.kernel_size = kernel_size From 639178a822ded3bf753575f4903714c4defd9ff3 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Tue, 17 Sep 2019 15:02:36 -0400 Subject: [PATCH 3/3] Small reformat --- tensorflow_addons/layers/optical_flow.py | 29 +++++++++---------- tensorflow_addons/layers/optical_flow_test.py | 6 ++-- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/tensorflow_addons/layers/optical_flow.py b/tensorflow_addons/layers/optical_flow.py index ff969f8d57..b3c58d28aa 100644 --- a/tensorflow_addons/layers/optical_flow.py +++ b/tensorflow_addons/layers/optical_flow.py @@ -82,8 +82,8 @@ def _correlation_cost(input_a, A `Tensor` of the format specified by `data_format`. """ - with tf.name_scope(name or "_correlation_cost"): - op_call = _correlation_cost_op_so._correlation_cost + with tf.name_scope(name or "correlation_cost"): + op_call = _correlation_cost_op_so.correlation_cost if data_format == "channels_last": op_data_format = "NHWC" @@ -141,28 +141,25 @@ def _correlation_cost_grad(op, grad_output): @keras_utils.register_keras_custom_object class CorrelationCost(tf.keras.layers.Layer): - """This layer implements the correlation operation from FlowNet Learning - Optical Flow with Convolutional Networks (Fischer et al.) r. + """Correlation Cost Layer. - Args: + This layer implements the correlation operation from FlowNet Learning + Optical Flow with Convolutional Networks (Fischer et al.): + https://arxiv.org/abs/1504.06 + Args: kernel_size: An integer specifying the height and width of the - patch used to compute the per-patch costs. - + patch used to compute the per-patch costs. max_displacement: An integer specifying the maximum search radius - for each position. - + for each position. stride_1: An integer specifying the stride length in the input. - stride_2: An integer specifying the stride length in the patch. - pad: An integer specifying the paddings in height and width. - data_format: Specifies the data format. - Possible values are: - "channels_last" float [batch, height, width, channels] - "channels_first" float [batch, channels, height, width] - Defaults to `"channels_last"`. + Possible values are: + "channels_last" float [batch, height, width, channels] + "channels_first" float [batch, channels, height, width] + Defaults to `"channels_last"`. """ def __init__(self, kernel_size, max_displacement, stride_1, stride_2, pad, diff --git a/tensorflow_addons/layers/optical_flow_test.py b/tensorflow_addons/layers/optical_flow_test.py index 7dedd49f50..c1834b19b4 100644 --- a/tensorflow_addons/layers/optical_flow_test.py +++ b/tensorflow_addons/layers/optical_flow_test.py @@ -19,7 +19,7 @@ import numpy as np import tensorflow as tf -from tensorflow_addons.layers.optical_flow import correlation_cost, CorrelationCost +from tensorflow_addons.layers.optical_flow import _correlation_cost, CorrelationCost from tensorflow_addons.utils import test_utils @@ -31,7 +31,7 @@ def _forward(self, input_a, input_b, kernel_size, max_displacement, input_a_op = tf.convert_to_tensor(input_a, dtype=tf.float32) input_b_op = tf.convert_to_tensor(input_b, dtype=tf.float32) - output = correlation_cost( + output = _correlation_cost( input_a_op, input_b_op, kernel_size=kernel_size, @@ -117,7 +117,7 @@ def _gradients(self, data_format): input_b_op = tf.convert_to_tensor(input_b) def correlation_fn(input_a, input_b): - return correlation_cost( + return _correlation_cost( input_a, input_b, kernel_size=kernel_size,