Skip to content

Instantly share code, notes, and snippets.

View torridgristle's full-sized avatar

torridgristle

View GitHub Profile
@torridgristle
torridgristle / depth_map_blur.py
Created August 24, 2022 18:58
Blur an image with a depth map in PyTorch. Splits the map into ranges of values, multiplies the image by those ranges, blurs them and the split map, sums all the blurred images and blurred maps together, divide blurred image sum by blurred map sum.
#1 is end and 0 is start in the map.
def map_blur(img,map,s_start=0.375,s_end=8,steps=8):
img_slices = img * 0
map_slices = map * 0
for s in range(steps):
sigma = (s/(steps-1)) * (s_end-s_start) + s_start
slice_start = (s+0)/steps
slice_end = (s+1)/steps
map_slice = torch.logical_and(
torch.greater_equal(map,slice_start),
@torridgristle
torridgristle / Max Smooth Unpooling.py
Created August 3, 2022 13:43
Max Pool 2d Unpooling
# Perform max pool 2d with indicies on a tensor
max_size = 8
max_output, max_indices = F.max_pool2d_with_indices(input_tensor,max_size)
# Unpool it to get a tensor of the original size with zeros in all non-max areas
max_unpool = F.max_unpool2d(max_output,max_indices,max_size,max_size)
# Unpool it using a tensor of ones with the same indices to get ones where the tensor was sampled
max_mask = F.max_unpool2d(torch.ones_like(max_output),max_indices,max_size,max_size)
# Makes a kernel that's round and the distance from the center
@torridgristle
torridgristle / sobel_scharr_farid_modules.py
Created February 28, 2022 14:25
Sobel and Farid edge detection modules for PyTorch. Option for using Scharr kernel instead of Sobel is enabled by default and has better rotational symmetry.
import torch
import torch.nn as nn
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Sobel(nn.Module):
def __init__(self,structure=False,scharr=True, padding_mode='reflect'):
super().__init__()
self.structure = structure
@torridgristle
torridgristle / kaiser_lowpass.py
Last active February 28, 2022 14:25
Kaiser Filter Lowpass Module for PyTorch. Torchvision's gaussian blur uses the "reflect" padding mode but I'm not sure if that makes sense so I've set it for "replicate" by default.
import torch
import torch.nn as nn
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class KaiserLowpass(nn.Module):
def __init__(self, width=7, beta=11, periodic=False, padding_mode='replicate'):
super().__init__()
self.padding_mode = padding_mode