PyTorch torch.nn.Conv3d-Funktion
PyTorch torch.nn Referenzhandbuch
torch.nn.Conv3dEs ist ein 3D-Faltungsmodul in PyTorch.
Es verarbeitet 3D-Eingaben wie Videos oder medizinische Bilder (Tiefe x Höhe x Breite).
Funktionsdefinition
torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
Eingabeform
(batch, channels, depth, height, width)
Anwendungsbeispiele
Beispiel 1: Grundlegende Verwendung
Beispiel
import torch
import torch.nn as nn
# 3D-Faltung
conv3d = nn.Conv3d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
# Eingabe: batch=2, channels=3, depth=16, height=32, width=32
x = torch.randn(2, 3, 16, 32, 32)
output = conv3d(x)
print("Eingabeform:", x.shape)
print("Ausgabeform:", output.shape)
import torch.nn as nn
# 3D-Faltung
conv3d = nn.Conv3d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
# Eingabe: batch=2, channels=3, depth=16, height=32, width=32
x = torch.randn(2, 3, 16, 32, 32)
output = conv3d(x)
print("Eingabeform:", x.shape)
print("Ausgabeform:", output.shape)
Beispiel 2: Videoklassifikation
Beispiel
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) # 16-Frame-Video
output = model(x)
print("Video:", x.shape, "-> Klasse:", output.shape)
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) # 16-Frame-Video
output = model(x)
print("Video:", x.shape, "-> Klasse:", output.shape)
Beispiel 3: Parameterberechnung
Beispiel
import torch
import torch.nn as nn
conv = nn.Conv3d(64, 128, kernel_size=3, padding=1)
# Berechnung der Parameteranzahl: out_ch * in_ch * k_d * k_h * k_w + bias
params = 128 * 64 * 3 * 3 * 3
print("Anzahl der Parameter:", params)
print("Gewichtsform:", conv.weight.shape)
import torch.nn as nn
conv = nn.Conv3d(64, 128, kernel_size=3, padding=1)
# Berechnung der Parameteranzahl: out_ch * in_ch * k_d * k_h * k_w + bias
params = 128 * 64 * 3 * 3 * 3
print("Anzahl der Parameter:", params)
print("Gewichtsform:", conv.weight.shape)
Anwendungsfälle
- Videoverarbeitung: Aktionserkennung
- Medizinische Bildgebung: CT、MRI
- 3D-Segmentierung: Voxeldaten
Hinweis: 3D-Faltung erfordert einen hohen Rechenaufwand und benötigt mehr GPU-Speicher.
Andere Erweiterungen