用梯度繁殖这些张量的方法
我具有两个输入的功能:加热地图和特征地图。 热图的形状为(20,14,64,64)
,并且该特征地图的形状为(20,64,64,64,64)
。其中20
是批处理大小,14
是关键点的数量。热图和特征地图都具有64x64
的空间维度,并且特征符号具有64
频道(在第二维度上)。
现在,我需要将每个热图乘以特征图的每个通道。因此,第一个热图必须乘以功能地图的所有64
通道。第二个带有所有频道的人,依此类推。
之后,我应该有一个形状的张量(20、14、64、64、64)
我需要应用全局的最大功能。
现在的问题是我无法创建一个新的张量来做到这一点,因为必须保留热图和特征地图的梯度。
我的实际(慢速且不gradient-keeping)代码是:
def get_keypoint_representation(self, heatmaps, features):
heatmaps = heatmaps[0]
pool = torch.nn.MaxPool2d(features.shape[2])
features = features[:, None, :, :, :]
features = features.expand(-1, 14, -1, -1, -1).clone()
for i in range(self.cfg.SINGLE_GPU_BATCH_SIZE):
for j in range(self.cfg.NUM_JOINTS):
for k in range(features.shape[2]):
features[i][j][k] = torch.matmul(heatmaps[i][j], features[i][j][k])
gmp = features.amax(dim=(-1, -2))
return gmp
任务概述:
I have a function with two inputs: heat maps and feature maps.
The heatmaps have a shape of (20, 14, 64, 64)
and the feature maps have a shape of (20, 64, 64, 64)
. Where 20
is the batch size and 14
is the number of key points. Both heatmaps and feature maps have spatial dimensions of 64x64
and the featuremaps have 64
channels (on the second dimension).
Now I need to multiply each heatmap by each channel of the feature maps. So the first heatmap has to be multiplied by all 64
channels of the feature maps. The second with all channels, and so on.
After that, I should have a tensor of shape (20, 14, 64, 64, 64)
on which I need to apply global max-pooling.
The problem is now that I can't create a new tensor to do that, because the gradients of the heatmaps and feature maps must be preserved.
My actual (slow and not-gradient-keeping) code is:
def get_keypoint_representation(self, heatmaps, features):
heatmaps = heatmaps[0]
pool = torch.nn.MaxPool2d(features.shape[2])
features = features[:, None, :, :, :]
features = features.expand(-1, 14, -1, -1, -1).clone()
for i in range(self.cfg.SINGLE_GPU_BATCH_SIZE):
for j in range(self.cfg.NUM_JOINTS):
for k in range(features.shape[2]):
features[i][j][k] = torch.matmul(heatmaps[i][j], features[i][j][k])
gmp = features.amax(dim=(-1, -2))
return gmp
Overview of the task:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定热图的张量
hm
形状(b,k,k,h,w)
和功能张量fm
fm shop(b,c,c,c,c ,h,w)
。您可以使用单个
/a>运算
符,然后使用 <代码> amax :
Given a tensor of heatmaps
hm
shaped(b, k, h, w)
and a feature tensorfm
shaped(b, c, h, w)
.You can perform such an operation with a single
einsum
operatorThen follow with a max-pooling operation over the spatial dimensions using
amax
: