PyTorch torch.nn.ConvTranspose2d 関数
torch.nn.ConvTranspose2dは PyTorch の2次元転置畳み込みで、逆畳み込みまたはアップサンプリング畳み込みとも呼ばれます。
これは特徴マップのアップサンプリングに使用され、生成ネットワークとセグメンテーションネットワークの重要な構成要素です。
関数定義
torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1)
パラメータ
output_padding: 追加出力パディング
使用例
例1: 基本的な使い方
実例
import torch
import torch.nn as nn
# 転置畳み込み: アップサンプリング
deconv = nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=2, stride=2)
x = torch.randn(1, 64, 16, 16)
output = deconv(x)
print("入力:", x.shape, "-> 出力:", output.shape)
import torch.nn as nn
# 転置畳み込み: アップサンプリング
deconv = nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=2, stride=2)
x = torch.randn(1, 64, 16, 16)
output = deconv(x)
print("入力:", x.shape, "-> 出力:", output.shape)
例2: 生成ネットワーク
実例
import torch
import torch.nn as nn
# 簡略化された DCGAN ジェネレーター
class Generator(nn.Module):
def __init__(self, latent_dim=100):
super(Generator, self).__init__()
self.fc = nn.Linear(latent_dim, 512 * 4 * 4)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(512, 256, 4, 2, 1), # 4->8
nn.BatchNorm2d(256),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 4, 2, 1), # 8->16
nn.BatchNorm2d(128),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 4, 2, 1), # 16->32
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, 3, 4, 2, 1), # 32->64
nn.Tanh()
)
def forward(self, x):
x = self.fc(x).view(-1, 512, 4, 4)
return self.deconv(x)
gen = Generator()
z = torch.randn(1, 100)
img = gen(z)
print("ノイズ:", z.shape, "-> 画像:", img.shape)
import torch.nn as nn
# 簡略化された DCGAN ジェネレーター
class Generator(nn.Module):
def __init__(self, latent_dim=100):
super(Generator, self).__init__()
self.fc = nn.Linear(latent_dim, 512 * 4 * 4)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(512, 256, 4, 2, 1), # 4->8
nn.BatchNorm2d(256),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 4, 2, 1), # 8->16
nn.BatchNorm2d(128),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 4, 2, 1), # 16->32
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, 3, 4, 2, 1), # 32->64
nn.Tanh()
)
def forward(self, x):
x = self.fc(x).view(-1, 512, 4, 4)
return self.deconv(x)
gen = Generator()
z = torch.randn(1, 100)
img = gen(z)
print("ノイズ:", z.shape, "-> 画像:", img.shape)
例3: セグメンテーションネットワークのアップサンプリング
実例
import torch
import torch.nn as nn
# U-Net デコーダー部分
decoder = nn.Sequential(
nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2),
nn.Conv2d(128, 128, 3, padding=1),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2),
nn.Conv2d(64, 64, 3, padding=1),
nn.ReLU()
)
x = torch.randn(1, 256, 8, 8)
output = decoder(x)
print("入力:", x.shape, "-> 出力:", output.shape)
import torch.nn as nn
# U-Net デコーダー部分
decoder = nn.Sequential(
nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2),
nn.Conv2d(128, 128, 3, padding=1),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2),
nn.Conv2d(64, 64, 3, padding=1),
nn.ReLU()
)
x = torch.randn(1, 256, 8, 8)
output = decoder(x)
print("入力:", x.shape, "-> 出力:", output.shape)
例4: stride=2 の出力計算
実例
import torch
import torch.nn as nn
# 異なる設定
configs = [
(1, 1, 0), # stride=1, padding=0
(2, 1, 0), # stride=2, padding=0
(2, 1, 1), # stride=2, padding=1
]
x = torch.randn(1, 64, 4, 4)
for stride, k, p in configs:
deconv = nn.ConvTranspose2d(64, 64, kernel_size=k, stride=stride, padding=p)
out = deconv(x)
print(f"k={k}, s={stride}, p={p}: {x.shape} -> {out.shape}")
import torch.nn as nn
# 異なる設定
configs = [
(1, 1, 0), # stride=1, padding=0
(2, 1, 0), # stride=2, padding=0
(2, 1, 1), # stride=2, padding=1
]
x = torch.randn(1, 64, 4, 4)
for stride, k, p in configs:
deconv = nn.ConvTranspose2d(64, 64, kernel_size=k, stride=stride, padding=p)
out = deconv(x)
print(f"k={k}, s={stride}, p={p}: {x.shape} -> {out.shape}")
使用シーン
- 生成ネットワーク: GAN、VAE
- セマンティックセグメンテーション: U-Net
- アップサンプリング: pooling の代替
注意:転置畳み込みは畳み込みの逆演算ではなく、アップサンプリングの1つの方法にすぎません。
その他の拡張
PyTorch torch.nn リファレンスマニュアル