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)

效果:

相关推荐
qwerasda1238522 分钟前
Mask-RCNN右转交通标志识别训练与优化
python
郝学胜-神的一滴10 分钟前
何友院士《人工智能发展前沿》全景解读:从理论基石到产业变革
人工智能·python·深度学习·算法·机器学习
2401_8403652113 分钟前
cuda-gdb Could not find CUDA Debugger back-end.
人工智能
苍何fly22 分钟前
首个国产芯片训练的多模态 SOTA 模型,已免费开源!
人工智能·经验分享
2401_8414956423 分钟前
具身智能:从理论到现实,人工智能的下一场革命
人工智能·算法·机器人·硬件·具身智能·通用智能·专用智能
方见华Richard36 分钟前
对话量子场论:语言如何产生认知粒子V0.3
人工智能·交互·学习方法·原型模式·空间计算
wfeqhfxz258878240 分钟前
基于YOLO12-A2C2f-DFFN-DYT-Mona的铁件部件状态识别与分类系统_1
人工智能·分类·数据挖掘
2501_9415079441 分钟前
脊柱结构异常检测与分类:基于Cascade-RCNN和HRNetV2p-W32模型的改进方案
人工智能·分类·数据挖掘
划水的code搬运工小李42 分钟前
自制py功能包解析IMU航迹推算
python·imu·航迹推算
珊珊而川42 分钟前
MBE(Model-based Evaluation) LLM-as-a-Judge
人工智能