mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
762 字
2 分钟
机器学习-图像分割

数据集#

原数据集作者没有公开,我另外找的有关无人机航拍多类别的数据集 数据集链接(Kaggle)

提取所需要的植被数据做一个二分类数据集

U-net#

论文原文: U-Net: Convolutional Networks for Biomedical Image Segmentation

关于U-net一些理解#

U-net是做图像分割的经典模型之一。最早是为了解决医学图像图像分割的一些任务所设计的卷积网络模型。在电子显微镜图像分割任务中表现突出。对于这个网络适合的场景以及它的计算方式,就是计算一个一个像素点的语义信息,我认为该网络非常适合处理细小密集的目标(当时了解到处理植被信息的时候也在思考检测任务应该怎么做,后来发现完全不一样)下载完数据集后发现标签和检测任务完全不一样,之前的标签是坐标形式,但是分割任务是以mask的形式(图片形式)存在的。

网络架构方面,整个网络的前向传播结构先做下采样,再做上采样,整个过程就像字母U。在学习过程中最不相同的其实就是后半段上采样的过程,与检测任务的卷积神经网络不同,它会进行复原操作(转置卷积操作)复原图片大小,恢复的时候,会结合对应层级的先前下采样的特征图最拼接(特征融合),在做下采样的的时候,特征图片的空间信息比较多,语义特征比较少;而上采样的时候,由于转置卷积的差值是按照深层特征图还原回原本的大小和分辨率,语义特征多,原本的空间信息因丢失反而少了,为了确保同时拿到语义信息和空间信息,需要做特征的拼接,最后得到的效果可以输出或者反向传播

Pytorch复现过程#

原项目是通过tensorflow实现的,我比较熟悉pytorch,所以逐步用pytorch复现U-net的原始架构

Encoder模块#

u-net的encoder下采样过程中是通过两层卷积+一层池化提取特征的

Lout=⌊Lin+2P−D(K−1)−1S+1⌋\boxed{ L_{out}= \left\lfloor \frac{L_{in}+2P-D(K-1)-1}{S}+1 \right\rfloor }
符号含义
LinL_{in}输入尺寸
LoutL_{out}输出尺寸
KK卷积核尺寸(Kernel Size)
SS步长(Stride)
PP填充(Padding)
DD膨胀率(Dilation)
class DoubleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
kernel_size=3,
padding=1,
bias=False
),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(
out_channels,
out_channels,
kernel_size=3,
padding=1,
bias=False
),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class Down(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxPool_conv = nn.Sequential(
nn.MaxPool2d(kernel_size=2),
DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxPool_conv(x)
class Encoder(nn.Module):
def __init__(self, in_channels=3):
super().__init__()
self.inc = DoubleConv(in_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
self.down4 = Down(512, 1024)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
return x1, x2, x3, x4, x5

Decoder模块#

通过转置卷积恢复原图片大小

Lout=(Lin−1)S−2P+D(K−1)+O+1\boxed{ L_{out}=(L_{in}-1)S-2P+D(K-1)+O+1 }
符号含义
LinL_{in}输入尺寸
LoutL_{out}输出尺寸
KK卷积核尺寸
SS步长(Stride)
PP填充(Padding)
DD膨胀率(Dilation)
OO输出填充(Output Padding)
class Up(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.up = nn.ConvTranspose2d(
in_channels,
out_channels,
kernel_size=2,
stride=2,
)
self.conv = DoubleConv(
out_channels*2,
out_channels
)
def forward(self, x, skip):
x = self.up(x)
assert x.shape[-2:] == skip.shape[-2:]
x = torch.cat([x, skip], dim=1)
x = self.conv(x)
return x
class Decoder(nn.Module):
def __init__(self):
super().__init__()
self.up1 = Up(1024, 512)
self.up2 = Up(512, 256)
self.up3 = Up(256, 128)
self.up4 = Up(128, 64)
def forward(self, x1, x2, x3, x4, x5):
d4 = self.up1(x5, x4)
d3 = self.up2(d4, x3)
d2 = self.up3(d3, x2)
d1 = self.up4(d2, x1)
return d1

训练结果#

踩得一些坑#

远程连接ssh是用不了conda指令#

Terminal window
call "[conda安装目录]\conda.bat" init cmd.exe

vsc的remote-ssh插件出bug的时候就要直接使用命令行

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

机器学习-图像分割
https://blog.hydrodyio.xyz/posts/机器学习-图像分割/
作者
Hydroiody
发布于
2026-11-15
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录