VGGネットワーク:モジュラー設計による画像認識モデル

深層ニューラルネットワークの設計において、モジュラー構造を導入した代表的なアーキテクチャがVGGネットワークである。VGGは畳み込み層の繰り返しブロックを基本単位とし、画像認識タスクにおける高い精度を実現した。

VGGブロックの構成

VGGブロックは複数の畳み込み層とプーリング層で構成される。各畳み込み層の特徴は:

  • カーネルサイズ:3×3
  • パディング:1(空間解像度の維持)
  • 活性化関数:ReLU

プーリング層には:

  • 2×2マックスプーリング
  • ストライド:2(解像度半減)

PyTorch実装例:

import torch
from torch import nn

class VGGBlock(nn.Module):
    def __init__(self, conv_layers, input_ch, output_ch):
        super().__init__()
        self.conv_seq = nn.Sequential()
        
        for _ in range(conv_layers):
            self.conv_seq.append(
                nn.Conv2d(input_ch, output_ch, kernel_size=3, padding=1)
            )
            self.conv_seq.append(nn.ReLU())
            input_ch = output_ch
            
        self.conv_seq.append(nn.MaxPool2d(kernel_size=2, stride=2))
    
    def forward(self, x):
        return self.conv_seq(x)

ネットワークアーキテクチャ

標準的なVGG-11アーキテクチャの構成:

conv_config = [(1, 64), (1, 128), (2, 256), (2, 512), (2, 512)]

def build_vgg(arch):
    blocks = []
    channels = 1
    
    for layers, out_ch in arch:
        blocks.append(VGGBlock(layers, channels, out_ch))
        channels = out_ch
        
    return nn.Sequential(
        *blocks,
        nn.Flatten(),
        nn.Linear(out_ch * 7 * 7, 4096), nn.ReLU(), nn.Dropout(0.5),
        nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5),
        nn.Linear(4096, 10)
    )

動作検証

入力テンソルによる形状変化の確認:

model = build_vgg(conv_config)
sample = torch.randn(1, 1, 224, 224)

for module in model:
    sample = module(sample)
    print(f"{module.__class__.__name__:15} output: {tuple(sample.shape)}")

実践的訓練

リソース制約対応のためチャネル数を縮小:

scaled_arch = [(l, c // 4) for l, c in conv_config]
compact_net = build_vgg(scaled_arch)

タグ: VGG CNN 画像認識 深層学習 PyTorch

5月18日 17:45 投稿