FCNでは、バックボーンから得られる高レベルの特徴量だけでなく、中間層の低レベルの特徴量も活用することで、セグメンテーション精度を向上させることができます。これを実現するために、Feature Pyramid Network (FPN) や U-Net構造 のようなスキップ接続を導入します。
ASPPは、異なるレートの空洞畳み込み(Dilated Convolution)を使用して、マルチスケールのコンテキスト情報を効果的にキャプチャします。これにより、セグメンテーションタスクで特に有益な広範囲の文脈情報を取得できます。
Dropoutは過学習を防ぐために有用ですが、セグメンテーションタスクでは過度に使用すると性能が低下することがあります。必要に応じて、Dropoutの確率を調整または削除します。
python Copy code
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
class ImprovedFCN(nn.Module):
def __init__(self, backbone, num_classes=21):
super(ImprovedFCN, self).__init__()
# Backbone: ResNet50の最終層(GAPとFC層)を除去
self.backbone = backbone
# 中間層の特徴量を取得するための層を分離
self.layer1 = nn.Sequential(*list(backbone.children())[:5]) # resnetのlayer1
self.layer2 = nn.Sequential(*list(backbone.children())[5]) # resnetのlayer2
self.layer3 = nn.Sequential(*list(backbone.children())[6]) # resnetのlayer3
self.layer4 = nn.Sequential(*list(backbone.children())[7]) # resnetのlayer4
# ASPPモジュールの定義
self.aspp = ASPP(in_channels=2048, out_channels=256)
# デコーダーモジュール(スキップ接続を含む)
self.decoder = Decoder(num_classes=num_classes)
def forward(self, x):
input_shape = x.shape[-2:] # 入力画像のサイズ (H, W)
# Encoderパス
x1 = self.layer1(x) # 低レベル特徴量
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x4 = self.layer4(x3) # 高レベル特徴量
# ASPPによるマルチスケール特徴量の抽出
x_aspp = self.aspp(x4)
# Decoderパス(スキップ接続を含む)
x = self.decoder(x_aspp, x1)
# 出力サイズへの補間
x = F.interpolate(x, size=input_shape, mode='bilinear', align_corners=True)
return x
# ASPPモジュールの定義
class ASPP(nn.Module):
def __init__(self, in_channels, out_channels):
super(ASPP, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=6, dilation=6, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=12, dilation=12, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=18, dilation=18, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
self.global_avg_pool = nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
self.conv_out = nn.Sequential(
nn.Conv2d(out_channels * 5, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
size = x.shape[2:]
feat1 = self.conv1(x)
feat2 = self.conv2(x)
feat3 = self.conv3(x)
feat4 = self.conv4(x)
feat5 = F.interpolate(self.global_avg_pool(x), size=size, mode='bilinear', align_corners=True)
out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
out = self.conv_out(out)
return out
# デコーダーモジュールの定義
class Decoder(nn.Module):
def __init__(self, num_classes):
super(Decoder, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(256, 48, 1, bias=False),
nn.BatchNorm2d(48),
nn.ReLU(inplace=True)
)
self.conv2 = nn.Sequential(
nn.Conv2d(304, 256, 3, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, num_classes, 1)
)
def forward(self, x, low_level_feat):
low_level_feat = self.conv1(low_level_feat)
x = F.interpolate(x, size=low_level_feat.shape[2:], mode='bilinear', align_corners=True)
x = torch.cat((x, low_level_feat), dim=1)
x = self.conv2(x)
return x
# モデルのインスタンス化
backbone = torchvision.models.resnet50(pretrained=torchvision.models.ResNet50_Weights.DEFAULT)
model = ImprovedFCN(backbone=backbone, num_classes=21)