我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用torch.nn.AdaptiveAvgPool2d()。
def _make_test_model(): import torch.nn as nn from inferno.extensions.layers.reshape import AsMatrix toy_net = nn.Sequential(nn.Conv2d(3, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, 1, 1), nn.ELU(), nn.AdaptiveAvgPool2d((1, 1)), AsMatrix(), nn.Linear(256, 10), nn.Softmax()) return toy_net
def build_graph_model(self): model = Graph() model\ .add_input_node('input')\ .add_node('conv1', Conv2D(3, 32, 3), 'input')\ .add_node('conv2', BNReLUConv2D(32, 32, 3), 'conv1')\ .add_node('pool1', nn.MaxPool2d(kernel_size=2, stride=2), 'conv2')\ .add_node('conv3', BNReLUConv2D(32, 32, 3), 'pool1')\ .add_node('pool2', nn.MaxPool2d(kernel_size=2, stride=2), 'conv3')\ .add_node('conv4', BNReLUConv2D(32, 32, 3), 'pool2')\ .add_node('pool3', nn.AdaptiveAvgPool2d(output_size=(1, 1)), 'conv4')\ .add_node('matrix', AsMatrix(), 'pool3')\ .add_node('linear', nn.Linear(32, self.NUM_CLASSES), 'matrix')\ .add_node('softmax', nn.Softmax(), 'linear')\ .add_output_node('output', 'softmax') return model
def __init__(self, cut_at_pooling=False, num_features=256, norm=False, dropout=0, num_classes=0): super(InceptionNet, self).__init__() self.cut_at_pooling = cut_at_pooling self.conv1 = _make_conv(3, 32) self.conv2 = _make_conv(32, 32) self.conv3 = _make_conv(32, 32) self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=1) self.in_planes = 32 self.inception4a = self._make_inception(64, 'Avg', 1) self.inception4b = self._make_inception(64, 'Max', 2) self.inception5a = self._make_inception(128, 'Avg', 1) self.inception5b = self._make_inception(128, 'Max', 2) self.inception6a = self._make_inception(256, 'Avg', 1) self.inception6b = self._make_inception(256, 'Max', 2) if not self.cut_at_pooling: self.num_features = num_features self.norm = norm self.dropout = dropout self.has_embedding = num_features > 0 self.num_classes = num_classes self.avgpool = nn.AdaptiveAvgPool2d(1) if self.has_embedding: self.feat = nn.Linear(self.in_planes, self.num_features) self.feat_bn = nn.BatchNorm1d(self.num_features) else: # Change the num_features to CNN output channels self.num_features = self.in_planes if self.dropout > 0: self.drop = nn.Dropout(self.dropout) if self.num_classes > 0: self.classifier = nn.Linear(self.num_features, self.num_classes) self.reset_params()
def __init__(self, in_dim, reduction_dim, setting): super(_PyramidPoolingModule, self).__init__() self.features = [] for s in setting: self.features.append(nn.Sequential( nn.AdaptiveAvgPool2d(s), nn.Conv2d(in_dim, reduction_dim, kernel_size=1, bias=False), nn.BatchNorm2d(reduction_dim, momentum=.95), nn.ReLU(inplace=True) )) self.features = nn.ModuleList(self.features)
def __init__(self, num_classes=1000, activation_fn=nn.ReLU(), drop_rate=0, global_pool='avg'): self.drop_rate = drop_rate self.global_pool = global_pool super(ResNeXt101_32x4d, self).__init__() self.features = resnext_101_32x4d_features(activation_fn=activation_fn) self.pool = nn.AdaptiveAvgPool2d(1) assert global_pool == 'avg' # other options not supported self.fc = nn.Linear(2048, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): init.kaiming_normal(m.weight) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_()
def __init__(self, output_size=1, pool_type='avg'): super(AdaptiveAvgMaxPool2d, self).__init__() self.output_size = output_size self.pool_type = pool_type if pool_type == 'avgmaxc' or pool_type == 'avgmax': self.pool = nn.ModuleList([nn.AdaptiveAvgPool2d(output_size), nn.AdaptiveMaxPool2d(output_size)]) elif pool_type == 'max': self.pool = nn.AdaptiveMaxPool2d(output_size) else: if pool_type != 'avg': print('Invalid pool type %s specified. Defaulting to average pooling.' % pool_type) self.pool = nn.AdaptiveAvgPool2d(output_size)