You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.8 KiB
119 lines
3.8 KiB
6 years ago
|
""" PyTorch selectable adaptive pooling
|
||
|
Adaptive pooling with the ability to select the type of pooling from:
|
||
|
* 'avg' - Average pooling
|
||
|
* 'max' - Max pooling
|
||
|
* 'avgmax' - Sum of average and max pooling re-scaled by 0.5
|
||
|
* 'avgmaxc' - Concatenation of average and max pooling along feature dim, doubles feature dim
|
||
|
|
||
|
Both a functional and a nn.Module version of the pooling is provided.
|
||
|
|
||
4 years ago
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
6 years ago
|
"""
|
||
|
import torch
|
||
|
import torch.nn as nn
|
||
|
import torch.nn.functional as F
|
||
|
|
||
|
|
||
5 years ago
|
def adaptive_pool_feat_mult(pool_type='avg'):
|
||
|
if pool_type == 'catavgmax':
|
||
|
return 2
|
||
|
else:
|
||
|
return 1
|
||
|
|
||
|
|
||
|
def adaptive_avgmax_pool2d(x, output_size=1):
|
||
|
x_avg = F.adaptive_avg_pool2d(x, output_size)
|
||
|
x_max = F.adaptive_max_pool2d(x, output_size)
|
||
|
return 0.5 * (x_avg + x_max)
|
||
|
|
||
|
|
||
|
def adaptive_catavgmax_pool2d(x, output_size=1):
|
||
|
x_avg = F.adaptive_avg_pool2d(x, output_size)
|
||
|
x_max = F.adaptive_max_pool2d(x, output_size)
|
||
|
return torch.cat((x_avg, x_max), 1)
|
||
|
|
||
|
|
||
|
def select_adaptive_pool2d(x, pool_type='avg', output_size=1):
|
||
6 years ago
|
"""Selectable global pooling function with dynamic input kernel size
|
||
|
"""
|
||
5 years ago
|
if pool_type == 'avg':
|
||
|
x = F.adaptive_avg_pool2d(x, output_size)
|
||
|
elif pool_type == 'avgmax':
|
||
|
x = adaptive_avgmax_pool2d(x, output_size)
|
||
|
elif pool_type == 'catavgmax':
|
||
|
x = adaptive_catavgmax_pool2d(x, output_size)
|
||
6 years ago
|
elif pool_type == 'max':
|
||
6 years ago
|
x = F.adaptive_max_pool2d(x, output_size)
|
||
6 years ago
|
else:
|
||
5 years ago
|
assert False, 'Invalid pool type: %s' % pool_type
|
||
6 years ago
|
return x
|
||
|
|
||
|
|
||
4 years ago
|
class FastAdaptiveAvgPool2d(nn.Module):
|
||
|
def __init__(self, flatten=False):
|
||
|
super(FastAdaptiveAvgPool2d, self).__init__()
|
||
|
self.flatten = flatten
|
||
|
|
||
|
def forward(self, x):
|
||
3 years ago
|
return x.mean((2, 3), keepdim=not self.flatten)
|
||
4 years ago
|
|
||
|
|
||
5 years ago
|
class AdaptiveAvgMaxPool2d(nn.Module):
|
||
5 years ago
|
def __init__(self, output_size=1):
|
||
|
super(AdaptiveAvgMaxPool2d, self).__init__()
|
||
|
self.output_size = output_size
|
||
|
|
||
|
def forward(self, x):
|
||
|
return adaptive_avgmax_pool2d(x, self.output_size)
|
||
|
|
||
|
|
||
5 years ago
|
class AdaptiveCatAvgMaxPool2d(nn.Module):
|
||
5 years ago
|
def __init__(self, output_size=1):
|
||
|
super(AdaptiveCatAvgMaxPool2d, self).__init__()
|
||
|
self.output_size = output_size
|
||
|
|
||
|
def forward(self, x):
|
||
|
return adaptive_catavgmax_pool2d(x, self.output_size)
|
||
|
|
||
|
|
||
5 years ago
|
class SelectAdaptivePool2d(nn.Module):
|
||
6 years ago
|
"""Selectable global pooling layer with dynamic input kernel size
|
||
|
"""
|
||
4 years ago
|
def __init__(self, output_size=1, pool_type='fast', flatten=False):
|
||
5 years ago
|
super(SelectAdaptivePool2d, self).__init__()
|
||
4 years ago
|
self.pool_type = pool_type or '' # convert other falsy values to empty string for consistent TS typing
|
||
3 years ago
|
self.flatten = nn.Flatten(1) if flatten else nn.Identity()
|
||
4 years ago
|
if pool_type == '':
|
||
|
self.pool = nn.Identity() # pass through
|
||
4 years ago
|
elif pool_type == 'fast':
|
||
|
assert output_size == 1
|
||
3 years ago
|
self.pool = FastAdaptiveAvgPool2d(flatten)
|
||
|
self.flatten = nn.Identity()
|
||
4 years ago
|
elif pool_type == 'avg':
|
||
|
self.pool = nn.AdaptiveAvgPool2d(output_size)
|
||
|
elif pool_type == 'avgmax':
|
||
5 years ago
|
self.pool = AdaptiveAvgMaxPool2d(output_size)
|
||
|
elif pool_type == 'catavgmax':
|
||
|
self.pool = AdaptiveCatAvgMaxPool2d(output_size)
|
||
6 years ago
|
elif pool_type == 'max':
|
||
|
self.pool = nn.AdaptiveMaxPool2d(output_size)
|
||
|
else:
|
||
4 years ago
|
assert False, 'Invalid pool type: %s' % pool_type
|
||
|
|
||
|
def is_identity(self):
|
||
3 years ago
|
return not self.pool_type
|
||
6 years ago
|
|
||
|
def forward(self, x):
|
||
4 years ago
|
x = self.pool(x)
|
||
3 years ago
|
x = self.flatten(x)
|
||
4 years ago
|
return x
|
||
5 years ago
|
|
||
|
def feat_mult(self):
|
||
|
return adaptive_pool_feat_mult(self.pool_type)
|
||
6 years ago
|
|
||
|
def __repr__(self):
|
||
|
return self.__class__.__name__ + ' (' \
|
||
4 years ago
|
+ 'pool_type=' + self.pool_type \
|
||
|
+ ', flatten=' + str(self.flatten) + ')'
|
||
|
|