【pytorch】1x1卷积

2023-05-16

对image进行1x1卷积,输入为 ( b a t c h , C , H , W ) (batch, C, H, W) (batch,C,H,W),如下例子,输入为 ( 4 , 3 , 225 , 225 ) (4, 3, 225, 225) (4,3,225,225)的图片,输出为 ( 4 , 256 , 225 , 225 ) (4, 256, 225, 225) (4,256,225,225)的特征图

import torch
import torch.nn as nn

batch = 4
C = 3
H = W = 225
x = torch.rand((batch, C, H, W))
conv = nn.Conv2d(in_channels=C, out_channels=256, kernel_size=1, stride=1, padding=0)
out = conv(x)
print(out.shape)  # (4, 256, 225, 225)

对3维张量进行1x1卷积,由于pytorch进行2D卷积要求输入是4维张量,因此要对形如 ( b a t c h , C , N ) (batch,C,N) (batch,C,N)的张量进行1x1卷积时要先将其扩展一个维度,可以扩展为 ( b a t c h , C , 1 , N ) (batch,C,1,N) (batch,C,1,N) ( b a t c h , C , N , 1 ) (batch,C,N,1) (batch,C,N,1)再进行1x1卷积,结果都是一样的,例如

import torch
import torch.nn as nn

torch.manual_seed(28)
x = torch.rand((4, 128, 2048))
conv = nn.Conv2d(128, 256, kernel_size=1, stride=1)

# 扩展最后一个维度和倒数第二个维度结果都是一样的
out1 = conv(x.unsqueeze(-1))
res1 = out1.squeeze(-1)
print(res1.shape)  # (4,256,2048)
print(torch.sum(torch.sum(torch.sum(res1, dim=-1), dim=-1), dim=-1))  # 17060.7676

out2 = conv(x.unsqueeze(-2))
res2 = out2.squeeze(-2)
print(res2.shape)  # (4,256,2048)
print(torch.sum(torch.sum(torch.sum(res2, dim=-1), dim=-1), dim=-1))  # 17060.7676
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【pytorch】1x1卷积 的相关文章

随机推荐

  • 【dlib库安装】

    直接pip安装dlib库会报错 xff0c 需要先安装cmake xff0c 再安装dlib conda下可以分别使用以下命令 xff08 一行一行来 xff09 conda span class token function instal
  • 【tensorflow】tf.slice()

    tf slice inputs begin size name 是tensorflow中对向量inputs的切片 xff0c begin表示每个维度要抽取的开始位置 xff0c size表示从开始位置往后要抽取的元素个数 xff0c 若某个
  • 【Anaconda配置tensorflow2.0GPU+CUDA+CUDNN】

    创建一个新环境conda create name tf gpu python 61 3 6 xff0c python版本为3 6 xff0c 然后分别执行以下语句 xff0c conda install cupy可以自动寻找符合版本的cud
  • 【python】不同目录下导入py文件

    https zhuanlan zhihu com p 64893308
  • 【python】使用cupy加速

    cupy和numpy使用类似 xff0c 不同的是cupy在计算数组和矩阵时直接调用GPU来加速 xff0c 而numpy是调用cpu计算 在使用cupy时需要注意 xff0c cupy适合高维大尺寸的向量计算 xff0c 因为初始化GPU
  • 【点云可视化】自制ply文件并使用open3d可视化点云

    使用open3d可视化点云 xff0c 需要将点云制作为ply文件传入函数中 安装open3d 直接使用pip安装 pip span class token function install span open3d ply文件 ply文件的
  • 【python】返回列表排序索引

    arr span class token operator 61 span span class token punctuation span span class token number 1 span span class token
  • 【win10】安装pytorch1.6.0+cuda10.1 + torch-geometric

    conda下新建一个环境 xff0c 使用python3 6 conda create name torch ge span class token assign left variable python span span class t
  • 【python】制作GIF

    导入库 span class token keyword import span matplotlib span class token punctuation span pyplot span class token keyword as
  • 【3D体素可视化】

    可视化3D体素 xff0c voxel被表示为一个3维的数组 xff0c 每个位置为0或1 span class token keyword from span mpl toolkits span class token punctuati
  • OVS 使用总结

    1 简介 Open vSwitch 是一个用C语言开发的多层虚拟交换机 1 1 工作原理 内核模块实现了多个 数据路径 xff08 类似网桥 xff09 每个都可以有多个 vports xff08 类似网桥的端口 xff09 每个数据路径也
  • MySQL常用函数学习

    前言 MySQL函数是MySQL数据库提供的内置函数 xff0c 这些内置函数可以更方便处理表中的数据 下面简单介绍一下MySQL中包含的几类常用函数 聚合函数 聚合函数可实现根据一组数据求出一个值 xff0c 聚合函数的结果值只根据选定数
  • 【双指针(快慢指针)】链表中倒数第k个结点

    题目 输入一个链表 xff0c 输出该链表中倒数第k个结点 暴力解法 很多种 xff0c 先遍历一遍 xff0c 统计链表的长度 xff0c 让后找正数的第n k 1个节点 快慢指针 使用两个指针 xff0c fast和slow xff0c
  • 【反转链表】

    题目 输入一个链表 xff0c 反转链表后 xff0c 输出新链表的表头 题解 遍历一遍链表 xff0c 在遍历的过程中不断将指向关系反转 xff0c 可以想到需要两个指针fast和slow xff0c fast在前 xff0c slow在
  • 【python】argparse转换为字典

    在使用argparse定义程序参数时 xff0c 常规用法如下 xff1a span class token keyword import span argparse parser span class token operator 61
  • 【pytorch】多GPU训练

    使用多GPU训练pytorch模型只需要加一句DataParallel即可 xff0c 如下 span class token keyword from span torch span class token punctuation spa
  • pytorch列表添加模块

    在使用pytorch将多个模块添加到列表时不能使用简单的list xff0c 需要使用torch nn ModuleList xff0c 否则框架无法跟踪到模块且不能使用model cuda 将模型转移到GPU上 xff0c 会产生以下错误
  • 【python】将列表划分为多个子列表

    参考 将列表划分为包含3个元素的子列表 a span class token operator 61 span span class token punctuation span span class token number 1 span
  • 【pytorch】点乘、矩阵乘法

    点乘即两个相同大小的矩阵对应位置相乘 xff0c 使用torch mul a b xff0c a和b的大小需要相等 矩阵乘法需要满足矩阵乘法的原则 xff0c 使用torch matmul a b xff0c a和b需要满足矩阵乘法的规则
  • 【pytorch】1x1卷积

    对image进行1x1卷积 xff0c 输入为 b a t c h