连接 ResNet-50 预测 PyTorch

Concatenating ResNet-50 predictions PyTorch

我正在使用预训练的 ResNet-50 模型,其中删除了最后一个密集层,平均池化层的输出被展平。这样做是为了提取特征。将图像调整为 (300, 300) 后从文件夹中读取图像;它是 RGB 图像。

torch 版本:1.8.1 & torchvision 版本:0.9.1 with Python 3.8.

代码如下:

model_resnet50 = torchvision.models.resnet50(pretrained = True)

# To remove last dense layer from pre-trained model, Use code-
model_resnet50_modified = torch.nn.Sequential(*list(model_resnet50.children())[:-1])

# Using 'AdaptiveAvgPool2d' layer, the predictions have shape-
model_resnet50_modified(images).shape
# torch.Size([32, 2048, 1, 1])

# Add a flatten layer after 'AdaptiveAvgPool2d(output_size=(1, 1))' layer at the end-
model_resnet50_modified.flatten = nn.Flatten()

# Sanity check- make predictions using a batch of images-
predictions = model_resnet50_modified(images)

predictions.shape
# torch.Size([32, 2048])

我现在想将成批图像输入该模型,并垂直连接模型 (32, 2048) 所做的预测。

# number of images in training and validation sets-
len(dataset_train), len(dataset_val)
# (22500, 2500)

一共有22500 + 2500 = 25000张图片。所以最后的 table/matrix 应该有这样的形状:(25000, 2048) -> 图像数量 = 25000 和提取的特征数量 = 2048.

我尝试 运行 使用 np.vstack() 的玩具代码如下:

x = np.random.random_sample(size = (1, 3))
x.shape
# (1, 3)

x
# array([[0.52381798, 0.12345404, 0.1556422 ]])

for i in range(5):
    y = np.random.random_sample(size = (1, 3))
    np.vstack((x, y))
    
x
# array([[0.52381798, 0.12345404, 0.1556422 ]])

解决方案?

谢谢!

如果你想将结果堆叠在张量中:

results = torch.empty((0,2048))
results.to(device)
results = torch.cat((results, predictions), 0)