我有一個Lusty(OpenResty的框架)API,它包裝了一個Torch分類器。到目前爲止,我已經能夠得到一個單一的請求工作,但是到API每個後續請求觸發以下錯誤,沒有詳細的堆棧跟蹤:Lua Torch7&OpenResty:試圖索引一個零值
attempt to index a nil value
出現的錯誤,當我打電話給拋出:
net:add(SpatialConvolution(3, 96, 7, 7, 2, 2))
成功完成第一個請求而每個附加請求失敗的行爲是解決問題的線索。
我已將以下完整代碼粘貼到app/requests/classify.lua。這似乎是某種變量緩存/初始化問題,雖然我對Lua的有限知識不能幫助我調試問題。我已經嘗試過做很多事情,包括將我的導入改爲local torch = require('torch')
等本地化變量,並將這些導入移到classifyImage()
函數中。
torch = require 'torch'
nn = require 'nn'
image = require 'image'
ParamBank = require 'ParamBank'
label = require 'classifier_label'
torch.setdefaulttensortype('torch.FloatTensor')
function classifyImage()
local opt = {
inplace = false,
network = "big",
backend = "nn",
save = "model.t7",
img = context.input.image,
spatial = false,
threads = 4
}
torch.setnumthreads(opt.threads)
require(opt.backend)
local SpatialConvolution = nn.SpatialConvolutionMM
local SpatialMaxPooling = nn.SpatialMaxPooling
local ReLU = nn.ReLU
local SpatialSoftMax = nn.SpatialSoftMax
local net = nn.Sequential()
print('==> init a big overfeat network')
net:add(SpatialConvolution(3, 96, 7, 7, 2, 2))
net:add(ReLU(opt.inplace))
net:add(SpatialMaxPooling(3, 3, 3, 3))
net:add(SpatialConvolution(96, 256, 7, 7, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialMaxPooling(2, 2, 2, 2))
net:add(SpatialConvolution(256, 512, 3, 3, 1, 1, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialConvolution(512, 512, 3, 3, 1, 1, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialConvolution(512, 1024, 3, 3, 1, 1, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialConvolution(1024, 1024, 3, 3, 1, 1, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialMaxPooling(3, 3, 3, 3))
net:add(SpatialConvolution(1024, 4096, 5, 5, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialConvolution(4096, 4096, 1, 1, 1, 1))
net:add(ReLU(opt.inplace))
net:add(SpatialConvolution(4096, 1000, 1, 1, 1, 1))
net:add(nn.View(1000))
net:add(SpatialSoftMax())
-- print(net)
-- init file pointer
print('==> overwrite network parameters with pre-trained weigts')
ParamBank:init("net_weight_1")
ParamBank:read( 0, {96,3,7,7}, net:get(1).weight)
ParamBank:read( 14112, {96}, net:get(1).bias)
ParamBank:read( 14208, {256,96,7,7}, net:get(4).weight)
ParamBank:read( 1218432, {256}, net:get(4).bias)
ParamBank:read( 1218688, {512,256,3,3}, net:get(7).weight)
ParamBank:read( 2398336, {512}, net:get(7).bias)
ParamBank:read( 2398848, {512,512,3,3}, net:get(9).weight)
ParamBank:read( 4758144, {512}, net:get(9).bias)
ParamBank:read( 4758656, {1024,512,3,3}, net:get(11).weight)
ParamBank:read( 9477248, {1024}, net:get(11).bias)
ParamBank:read( 9478272, {1024,1024,3,3}, net:get(13).weight)
ParamBank:read(18915456, {1024}, net:get(13).bias)
ParamBank:read(18916480, {4096,1024,5,5}, net:get(16).weight)
ParamBank:read(123774080, {4096}, net:get(16).bias)
ParamBank:read(123778176, {4096,4096,1,1}, net:get(18).weight)
ParamBank:read(140555392, {4096}, net:get(18).bias)
ParamBank:read(140559488, {1000,4096,1,1}, net:get(20).weight)
ParamBank:read(144655488, {1000}, net:get(20).bias)
ParamBank:close()
-- load and preprocess image
print('==> prepare an input image')
local img = image.load(opt.img):mul(255)
-- use image larger than the eye size in spatial mode
if not opt.spatial then
local dim = (opt.network == 'small') and 231 or 221
local img_scale = image.scale(img, '^'..dim)
local h = math.ceil((img_scale:size(2) - dim)/2)
local w = math.ceil((img_scale:size(3) - dim)/2)
img = image.crop(img_scale, w, h, w + dim, h + dim):floor()
end
-- memcpy from system RAM to GPU RAM if cuda enabled
if opt.backend == 'cunn' or opt.backend == 'cudnn' then
net:cuda()
img = img:cuda()
end
-- save bare network (before its buffer filled with temp results)
print('==> save model to:', opt.save)
torch.save(opt.save, net)
-- feedforward network
print('==> feed the input image')
timer = torch.Timer()
img:add(-118.380948):div(61.896913)
local out = net:forward(img)
-- find output class name in non-spatial mode
local results = {}
local topN = 10
local probs, idxs = torch.topk(out, topN, 1, true)
for i=1,topN do
print(label[idxs[i]], probs[i])
local r = {}
r.label = label[idxs[i]]
r.prob = probs[i]
results[i] = r
end
return results
end
function errorHandler(err)
return tostring(err)
end
local success, result = xpcall(classifyImage, errorHandler)
context.template = {
type = "mustache",
name = "app/templates/layout",
partials = {
content = "app/templates/classify",
}
}
context.output = {
success = success,
result = result,
request = context.input
}
context.response.status = 200
感謝您的幫助!
更新1
新增print(net)
之前和之後local net
而且之後我打電話net:add
。每次在local net
初始化之前,它顯示的值爲nil
。正如預期的那樣,在初始化net
之後,它顯示了一個火炬對象作爲值。看樣子:add
調用裏面的東西被創造的錯誤,所以我加了聲明我classifyImage
功能後立即以下幾點:
print(tostring(torch))
print(tostring(nn))
print(tostring(net))
添加這些新的打印報表後,我得到的一號請求以下
nil
nil
nil
,然後在第二個請求:
table: 0x41448a08
table: 0x413bdb10
nil
和位於3要求:
table: 0x41448a08
table: 0x413bdb10
nil
那些看起來像指向內存中的對象的指針,所以在這裏假設Torch正在創建它自己的全局對象是安全的嗎?
嘗試在調用之前和之後放置'print(net)'。 – hjpotter92
很快完成並在問題中添加詳細信息。本質上,在我在第一/第二次調用中刪除「本地網絡」之前,我成功地獲得了'nil'。初始化'net'後,我也得到一個新的對象。只有當我調用add時,它纔會失敗。你認爲這與'torch'或'nn'本身有關? – crockpotveggies
@ hjpotter92增加了一些更多的信息,看起來像'torch'本身正在創建內存中的干擾代碼的全局對象? – crockpotveggies