PCAM数据集上面验证CNN CNN+AutoEncoder TL-CNN效果差异与比较
1. 数据集理解
PCam(PatchCamelyon)数据集是一个面向医学图像分析的二分类图像数据集,专为转移性癌症检测任务设计。它由从淋巴结切片的数字化组织病理学扫描中提取的327,680张96×96像素的彩色图像(具有三个通道)组成,每张图像都带有一个二进制标签,表示该图像中是否存在转移性肿瘤组织。
数据集特点:
- 任务类型:图像二分类(有无转移性癌症)
- 图像尺寸:三通道的96×96 像素
- 图像来源:淋巴结组织病理切片
- 标签形式:二进制(0:无转移,1:有转移)
- 数据规模:约32.7万张图像,介于CIFAR-10与ImageNet之间,适合在单GPU上训练
2.网络设计
2.1 基础CNN
class BaselineCNN(nn.Module):"""基准 CNN 分类器架构"""def __init__(self, num_classes=2):super(BaselineCNN, self).__init__()# 卷积块 1self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)self.bn1 = nn.BatchNorm2d(32)self.pool1 = nn.MaxPool2d(2, 2) # 96x96 -> 48x48# 卷积块 2self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)self.bn2 = nn.BatchNorm2d(64)self.pool2 = nn.MaxPool2d(2, 2) # 48x48 -> 24x24# 卷积块 3self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)self.bn3 = nn.BatchNorm2d(128)self.pool3 = nn.MaxPool2d(2, 2) # 24x24 -> 12x12# 全连接层self.fc1 = nn.Linear(128 * 12 * 12, 512)self.dropout = nn.Dropout(0.5)self.fc2 = nn.Linear(512, num_classes)
卷积块可视化
https://poloclub.github.io/cnn-explainer/#article-input
https://poloclub.github.io/cnn-explainer/#article-input
2.2 AutoEncoder+CNN
class Encoder(nn.Module):"""编码器部分:图像压缩"""def __init__(self):super(Encoder, self).__init__()# 卷积块 1self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)self.bn1 = nn.BatchNorm2d(32)self.pool1 = nn.MaxPool2d(2, 2) # 96x96 -> 48x48# 卷积块 2self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)self.bn2 = nn.BatchNorm2d(64)self.pool2 = nn.MaxPool2d(2, 2) # 48x48 -> 24x24# 卷积块 3self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)self.bn3 = nn.BatchNorm2d(128)self.pool3 = nn.MaxPool2d(2, 2) # 24x24 -> 12x12def forward(self, x):x = self.pool1(torch.relu(self.bn1(self.conv1(x))))x = self.pool2(torch.relu(self.bn2(self.conv2(x))))x = self.pool3(torch.relu(self.bn3(self.conv3(x))))return xclass Decoder(nn.Module):"""解码器部分:图像重建"""def __init__(self):super(Decoder, self).__init__()# 转置卷积块 1self.conv1 = nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1, output_padding=1)self.bn1 = nn.BatchNorm2d(64)# 转置卷积块 2self.conv2 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1, output_padding=1)self.bn2 = nn.BatchNorm2d(32)# 转置卷积块 3self.conv3 = nn.ConvTranspose2d(32, 3, kernel_size=3, stride=2, padding=1, output_padding=1)def forward(self, x):x = torch.relu(self.bn1(self.conv1(x))) # 12x12 -> 24x24x = torch.relu(self.bn2(self.conv2(x))) # 24x24 -> 48x48x = torch.sigmoid(self.conv3(x)) # 48x48 -> 96x96return xclass ConvolutionalAutoencoder(nn.Module):"""卷积自动编码器:编码器 + 解码器"""def __init__(self):super(ConvolutionalAutoencoder, self).__init__()self.encoder = Encoder()self.decoder = Decoder()def forward(self, x):encoded = self.encoder(x)decoded = self.decoder(encoded)return decoded
解码器可视化
https://blog.csdn.net/weixin_53598445/article/details/130596673
https://blog.csdn.net/weixin_53598445/article/details/130596673
2.3 TL-CNN
class TransferLearningClassifier(nn.Module):"""基于迁移学习的分类器:编码器 + 分类层"""def __init__(self, encoder, num_classes=2):super(TransferLearningClassifier, self).__init__()self.encoder = encoder# 分类层self.fc1 = nn.Linear(128 * 12 * 12, 512)self.dropout = nn.Dropout(0.5)self.fc2 = nn.Linear(512, num_classes)
迁移学习(Transfer Learning)是一种机器学习方法,它允许模型将在一个任务上学到的知识应用到另一个相关任务上。这种方法特别有用,因为它可以减少训练新模型所需的数据量和计算资源,同时加快训练过程。
这个就是基于特征迁移学习,将编码器学到的数据集的特征直接用来训练新的分类任务。
