深度学习-理解卷积神经网络中的通道(channel)

1,138 阅读1分钟

1 Conv Channel概念

灰色图片 grayscale: w*h  
彩色图片 color image: w*h*c  c表示channel(c=3,表示red/green/bule)

1 最初输入的图片样本的 channels ,取决于图片类型,比如RGB,channels=3;
2 卷积操作完成后输出的 out_channels ,取决于卷积核的数量。此时的 out_channels 也会作为下一次卷积时的卷积核的 in_channels;
3 卷积核中的 in_channels ,刚刚2中已经说了,就是上一次卷积的 out_channels ,如果是第1次做卷积,就是1中样本图片的 channels 。

image.png

输入通道数是3,卷积核通道个数是3(输入通道个数 等于 卷积核通道个数),每个通道都需要跟一个卷积核做卷积运算,然后将结果相加得到一个特征图的输出,这里有4个过滤器,因此得到4个特征图的输出,输出通道数为4。

2 torch demo

import torch
 
in_channels = 5  #输入通道数量
out_channels =10 #输出通道数量
width = 100      #每个输入通道上的卷积尺寸的宽
heigth = 100     #每个输入通道上的卷积尺寸的高
kernel_size = 3  #每个输入通道上的卷积尺寸
batch_size = 1   #批数量
 
input = torch.randn(batch_size,in_channels,width,heigth)
conv_layer = torch.nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size)
 
out_put = conv_layer(input)
 
print(input.shape)   [1,5,100,100]  batch_size,in_channels,width,height
print(out_put.shape) [1,10,100,100]   batch_size,out_channels,width',height'
print(conv_layer.weight.shape)  [10,5,3,3]  out_channels=10,in_channels=5,kernel_size ,kernel_size

总结规律:
1 输入通道个数 等于 卷积核通道个数
2 卷积核个数等于输出通道个数

参考:
blog.csdn.net/weixin_3848…