分开张量通道

发布于 2025-01-23 20:27:56 字数 140 浏览 2 评论 0原文

我正在将大小的张量[1,3,224,224]倾倒到一个文件中,并希望将大小的3个张量分为[1,1,1,224,224],每个RGB通道一个并将它们倒入3个单独的文件中。我该如何实施?

I am dumping a tensor of size [1,3,224,224] to a file and would like to split into 3 tensors of size [1,1,224,224], one for each RGB channel and dump them into 3 separate files. How do I implement this?

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

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

发布评论

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

评论(1

我爱人 2025-01-30 20:27:56

我认为最简单的方法是通过循环:

for c in range(x.shape[1]):
  torch.save(x[:, c:c+1, ...], f'channel{c}.pth')

注意第二个(通道)维度的索引:您希望保存的张量具有单元通道尺寸。如果要使用x [:,c,...]进行索引全部)。

I think the simplest way is by a loop:

for c in range(x.shape[1]):
  torch.save(x[:, c:c+1, ...], f'channel{c}.pth')

Note the indexing of the second (channel) dimension: you want the saved tensor to have a singleton channel dimension. If you were to index it using x[:, c, ...] you will get a tensor of shape [1, 224, 224] (no channel dimension at all).

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