从张量列表从3个通道转换为1个通道Pytorch张量

发布于 2025-02-12 05:53:13 字数 603 浏览 0 评论 0原文

说我有一个list张量,,我可以迭代:

    for volume in range(len(volumes)):
        print (volume.shape)



torch.Size([3, 512, 512, 222])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 185])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 271])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 261])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 215])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 284])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 191])
<class 'torch.Tensor'>

如何将频道从3更改为所有卷?

谢谢

Say I have a list of tensors, volumes, which I can iterate over:

    for volume in range(len(volumes)):
        print (volume.shape)



torch.Size([3, 512, 512, 222])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 185])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 271])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 261])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 215])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 284])
<class 'torch.Tensor'>
torch.Size([3, 512, 512, 191])
<class 'torch.Tensor'>

How can I change the channel from 3 to 1, for all volumes?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2025-02-19 05:53:13

如果要删除前两个频道,则只能保留最后一个dim = -1 ie dim = 2

>>> vnew = [v[2:] for v in volumes] # list of (1, 512, 512, *)

如果您想在此过程中挤压单例尺寸,然后做:

>>> vnew = [v[2] for v in volumes] # list of (512, 512, *)

If you are looking to remove the first two channels, then you should only keep the last one dim=-1 i.e. dim=2:

>>> vnew = [v[2:] for v in volumes] # list of (1, 512, 512, *)

If you want to squeeze the singleton dimensions in the process then do:

>>> vnew = [v[2] for v in volumes] # list of (512, 512, *)
故笙诉离歌 2025-02-19 05:53:13

如果您只想保留每个卷的第一个频道,则可以创建一个这样的新列表:

new_volumes = [volume[0,...] for volume in volumes]

If you like just to keep the first channel for each volume, you can create a new list like that:

new_volumes = [volume[0,...] for volume in volumes]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文