PyTorch torch.nn.ConvTranspose2d-Funktion
PyTorch torch.nn Referenzhandbuch
torch.nn.ConvTranspose2dEs ist eine 2D-transponierte Faltung in PyTorch, auch bekannt als Dekonvolution oder Upsampling-Faltung.
Sie wird zum Upsampling von Feature-Maps verwendet und ist eine Schlüsselkomponente generativer Netzwerke und Segmentierungsnetzwerke.
Funktionsdefinition
torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1)
Parameters
output_padding: zusätzliches Output-Padding
Anwendungsbeispiele
Beispiel 1: Grundlegende Verwendung
Beispiel
import torch
import torch.nn as nn
# Transponierte Faltung: Upsampling
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(Eingabe:, x.shape, -> Ausgabe:, output.shape)
import torch.nn as nn
# Transponierte Faltung: Upsampling
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(Eingabe:, x.shape, -> Ausgabe:, output.shape)
Beispiel 2: Generatives Netzwerk
Beispiel
import torch
import torch.nn as nn
# Vereinfachter DCGAN-Generator
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(Rauschen:, z.shape, -> Bild:, img.shape)
import torch.nn as nn
# Vereinfachter DCGAN-Generator
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(Rauschen:, z.shape, -> Bild:, img.shape)
Beispiel 3: Segmentierungsnetzwerk-Upsampling
Beispiel
import torch
import torch.nn as nn
# U-Net-Decoder-Teil
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(Eingabe:, x.shape, -> Ausgabe:, output.shape)
import torch.nn as nn
# U-Net-Decoder-Teil
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(Eingabe:, x.shape, -> Ausgabe:, output.shape)
Beispiel 4: Ausgabeberechnung mit stride=2
Beispiel
import torch
import torch.nn as nn
# Verschiedene Konfigurationen
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
# Verschiedene Konfigurationen
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}")
Anwendungsfälle
- Generative Netzwerke: GAN, VAE
- Semantische Segmentierung: U-Net
- Upsampling: Pooling ersetzen
Hinweis: Die transponierte Faltung ist nicht die Umkehroperation der Faltung, sondern nur eine Möglichkeit des Upsamplings.
Weitere Erweiterungen