示例#1
0
    def __getitem__(self, item):
        """Index or slice into array."""
        if self.sparse:
            raise SignalError("Attempting to create a view of a sparse Signal")

        if item is Ellipsis or (isinstance(item, slice) and item == slice(None)):
            return self

        if not isinstance(item, tuple):
            item = (item,)

        if not all(npext.is_integer(i) or isinstance(i, slice) for i in item):
            raise SignalError("Can only index or slice into signals")

        if all(npext.is_integer(i) for i in item):
            # turn one index into slice to get a view from numpy
            item = item[:-1] + (slice(item[-1], item[-1] + 1),)

        view = self._initial_value[item]
        offset = npext.array_offset(view) - npext.array_offset(self._initial_value)
        return Signal(
            view,
            name=f"{self.name}[{item}]",
            base=self.base,
            offset=offset,
            readonly=self.readonly,
        )
示例#2
0
    def __getitem__(self, item):
        """Index or slice into array"""
        if not isinstance(item, tuple):
            item = (item,)

        if not all(is_integer(i) or isinstance(i, slice) for i in item):
            raise SignalError("Can only index or slice into signals")

        if all(map(is_integer, item)):
            # turn one index into slice to get a view from numpy
            item = item[:-1] + (slice(item[-1], item[-1]+1),)

        view = self._initial_value[item]
        offset = (npext.array_offset(view)
                  - npext.array_offset(self._initial_value))
        return Signal(view, name="%s[%s]" % (self.name, item),
                      base=self.base, offset=offset)
示例#3
0
    def __getitem__(self, item):
        """Index or slice into array"""
        if not isinstance(item, tuple):
            item = (item, )

        if not all(is_integer(i) or isinstance(i, slice) for i in item):
            raise SignalError("Can only index or slice into signals")

        if all(map(is_integer, item)):
            # turn one index into slice to get a view from numpy
            item = item[:-1] + (slice(item[-1], item[-1] + 1), )

        view = self._initial_value[item]
        offset = (npext.array_offset(view) -
                  npext.array_offset(self._initial_value))
        return Signal(view,
                      name="%s[%s]" % (self.name, item),
                      base=self.base,
                      offset=offset)
示例#4
0
def extract_sliding_windows(x, ksize, pad, stride, floor_first=True):
    """Converts a tensor to sliding windows.

    .. note:: Copied from MIT licensed https://github.com/renmengye/np-conv2d

    Arguments
    ---------
    x : np.array
        Input with shape [N, H, W, C]
    ksize : Tuple
        KH, KW
    pad : str or int
        Padding strategy or [PH, PW].
    stride : int
        Stride, [SH, SW].

    Returns
    -------
    y : np.array
        Sliding window: [N, (H-KH+PH+1)/SH, (W-KW+PW+1)/SW, KH * KW, C]
    """
    n, h, w, c = x.shape
    kh, kw = ksize
    sh, sw = stride

    h2 = int(calc_size(h, kh, pad, sh))
    w2 = int(calc_size(w, kw, pad, sw))
    ph = int(calc_pad(pad, h, h2, sh, kh))
    pw = int(calc_pad(pad, w, w2, sw, kw))

    ph0 = int(np.floor(ph / 2))
    ph1 = int(np.ceil(ph / 2))
    pw0 = int(np.floor(pw / 2))
    pw1 = int(np.ceil(pw / 2))

    if floor_first:
        pph = (ph0, ph1)
        ppw = (pw0, pw1)
    else:
        pph = (ph1, ph0)
        ppw = (pw1, pw0)
    x = np.pad(x, ((0, 0), pph, ppw, (0, 0)),
               mode="constant",
               constant_values=(0.0, ))

    x_sn, x_sh, x_sw, x_sc = x.strides  # pylint: disable=unpacking-non-sequence
    y_strides = (x_sn, sh * x_sh, sw * x_sw, x_sh, x_sw, x_sc)
    y = np.ndarray(
        (n, h2, w2, kh, kw, c),
        dtype=x.dtype,
        buffer=x.data,
        offset=array_offset(x),
        strides=y_strides,
    )
    return y
示例#5
0
    def __getstate__(self):
        state = dict(self.__dict__)

        if not self.sparse:
            # For normal arrays, the initial value could be a view on another
            # signal's data. To make sure we do not make a copy of the data,
            # we store the underlying metadata in a tuple, which pickle can
            # inspect to see that v.base is the same in different signals
            # and avoid serializing it multiple times.
            v = self._initial_value
            state['_initial_value'] = (v.shape, v.base, npext.array_offset(v),
                                       v.strides)

        return state
示例#6
0
def extract_sliding_windows(x, ksize, pad, stride, floor_first=True):
    """Converts a tensor to sliding windows.
    Args:
        x: [N, H, W, C]
        k: [KH, KW]
        pad: [PH, PW]
        stride: [SH, SW]
    Returns:
        y: [N, (H-KH+PH+1)/SH, (W-KW+PW+1)/SW, KH * KW, C]
    """
    n = x.shape[0]
    h = x.shape[1]
    w = x.shape[2]
    c = x.shape[3]
    kh = ksize[0]
    kw = ksize[1]
    sh = stride[0]
    sw = stride[1]

    h2 = int(calc_size(h, kh, pad, sh))
    w2 = int(calc_size(w, kw, pad, sw))
    ph = int(calc_pad(pad, h, h2, sh, kh))
    pw = int(calc_pad(pad, w, w2, sw, kw))

    ph0 = int(np.floor(ph / 2))
    ph1 = int(np.ceil(ph / 2))
    pw0 = int(np.floor(pw / 2))
    pw1 = int(np.ceil(pw / 2))

    if floor_first:
        pph = (ph0, ph1)
        ppw = (pw0, pw1)
    else:
        pph = (ph1, ph0)
        ppw = (pw1, pw0)
    x = np.pad(x, ((0, 0), pph, ppw, (0, 0)),
               mode='constant',
               constant_values=(0.0, ))

    x_sn, x_sh, x_sw, x_sc = x.strides
    y_strides = (x_sn, sh * x_sh, sw * x_sw, x_sh, x_sw, x_sc)
    y = np.ndarray((n, h2, w2, kh, kw, c),
                   dtype=x.dtype,
                   buffer=x.data,
                   offset=array_offset(x),
                   strides=y_strides)
    return y
示例#7
0
文件: conv2d.py 项目: nengo/nengo
def extract_sliding_windows(x, ksize, pad, stride, floor_first=True):
    """Converts a tensor to sliding windows.
    Args:
        x: [N, H, W, C]
        k: [KH, KW]
        pad: [PH, PW]
        stride: [SH, SW]
    Returns:
        y: [N, (H-KH+PH+1)/SH, (W-KW+PW+1)/SW, KH * KW, C]
    """
    n = x.shape[0]
    h = x.shape[1]
    w = x.shape[2]
    c = x.shape[3]
    kh = ksize[0]
    kw = ksize[1]
    sh = stride[0]
    sw = stride[1]

    h2 = int(calc_size(h, kh, pad, sh))
    w2 = int(calc_size(w, kw, pad, sw))
    ph = int(calc_pad(pad, h, h2, sh, kh))
    pw = int(calc_pad(pad, w, w2, sw, kw))

    ph0 = int(np.floor(ph / 2))
    ph1 = int(np.ceil(ph / 2))
    pw0 = int(np.floor(pw / 2))
    pw1 = int(np.ceil(pw / 2))

    if floor_first:
        pph = (ph0, ph1)
        ppw = (pw0, pw1)
    else:
        pph = (ph1, ph0)
        ppw = (pw1, pw0)
    x = np.pad(
        x, ((0, 0), pph, ppw, (0, 0)),
        mode='constant',
        constant_values=(0.0,))

    x_sn, x_sh, x_sw, x_sc = x.strides
    y_strides = (x_sn, sh*x_sh, sw*x_sw, x_sh, x_sw, x_sc)
    y = np.ndarray((n, h2, w2, kh, kw, c), dtype=x.dtype, buffer=x.data,
                   offset=array_offset(x), strides=y_strides)
    return y
示例#8
0
文件: signal.py 项目: JolyZhang/nengo
    def init(self, signal):
        """Set up a permanent mapping from signal -> ndarray."""
        if signal in self:
            raise SignalError("Cannot add signal twice")

        x = signal.initial_value
        if signal.is_view:
            if signal.base not in self:
                self.init(signal.base)

            # get a view onto the base data
            offset = npext.array_offset(x)
            view = np.ndarray(shape=x.shape, strides=x.strides, offset=offset,
                              dtype=x.dtype, buffer=self[signal.base].data)
            view.setflags(write=not signal.readonly)
            dict.__setitem__(self, signal, view)
        else:
            x = x.view() if signal.readonly else x.copy()
            dict.__setitem__(self, signal, x)
示例#9
0
文件: signal.py 项目: shaunren/nengo
    def init(self, signal):
        """Set up a permanent mapping from signal -> ndarray."""
        if signal in self:
            raise SignalError("Cannot add signal twice")

        x = signal.initial_value
        if signal.is_view:
            if signal.base not in self:
                self.init(signal.base)

            # get a view onto the base data
            offset = npext.array_offset(x)
            view = np.ndarray(shape=x.shape, strides=x.strides, offset=offset,
                              dtype=x.dtype, buffer=self[signal.base].data)
            view.setflags(write=not signal.readonly)
            dict.__setitem__(self, signal, view)
        else:
            x = x.view() if signal.readonly else x.copy()
            dict.__setitem__(self, signal, x)
示例#10
0
文件: signal.py 项目: utsavakru/nengo
 def __getstate__(self):
     state = dict(self.__dict__)
     v = self._initial_value
     state['_initial_value'] = (v.shape, v.base, npext.array_offset(v),
                                v.strides)
     return state
示例#11
0
 def offset(self):
     """Offset of data from base in bytes."""
     return npext.array_offset(self.initial_value)
示例#12
0
文件: signal.py 项目: JolyZhang/nengo
 def offset(self):
     """Offset of data from base in bytes."""
     return npext.array_offset(self.initial_value)
示例#13
0
 def offset(self):
     return npext.array_offset(self.value) / self.itemsize
示例#14
0
def extract_sliding_windows_gradx(x,
                                  ksize,
                                  pad,
                                  stride,
                                  orig_size,
                                  floor_first=False):
    """Extracts windows on a dilated image.

    .. note:: Copied from MIT licensed https://github.com/renmengye/np-conv2d

    Arguments
    ---------
    x : np.array
        Input with shape [N, H', W', C] (usually dy)
    ksize : Tuple
        KH, KW
    pad : str or int
        Padding strategy or [PH, PW].
    stride : int
        Stride, [SH, SW].
    orig_size : Tuple
        H, W

    Returns
    -------
    y : np.array
        Sliding window: [N, H, W, KH, KW, C]
    """
    n, h, w, c = x.shape
    kh, kw = ksize
    sh, sw = stride
    ph, pw = pad
    sh, sw = stride
    h2, w2 = orig_size

    xs = np.zeros([n, h, sh, w, sw, c])
    xs[:, :, 0, :, 0, :] = x
    xss = xs.shape
    x = xs.reshape([xss[0], xss[1] * xss[2], xss[3] * xss[4], xss[5]])
    x = x[:, :h2, :w2, :]

    ph2 = int(np.ceil(ph / 2))
    ph3 = int(np.floor(ph / 2))
    pw2 = int(np.ceil(pw / 2))
    pw3 = int(np.floor(pw / 2))
    if floor_first:
        pph = (ph3, ph2)
        ppw = (pw3, pw2)
    else:
        pph = (ph2, ph3)
        ppw = (pw2, pw3)
    x = np.pad(x, ((0, 0), pph, ppw, (0, 0)),
               mode="constant",
               constant_values=(0.0, ))

    # The following code extracts window without copying the data:
    # y = np.zeros([n, h2, w2, kh, kw, c])
    # for ii in range(h2):
    #     for jj in range(w2):
    #         y[:, ii, jj, :, :, :] = x[:, ii:ii + kh, jj:jj + kw, :]
    x_sn, x_sh, x_sw, x_sc = x.strides  # pylint: disable=unpacking-non-sequence
    y_strides = (x_sn, x_sh, x_sw, x_sh, x_sw, x_sc)
    y = np.ndarray(
        (n, h2, w2, kh, kw, c),
        dtype=x.dtype,
        buffer=x.data,
        offset=array_offset(x),
        strides=y_strides,
    )
    return y
示例#15
0
文件: signal.py 项目: nengo/nengo
 def __getstate__(self):
     state = dict(self.__dict__)
     v = self._initial_value
     state['_initial_value'] = (
         v.shape, v.base, npext.array_offset(v), v.strides)
     return state
示例#16
0
 def offset(self):
     return npext.array_offset(self.value) / self.itemsize