| 3 | |
| 4 | |
| 5 | class Sobel(nn.Module): |
| 6 | def __init__(self): |
| 7 | super().__init__() |
| 8 | self.filter = nn.Conv2d(in_channels=1, out_channels=2, kernel_size=3, stride=1, padding=1, bias=False) |
| 9 | |
| 10 | Gx = torch.tensor([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]]) |
| 11 | Gy = torch.tensor([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]]) |
| 12 | G = torch.cat([Gx.unsqueeze(0), Gy.unsqueeze(0)], 0) |
| 13 | G = G.unsqueeze(1) |
| 14 | self.filter.weight = nn.Parameter(G, requires_grad=False) |
| 15 | |
| 16 | def forward(self, img): |
| 17 | if img.shape[1] == 3: |
| 18 | img = torch.mean(img, dim=1, keepdim=True) |
| 19 | x = self.filter(img) |
| 20 | x = torch.mul(x, x) |
| 21 | x = torch.sum(x, dim=1, keepdim=True) |
| 22 | x = torch.sqrt(x) |
| 23 | return x |
| 24 | |
| 25 | |
| 26 | class Laplacian(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected