“[...]“、“[::]“与torch.cat的解析(YOLOv5 Focus common.py)

"[...]"、"[::]"与torch.cat的解析(YOLOv5 common.py)

YOLOv5中common.py的函数Focus.forward,返回为self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)),这句话的解析。

def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
    return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))

1. "[...]"简单的使用

A = np.arange(25).reshape(5,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

  (1) 遍历每行,2表示索引为2的所在列

A[...,2]
array([ 2,  7, 12, 17, 22])

(2)遍历每行,:2表示索引<2的0,1所在的列

A[...,:2]
array([[ 0,  1],
       [ 5,  6],
       [10, 11],
       [15, 16],
       [20, 21]])

(3)遍历每行,::2表示步长为2,即0,2,4列

A[...,::2]
array([[ 0,  2,  4],
       [ 5,  7,  9],
       [10, 12, 14],
       [15, 17, 19],
       [20, 22, 24]])

(4)::2表示取间隔步长为2的取行数据,遍历每列

A[::2,...]
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [20, 21, 22, 23, 24]])

(5)相当于插入维度,即reshape(A,[1,4,4])

A[None,...]
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]]])

总结:"..."表示所有,"::x"表示x个步长

2. torch.cat

A = np.arange(9).reshape(3,3)
B = np.arange(1,10).reshape(3,3)
AA = torch.from_numpy(A)
BB = torch.from_numpy(B)

(1)按维度0拼接,即竖着拼接

C1 = torch.cat([AA,BB],0)
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]], dtype=torch.int32)

(2)按维度1拼接,即横着拼接

C2 = torch.cat([AA,BB],1)
tensor([[0, 1, 2, 1, 2, 3],
        [3, 4, 5, 4, 5, 6],
        [6, 7, 8, 7, 8, 9]], dtype=torch.int32)

3. torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)

X = np.arange(16*3).reshape(3,4,4)
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23],
        [24, 25, 26, 27],
        [28, 29, 30, 31]],

       [[32, 33, 34, 35],
        [36, 37, 38, 39],
        [40, 41, 42, 43],
        [44, 45, 46, 47]]])

(1)行和列步长间隔都为2

X[...,::2,::2]
array([[[ 0,  2],
        [ 8, 10]],

       [[16, 18],
        [24, 26]],

       [[32, 34],
        [40, 42]]])

(2)行和列步长间隔都为2,行的起始位置为1

X[..., 1::2, ::2]
array([[[ 4,  6],
        [12, 14]],

       [[20, 22],
        [28, 30]],

       [[36, 38],
        [44, 46]]])

(3)行和列步长间隔都为2,列的起始位置为1

X[..., ::2, 1::2]
array([[[ 1,  3],
        [ 9, 11]],

       [[17, 19],
        [25, 27]],

       [[33, 35],
        [41, 43]]])

(4)torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)

X = torch.from_numpy(X)
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11],
         [12, 13, 14, 15]],

        [[16, 17, 18, 19],
         [20, 21, 22, 23],
         [24, 25, 26, 27],
         [28, 29, 30, 31]],

        [[32, 33, 34, 35],
         [36, 37, 38, 39],
         [40, 41, 42, 43],
         [44, 45, 46, 47]]], dtype=torch.int32)


torch.cat([X[..., ::2, ::2], X[..., 1::2, ::2], X[..., ::2, 1::2], X[..., 1::2, 1::2]], 1)
tensor([[[ 0,  2],
         [ 8, 10],
         [ 4,  6],
         [12, 14],
         [ 1,  3],
         [ 9, 11],
         [ 5,  7],
         [13, 15]],

        [[16, 18],
         [24, 26],
         [20, 22],
         [28, 30],
         [17, 19],
         [25, 27],
         [21, 23],
         [29, 31]],

        [[32, 34],
         [40, 42],
         [36, 38],
         [44, 46],
         [33, 35],
         [41, 43],
         [37, 39],
         [45, 47]]], dtype=torch.int32)

  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
当调用`setup.py`时,出现`ModuleNotFoundError: No module named 'torch'`的错误,这通常表示您的Python环境缺少`torch`模块。解决这个问题的一种方法是安装缺失的模块。您可以使用以下命令来安装`torch`模块: ``` pip install torch ``` 请确保您的Python环境已正确设置,并且您具有适当的权限来安装库。如果您已经安装了`torch`模块但仍然遇到该错误,请确保您安装的是与您正在使用Python版本兼容的正确版本的`torch`模块。 引用提到,如果您下载了libpytorch,则不要导入其cmake文件夹。确保您按照正确的方式安装和导入`torch`模块。 另外,引用提到可能会出现`Could NOT find Torch (missing: TORCH_LIBRARY)`的错误,这可能是由于缺少Torch的库文件。如果您遇到此错误,请确保您已正确安装Torch并设置了正确的库路径。 总结起来,解决`setup.py`调用时的`ModuleNotFoundError: No module named 'torch'`错误的步骤如下: 1. 确保您的Python环境已正确设置。 2. 使用`pip install torch`命令安装缺失的`torch`模块。 3. 检查您是否正确安装了与您的Python版本兼容的`torch`模块。 4. 确保正确设置了Torch的库路径,以避免`Could NOT find Torch (missing: TORCH_LIBRARY)`的错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [faster-rcnn.pytorch 小白踩坑](https://blog.csdn.net/u010826850/article/details/103666308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [自己的完整c++ cuda包](https://blog.csdn.net/zxyOVO/article/details/130166399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值