【发布时间】:2021-09-08 13:57:00
【问题描述】:
我使用的是预训练的 ResNet-50 模型,其中最后一个密集被移除,平均池化层的输出被展平。这样做是为了特征提取目的。图片大小调整为(300, 300)后从文件夹中读取;这是 RGB 图像。
torch 版本:1.8.1 和 torchvision 版本:0.9.1 和 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 张图片。所以最终表格/矩阵的形状应该是:(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 ]])
解决方案?
谢谢!
【问题讨论】:
-
您要获取的数据类型是什么?您想将它们堆叠在张量中吗?
标签: numpy pytorch conv-neural-network python-3.8 resnet