Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perlin noise #44

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ PyYAML
statsmodels
jsonargparse[all]
zarr
rich
rich
perlin-noise
54 changes: 54 additions & 0 deletions sslt/models/nets/deeplabv3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import lightning as L
from torchvision import models

from .base import SimpleSupervisedModel

class Resnet50Backbone(nn.Module):

def __init__(self) -> None:
super().__init__()
self.resnet50 = models.resnet50()
self.resnet50 = nn.Sequential(*list(self.resnet50.children())[:-2])

def forward(self, x):
return self.resnet50(x)

class DeepLabV3_Head(nn.Module):

def __init__(self) -> None:
super().__init__()
raise NotImplementedError("DeepLabV3's head has not yet been implemented")

def forward(self, x):
raise NotImplementedError("DeepLabV3's head has not yet been implemented")


class DeepLabV3(SimpleSupervisedModel):

"""A DeeplabV3 with a ResNet50 backbone

References
----------
Liang-Chieh Chen, George Papandreou, Florian Schroff, Hartwig Adam. "Rethinking Atrous Convolution for Semantic Image Segmentation", 2017
"""

def __init__(self, learning_rate: float = 1e-3,loss_fn: torch.nn.Module = None):
"""Wrapper implementation of the DeepLabv3 model.

Parameters
----------
learning_rate : float, optional
The learning rate to Adam optimizer, by default 1e-3
loss_fn : torch.nn.Module, optional
The function used to compute the loss. If `None`, it will be used
the MSELoss, by default None.
"""
super().__init__(
backbone=Resnet50Backbone(),
fc=DeepLabV3_Head(),
loss_fn=loss_fn or torch.nn.MSELoss(),
learning_rate=learning_rate,
)
41 changes: 40 additions & 1 deletion sslt/transforms/transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any, List, Sequence

from perlin_noise import PerlinNoise
from itertools import product
import torch
import numpy as np


Expand Down Expand Up @@ -76,3 +78,40 @@ def __call__(self, x: np.ndarray) -> np.ndarray:
x = np.flip(x, axis=axis)

return x


class PerlinMasker(_Transform):
"""Zeroes entries of a tensor according to the sign of Perlin noise. Seed for the noise generator given by torch.randint"""

def __init__(self, octaves: int, scale: float = 1):
"""Zeroes entries of a tensor according to the sign of Perlin noise. Seed for the noise generator given by torch.randint

Parameters
----------
octaves: int
Level of detail for the Perlin noise generator
scale: float = 1
Optionally rescale the Perlin noise. Default is 1 (no rescaling)
"""
if octaves <= 0: raise ValueError(f"Number of octaves must be positive, but got {octaves=}")
if scale == 0: raise ValueError(f"Scale can't be 0")
self.octaves = octaves
self.scale = scale

def __call__(self, x: np.ndarray) -> np.ndarray:
"""Zeroes entries of a tensor according to the sign of Perlin noise.

Parameters
----------
x: np.ndarray
The tensor whose entries to zero.
"""

mask = np.empty_like(x, dtype=bool)
noise = PerlinNoise(self.octaves, torch.randint(0, 2**32, (1,)).item())
denom = self.scale * max(x.shape)

for pos in product(*[range(i) for i in mask.shape]):
mask[pos] = (noise([i/denom for i in pos]) < 0)

return x * mask