Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Yet-Another-EfficientDet-Pytorch/efficientnet/utils_extra.py /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
86 lines (64 sloc)
2.61 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Author: Zylo117 | |
| import math | |
| from torch import nn | |
| import torch.nn.functional as F | |
| class Conv2dStaticSamePadding(nn.Module): | |
| """ | |
| created by Zylo117 | |
| The real keras/tensorflow conv2d with same padding | |
| """ | |
| def __init__(self, in_channels, out_channels, kernel_size, stride=1, bias=True, groups=1, dilation=1, **kwargs): | |
| super().__init__() | |
| self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, | |
| bias=bias, groups=groups) | |
| self.stride = self.conv.stride | |
| self.kernel_size = self.conv.kernel_size | |
| self.dilation = self.conv.dilation | |
| if isinstance(self.stride, int): | |
| self.stride = [self.stride] * 2 | |
| elif len(self.stride) == 1: | |
| self.stride = [self.stride[0]] * 2 | |
| if isinstance(self.kernel_size, int): | |
| self.kernel_size = [self.kernel_size] * 2 | |
| elif len(self.kernel_size) == 1: | |
| self.kernel_size = [self.kernel_size[0]] * 2 | |
| def forward(self, x): | |
| h, w = x.shape[-2:] | |
| extra_h = (math.ceil(w / self.stride[1]) - 1) * self.stride[1] - w + self.kernel_size[1] | |
| extra_v = (math.ceil(h / self.stride[0]) - 1) * self.stride[0] - h + self.kernel_size[0] | |
| left = extra_h // 2 | |
| right = extra_h - left | |
| top = extra_v // 2 | |
| bottom = extra_v - top | |
| x = F.pad(x, [left, right, top, bottom]) | |
| x = self.conv(x) | |
| return x | |
| class MaxPool2dStaticSamePadding(nn.Module): | |
| """ | |
| created by Zylo117 | |
| The real keras/tensorflow MaxPool2d with same padding | |
| """ | |
| def __init__(self, *args, **kwargs): | |
| super().__init__() | |
| self.pool = nn.MaxPool2d(*args, **kwargs) | |
| self.stride = self.pool.stride | |
| self.kernel_size = self.pool.kernel_size | |
| if isinstance(self.stride, int): | |
| self.stride = [self.stride] * 2 | |
| elif len(self.stride) == 1: | |
| self.stride = [self.stride[0]] * 2 | |
| if isinstance(self.kernel_size, int): | |
| self.kernel_size = [self.kernel_size] * 2 | |
| elif len(self.kernel_size) == 1: | |
| self.kernel_size = [self.kernel_size[0]] * 2 | |
| def forward(self, x): | |
| h, w = x.shape[-2:] | |
| extra_h = (math.ceil(w / self.stride[1]) - 1) * self.stride[1] - w + self.kernel_size[1] | |
| extra_v = (math.ceil(h / self.stride[0]) - 1) * self.stride[0] - h + self.kernel_size[0] | |
| left = extra_h // 2 | |
| right = extra_h - left | |
| top = extra_v // 2 | |
| bottom = extra_v - top | |
| x = F.pad(x, [left, right, top, bottom]) | |
| x = self.pool(x) | |
| return x |