使用resnet模型进行图像语义分割的Python代码示例
import torch
import torch.nn as nn
import torchvision.models as models
def create_resnet_segmenter(num_classes):
resnet = models.resnet50(pretrained=True) # Load pre-trained ResNet50 model
num_features = resnet.fc.in_features
resnet.fc = nn.Conv2d(num_features, num_classes, kernel_size=1) # Replace fully connected layer with 1x1 convolutional layer
segmenter = nn.Sequential(
resnet,
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=32, mode='bilinear', align_corners=True) # Upsample the output to match the original image size
)
return segmenter
# Create an instance of the ResNet based segmenter
segmenter = create_resnet_segmenter(num_classes=21) # 21 classes for PASCAL VOC dataset
# Load an example image
image = torch.randn(1, 3, 224, 224)
# Pass the image through the segmenter
output = segmenter(image)
# Print the output shape
print(output.shape)
# Output:
# torch.Size([1, 21, 224, 224])
In this code example, we first define a function create_resnet_segmenter which takes as input the number of classes to be segmented. It creates an instance of the ResNet50 model pretrained on ImageNet dataset and replaces the fully connected layer with a 1x1 convolutional layer. It also includes a ReLU activation and an upsampling layer to match the original image size.
We then create an instance of the ResNet based segmenter with 21 classes (assuming Pascal VOC dataset) and load an example image.
Finally, we pass the image through the segmenter and print the output shape. The output shape is [1, 21, 224, 224], where the first dimension represents the batch size, the second dimension represents the number of classes, and the last two dimensions represent the image size.
