【问题标题】:Concatenating ResNet-50 predictions PyTorch连接 ResNet-50 预测 PyTorch
【发布时间】: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


【解决方案1】:

如果要将结果堆叠在张量中:

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

【讨论】:

  • 如何将“结果”(即“NoneType”对象)移动到“设备”?做results.to(device) throws: "AttributeError: 'NoneType' object has no attribute 'to'"
  • 将答案更改为从一开始就使用张量。现在应该可以使用to(device)了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2021-04-15
  • 2021-10-23
  • 2018-12-22
  • 2017-12-09
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多