Python torch.roll() Examples
The following are 12
code examples of torch.roll().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example #1
Source File: utils_deblur.py From KAIR with MIT License | 6 votes |
def p2o(psf, shape): ''' # psf: NxCxhxw # shape: [H,W] # otf: NxCxHxWx2 ''' otf = torch.zeros(psf.shape[:-2] + shape).type_as(psf) otf[...,:psf.shape[2],:psf.shape[3]].copy_(psf) for axis, axis_size in enumerate(psf.shape[2:]): otf = torch.roll(otf, -int(axis_size / 2), dims=axis+2) otf = torch.rfft(otf, 2, onesided=False) n_ops = torch.sum(torch.tensor(psf.shape).type_as(psf) * torch.log2(torch.tensor(psf.shape).type_as(psf))) otf[...,1][torch.abs(otf[...,1])<n_ops*2.22e-16] = torch.tensor(0).type_as(psf) return otf # otf2psf: not sure where I got this one from. Maybe translated from Octave source code or whatever. It's just math.
Example #2
Source File: utils_sisr.py From KAIR with MIT License | 6 votes |
def p2o(psf, shape): ''' Args: psf: NxCxhxw shape: [H,W] Returns: otf: NxCxHxWx2 ''' otf = torch.zeros(psf.shape[:-2] + shape).type_as(psf) otf[...,:psf.shape[2],:psf.shape[3]].copy_(psf) for axis, axis_size in enumerate(psf.shape[2:]): otf = torch.roll(otf, -int(axis_size / 2), dims=axis+2) otf = torch.rfft(otf, 2, onesided=False) n_ops = torch.sum(torch.tensor(psf.shape).type_as(psf) * torch.log2(torch.tensor(psf.shape).type_as(psf))) otf[...,1][torch.abs(otf[...,1])<n_ops*2.22e-16] = torch.tensor(0).type_as(psf) return otf
Example #3
Source File: test_additive_shared.py From PySyft with Apache License 2.0 | 6 votes |
def test_roll(workers): bob, alice, james = (workers["bob"], workers["alice"], workers["james"]) t = torch.tensor([[1, 2, 3], [4, 5, 6]]) x = t.share(bob, alice, crypto_provider=james) res1 = torch.roll(x, 2) res2 = torch.roll(x, 2, dims=1) res3 = torch.roll(x, (1, 2), dims=(0, 1)) assert (res1.get() == torch.roll(t, 2)).all() assert (res2.get() == torch.roll(t, 2, dims=1)).all() assert (res3.get() == torch.roll(t, (1, 2), dims=(0, 1))).all() # With MultiPointerTensor shifts = torch.tensor(1).send(alice, bob) res = torch.roll(x, shifts) shifts1 = torch.tensor(1).send(alice, bob) shifts2 = torch.tensor(2).send(alice, bob) res2 = torch.roll(x, (shifts1, shifts2), dims=(0, 1)) assert (res.get() == torch.roll(t, 1)).all() assert (res2.get() == torch.roll(t, (1, 2), dims=(0, 1))).all()
Example #4
Source File: mpnn.py From marl_transfer with MIT License | 6 votes |
def calculate_mask(self, inp): # inp is batch_size x self.input_size where batch_size is num_processes*num_agents pos = inp[:, self.pos_index:self.pos_index+2] bsz = inp.size(0)//self.num_agents mask = torch.full(size=(bsz,self.num_agents,self.num_agents),fill_value=0,dtype=torch.uint8) if self.mask_dist is not None and self.mask_dist > 0: for i in range(1,self.num_agents): shifted = torch.roll(pos,-bsz*i,0) dists = torch.norm(pos-shifted,dim=1) restrict = dists > self.mask_dist for x in range(self.num_agents): mask[:,x,(x+i)%self.num_agents].copy_(restrict[bsz*x:bsz*(x+1)]) elif self.mask_dist is not None and self.mask_dist == -10: if self.dropout_mask is None or bsz!=self.dropout_mask.shape[0] or np.random.random_sample() < 0.1: # sample new dropout mask temp = torch.rand(mask.size()) > 0.85 temp.diagonal(dim1=1,dim2=2).fill_(0) self.dropout_mask = (temp+temp.transpose(1,2))!=0 mask.copy_(self.dropout_mask) return mask
Example #5
Source File: utils_deblur.py From KAIR with MIT License | 5 votes |
def otf2psf(otf, outsize=None): insize = np.array(otf.shape) psf = np.fft.ifftn(otf, axes=(0, 1)) for axis, axis_size in enumerate(insize): psf = np.roll(psf, np.floor(axis_size / 2).astype(int), axis=axis) if type(outsize) != type(None): insize = np.array(otf.shape) outsize = np.array(outsize) n = max(np.size(outsize), np.size(insize)) # outsize = postpad(outsize(:), n, 1); # insize = postpad(insize(:) , n, 1); colvec_out = outsize.flatten().reshape((np.size(outsize), 1)) colvec_in = insize.flatten().reshape((np.size(insize), 1)) outsize = np.pad(colvec_out, ((0, max(0, n - np.size(colvec_out))), (0, 0)), mode="constant") insize = np.pad(colvec_in, ((0, max(0, n - np.size(colvec_in))), (0, 0)), mode="constant") pad = (insize - outsize) / 2 if np.any(pad < 0): print("otf2psf error: OUTSIZE must be smaller than or equal than OTF size") prepad = np.floor(pad) postpad = np.ceil(pad) dims_start = prepad.astype(int) dims_end = (insize - postpad).astype(int) for i in range(len(dims_start.shape)): psf = np.take(psf, range(dims_start[i][0], dims_end[i][0]), axis=i) n_ops = np.sum(otf.size * np.log2(otf.shape)) psf = np.real_if_close(psf, tol=n_ops) return psf # psf2otf copied/modified from https://github.com/aboucaud/pypher/blob/master/pypher/pypher.py
Example #6
Source File: roll_dataset.py From fairseq with MIT License | 5 votes |
def __getitem__(self, index): item = self.dataset[index] return torch.roll(item, self.shifts)
Example #7
Source File: test_native.py From PySyft with Apache License 2.0 | 5 votes |
def test_roll(workers): x = torch.tensor([1.0, 2.0, 3, 4, 5]) expected = torch.roll(x, -1) index = torch.tensor([-1.0]) result = torch.roll(x, index) assert (result == expected).all()
Example #8
Source File: roll_dataset.py From attn2d with MIT License | 5 votes |
def __getitem__(self, index): item = self.dataset[index] return torch.roll(item, self.shifts)
Example #9
Source File: utils_deblur.py From KAIR with MIT License | 4 votes |
def psf2otf(psf, shape=None): """ Convert point-spread function to optical transfer function. Compute the Fast Fourier Transform (FFT) of the point-spread function (PSF) array and creates the optical transfer function (OTF) array that is not influenced by the PSF off-centering. By default, the OTF array is the same size as the PSF array. To ensure that the OTF is not altered due to PSF off-centering, PSF2OTF post-pads the PSF array (down or to the right) with zeros to match dimensions specified in OUTSIZE, then circularly shifts the values of the PSF array up (or to the left) until the central pixel reaches (1,1) position. Parameters ---------- psf : `numpy.ndarray` PSF array shape : int Output shape of the OTF array Returns ------- otf : `numpy.ndarray` OTF array Notes ----- Adapted from MATLAB psf2otf function """ if type(shape) == type(None): shape = psf.shape shape = np.array(shape) if np.all(psf == 0): # return np.zeros_like(psf) return np.zeros(shape) if len(psf.shape) == 1: psf = psf.reshape((1, psf.shape[0])) inshape = psf.shape psf = zero_pad(psf, shape, position='corner') for axis, axis_size in enumerate(inshape): psf = np.roll(psf, -int(axis_size / 2), axis=axis) # Compute the OTF otf = np.fft.fft2(psf, axes=(0, 1)) # Estimate the rough number of operations involved in the FFT # and discard the PSF imaginary part if within roundoff error # roundoff error = machine epsilon = sys.float_info.epsilon # or np.finfo().eps n_ops = np.sum(psf.size * np.log2(psf.shape)) otf = np.real_if_close(otf, tol=n_ops) return otf
Example #10
Source File: utils_sisr.py From KAIR with MIT License | 4 votes |
def psf2otf(psf, shape=None): """ Convert point-spread function to optical transfer function. Compute the Fast Fourier Transform (FFT) of the point-spread function (PSF) array and creates the optical transfer function (OTF) array that is not influenced by the PSF off-centering. By default, the OTF array is the same size as the PSF array. To ensure that the OTF is not altered due to PSF off-centering, PSF2OTF post-pads the PSF array (down or to the right) with zeros to match dimensions specified in OUTSIZE, then circularly shifts the values of the PSF array up (or to the left) until the central pixel reaches (1,1) position. Parameters ---------- psf : `numpy.ndarray` PSF array shape : int Output shape of the OTF array Returns ------- otf : `numpy.ndarray` OTF array Notes ----- Adapted from MATLAB psf2otf function """ if type(shape) == type(None): shape = psf.shape shape = np.array(shape) if np.all(psf == 0): # return np.zeros_like(psf) return np.zeros(shape) if len(psf.shape) == 1: psf = psf.reshape((1, psf.shape[0])) inshape = psf.shape psf = zero_pad(psf, shape, position='corner') for axis, axis_size in enumerate(inshape): psf = np.roll(psf, -int(axis_size / 2), axis=axis) # Compute the OTF otf = np.fft.fft2(psf, axes=(0, 1)) # Estimate the rough number of operations involved in the FFT # and discard the PSF imaginary part if within roundoff error # roundoff error = machine epsilon = sys.float_info.epsilon # or np.finfo().eps n_ops = np.sum(psf.size * np.log2(psf.shape)) otf = np.real_if_close(otf, tol=n_ops) return otf
Example #11
Source File: securenn.py From PySyft with Apache License 2.0 | 4 votes |
def maxpool_deriv(x_sh): """ Compute derivative of MaxPool Args: x_sh (AdditiveSharingTensor): the private tensor on which the op applies Returns: an AdditiveSharingTensor of the same shape as x_sh full of zeros except for a 1 at the position of the max value """ assert ( x_sh.dtype != "custom" ), "`custom` dtype shares are unsupported in SecureNN, use dtype = `long` or `int` instead" workers = x_sh.locations crypto_provider = x_sh.crypto_provider L = x_sh.field dtype = get_dtype(L) torch_dtype = get_torch_dtype(L) n1, n2 = x_sh.shape n = n1 * n2 assert L % n == 0 x_sh = x_sh.view(-1) # Common Randomness U_sh = _shares_of_zero(n, L, dtype, crypto_provider, *workers) r = _random_common_value(L, *workers) # 1) _, ind_max_sh = maxpool(x_sh) # 2) j = sy.MultiPointerTensor( children=[torch.tensor([int(i == 0)]).send(w, **no_wrap) for i, w in enumerate(workers)] ) k_sh = ind_max_sh + j * r # 3) t = k_sh.get() k = t % n E_k = torch.zeros(n, dtype=torch_dtype) E_k[k] = 1 E_sh = E_k.share(*workers, field=L, dtype=dtype, **no_wrap) # 4) g = r % n D_sh = torch.roll(E_sh, -g) maxpool_d_sh = D_sh + U_sh return maxpool_d_sh.view(n1, n2)
Example #12
Source File: fog.py From advex-uar with Apache License 2.0 | 4 votes |
def fog_creator(fog_vars, bsize=1, mapsize=256, wibbledecay=1.75): assert (mapsize & (mapsize - 1) == 0) maparray = torch.from_numpy(np.empty((bsize, mapsize, mapsize), dtype=np.float32)).cuda() maparray[:, 0, 0] = 0 stepsize = mapsize wibble = 100 var_num = 0 def wibbledmean(array, var_num): result = array / 4. + fog_vars[var_num] * 2 * wibble - wibble return result def fillsquares(var_num): """For each square of points stepsize apart, calculate middle value as mean of points + wibble""" cornerref = maparray[:, 0:mapsize:stepsize, 0:mapsize:stepsize] squareaccum = cornerref + torch.roll(cornerref, -1, 1) squareaccum = squareaccum + torch.roll(squareaccum, -1, 2) maparray[:, stepsize // 2:mapsize:stepsize, stepsize // 2:mapsize:stepsize] = wibbledmean(squareaccum, var_num) return var_num + 1 def filldiamonds(var_num): """For each diamond of points stepsize apart, calculate middle value as mean of points + wibble""" mapsize = maparray.size(1) drgrid = maparray[:, stepsize // 2:mapsize:stepsize, stepsize // 2:mapsize:stepsize] ulgrid = maparray[:, 0:mapsize:stepsize, 0:mapsize:stepsize] ldrsum = drgrid + torch.roll(drgrid, 2, 1) lulsum = ulgrid + torch.roll(ulgrid, -1, 2) ltsum = ldrsum + lulsum maparray[:, 0:mapsize:stepsize, stepsize // 2:mapsize:stepsize] = wibbledmean(ltsum, var_num) var_num += 1 tdrsum = drgrid + torch.roll(drgrid, 2, 2) tulsum = ulgrid + torch.roll(ulgrid, -1, 1) ttsum = tdrsum + tulsum maparray[:, stepsize // 2:mapsize:stepsize, 0:mapsize:stepsize] = wibbledmean(ttsum, var_num) return var_num + 1 while stepsize >= 2: var_num = fillsquares(var_num) var_num = filldiamonds(var_num) stepsize //= 2 wibble /= wibbledecay maparray = maparray - maparray.min() return (maparray / maparray.max()).reshape(bsize, 1, mapsize, mapsize)