本文整理汇总了Python中torch.nn.functional.adaptive_avg_pool2d方法的典型用法代码示例。如果您正苦于以下问题:Python functional.adaptive_avg_pool2d方法的具体用法?Python functional.adaptive_avg_pool2d怎么用?Python functional.adaptive_avg_pool2d使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.adaptive_avg_pool2d方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _bbox_forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def _bbox_forward(self, stage, x, rois, semantic_feat=None):
"""Box head forward function used in both training and testing."""
bbox_roi_extractor = self.bbox_roi_extractor[stage]
bbox_head = self.bbox_head[stage]
bbox_feats = bbox_roi_extractor(
x[:len(bbox_roi_extractor.featmap_strides)], rois)
if self.with_semantic and 'bbox' in self.semantic_fusion:
bbox_semantic_feat = self.semantic_roi_extractor([semantic_feat],
rois)
if bbox_semantic_feat.shape[-2:] != bbox_feats.shape[-2:]:
bbox_semantic_feat = F.adaptive_avg_pool2d(
bbox_semantic_feat, bbox_feats.shape[-2:])
bbox_feats += bbox_semantic_feat
cls_score, bbox_pred = bbox_head(bbox_feats)
bbox_results = dict(cls_score=cls_score, bbox_pred=bbox_pred)
return bbox_results
示例2: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
levels = []
target_size = x.size()[2:4]
ar = target_size[1] / target_size[0]
x = self.spp[0].forward(x)
levels.append(x)
num = len(self.spp) - 1
for i in range(1, num):
if not self.square_grid:
grid_size = (self.grids[i - 1], max(1, round(ar * self.grids[i - 1])))
x_pooled = F.adaptive_avg_pool2d(x, grid_size)
else:
x_pooled = F.adaptive_avg_pool2d(x, self.grids[i - 1])
level = self.spp[i].forward(x_pooled)
level = upsample(level, target_size)
levels.append(level)
x = torch.cat(levels, 1)
x = self.spp[-1].forward(x)
return x
示例3: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, inputs):
# Feature Extraction
conv1 = self.conv1(inputs)
maxpool1 = self.maxpool1(conv1)
conv2 = self.conv2(maxpool1)
maxpool2 = self.maxpool2(conv2)
conv3 = self.conv3(maxpool2)
maxpool3 = self.maxpool3(conv3)
conv4 = self.conv4(maxpool3)
maxpool4 = self.maxpool4(conv4)
conv5 = self.conv5(maxpool4)
conv5_p = self.conv5_p(conv5)
conv6_p = self.conv6_p(conv5_p)
batch_size = inputs.shape[0]
pooled = F.adaptive_avg_pool2d(conv6_p, (1, 1)).view(batch_size, -1)
return pooled
示例4: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
assert len(self.branches) == len(x)
x = [branch(b) for branch, b in zip(self.branches, x)]
if self.use_global:
x_global = [F.adaptive_avg_pool2d(b, 1) for b in x]
x_global = torch.cat(tuple(x_global), 1)
x_fused = []
for i in range(len(self.fuse_layers)):
for j in range(0, len(self.branches)):
if j == 0:
x_fused.append(self.fuse_layers[i][0](x[0]))
else:
x_fused[i] = x_fused[i] + self.fuse_layers[i][j](x[j])
if self.use_global:
x_fused[i] = x_fused[i] * self.global_layers[i](x_global)
for i in range(len(x_fused)):
x_fused[i] = self.relu(x_fused[i])
return x_fused
示例5: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
# aux1: N x 512 x 14 x 14, aux2: N x 528 x 14 x 14
x = F.adaptive_avg_pool2d(x, (4, 4))
# aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4
x = self.conv(x)
# N x 128 x 4 x 4
x = x.view(x.size(0), -1)
# N x 2048
x = F.relu(self.fc1(x), inplace=True)
# N x 2048
x = F.dropout(x, 0.7, training=self.training)
# N x 2048
x = self.fc2(x)
# N x 1024
return x
示例6: generate
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def generate(self, target_layer):
fmaps = self._find(self.fmap_pool, target_layer)
grads = self._find(self.grad_pool, target_layer)
weights = F.adaptive_avg_pool2d(grads, 1)
gcam = torch.mul(fmaps, weights).sum(dim=1, keepdim=True)
gcam = F.relu(gcam)
gcam = F.interpolate(
gcam, self.image_shape, mode="bilinear", align_corners=False
)
B, C, H, W = gcam.shape
gcam = gcam.view(B, -1)
gcam -= gcam.min(dim=1, keepdim=True)[0]
gcam /= gcam.max(dim=1, keepdim=True)[0]
gcam = gcam.view(B, C, H, W)
return gcam
示例7: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, inputs, noise):
out = self.input(inputs)
cond = torch.zeros(
inputs.size(0), 10,
dtype=inputs.dtype,
device=inputs.device
)
offset = (torch.log(noise) / torch.log(torch.tensor(0.60))).long()
cond[torch.arange(inputs.size(0)), offset.view(-1)] = 1
connections = []
for norm, block in zip(self.down_norm, self.down):
out = func.elu(block(norm(out, cond)))
connections.append(out)
features = func.adaptive_avg_pool2d(out, 1)
logits = self.predict(features.view(features.size(0), -1))
for norm, block, shortcut in zip(self.up_norm, self.up, reversed(connections)):
out = func.elu(block(norm(torch.cat((out, shortcut), dim=1), cond)))
del connections
return self.output(out), logits
示例8: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, input):
upsize = tuple(input.size()[-2:])
if self.is_module:
out = self.modules(input)
out = func.adaptive_avg_pool2d(input, 1)
out = func.interpolate(out, size=upsize)
for idx, output in enumerate(self.outputs):
self.outputs[idx] = func.interpolate(output, size=upsize, mode='bilinear')
outputs = self.outputs + [out]
self.outputs = []
else:
out = input
outputs = []
for idx, module in enumerate(self.modules):
out = module(outputs)
if idx in self.branch:
outputs.append(out)
out = func.adaptive_avg_pool2d(input, 1)
out = func.interpolate(out, size=upsize)
outputs.append(out)
if self.refine:
for idx, output in enumerate(outputs):
if idx < len(outputs) - 1:
outputs[idx] = self.attention_refinements(idx) * output
return torch.cat(outputs, dim=1)
示例9: expectation
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def expectation(self):
self.net.eval()
with torch.no_grad():
embedding = []
batch_loader = DataLoader(
self.data,
batch_size=self.batch_size,
shuffle=False
)
for point, *_ in batch_loader:
features, mean, logvar = self.net(point.to(self.device))
std = torch.exp(0.5 * logvar)
sample = torch.randn_like(std).mul(std).add_(mean)
latent_point = func.adaptive_avg_pool2d(sample, 1)
latent_point = latent_point
latent_point = latent_point.reshape(latent_point.size(0), -1)
embedding.append(latent_point)
embedding = torch.cat(embedding, dim=0)
expectation = self.classifier(embedding)
self.net.train()
return expectation.to("cpu"), embedding.to("cpu")
示例10: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
levels = []
target_size = self.fixed_size if self.fixed_size is not None else x.size()[2:4]
ar = target_size[1] / target_size[0]
x = self.spp[0].forward(x)
levels.append(x)
num = len(self.spp) - 1
for i in range(1, num):
if not self.square_grid:
grid_size = (self.grids[i - 1], max(1, round(ar * self.grids[i - 1])))
x_pooled = F.adaptive_avg_pool2d(x, grid_size)
else:
x_pooled = F.adaptive_avg_pool2d(x, self.grids[i - 1])
level = self.spp[i].forward(x_pooled)
level = self.upsampling_method(level, target_size)
levels.append(level)
x = torch.cat(levels, 1)
x = self.spp[-1].forward(x)
return x
示例11: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(self.dropout(x))
feat = x
x = self.out(x)
x = F.adaptive_avg_pool2d(x, (1, 1))
x = torch.sigmoid(x) # relu + tanh? thresholded?
x = torch.squeeze(x)
return {'logit': x, 'feat': feat}
示例12: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, img, att_size=14):
x = img.unsqueeze(0)
x = self.resnet.conv1(x)
x = self.resnet.bn1(x)
x = self.resnet.relu(x)
x = self.resnet.maxpool(x)
x = self.resnet.layer1(x)
x = self.resnet.layer2(x)
x = self.resnet.layer3(x)
x = self.resnet.layer4(x)
fc = x.mean(3).mean(2).squeeze()
att = F.adaptive_avg_pool2d(x,[att_size,att_size]).squeeze().permute(1, 2, 0)
return fc, att
示例13: _mask_forward_test
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def _mask_forward_test(self, stage, x, bboxes, semantic_feat=None):
"""Mask head forward function for testing."""
mask_roi_extractor = self.mask_roi_extractor[stage]
mask_head = self.mask_head[stage]
mask_rois = bbox2roi([bboxes])
mask_feats = mask_roi_extractor(
x[:len(mask_roi_extractor.featmap_strides)], mask_rois)
if self.with_semantic and 'mask' in self.semantic_fusion:
mask_semantic_feat = self.semantic_roi_extractor([semantic_feat],
mask_rois)
if mask_semantic_feat.shape[-2:] != mask_feats.shape[-2:]:
mask_semantic_feat = F.adaptive_avg_pool2d(
mask_semantic_feat, mask_feats.shape[-2:])
mask_feats += mask_semantic_feat
if self.mask_info_flow:
last_feat = None
last_pred = None
for i in range(stage):
mask_pred, last_feat = self.mask_head[i](mask_feats, last_feat)
if last_pred is not None:
mask_pred = mask_pred + last_pred
last_pred = mask_pred
mask_pred = mask_head(mask_feats, last_feat, return_feat=False)
if last_pred is not None:
mask_pred = mask_pred + last_pred
else:
mask_pred = mask_head(mask_feats)
return mask_pred
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
# pre-context
avg_x = F.adaptive_avg_pool2d(x, output_size=1)
avg_x = self.pre_context(avg_x)
avg_x = avg_x.expand_as(x)
x = x + avg_x
# switch
avg_x = F.pad(x, pad=(2, 2, 2, 2), mode='reflect')
avg_x = F.avg_pool2d(avg_x, kernel_size=5, stride=1, padding=0)
switch = self.switch(avg_x)
# sac
weight = self._get_weight(self.weight)
if self.use_deform:
offset = self.offset_s(avg_x)
out_s = deform_conv(x, offset, weight, self.stride, self.padding,
self.dilation, self.groups, 1)
else:
out_s = super().conv2d_forward(x, weight)
ori_p = self.padding
ori_d = self.dilation
self.padding = tuple(3 * p for p in self.padding)
self.dilation = tuple(3 * d for d in self.dilation)
weight = weight + self.weight_diff
if self.use_deform:
offset = self.offset_l(avg_x)
out_l = deform_conv(x, offset, weight, self.stride, self.padding,
self.dilation, self.groups, 1)
else:
out_l = super().conv2d_forward(x, weight)
out = switch * out_s + (1 - switch) * out_l
self.padding = ori_p
self.dilation = ori_d
# post-context
avg_x = F.adaptive_avg_pool2d(out, output_size=1)
avg_x = self.post_context(avg_x)
avg_x = avg_x.expand_as(out)
out = out + avg_x
return out
示例15: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import adaptive_avg_pool2d [as 别名]
def forward(self, x):
features = self.features(x)
out = F.relu(features, inplace=True)
out = F.adaptive_avg_pool2d(out, (1, 1)).view(features.size(0), -1)
out = self.classifier(out)
return out