def test_1(self): input = np.random.random([3, 4, 5, 6]).astype("float64") minus_1 = fluid.layers.fill_constant([1], "int32", -1) minus_3 = fluid.layers.fill_constant([1], "int32", -3) starts = fluid.layers.data( name='starts', shape=[3], dtype='int32', append_batch_size=False) ends = fluid.layers.data( name='ends', shape=[3], dtype='int32', append_batch_size=False) strides = fluid.layers.data( name='strides', shape=[3], dtype='int32', append_batch_size=False) x = fluid.layers.data( name="x", shape=[3, 4, 5, 6], append_batch_size=False, dtype="float64") out_1 = paddle.strided_slice( x, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1]) out_2 = paddle.strided_slice( x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1]) out_3 = paddle.strided_slice( x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, minus_1], strides=[1, 1, 1]) out_4 = paddle.strided_slice( x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides) out_5 = x[-3:3, 0:100:2, -1:2:-1] out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1] out_7 = x[minus_1, 0:100:2, :, -1:2:-1] exe = fluid.Executor(place=fluid.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( fluid.default_main_program(), feed={ "x": input, 'starts': np.array([-3, 0, 2]).astype("int32"), 'ends': np.array([3, 2147483648, -1]).astype("int64"), 'strides': np.array([1, 1, 1]).astype("int32") }, fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7]) assert np.array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) assert np.array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) assert np.array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) assert np.array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) assert np.array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :]) assert np.array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1]) assert np.array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1])
def slice_select_prim2orig(op, x): return paddle.strided_slice( x, axes=op.attr('axis'), starts=op.attr('starts'), ends=op.attr('ends'), strides=op.attr('strides'))
def test_dygraph_op(self): x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32") axes = [1, 2, 3] starts = [-3, 0, 2] ends = [3, 2, 4] strides_1 = [1, 1, 1] sliced_1 = paddle.strided_slice( x, axes=axes, starts=starts, ends=ends, strides=strides_1) assert sliced_1.shape == (3, 2, 2, 2)
def test_compare_paddle_strided_slice_with_numpy(self): paddle.disable_static() array = np.arange(5) pt = paddle.to_tensor(array) s1 = 3 e1 = 1 stride1 = -2 sl = paddle.strided_slice( pt, axes=[0, ], starts=[s1, ], ends=[e1, ], strides=[stride1, ]) self.assertTrue(array[s1:e1:stride1], sl) array = np.arange(6 * 6).reshape((6, 6)) pt = paddle.to_tensor(array) s2 = [8, -1] e2 = [1, -5] stride2 = [-2, -3] sl = paddle.strided_slice( pt, axes=[0, 1], starts=s2, ends=e2, strides=stride2) self.assertTrue( np.array_equal(sl.numpy(), array[s2[0]:e2[0]:stride2[0], s2[1]:e2[ 1]:stride2[1]])) array = np.arange(6 * 7 * 8).reshape((6, 7, 8)) pt = paddle.to_tensor(array) s2 = [7, -1] e2 = [2, -5] stride2 = [-2, -3] sl = paddle.strided_slice( pt, axes=[0, 2], starts=s2, ends=e2, strides=stride2) array_slice = array[s2[0]:e2[0]:stride2[0], ::, s2[1]:e2[1]:stride2[1]] self.assertTrue( np.array_equal(sl.numpy(), array_slice), msg="paddle.strided_slice:\n {} \n numpy slice:\n{}".format( sl.numpy(), array_slice))
def forward(self, inputs): """ forward """ axes = self.config['axes'] starts = self.config['starts'] ends = self.config['ends'] # strides = self.config['strides'] # strides = [1, paddle.to_tensor(1).astype('int32'), 1] # strides = [1, paddle.to_tensor(1, dtype='int32'), 1] strides = [1, paddle.to_tensor(np.array(1).astype("int32")), 1] x = paddle.strided_slice(inputs, axes=axes, starts=starts, ends=ends, strides=strides) return x
def forward(self, inputs): """ forward """ axes = self.config['axes'] starts = self.config['starts'] ends = self.config['ends'] strides = self.config['strides'] if self.config['isStartsTensor']: starts = paddle.to_tensor(np.array(starts).astype('int32')) if self.config['isEndsTensor']: ends = paddle.to_tensor(np.array(ends).astype('int32')) if self.config['isStridesTensor']: strides = paddle.to_tensor(np.array(strides).astype('int32')) x = paddle.strided_slice(inputs, axes=axes, starts=starts, ends=ends, strides=strides) return x
def forward(self): input = self.input('Input', 0) weight = self.input('Filter', 0) mask = self.input('Mask', 0) offset = self.input('Offset', 0) input = layers.pad2d(input, self.padding) input_shape = paddle.shape(input) if self.padded_x_h < 0 or self.padded_x_w < 0: self.padded_x_h = input_shape[2] self.padded_x_w = input_shape[3] offset_x = paddle.strided_slice(offset, axes=[1], starts=[0], ends=[self.offset_channel], strides=[2]) offset_y = paddle.strided_slice(offset, axes=[1], starts=[1], ends=[self.offset_channel], strides=[2]) offset = paddle.concat([offset_x, offset_y], axis=1) offset_shape = paddle.shape(offset) offset_h = offset_shape[2] offset_w = offset_shape[3] coordinate = self.get_offset_coordinate(offset, 'float32', offset_shape) coordinate = coordinate.transpose((0, 2, 3, 1)) coord_lt, coord_rb, coord_lb, coord_rt = self.get_bilinear_corner_coordinate( coordinate, self.padded_x_h, self.padded_x_w) # clip coordinate coordinate = paddle.concat([ paddle.clip(coordinate[:, :, :, :self.N], 0, self.padded_x_h - 1), paddle.clip(coordinate[:, :, :, self.N:], 0, self.padded_x_w - 1) ], axis=-1) cof_lt, cof_rb, cof_lb, cof_rt = self.get_bilinear_coefficient( coord_lt, coord_rb, coord_lb, coord_rt, coordinate) feature_lt = self.get_feature_by_coordinate(input, coord_lt, offset_h, offset_w, self.padded_x_w) feature_rb = self.get_feature_by_coordinate(input, coord_rb, offset_h, offset_w, self.padded_x_w) feature_lb = self.get_feature_by_coordinate(input, coord_lb, offset_h, offset_w, self.padded_x_w) feature_rt = self.get_feature_by_coordinate(input, coord_rt, offset_h, offset_w, self.padded_x_w) feature_after_deformation = paddle.unsqueeze(cof_lt, 1) * feature_lt + \ paddle.unsqueeze(cof_rb, 1) * feature_rb + \ paddle.unsqueeze(cof_lb, 1) * feature_lb + \ paddle.unsqueeze(cof_rt, 1) * feature_rt # modulation if mask is not None: mask = paddle.transpose(mask, (0, 2, 3, 1)) mask = paddle.unsqueeze(mask, 1) mask = paddle.tile(mask, [1, self.in_channel, 1, 1, 1]) feature_after_deformation *= mask feature_after_deformation = self.reshape_feature( feature_after_deformation, offset_h, offset_w) out = paddle.nn.functional.conv2d(feature_after_deformation, weight, stride=self.kernel_size, groups=self.groups) return {'Output': [out]}