Skip to content

Commit ee9a8f4

Browse files
committed
Add customized python layer for converting tf.StridedSlice layer.
1 parent d2680ac commit ee9a8f4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

python/stridedslice.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import caffe
2+
import numpy as np
3+
4+
5+
class TensorSlice(caffe.Layer):
6+
"""
7+
Get a tensor's slicing: realize the function of the tf.strided_slice().
8+
9+
TO OPTIMIZE: For simplification, assume all the input tensors are 4 dimensions now.
10+
"""
11+
12+
def setup(self, bottom, top):
13+
# check number of inputs and outputs
14+
if len(bottom) != 1:
15+
raise Exception("Only input one Tensor at a time!")
16+
if len(top) != 1:
17+
raise Exception("Only output one Tensor at a time!")
18+
19+
params = eval(self.param_str)
20+
self.begin = np.array(params["begins"])
21+
self.end = np.array(params["ends"])
22+
if params["strides"] != None:
23+
self.strides = np.array(params["strides"])
24+
else:
25+
self.strides = np.array([1, 1, 1, 1])
26+
27+
if params["beginmask"] != None:
28+
self.beginmask = int(params["beginmask"])
29+
else:
30+
self.beginmask = None
31+
if params["endmask"] != None:
32+
self.endmask = int(params["endmask"])
33+
else:
34+
self.endmask = None
35+
36+
# Handles the condition where the "ends" is assigned (0, 0, 0, 0) meaning the end of the input Tensor
37+
if ((self.end==0).all() and (self.strides>0).all()):
38+
self.end = bottom[0].shape
39+
40+
# According to the tf.strided_slice():
41+
# If the ith bit of beginmask is set, begin[i] is ignored and the fullest possible range in that dimension is used instead;
42+
# The endmask works analogously, except with the end range.
43+
if self.beginmask != None:
44+
self.begin[self.beginmask] = 0
45+
if self.endmask != None:
46+
self.end[self.endmask] = bottom[0].shape[self.endmask]
47+
48+
# other parameters...
49+
50+
51+
def reshape(self, bottom, top):
52+
# check input dimensions
53+
if bottom[0].count == 0:
54+
raise Exception("Input must not be empty!")
55+
56+
#top[0].reshape(*bottom[0].data.shape)
57+
#dim = len(self.begin)
58+
num = [0, 0, 0, 0]
59+
for i in range(4):
60+
if self.strides[i]==0:
61+
raise Exception("Strides should never equal to 0!")
62+
else:
63+
num[i] = (abs(self.end[i]-self.begin[i])/abs(self.strides[i]))
64+
top[0].reshape(num[0], num[1], num[2], num[3])
65+
66+
67+
def forward(self, bottom, top):
68+
#pass
69+
#top[0].data[...] = bottom[0].data[:]
70+
top[0].data[...] = np.zeros(top[0].shape)
71+
for i in range(len(self.begin)):
72+
top[0].data[...] = bottom[0].data[self.begin[0]:self.end[0]:self.strides[0], self.begin[1]:self.end[1]:self.strides[1], self.begin[2]:self.end[2]:self.strides[2], self.begin[3]:self.end[3]:self.strides[3]]
73+
74+
75+
def backward(self, top, propagate_down, bottom):
76+
for i in range(len(propagate_down)):
77+
if not propagate_down[i]:
78+
continue
79+
bottom[i].diff[...] = top[i].diff[:]
80+

0 commit comments

Comments
 (0)