Skip to content

Commit 7d82d6d

Browse files
minor changes
1 parent ea4da22 commit 7d82d6d

File tree

130 files changed

+3987508
-1146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+3987508
-1146
lines changed

crfasrnn/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import crfasrnn_layer

crfasrnn/compile.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ------------------------------------------------------------------------------------------------------------i----------
2+
# * Activate your Tensorflow virtualenv before running this script.
3+
# * This script assumes gcc version >=5. If you have an older version, remove the -D_GLIBCXX_USE_CXX11_ABI=0 flag below.
4+
# * On Mac OS X, the additional flag "-undefined dynamic_lookup" is required.
5+
# * If this script fails, please refer to https://www.tensorflow.org/extend/adding_an_op#build_the_op_library for help.
6+
# -----------------------------------------------------------------------------------------------------------------------
7+
8+
TF_INC=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')
9+
TF_LIB=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_lib())')
10+
11+
g++ -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0 -shared high_dim_filter.cc modified_permutohedral.cc -o high_dim_filter.so -fPIC -I $TF_INC -I$TF_INC/external/nsync/public/ -L$TF_LIB -ltensorflow_framework -O2

crfasrnn_layer.py crfasrnn/crfasrnn_layer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import numpy as np
2222
import tensorflow as tf
2323
from keras.engine.topology import Layer
24-
custom_module = tf.load_op_library('./cpp/high_dim_filter.so')
24+
custom_module = tf.load_op_library('./crfasrnn/high_dim_filter.so')
2525
import high_dim_filter_grad # Register gradients for the custom op
2626

2727

crfasrnn/high_dim_filter.cc

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017 Sadeep Jayasumana
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "tensorflow/core/framework/op.h"
26+
#include "tensorflow/core/framework/shape_inference.h"
27+
#include "tensorflow/core/framework/op_kernel.h"
28+
#include "tensorflow/core/framework/tensor_shape.h"
29+
#include "modified_permutohedral.h"
30+
31+
using namespace tensorflow;
32+
33+
void compute_spatial_kernel(float * const output_kernel, const int width,
34+
const int height, const float theta_gamma) {
35+
36+
const int num_pixels = width * height;
37+
for (int p = 0; p < num_pixels; ++p) {
38+
output_kernel[2 * p] = static_cast<float>(p % width) / theta_gamma;
39+
output_kernel[2 * p + 1] = static_cast<float>(p / width) / theta_gamma;
40+
}
41+
}
42+
43+
void compute_bilateral_kernel(float * const output_kernel, const Tensor& rgb_tensor,
44+
const float theta_alpha, const float theta_beta) {
45+
46+
const int height = rgb_tensor.dim_size(1);
47+
const int width = rgb_tensor.dim_size(2);
48+
const int num_pixels = height * width;
49+
auto rgb = rgb_tensor.flat<float>();
50+
51+
for (int p = 0; p < num_pixels; ++p) {
52+
// Spatial terms
53+
output_kernel[5 * p] = static_cast<float>(p % width) / theta_alpha;
54+
output_kernel[5 * p + 1] = static_cast<float>(p / width) / theta_alpha;
55+
56+
// Color terms
57+
output_kernel[5 * p + 2] = static_cast<float>(rgb(p) / theta_beta);
58+
output_kernel[5 * p + 3] = static_cast<float>(rgb(num_pixels + p) / theta_beta);
59+
output_kernel[5 * p + 4] = static_cast<float>(rgb(2 * num_pixels + p) / theta_beta);
60+
}
61+
}
62+
63+
REGISTER_OP("HighDimFilter")
64+
.Attr("bilateral: bool")
65+
.Attr("theta_alpha: float = 1.0")
66+
.Attr("theta_beta: float = 1.0")
67+
.Attr("theta_gamma: float = 1.0")
68+
.Attr("backwards: bool = false")
69+
.Input("raw: float32")
70+
.Input("rgb: float32")
71+
.Output("filtered: float32")
72+
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
73+
c->set_output(0, c->input(0));
74+
return Status::OK();
75+
});
76+
77+
class HighDimFilterOp : public OpKernel {
78+
public:
79+
explicit HighDimFilterOp(OpKernelConstruction* context) : OpKernel(context) {
80+
81+
OP_REQUIRES_OK(context,
82+
context->GetAttr("bilateral", &bilateral_));
83+
OP_REQUIRES_OK(context,
84+
context->GetAttr("theta_alpha", &theta_alpha_));
85+
OP_REQUIRES_OK(context,
86+
context->GetAttr("theta_beta", &theta_beta_));
87+
OP_REQUIRES_OK(context,
88+
context->GetAttr("theta_gamma", &theta_gamma_));
89+
OP_REQUIRES_OK(context,
90+
context->GetAttr("backwards", &backwards_));
91+
}
92+
93+
void Compute(OpKernelContext* context) override {
94+
95+
// Grab the unary tensor
96+
const Tensor& input_tensor = context->input(0);
97+
// Grab the RGB image tensor
98+
const Tensor& image_tensor = context->input(1);
99+
100+
const int channels = input_tensor.dim_size(0);
101+
const int height = input_tensor.dim_size(1);
102+
const int width = input_tensor.dim_size(2);
103+
const int num_pixels = width * height;
104+
105+
// Create the output tensor
106+
Tensor* output_tensor = NULL;
107+
OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
108+
&output_tensor));
109+
ModifiedPermutohedral mp;
110+
111+
if (bilateral_) {
112+
float * const kernel_vals = new float[5 * num_pixels];
113+
compute_bilateral_kernel(kernel_vals, image_tensor,
114+
theta_alpha_, theta_beta_);
115+
mp.init(kernel_vals, 5, num_pixels);
116+
mp.compute(*output_tensor, input_tensor, channels, backwards_);
117+
118+
delete[] kernel_vals;
119+
} else {
120+
float * const kernel_vals = new float[2 * num_pixels];
121+
compute_spatial_kernel(kernel_vals, width, height, theta_gamma_);
122+
mp.init(kernel_vals, 2, num_pixels);
123+
mp.compute(*output_tensor, input_tensor, channels, backwards_);
124+
125+
delete[] kernel_vals;
126+
}
127+
128+
}
129+
130+
private:
131+
bool bilateral_;
132+
float theta_alpha_;
133+
float theta_beta_;
134+
float theta_gamma_;
135+
bool backwards_;
136+
};
137+
138+
REGISTER_KERNEL_BUILDER(Name("HighDimFilter").Device(DEVICE_CPU), HighDimFilterOp);

crfasrnn/high_dim_filter_grad.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
MIT License
3+
4+
Copyright (c) 2017 Sadeep Jayasumana
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
"""
24+
25+
import tensorflow as tf
26+
from tensorflow.python.framework import ops
27+
custom_module = tf.load_op_library('./crfasrnn/high_dim_filter.so')
28+
29+
30+
@ops.RegisterGradient("HighDimFilter")
31+
def _high_dim_filter_grad(op, grad):
32+
""" Gradients for the HighDimFilter op. We only need to calculate the gradients
33+
w.r.t. the first input (unaries) as we never need to backprop errors to the
34+
second input (RGB values of the image).
35+
36+
Args:
37+
op: The `high_dim_filter` operation that we are differentiating.
38+
grad: Gradients with respect to the output of the `high_dim_filter` op.
39+
40+
Returns:
41+
Gradients with respect to the input of `high_dim_filter`.
42+
"""
43+
44+
rgb = op.inputs[1]
45+
grad_vals = custom_module.high_dim_filter(grad, rgb,
46+
bilateral=op.get_attr("bilateral"),
47+
theta_alpha=op.get_attr("theta_alpha"),
48+
theta_beta=op.get_attr("theta_beta"),
49+
theta_gamma=op.get_attr("theta_gamma"),
50+
backwards=True)
51+
52+
return [grad_vals, tf.zeros_like(rgb)]

crfasrnn/high_dim_filter_grad.pyc

2.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)