Meomory Error with Affine

I am facing RAM memory error with below Affine Function . After few iteration of the function below RAM gets full.

def transform_matrix_offset_center(matrix, x, y):
    """Apply offset to a transform matrix so that the image is
    transformed about the center of the image. 
    NOTE: This is a fairly simple operaion, so can easily be
    moved to full torch.
    matrix : 3x3 matrix/array
    x : integer
        height dimension of image to be transformed
    y : integer
        width dimension of image to be transformed
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix =, matrix), reset_matrix)
    return transform_matrix

def apply_transform(x, transform, fill_mode='nearest', fill_value=0.):
    """Applies an affine transform to a 2D array, or to each channel of a 3D array.
    NOTE: this can and certainly should be moved to full torch operations.
    x : np.ndarray
        array to transform. NOTE: array should be ordered CHW
    transform : 3x3 affine transform matrix
        matrix to apply
    x = x.astype('float32')
    transform = transform_matrix_offset_center(transform, x.shape[1], x.shape[2])
    final_affine_matrix = transform[:2, :2]
    final_offset = transform[:2, 2]
    channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix,
            final_offset, order=0, mode=fill_mode, cval=fill_value) for x_channel in x]
    x = np.stack(channel_images, axis=0)
    return x

class Zoom(object):

    def __init__(self, 
        """Randomly zoom in and/or out on an image 
        zoom_range : tuple or list with 2 values, both between (0, infinity)
            lower and upper bounds on percent zoom. 
            Anything less than 1.0 will zoom in on the image, 
            anything greater than 1.0 will zoom out on the image.
            e.g. (0.7, 1.0) will only zoom in, 
                 (1.0, 1.4) will only zoom out,
                 (0.7, 1.4) will randomly zoom in or out
        fill_mode : string in {'constant', 'nearest'}
            how to fill the empty space caused by the transform
        fill_value : float
            the value to fill the empty space with if fill_mode='constant'
        lazy    : boolean
            if true, perform the transform on the tensor and return the tensor
            if false, only create the affine transform matrix and return that
        if not isinstance(zoom_range, list) and not isinstance(zoom_range, tuple):
            raise ValueError('zoom_range must be tuple or list with 2 values')
        self.zoom_range = zoom_range
        self.fill_mode = fill_mode
        self.fill_value = fill_value
        self.target_fill_mode = target_fill_mode
        self.target_fill_value = target_fill_value
        self.lazy = lazy

    def __call__(self, x, y=None):
        zx = np.random.uniform(self.zoom_range[0], self.zoom_range[1])
        zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1])
        zoom_matrix = np.array([[1/zx, 0, 0],
                                [0, 1/zy, 0],
                                [0, 0, 1]])
        if self.lazy:
            return zoom_matrix
            x_transformed = torch.from_numpy(apply_transform(x.numpy(), 
                zoom_matrix, fill_mode=self.fill_mode, fill_value=self.fill_value))
            if y:
                y_transformed = torch.from_numpy(apply_transform(y.numpy(), zoom_matrix,
                fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
                return x_transformed, y_transformed
                return x_transformed


aug_cur has zoom_fn in list.

for aug_cur in aug:
        #py = to_np(aug_cur(torch.sigmoid(learn.model(aug_cur(x))),
                           #fwd=False, mask=True))
        #py =aug_cur(torch.sigmoid(learn.model(aug_cur(x))),
                           #fwd=False, mask=True) 
        #aug_pred= model([aug_cur(x) for x in x])
        aug_pred= model(aug_cur(

If I understand the issue correctly, you are seeing an increased memory usage, if you run just the posted code?
If so, could you provide all shapes and arguments, so that we could reproduce it?

Shape of Tensor is
6 * 3*280*280 during an inference.
aug_cur is list of Aug which are all flips and Transposes apart from zoom which is last.