|
| 1 | +""" |
| 2 | +Modified from https://github.com/facebookresearch/detectron2/blob/master |
| 3 | +/detectron2/layers/wrappers.py |
| 4 | +Wrap some nn modules to support empty tensor input. |
| 5 | +Currently, these wrappers are mainly used in mask heads like fcn_mask_head |
| 6 | +and maskiou_heads since mask heads are trained on only positive RoIs. |
| 7 | +""" |
| 8 | +import math |
| 9 | + |
| 10 | +import torch |
| 11 | +import torch.nn as nn |
| 12 | +from torch.nn.modules.utils import _pair |
| 13 | + |
| 14 | + |
| 15 | +class NewEmptyTensorOp(torch.autograd.Function): |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def forward(ctx, x, new_shape): |
| 19 | + ctx.shape = x.shape |
| 20 | + return x.new_empty(new_shape) |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def backward(ctx, grad): |
| 24 | + shape = ctx.shape |
| 25 | + return NewEmptyTensorOp.apply(grad, shape), None |
| 26 | + |
| 27 | + |
| 28 | +class Conv2d(nn.Conv2d): |
| 29 | + |
| 30 | + def forward(self, x): |
| 31 | + if x.numel() == 0 and torch.__version__ <= '1.4': |
| 32 | + out_shape = [x.shape[0], self.out_channels] |
| 33 | + for i, k, p, s, d in zip(x.shape[-2:], self.kernel_size, |
| 34 | + self.padding, self.stride, self.dilation): |
| 35 | + o = (i + 2 * p - (d * (k - 1) + 1)) // s + 1 |
| 36 | + out_shape.append(o) |
| 37 | + empty = NewEmptyTensorOp.apply(x, out_shape) |
| 38 | + if self.training: |
| 39 | + # produce dummy gradient to avoid DDP warning. |
| 40 | + dummy = sum(x.view(-1)[0] for x in self.parameters()) * 0.0 |
| 41 | + return empty + dummy |
| 42 | + else: |
| 43 | + return empty |
| 44 | + |
| 45 | + return super().forward(x) |
| 46 | + |
| 47 | + |
| 48 | +class ConvTranspose2d(nn.ConvTranspose2d): |
| 49 | + |
| 50 | + def forward(self, x): |
| 51 | + if x.numel() == 0 and torch.__version__ <= '1.4.0': |
| 52 | + out_shape = [x.shape[0], self.out_channels] |
| 53 | + for i, k, p, s, d, op in zip(x.shape[-2:], self.kernel_size, |
| 54 | + self.padding, self.stride, |
| 55 | + self.dilation, self.output_padding): |
| 56 | + out_shape.append((i - 1) * s - 2 * p + (d * (k - 1) + 1) + op) |
| 57 | + empty = NewEmptyTensorOp.apply(x, out_shape) |
| 58 | + if self.training: |
| 59 | + # produce dummy gradient to avoid DDP warning. |
| 60 | + dummy = sum(x.view(-1)[0] for x in self.parameters()) * 0.0 |
| 61 | + return empty + dummy |
| 62 | + else: |
| 63 | + return empty |
| 64 | + |
| 65 | + return super(ConvTranspose2d, self).forward(x) |
| 66 | + |
| 67 | + |
| 68 | +class MaxPool2d(nn.MaxPool2d): |
| 69 | + |
| 70 | + def forward(self, x): |
| 71 | + if x.numel() == 0 and torch.__version__ <= '1.4': |
| 72 | + out_shape = list(x.shape[:2]) |
| 73 | + for i, k, p, s, d in zip(x.shape[-2:], _pair(self.kernel_size), |
| 74 | + _pair(self.padding), _pair(self.stride), |
| 75 | + _pair(self.dilation)): |
| 76 | + o = (i + 2 * p - (d * (k - 1) + 1)) / s + 1 |
| 77 | + o = math.ceil(o) if self.ceil_mode else math.floor(o) |
| 78 | + out_shape.append(o) |
| 79 | + empty = NewEmptyTensorOp.apply(x, out_shape) |
| 80 | + return empty |
| 81 | + |
| 82 | + return super().forward(x) |
| 83 | + |
| 84 | + |
| 85 | +class Linear(torch.nn.Linear): |
| 86 | + |
| 87 | + def forward(self, x): |
| 88 | + if x.numel() == 0: |
| 89 | + out_shape = [x.shape[0], self.out_features] |
| 90 | + empty = NewEmptyTensorOp.apply(x, out_shape) |
| 91 | + if self.training: |
| 92 | + # produce dummy gradient to avoid DDP warning. |
| 93 | + dummy = sum(x.view(-1)[0] for x in self.parameters()) * 0.0 |
| 94 | + return empty + dummy |
| 95 | + else: |
| 96 | + return empty |
| 97 | + |
| 98 | + return super().forward(x) |
0 commit comments