1. 卷积原理

① 卷积核不停的在原图上进行滑动,对应元素相乘再相加。

② 下图为每次滑动移动1格,然后再利用原图与卷积核上的数值进行计算得到缩略图矩阵的数据,如下图右所示。

python 复制代码
import torch
import torch.nn.functional as F

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]])

kernel = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output = F.conv2d(input, kernel, stride=1)
print(output)

结果:

效果:

python 复制代码
import torch
import torch.nn.functional as F

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]])

kernel = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output2 = F.conv2d(input, kernel, stride=2)  # 步伐为2
print(output2)

结果 :

python 复制代码
import torch
import torch.nn.functional as F

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]])

kernel = torch.tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])

print(input.shape)
print(kernel.shape)
input = torch.reshape(input, (1,1,5,5))
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)

output3 = F.conv2d(input, kernel, stride=1, padding=1)  # 周围只填充一层
print(output3)

效果:

相关推荐
Eric.Lee202117 分钟前
数据集-目标检测系列- 杯子 数据集 bottle >> DataBall
人工智能·目标检测·计算机视觉·杯子检测·bottle detect
安迪小宝30 分钟前
16 celery集成其他工具
数据库·python·sqlite·celery
苏苏susuus33 分钟前
深度学习:张量标量概念、PyTorch张量创建、类型转换等
人工智能·pytorch·深度学习
成都犀牛41 分钟前
工作流和Agent 的区别与联系
人工智能·python·深度学习·神经网络·agent·工作流
集和诚JHCTECH1 小时前
【案例分享】KMDA-7611-S001--高性能嵌入式电脑助力双臂轮式人形机器人应用
人工智能·嵌入式硬件·机器人
雪兽软件1 小时前
网络安全中的人工智能应用
人工智能·web安全
寄思~1 小时前
Python学习笔记:错误和异常处理
开发语言·笔记·python·学习
啊哈哈哈哈哈啊哈哈1 小时前
G2打卡——人脸图像生成
pytorch·学习·gan
人工智能培训咨询叶梓1 小时前
大模型在关键社会领域的应用研究:金融、医疗和法律
人工智能·语言模型·金融·大模型实战·大模型应用·人工智能培训·大模型工具
是紫焅呢1 小时前
O数据可视化基础.py
python·青少年编程·信息可视化·数据分析·学习方法·数据可视化·visual studio code