示例#1
0
def rotate_sample(sample, rotation, reverse=False):

    if reverse:
        if rotation == 1:
            sample = cp.rot90(sample, -2, (0, 1)).copy()
        elif rotation == 2:
            sample = cp.rot90(sample, -1, (0, 1)).copy()
        elif rotation == 3:
            sample = cp.rot90(sample, -1, (1, 0)).copy()
        elif rotation == 4:
            sample = cp.rot90(sample, -1, (2, 0)).copy()
        elif rotation == 5:
            sample = cp.rot90(sample, -1, (0, 2)).copy()
    else:
        if rotation == 1:
            sample = cp.rot90(sample, 2, (0, 1)).copy()
        elif rotation == 2:
            sample = cp.rot90(sample, 1, (0, 1)).copy()
        elif rotation == 3:
            sample = cp.rot90(sample, 1, (1, 0)).copy()
        elif rotation == 4:
            sample = cp.rot90(sample, 1, (2, 0)).copy()
        elif rotation == 5:
            sample = cp.rot90(sample, 1, (0, 2)).copy()

    return sample
示例#2
0
def test_roberts_diagonal2():
    """Roberts' filter on a diagonal edge should be a diagonal line."""
    image = cp.rot90(cp.tri(10, 10, 0), 3)
    expected = ~cp.rot90(
        cp.tri(10, 10, -1).astype(bool)
        | cp.tri(10, 10, -2).astype(bool).transpose())
    expected = _mask_filter_result(expected, None)
    result = filters.roberts(image).astype(bool)
    assert_array_almost_equal(result, expected)
示例#3
0
def regularize_conv(kernel, input_shape, clip_to, devices=-1):

    kernel = cp.transpose(kernel, (3, 2, 1, 0))  # tensor flow format

    kernel = cp.rot90(kernel, k=2, axes=(0, 1))  # normalizing the back prop
    kernel = cp.transpose(kernel, (0, 1, 3, 2))

    kernel = cp_regularize_conv(kernel, input_shape, clip_to, devices)

    kernel = cp.transpose(kernel, (0, 1, 3, 2))
    kernel = cp.rot90(kernel, k=2, axes=(0, 1))

    kernel = cp.transpose(kernel, (3, 2, 1, 0))  # back to chainer format
    return kernel
示例#4
0
    def backward(self, dA):
        """Using numpy stride tricks for the backward propagation implementation.
        Args:
            dA (np.array): gradient of output values
        Returns:
            np.array: dX gradient of input values
        """
        if len(dA.shape) == 2:
            dZ = dA.reshape(dA.shape[1], *self.dim_out[1:]) * self._deriv_relu(
                self.Z)
        else:
            dZ = dA * self._deriv_relu(self.Z)
        if dZ.shape[0] != self.dZ_pad.shape[0]:
            self.dZ_pad = self._allocate_dZ_pad(dZ.shape[0])
        self.dW[:, :, :, :] = 0
        self.db[:, :, :, :] = 0
        (m, n_H_prev, n_W_prev, n_C_prev) = self.dim_in
        (f, f, n_C_prev, n_C) = self.W.shape
        stride = self.stride
        (m, n_H, n_W, n_C) = dZ.shape
        W_rot = np.rot90(self.W, 2)
        pad_dZ = self.W.shape[0] - (self.pad + 1)
        if pad_dZ == 0:
            self.dZ_pad[:, 0::stride, 0::stride] = dZ
        else:
            self.dZ_pad[:, pad_dZ:-pad_dZ:stride,
                        pad_dZ:-pad_dZ:stride, :] = dZ

        shape = (
            self.dZ_pad.shape[0],  # m
            self.dZ_pad.shape[1] - W_rot.shape[0] + 1,  # X_nx
            self.dZ_pad.shape[2] - W_rot.shape[1] + 1,  # X_ny
            self.dZ_pad.shape[3],  # dZ_nc
            W_rot.shape[0],  # f
            W_rot.shape[1])  # f
        strides = (self.dZ_pad.strides[0], self.dZ_pad.strides[1],
                   self.dZ_pad.strides[2], self.dZ_pad.strides[3],
                   self.dZ_pad.strides[1], self.dZ_pad.strides[2])
        M = np.lib.stride_tricks.as_strided(
            self.dZ_pad, shape=shape, strides=strides)  # , writeable=False,)
        self.dX = np.tensordot(M, W_rot, axes=((4, 5, 3), (0, 1, 3)))
        #self.dX = np.einsum('pqrs,bmnspq->bmnr', W_rot, M)

        shape_Z = (f, f, n_C_prev, m, n_H, n_W)
        strides_Z = (self.X_pad.strides)[1:] + (self.X_pad.strides)[0:3]
        strides_Z = (*strides_Z[:-2], strides_Z[-2] * stride,
                     strides_Z[-1] * stride)

        M = np.lib.stride_tricks.as_strided(
            self.X_pad, shape=shape_Z, strides=strides_Z)  # , writeable=False)
        # self.dW = np.einsum('abcd,pqsabc->pqsd', dZ, M)
        self.dW = np.tensordot(M, dZ, axes=((3, 4, 5), (0, 1, 2)))
        self.dW += self.lamb / self.dim_in[0] * self.W

        # self.db = np.einsum('abcd->d', dZ).reshape(1, 1, 1, n_C)
        self.db = np.sum(dZ, axis=(0, 1, 2)).reshape(1, 1, 1, n_C)

        return self.dX
示例#5
0
    def __getitem__(self, index):

        idx = index

        ID = self.list_IDs[idx][0]
        rotation = self.list_IDs[idx][1]

        filename = 'data/' + str(self.resolution) + '/' + ID + '.binvox'

        with open(filename, 'rb') as f:
            sample = libs.binvox_rw.read_as_3d_array(f).data

        if rotation == 1:
            sample = cp.rot90(sample, 2, (0, 1)).copy()
        elif rotation == 2:
            sample = cp.rot90(sample, 1, (0, 1)).copy()
        elif rotation == 3:
            sample = cp.rot90(sample, 1, (1, 0)).copy()
        elif rotation == 4:
            sample = cp.rot90(sample, 1, (2, 0)).copy()
        elif rotation == 5:
            sample = cp.rot90(sample, 1, (0, 2)).copy()

        if self.data_augmentation:
            sample = dataAugmentation(sample).copy()

        label = int(os.path.basename(filename).split('_')[0])

        if self.output_type == '3d':
            sample = cp.expand_dims(sample, axis=3)
            sample = torch.from_numpy(sample).float()
            sample = 2 * (sample - 0.5)
        elif self.output_type == '2d_multiple':
            sample = createImgs(sample, self.num_cuts)
        elif self.output_type == '2d_single':
            sample = createImgs(sample, self.num_cuts)
            label = torch.zeros(self.num_cuts, dtype=torch.int64) + label

        return sample, label
示例#6
0
def rotateStack(origStack, angleInitial=0, angleStep=90, rotateaxes=(0,1), gpu=True):
    # pop out a warning
    print('Rotating the stack...')
    print('This functions assumes the axis=0 as the one storing different views')
    
    # create the 
    rotatedStack = np.zeros_like(origStack)
    
    if gpu == True:
        # perform the rotation
        print('Running on the GPU')
        for i in range(origStack.shape[0]):
            if angleStep == 0:
                rotatedStack[i,:,:,:] = origStack[i,:,:,:]
            elif angleStep != 90:
                tempStack = cp.asarray(origStack[i,:,:,:])
                rotatedStack[i,:,:,:] = cupyx.scipy.ndimage.rotate(tempStack,   -angleStep*i, axes=rotateaxes, reshape=False).get()    
            else:
                tempStack = cp.asarray(origStack[i,:,:,:])
                rotatedStack[i,:,:,:] = cp.rot90(tempStack, -i, axes=rotateaxes).get()
            
            print('Rotating view ' + str(i) + ' - deg = ' + str(-angleStep*i))
    
    else:
        # perform the rotation
        print('Running on the CPU')
        for i in range(origStack.shape[0]):
            if angleStep == 0:
                rotatedStack[i,:,:,:] = origStack[i,:,:,:]
            elif angleStep != 90:
                rotatedStack[i,:,:,:] = scipy.ndimage.rotate(origStack[i,:,:,:],   -angleStep*i, axes=rotateaxes, reshape=False)    
            else:
                rotatedStack[i,:,:,:] = np.rot90(origStack[i,:,:,:], -i, axes=rotateaxes)
            
            print('Rotating view ' + str(i) + ' - deg = ' + str(-angleStep*i))

    return rotatedStack
示例#7
0
def rotate_sample24(sample):

    #    strategy = random.randint(0,3)
    #
    #    if strategy <= 2:
    #        sample = cp.flip(sample,strategy).copy()

    rotation = random.randint(0, 23)

    if rotation == 1:
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 2:
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 3:
        sample = cp.rot90(sample, 1, (2, 1)).copy()
    elif rotation == 4:
        sample = cp.rot90(sample, 1, (0, 1)).copy()
    elif rotation == 5:
        sample = cp.rot90(sample, 1, (0, 1)).copy()
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 6:
        sample = cp.rot90(sample, 1, (0, 1)).copy()
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 7:
        sample = cp.rot90(sample, 1, (0, 1)).copy()
        sample = cp.rot90(sample, 1, (2, 1)).copy()
    elif rotation == 8:
        sample = cp.rot90(sample, 1, (1, 0)).copy()
    elif rotation == 9:
        sample = cp.rot90(sample, 1, (1, 0)).copy()
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 10:
        sample = cp.rot90(sample, 1, (1, 0)).copy()
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 11:
        sample = cp.rot90(sample, 1, (1, 0)).copy()
        sample = cp.rot90(sample, 1, (2, 1)).copy()
    elif rotation == 12:
        sample = cp.rot90(sample, 2, (1, 0)).copy()
    elif rotation == 13:
        sample = cp.rot90(sample, 2, (1, 0)).copy()
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 14:
        sample = cp.rot90(sample, 2, (1, 0)).copy()
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 15:
        sample = cp.rot90(sample, 2, (1, 0)).copy()
        sample = cp.rot90(sample, 1, (2, 1)).copy()
    elif rotation == 16:
        sample = cp.rot90(sample, 1, (0, 2)).copy()
    elif rotation == 17:
        sample = cp.rot90(sample, 1, (0, 2)).copy()
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 18:
        sample = cp.rot90(sample, 1, (0, 2)).copy()
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 19:
        sample = cp.rot90(sample, 1, (0, 2)).copy()
        sample = cp.rot90(sample, 1, (2, 1)).copy()
    elif rotation == 20:
        sample = cp.rot90(sample, 1, (2, 0)).copy()
    elif rotation == 21:
        sample = cp.rot90(sample, 1, (2, 0)).copy()
        sample = cp.rot90(sample, 1, (1, 2)).copy()
    elif rotation == 22:
        sample = cp.rot90(sample, 1, (2, 0)).copy()
        sample = cp.rot90(sample, 2, (1, 2)).copy()
    elif rotation == 23:
        sample = cp.rot90(sample, 1, (2, 0)).copy()
        sample = cp.rot90(sample, 1, (2, 1)).copy()

    return sample
示例#8
0
i_shift = int(shift)

csv_path = stack[0:stack.rfind(".")] + "_Tij.csv"
if (os.path.isfile(csv_path)):
    print("csv_file already exists")
    print()
else:
    with mrcfile.open(stack, permissive=True) as mrc:
        cp_dens = cp.asarray(mrc.data, dtype="float32")
    mrc.close

    sta_dens = cp_dens.shape[0]
    print("number of density images = " + str(sta_dens))
    print()

    cp_dens_rot = cp.rot90(cp_dens, axes=(1, 2))
    cp_dens_rot = cp.rot90(cp_dens_rot, axes=(1, 2))

    Tij = cp.zeros((sta_dens, sta_dens))
    temp_dens = cp.zeros(cp_dens.shape)
    for i in range(sta_dens):
        Tij_shift_rot = cp.zeros((2, sta_dens, 2 * i_shift, 2 * i_shift))
        temp_dens[:, :, :] = cp_dens[i, :, :]
        for x in range(2 * i_shift):
            for y in range(2 * i_shift):
                if ((y - i_shift == 0) & (x - i_shift == 0)):
                    cp_dens_shift[:, :, :] = cp_dens[:, :, :]
                    cp_dens_rot_shift[:, :, :] = cp_dens_rot[:, :, :]
                elif ((y - i_shift == 0) & (x - i_shift != 0)):
                    cp_dens_shift = cp.roll(cp_dens, x - i_shift, axis=1)
                    cp_dens_rot_shift = cp.roll(cp_dens_rot,