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)

效果:

相关推荐
IvanCodes31 分钟前
LangChain(15):LangSmith 应用评估与回归测试
python·langchain·agent
土星云SaturnCloud1 小时前
DirectMHP 边缘部署实战(下):土星云SE110S 三机型 28.98ms 实测——模型编译、精度验证与七条工程化经验
人工智能·边缘计算·lightstereo
炸薯条!1 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
stereohomology1 小时前
解锁和提升特定型号NVIDia显卡性能的开源代码
人工智能·算力·解锁·nvidia显卡
master3361 小时前
基于 Spring AI + Spring Cloud 构建 Web 版 AI Chat:多模型接入、流式对话与全链路验证
人工智能·spring·spring cloud
一级新生1 小时前
Dify 迭代节点:从输入输出到九步实操
数据库·人工智能·mysql·低代码·工作流·dify
互联网江湖2 小时前
逐际动力张巍:具身智能的瓶颈在“技能”,而非“智能”
大数据·人工智能
程序员cxuan2 小时前
阿里又来一王炸,QBot 能组团干活了
人工智能·后端·程序员
约书亚的博客2 小时前
AI 主动攻击网络与计算机设备的真实案例整理
网络·人工智能
程序员cxuan2 小时前
WorkBuddy 接入 Step 5,干活太爽了
人工智能·后端·程序员