forked from mgharbi/hdrnet_legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhdrnet_ops.py
73 lines (58 loc) · 2.42 KB
/
hdrnet_ops.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
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python interface to custom Tensorflow operations for HDRnet."""
import os
import tensorflow as tf
from tensorflow.python.framework import ops
__all__ = ['bilateral_slice']
path = os.path.dirname(os.path.abspath(__file__))
path = tf.resource_loader.get_path_to_datafile(
os.path.join(path, 'lib', 'hdrnet_ops.so'))
_hdrnet = tf.load_op_library(path)
# -- Register operations ------------------------------------------------------
bilateral_slice = _hdrnet.bilateral_slice
bilateral_slice_apply = _hdrnet.bilateral_slice_apply
# ----------- Register gradients ----------------------------------------------
@ops.RegisterGradient('BilateralSlice')
def _bilateral_slice_grad(op, grad):
grid_tensor = op.inputs[0]
guide_tensor = op.inputs[1]
return _hdrnet.bilateral_slice_grad(grid_tensor, guide_tensor, grad)
@ops.RegisterGradient('BilateralSliceApply')
def _bilateral_slice_grad(op, grad):
grid_tensor = op.inputs[0]
guide_tensor = op.inputs[1]
input_tensor = op.inputs[2]
has_offset = op.get_attr('has_offset')
return _hdrnet.bilateral_slice_apply_grad(
grid_tensor, guide_tensor, input_tensor, grad, has_offset=has_offset)
# ----------- Register Shape inference ----------------------------------------
@ops.RegisterShape('BilateralSlice')
def _bilateral_slice_shape(op):
input_tensor = op.inputs[0]
guide_tensor = op.inputs[1]
return [guide_tensor.get_shape().concatenate(input_tensor.get_shape()[-1])]
@ops.RegisterShape('BilateralSliceApply')
def _bilateral_slice_shape(op):
grid_tensor = op.inputs[0]
guide_tensor = op.inputs[1]
input_tensor = op.inputs[2]
has_offset = op.get_attr('has_offset')
chan_in = input_tensor.get_shape()[-1]
chan_grid = grid_tensor.get_shape()[-1]
if has_offset:
chan_out = chan_grid // (chan_in+1)
else:
chan_out = chan_grid // chan_in
return [guide_tensor.get_shape().concatenate(chan_out)]