Pytorch Torch Nn Conv3D
# PyTorch torch.nn.Conv3d Function
[ PyTorch torch.nn Reference Manual](#)
* * *
`torch.nn.Conv3d` is a 3D convolution module in PyTorch.
It processes 3D inputs, such as video or medical images (depth x height x width).
### Function Definition
torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
### Input Shape
(batch, channels, depth, height, width)
* * *
## Usage Examples
### Example 1: Basic Usage
## Example
import torch
import torch.nn as nn
# 3D convolution
conv3d = nn.Conv3d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
# Input: batch=2, channels=3, depth=16, height=32, width=32
x = torch.randn(2,3,16,32,32)
output = conv3d(x)
print("Input shape:", x.shape)
print("Output shape:", output.shape)
### Example 2: Video Classification
## Example
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,"-> Classes:", output.shape)
### Example 3: Parameter Calculation
## Example
import torch
import torch.nn as nn
conv = nn.Conv3d(64,128, kernel_size=3, padding=1)
# Parameter count calculation: out_ch * in_ch * k_d * k_h * k_w + bias
params =128 * 64 * 3 * 3 * 3
print("Parameters:", params)
print("Weight shape:", conv.weight.shape)
* * *
## Use Cases
* **Video Processing**: Action recognition
* **Medical Imaging**: CT, MRI
* **3D Segmentation**: Voxel data
>
YouTip