Fonction PyTorch torch.nn.ConvTranspose2d

PyTorch torch.nn 参考手册Manuel de référence torch.nn de PyTorch


torch.nn.ConvTranspose2dIl s'agit d'une convolution transposée 2D dans PyTorch, également appelée déconvolution ou convolution de suréchantillonnage.

Elle est utilisée pour suréchantillonner les cartes de caractéristiques et est un composant clé des réseaux génératifs et des réseaux de segmentation.

Définition de fonction

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: remplissage de sortie supplémentaire

Exemples d'utilisation

Exemple 1 : Utilisation de base

Exemple

import torch
import torch.nn as nn

# Convolution transposée : suréchantillonnage
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(Entrée :, x.shape, -> Sortie :, output.shape)

Exemple 2 : Réseau génératif

Exemple

import torch
import torch.nn as nn

# Générateur DCGAN simplifié
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(Bruit :, z.shape, -> Image :, img.shape)

Exemple 3 : Suréchantillonnage du réseau de segmentation

Exemple

import torch
import torch.nn as nn

# Partie décodeur 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(Entrée :, x.shape, -> Sortie :, output.shape)

Exemple 4 : Calcul de la sortie avec stride=2

Exemple

import torch
import torch.nn as nn

# Différentes configurations
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}")

Cas d'utilisation

  • Réseaux génératifs: GAN, VAE
  • Segmentation sémantique: U-Net
  • Suréchantillonnage: remplacer le pooling

Remarque : la convolution transposée n'est pas l'opération inverse de la convolution, mais simplement une façon de suréchantillonner.


PyTorch torch.nn 参考手册Manuel de référence torch.nn de PyTorch

Autres extensions