1.对一个3通道,5*5的矩阵,进行全局平均池化
每个矩阵的大小都是 5x5,假设这些矩阵代表一幅图像的三个不同通道。为简单起见,我们将这三个矩阵分别称为 A、B 和 C。合成图像将是一个三通道图像,每个通道由其中一个矩阵表示。
A = [[a11, a12, a13, a14, a15],
[a21, a22, a23, a24, a25],
[a31, a32, a33, a34, a35],
[a41, a42, a43, a44, a45],
[a51, a52, a53, a54, a55]]
B = [[b11, b12, b13, b14, b15],
[b21, b22, b23, b24, b25],
[b31, b32, b33, b34, b35],
[b41, b42, b43, b44, b45],
[b51, b52, b53, b54, b55]]
C = [[c11, c12, c13, c14, c15],
[c21, c22, c23, c24, c25],
[c31, c32, c33, c34, c35],
[c41, c42, c43, c44, c45],
[c51, c52, c53, c54, c55]]
现在,全局平均池操作将独立应用于每个通道。对于每个通道,它会计算该通道中所有元素的平均值。计算结果是一个向量,每个通道只有一个值。
avg_A = (a11 + a12 + ... + a55) / 25
avg_B = (b11 + b12 + ... + b55) / 25
avg_C = (c11 + c12 + ... + c55) / 25
这样,经过全局平均汇集后的合成图像将是一个 3 通道图像,每个通道由其原始矩阵的平均值表示:
Composite Image = [[avg_A, avg_B, avg_C]]
2.torch示例
import torch
import torch.nn as nn
# Generate a random 3-channel matrix with integer values for a batch of size 2
torch.manual_seed(42) # Setting seed for reproducibility
batch_size = 1
image_matrix = torch.randint(0, 10, (batch_size, 3, 5, 5), dtype=torch.float32) # Batch size 2, 3 channels, 5x5 matrix
# Display the original matrix
print("Original Matrix:")
print(image_matrix)
# Apply global average pooling using nn.AdaptiveAvgPool2d
adaptive_avg_pool = nn.AdaptiveAvgPool2d(1)
global_avg_pooled = adaptive_avg_pool(image_matrix)
# Display the result after global average pooling
print("\nResult after Global Average Pooling:")
print(global_avg_pooled)
Original Matrix:
tensor([[[[2., 7., 6., 4., 6.],
[5., 0., 4., 0., 3.],
[8., 4., 0., 4., 1.],
[2., 5., 5., 7., 6.],
[9., 6., 3., 1., 9.]],
[[3., 1., 9., 7., 9.],
[2., 0., 5., 9., 3.],
[4., 9., 6., 2., 0.],
[6., 2., 7., 9., 7.],
[3., 3., 4., 3., 7.]],
[[0., 9., 0., 9., 6.],
[9., 5., 4., 8., 8.],
[6., 0., 0., 0., 0.],
[1., 3., 0., 1., 1.],
[7., 9., 4., 3., 8.]]]])
Result after Global Average Pooling:
tensor([[[[4.2800]],
[[4.8000]],
[[4.0400]]]])