Skip to content

Instantly share code, notes, and snippets.

@velikodniy
Created May 25, 2018 17:29
Show Gist options
  • Save velikodniy/c94751dad217d0c431d5d7cfd28322ac to your computer and use it in GitHub Desktop.
Save velikodniy/c94751dad217d0c431d5d7cfd28322ac to your computer and use it in GitHub Desktop.
Estimate image sharpness
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
class Sharpness(nn.Module):
def __init__(self):
super().__init__()
kernel = [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
kernel = Tensor(kernel)
kernel.unsqueeze_(0)
kernel.unsqueeze_(0)
kernel = kernel.expand((3, 1, 3, 3))
self.register_buffer('kernel', kernel)
def forward(self, x):
y = F.conv2d(x, self.kernel, groups=3)
b, c, h, w = y.shape
y = y.view(b, c, h * w)
return y.std(dim=-1).pow(2).mean(dim=-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment