-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResample.py
More file actions
177 lines (127 loc) · 5.07 KB
/
Resample.py
File metadata and controls
177 lines (127 loc) · 5.07 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 7 16:18:05 2023
@author: ubuntu
"""
import tensorflow as tf
import numpy as np
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Activation, Input, Layer, LeakyReLU, GlobalAveragePooling2D, ReLU
def tf_flatten(a):
"""Flatten tensor"""
return tf.reshape(a, [-1])
def tf_repeat(a, repeats, axis=0):
"""TensorFlow version of np.repeat for 1D"""
assert len(a.get_shape()) == 1
a = tf.expand_dims(a, -1)
a = tf.tile(a, [1, repeats])
a = tf_flatten(a)
return a
def tf_repeat_2d(a, repeats):
"""Tensorflow version of np.repeat for 2D"""
assert len(a.get_shape()) == 2
a = tf.expand_dims(a, 0)
a = tf.tile(a, [repeats, 1, 1])
return a
def tf_map_coordinates(input, coords, order=1):
"""Tensorflow verion of scipy.ndimage.map_coordinates
Note that coords is transposed and only 2D is supported
Parameters
----------
input : tf.Tensor. shape = (s, s)
coords : tf.Tensor. shape = (n_points, 2)
"""
assert order == 1
coords_lt = tf.cast(tf.floor(coords), 'int32')
coords_rb = tf.cast(tf.ceil(coords), 'int32')
coords_lb = tf.stack([coords_lt[:, 0], coords_rb[:, 1]], axis=1)
coords_rt = tf.stack([coords_rb[:, 0], coords_lt[:, 1]], axis=1)
vals_lt = tf.gather_nd(input, coords_lt)
vals_rb = tf.gather_nd(input, coords_rb)
vals_lb = tf.gather_nd(input, coords_lb)
vals_rt = tf.gather_nd(input, coords_rt)
coords_offset_lt = coords - tf.cast(coords_lt, 'float32')
vals_t = vals_lt + (vals_rt - vals_lt) * coords_offset_lt[:, 0]
vals_b = vals_lb + (vals_rb - vals_lb) * coords_offset_lt[:, 0]
mapped_vals = vals_t + (vals_b - vals_t) * coords_offset_lt[:, 1]
return mapped_vals
def tf_batch_map_coordinates(input, coords, order=1):
"""Batch version of tf_map_coordinates
Only supports 2D feature maps
Parameters
----------
input : tf.Tensor. shape = (b, s, s, c)
coords : tf.Tensor. shape = (b, n_points, 2)
"""
input_shape = tf.shape(input)
batch_size = input_shape[0]
input_size = input_shape[1]
n_c = input_shape[3]
n_coords = tf.shape(coords)[1]
coords = tf.clip_by_value(coords, 0, tf.cast(input_size, 'float32') - 1)
coords_lt = tf.cast(tf.floor(coords), 'int32')
coords_rb = tf.cast(tf.ceil(coords), 'int32')
# coords_lb = tf.stack([coords_lt[..., 0], coords_rb[..., 1]], axis=-1)
# coords_rt = tf.stack([coords_rb[..., 0], coords_lt[..., 1]], axis=-1)
#new
coords_lb = tf.stack([coords_rb[..., 0], coords_lt[..., 1]], axis=-1)
coords_rt = tf.stack([coords_lt[..., 0], coords_rb[..., 1]], axis=-1)
idx = tf_repeat(tf.range(batch_size), n_coords)
def _get_vals_by_coords(input, coords):
indices = tf.stack([
idx, tf_flatten(coords[..., 0]), tf_flatten(coords[..., 1])
], axis=-1)
vals = tf.gather_nd(input, indices)
vals = tf.reshape(vals, (batch_size, n_coords, n_c))
return vals
vals_lt = _get_vals_by_coords(input, coords_lt)
vals_rb = _get_vals_by_coords(input, coords_rb)
vals_lb = _get_vals_by_coords(input, coords_lb)
vals_rt = _get_vals_by_coords(input, coords_rt)
print(vals_rt.get_shape().as_list())
coords_offset_lt = coords - tf.cast(coords_lt, 'float32')
print(coords_offset_lt.get_shape().as_list())
coords_offset_lt = tf.expand_dims(coords_offset_lt, -2)
coords_offset_lt = tf.tile(coords_offset_lt, (1, 1, 1, 1))
vals_t = vals_lt + (vals_rt - vals_lt) * coords_offset_lt[..., 1]
vals_b = vals_lb + (vals_rb - vals_lb) * coords_offset_lt[..., 1]
mapped_vals = vals_t + (vals_b - vals_t) * coords_offset_lt[..., 0]
# print(mapped_vals.get_shape().as_list())
return mapped_vals
def tf_batch_map_offsets(input, offsets, order=1):
"""Batch map offsets into input
Parameters
---------
input : tf.Tensor. shape = (b, s, s, c)
offsets: tf.Tensor. shape = (b, s, s, 2)
"""
input_shape = tf.shape(input)
batch_size = input_shape[0]
input_size = input_shape[1]
offsets = tf.reshape(offsets, (batch_size, -1, 2))
grid = tf.meshgrid(
tf.range(input_size), tf.range(input_size), indexing='ij'
)
grid = tf.stack(grid, axis=-1)
grid = tf.cast(grid, 'float32')
grid = tf.reshape(grid, (-1, 2))
grid = tf_repeat_2d(grid, batch_size)
coords = offsets + grid
mapped_vals = tf_batch_map_coordinates(input, coords)
return mapped_vals
class resample(Layer):
"""offset"""
def __init__(self, **kwargs):
"""Init"""
super(resample, self).__init__(**kwargs)
def call(self, inputs):
offsets = inputs[0]
inputs = inputs[1]
b,h,w,c = inputs.get_shape().as_list()
x = tf.reshape(inputs, (-1, h, w, c))
x_offset = tf_batch_map_offsets(x, offsets)
x_offset = tf.reshape(x_offset, (-1, h, w, c))
self.out_size = x_offset.get_shape().as_list()
return x_offset
def compute_output_shape(self, input_shape):
return tuple(self.out_size)