Fonction torch.nn.Conv3d de PyTorch

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


torch.nn.Conv3dIl s'agit d'un module de convolution 3D dans PyTorch.

Il traite des entrées 3D, telles que des vidéos ou des images médicales (profondeur x hauteur x largeur).

Définition de fonction

torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

Forme d'entrée

(batch, channels, depth, height, width)

Exemples d'utilisation

Exemple 1 : Utilisation de base

Exemple

import torch
import torch.nn as nn

# Convolution 3D
conv3d = nn.Conv3d(in_channels=3, out_channels=32, kernel_size=3, padding=1)

# Entrée : batch=2, channels=3, depth=16, height=32, width=32
x = torch.randn(2, 3, 16, 32, 32)
output = conv3d(x)

print("Forme d'entrée :", x.shape)
print("Forme de sortie :", output.shape)

Exemple 2 : Classification vidéo

Exemple

import torch
import torch.nn as nn

class VideoCNN(nn.Module):
    def __init__(self, num_classes=10):
        super(VideoCNN, self).__init__()
        self.conv1 = nn.Conv3d(3, 32, kernel_size=3, padding=1)
        self.pool = nn.MaxPool3d(2)
        self.conv2 = nn.Conv3d(32, 64, kernel_size=3, padding=1)
        self.gap = nn.AdaptiveAvgPool3d(1)
        self.fc = nn.Linear(64, num_classes)

    def forward(self, x):
        x = torch.relu(self.conv1(x))
        x = self.pool(x)
        x = torch.relu(self.conv2(x))
        x = self.gap(x)
        x = x.view(x.size(0), -1)
        return self.fc(x)

model = VideoCNN()
x = torch.randn(4, 3, 16, 112, 112)  # vidéo de 16 images
output = model(x)

print("Vidéo :", x.shape, "-> Classe :", output.shape)

Exemple 3 : Calcul des paramètres

Exemple

import torch
import torch.nn as nn

conv = nn.Conv3d(64, 128, kernel_size=3, padding=1)

# Calcul du nombre de paramètres : out_ch * in_ch * k_d * k_h * k_w + bias
params = 128 * 64 * 3 * 3 * 3
print("Nombre de paramètres :", params)
print("Forme des poids :", conv.weight.shape)

Cas d'utilisation

  • Traitement vidéo: reconnaissance d'actions
  • Imagerie médicale: CT, IRM
  • Segmentation 3D: données de voxels

Remarque : la convolution 3D nécessite une grande quantité de calcul et davantage de mémoire GPU.


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

Autres extensions