File size: 1,078 Bytes
36fdbcf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import math
import torch
from typing import List
from torch import Tensor
from .filter import gaussian_blur
def perlin(shape, smoothing, magnitude=1.0, device=None):
"""
Generates a perlin noise image.
Parameters
----------
shape : List[int]
The desired shape of the output tensor. Can be 2D or 3D.
smoothing : float or List[float]
The spatial smoothing sigma in voxel coordinates. If a
single value is provided, it will be used for all dimensions.
magnitude : float or List[float]
The standard deviation of the noise across dimensions. If a single value is
provided, it will be used for all dimensions.
device : torch.device or None, optional
The device on which the output tensor is allocated. If None, defaults to CPU.
Returns
-------
Tensor
A Perlin noise image of shape `shape`.
"""
noise = torch.normal(0, 1, size=shape, device=device).unsqueeze(0)
noise = gaussian_blur(noise, smoothing).squeeze(0)
noise *= magnitude / noise.std()
return noise
|