深度学习学习日记4.14 数据增强 Unet网络部分

数据增强

transforms.Compose([:这表示创建一个转换组合将多个数据转换操作串联在一起

transforms.RandomHorizontalFlip():这个操作是随机水平翻转图像 ,以增加数据的多样性。它以一定的概率随机地水平翻转 输入的图像。

transforms.Resize(image_size):这个操作用于将图像调整为指定的大小。image_size 是所需的输出图像大小,可以是一个整数或一个 (height, width) 元组。

transforms.CenterCrop(image_size):这个操作用于从图像的中心裁剪出指定大小的区域 。同样,image_size 可以是一个整数或一个 (height, width) 元组。

transforms.ToTensor():这个操作将图像转换为 PyTorch 张量格式。它会将 PIL 图像或 ndarray 转换为张量 ,并对像素值进行归一化到 [0, 1] 的范围内。

transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]):这个操作用于对图像进行标准化 。它对张量的每个通道进行归一化处理,使得每个通道的均值为 0.485、0.456、0.406,标准差为 0.229、0.224、0.225。

Unet下采样:两层的卷积+relu+maxpooling

1.继承nn.model

2.初始化参数,输入channel,输出channel

nn.sequential序列 中写 卷积,relu(inplce=True节省计算资源),卷积,Relu

最大池化层,缩减为1/2 长宽都减小一般

3.前向传播:需要有参数是否做maxpooling

Unet上采样:卷积、卷积 反卷积 不需要设置outchannel

1.继承nn.model

2.初始化参数,只需要输入通道数

nn.sequential序列中写 卷积(输入是输出的2倍(有contact操作))relu ,卷积,relu

反卷积的nn.sequential 输出通道数减半,保证图片的长宽是原来的2倍和relu函数

3.前向传播,卷积卷积 ,反卷积

Unet的整体结构:

encoder:先池化后卷积

decoder:卷积卷积反卷积

需要把前面卷积的数据进行融合


python 复制代码
#网络
class Downsample(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(Downsample, self).__init__()
        self.conv_relu = nn.Sequential(
                            nn.Conv2d(in_channels, out_channels, 
                                      kernel_size=3, padding=1),
                            nn.ReLU(inplace=True),
                            nn.Conv2d(out_channels, out_channels, 
                                      kernel_size=3, padding=1),
                            nn.ReLU(inplace=True)
            )
        self.pool = nn.MaxPool2d(kernel_size=2)
    def forward(self, x, is_pool=True):
        if is_pool:
            x = self.pool(x)
        x = self.conv_relu(x)
        return x
class Upsample(nn.Module):
    def __init__(self, channels):
        super(Upsample, self).__init__()
        self.conv_relu = nn.Sequential(
                            nn.Conv2d(2*channels, channels, 
                                      kernel_size=3, padding=1),
                            nn.ReLU(inplace=True),
                            nn.Conv2d(channels, channels,  
                                      kernel_size=3, padding=1),
                            nn.ReLU(inplace=True)
            )
        self.upconv_relu = nn.Sequential(
                               nn.ConvTranspose2d(channels, 
                                                  channels//2, 
                                                  kernel_size=3,
                                                  stride=2,
                                                  padding=1,
                                                  output_padding=1),
                               nn.ReLU(inplace=True)
            )
        
    def forward(self, x):
        x = self.conv_relu(x)
        x = self.upconv_relu(x)
        return x
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.down1 = Downsample(3, 64)
        self.down2 = Downsample(64, 128)
        self.down3 = Downsample(128, 256)
        self.down4 = Downsample(256, 512)
        self.down5 = Downsample(512, 1024)
        
        self.up = nn.Sequential(
                               nn.ConvTranspose2d(1024, 
                                                  512, 
                                                  kernel_size=3,
                                                  stride=2,
                                                  padding=1,
                                                  output_padding=1),
                               nn.ReLU(inplace=True)
            )
        
        self.up1 = Upsample(512)
        self.up2 = Upsample(256)
        self.up3 = Upsample(128)
        
        self.conv_2 = Downsample(128, 64)
        self.last = nn.Conv2d(64, 2, kernel_size=1)

    def forward(self, x):
        x1 = self.down1(x, is_pool=False)
        x2 = self.down2(x1)
        x3 = self.down3(x2)
        x4 = self.down4(x3)
        x5 = self.down5(x4)
        
        x5 = self.up(x5)
        
        x5 = torch.cat([x4, x5], dim=1)           # 32*32*1024
        x5 = self.up1(x5)                         # 64*64*256)
        x5 = torch.cat([x3, x5], dim=1)           # 64*64*512  
        x5 = self.up2(x5)                         # 128*128*128
        x5 = torch.cat([x2, x5], dim=1)           # 128*128*256
        x5 = self.up3(x5)                         # 256*256*64
        x5 = torch.cat([x1, x5], dim=1)           # 256*256*128
        
        x5 = self.conv_2(x5, is_pool=False)       # 256*256*64
        
        x5 = self.last(x5)                        # 256*256*3

        return x5
python 复制代码
#测试模型
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model=Net().to(device)
# x = torch.rand([8,3,256,256])
# x=x.to(device)
# y=model(x)
# y.shape
相关推荐
d111111111d12 分钟前
在STM32函数指针是什么,怎么使用还有典型应用场景。
笔记·stm32·单片机·嵌入式硬件·学习·算法
【建模先锋】1 小时前
特征提取+概率神经网络 PNN 的轴承信号故障诊断模型
人工智能·深度学习·神经网络·信号处理·故障诊断·概率神经网络·特征提取
轲轲011 小时前
Week02 深度学习基本原理
人工智能·深度学习
老蒋新思维1 小时前
创客匠人:认知即资产 ——AI 时代创始人 IP 知识变现的底层逻辑
网络·人工智能·网络协议·tcp/ip·重构·创始人ip·创客匠人
smile_Iris1 小时前
Day 40 复习日
人工智能·深度学习·机器学习
深度学习实战训练营1 小时前
TransUNet:Transformer 成为医学图像分割的强大编码器,Transformer 编码器 + U-Net 解码器-k学长深度学习专栏
人工智能·深度学习·transformer
嗷嗷哦润橘_1 小时前
AI Agent学习:MetaGPT之我的工作
人工智能·学习·flask
知识分享小能手2 小时前
CentOS Stream 9入门学习教程,从入门到精通,Linux日志分析工具及应用 —语法详解与实战案例(17)
linux·学习·centos
火山kim2 小时前
经典论文研读报告:DAGGER (Dataset Aggregation)
人工智能·深度学习·机器学习
Coding茶水间2 小时前
基于深度学习的水果检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉