在Torch中为ResNet模型添加Softmax

背景:我正在尝试修改这个Facebook的ResNet特征提取脚本,以对图像进行分类并打印ImageNet类标签。 假设我有一个Torch模型:

local model = torch.load('resnet-101.t7')
local output = model:forward(img:cuda()):squeeze(1)

这给我提供了每个类的得分。 我想获得前5个类及其概率。 我认为要将得分转换为概率,应首先使用SoftMax层。

所以我这样做:

local model = torch.load('resnet-101.t7')
local softMaxLayer = cudnn.LogSoftMax()
model:add(softMaxLayer)
local output = model:forward(img:cuda()):squeeze(1)

但是当我运行它时,我得到了以下错误:

/SpatialSoftMax.lua:38: bad argument #1 to 'resizeAs' (torch.DoubleTensor expected, got torch.CudaTensor)

对我来说,模型看起来很好:(仅显示最后几层)

  ...
  (9): cudnn.SpatialAveragePooling(7,7,1,1)
  (10): nn.View(2048)
  (11): nn.Linear(2048 -> 1000)
  (12): cudnn.LogSoftMax
}

有什么想法可能错了吗?

点赞
用户117844
用户117844

图层具有与之关联的类型。默认情况下,会给您一个双重类型

local softMaxLayer = cudnn.LogSoftMax():cuda()
model:add(softMaxLayer)
2016-02-20 18:46:56