def test_arr_4_4_2_filter_3_3(): pad_val = 0.98 arr = np.arange(2*2*2).reshape((2, 2, 2)).astype(DTYPE) filter_shape = (3, 3) ref = np.array([[[pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val]], [[pad_val, pad_val], [0., 1.], [2., 3.], [pad_val, pad_val]], [[pad_val, pad_val], [4., 5.], [6., 7.], [pad_val, pad_val]], [[pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val]]]).astype(DTYPE) res = filter_pad2d(arr, filter_shape, constant=pad_val) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def _process_one_op(self, arr, X, Y, layer_idx, op_idx, kwargs, op_params, op_name, nbh, nbw, stride, pad_apron=False, interleave_stride=False): out_l = [] # -- here we compute the pixel coordinates of # the central pixel in a patch hc, wc = nbh / 2, nbw / 2 if pad_apron: arr = filter_pad2d(arr, (nbh, nbw)) X = np.squeeze(filter_pad2d(X[..., np.newaxis], (nbh, nbw), constant=-1)) Y = np.squeeze(filter_pad2d(Y[..., np.newaxis], (nbh, nbw), constant=-1)) if interleave_stride: for i in xrange(stride): for j in xrange(stride): arr_out_ij = self._get_feature_map(arr[i::, j::, ...], layer_idx, op_idx, kwargs, op_params, op_name) X_out_ij = view_as_windows(X[i::, j::], (nbh, nbw))[::stride, ::stride, hc, wc] Y_out_ij = view_as_windows(Y[i::, j::], (nbh, nbw))[::stride, ::stride, hc, wc] out_l += [(arr_out_ij, X_out_ij, Y_out_ij)] else: arr_out = self._get_feature_map(arr, layer_idx, op_idx, kwargs, op_params, op_name) X_out = view_as_windows(X, (nbh, nbw))[::stride, ::stride, hc, wc] Y_out = view_as_windows(Y, (nbh, nbw))[::stride, ::stride, hc, wc] out_l += [(arr_out, X_out, Y_out)] return out_l
def test_arr_3_3_1_filter_2_2(): pad_val = 1.23 arr = np.arange(3*3*1).reshape((3, 3, 1)).astype(DTYPE) filter_shape = (2, 2) ref = np.array([[[pad_val], [pad_val], [pad_val], [pad_val]], [[pad_val], [0.], [1.], [2.]], [[pad_val], [3.], [4.], [5.]], [[pad_val], [6.], [7.], [8.]]]) res = filter_pad2d(arr, filter_shape, constant=pad_val) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arr_3_3_1_filter_2_2(): pad_val = 1.23 arr = np.arange(3 * 3 * 1).reshape((3, 3, 1)).astype(DTYPE) filter_shape = (2, 2) ref = np.array([[[pad_val], [pad_val], [pad_val], [pad_val]], [[pad_val], [0.], [1.], [2.]], [[pad_val], [3.], [4.], [5.]], [[pad_val], [6.], [7.], [8.]]]) res = filter_pad2d(arr, filter_shape, constant=pad_val) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arr_4_4_2_filter_3_3(): pad_val = 0.98 arr = np.arange(2 * 2 * 2).reshape((2, 2, 2)).astype(DTYPE) filter_shape = (3, 3) ref = np.array([[[pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val]], [[pad_val, pad_val], [0., 1.], [2., 3.], [pad_val, pad_val]], [[pad_val, pad_val], [4., 5.], [6., 7.], [pad_val, pad_val]], [[pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val], [pad_val, pad_val]]]).astype(DTYPE) res = filter_pad2d(arr, filter_shape, constant=pad_val) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def transform(self, arr_in, pad_apron=False, interleave_stride=False): """XXX: docstring for transform""" rcpt_field = self.receptive_field_shape description = self.description input_shape = arr_in.shape assert input_shape[:2] == self.in_shape assert len(input_shape) == 2 or len(input_shape) == 3 if len(input_shape) == 3: tmp_out = arr_in elif len(input_shape) == 2: tmp_out = arr_in[..., np.newaxis] else: raise ValueError("The input array should be 2D or 3D") # -- first we initialize some variables to be used in # the processing of the feature maps h, w = self.in_shape Y, X = np.mgrid[:h, :w] tmp_out_l = [(tmp_out, X, Y)] nbh_nbw_stride_l = self.ops_nbh_nbw_stride # -- loop over all the SLM operations in order op_counter = 0 for layer_idx, layer_desc in enumerate(description): for op_idx, (op_name, op_params) in enumerate(layer_desc): kwargs = op_params['kwargs'] tmp_l = [] _, nbh, nbw, stride = nbh_nbw_stride_l[op_counter] for arr, X, Y in tmp_out_l: tmp_l += self._process_one_op(arr, X, Y, layer_idx, op_idx, kwargs, op_params, op_name, nbh, nbw, stride, pad_apron=pad_apron, interleave_stride=interleave_stride) tmp_out_l = tmp_l op_counter += 1 # -- now we need to possibly interleave the arrays # in ``tmp_out_l`` if interleave_stride: out_shape = (h, w, tmp_out_l[0][0].shape[-1]) arr_out = np.empty(out_shape, dtype=arr_in.dtype) Y_ref, X_ref = np.mgrid[:h, :w] Y_int, X_int = np.zeros((h, w), dtype=np.int), \ np.zeros((h, w), dtype=np.int) if pad_apron: for arr, Xc, Yc in tmp_out_l: anchor_h, anchor_w = Yc[0, 0], Xc[0, 0] stride_h = Yc[1, 0] - Yc[0, 0] stride_w = Xc[0, 1] - Xc[0, 0] arr_out[anchor_h::stride_h, anchor_w::stride_w, ...] = arr X_int[anchor_h::stride_h, anchor_w::stride_w] = Xc Y_int[anchor_h::stride_h, anchor_w::stride_w] = Yc assert (X_int == X_ref).all() assert (Y_int == Y_ref).all() return arr_out else: X_int = filter_pad2d(X_int[..., np.newaxis], rcpt_field, reverse_padding=True).squeeze() Y_int = filter_pad2d(Y_int[..., np.newaxis], rcpt_field, reverse_padding=True).squeeze() X_ref = filter_pad2d(X_ref[..., np.newaxis], rcpt_field, reverse_padding=True).squeeze() Y_ref = filter_pad2d(Y_ref[..., np.newaxis], rcpt_field, reverse_padding=True).squeeze() arr_out = filter_pad2d(arr_out, rcpt_field, reverse_padding=True) offset_Y = Y_ref.min() offset_X = X_ref.min() for arr, Xc, Yc in tmp_out_l: anchor_h, anchor_w = Yc[0, 0] - offset_Y, \ Xc[0, 0] - offset_X stride_h = Yc[1, 0] - Yc[0, 0] stride_w = Xc[0, 1] - Xc[0, 0] arr_out[anchor_h::stride_h, anchor_w::stride_w, ...] = arr X_int[anchor_h::stride_h, anchor_w::stride_w] = Xc Y_int[anchor_h::stride_h, anchor_w::stride_w] = Yc assert (X_int == X_ref).all() assert (Y_int == Y_ref).all() return arr_out else: assert len(tmp_out_l) == 1 arr_out, _, _ = tmp_out_l[0] return arr_out