代码拉取完成,页面将自动刷新
# Copyright (c) 2020 Huawei Technologies Co., Ltd
# All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import torch_npu
from torch_npu.testing.testcase import TestCase, run_tests
from torch_npu.testing.common_utils import create_common_tensor
from torch_npu.contrib.module import PSROIPool
class TestPsRoiPooling(TestCase):
def get_random_rois(self, shape):
rois_init = torch.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
pi1 = torch.rand(1, 2).uniform_(0, 10)
pi2 = torch.rand(1, 2).uniform_(10, 100)
boxi = torch.cat((pi1, pi2), 1)
n = torch.tensor([[float(i)]])
boxi = torch.cat((n, boxi), 1)
rois_init[i, j, :] = boxi
return rois_init
def npu_ps_roi_align(self, cls_feat, rois_tensor, pooled_height, \
pooled_width, spatial_scale, group_size, output_dim):
cls_feat.requires_grad = True
model = PSROIPool(pooled_height, pooled_width, spatial_scale, group_size, output_dim)
output = model(cls_feat, rois_tensor) # 512,22,7,7
l = output.sum()
l.backward()
return output.detach().cpu(), cls_feat.grad.cpu()
def test_npu_roi_align_1(self):
cls_feat = torch.randn(4, 1078, 84, 84).float().npu()
rois_tensor = self.get_random_rois((4, 128, 5)).permute(0, 2, 1).float().npu()
pooled_height = 7
pooled_width = 7
spatial_scale = 1 / 16.0
group_size = 7
output_dim = 22
npu_output, npu_inputgrad = self.npu_ps_roi_align(cls_feat, rois_tensor, pooled_height, \
pooled_width, spatial_scale, group_size, output_dim)
expedt_cpu_output_shape = torch.randn(512, 22, 7, 7).shape
expedt_cpu_inputgrad_shape = cls_feat.shape
self.assertEqual(expedt_cpu_output_shape, npu_output.shape)
self.assertEqual(expedt_cpu_inputgrad_shape, npu_inputgrad.shape)
if __name__ == "__main__":
run_tests()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。