2016-08-12 176 views
1

我創建了一個OpenResty/Lusty項目,並試圖用它來圍繞我正在使用的分類器包裝REST API。不幸的是,我還沒有得到多少運氣,而我的根本原因是,當我嘗試Lua:爲什麼我不需要本地安裝的軟件包?

require 'nn' 
require 'image' 

Lua中無法解釋文件。不幸的是,Lusty不會在日誌中給我一個堆棧跟蹤,而只是返回一個404錯誤。但是,經過很多代碼評論和試用/錯誤之後,我確定了根本原因是當我嘗試需要這些軟件包時發生錯誤。

我已經在Docker容器上安裝了使用Luarocks的OpenResty/Lusty,該容器是使用Torch7和其他實用程序預先構建的。當我嘗試自行運行分類器時,我可以輕鬆地使用th classify.lua。然而,當我嘗試使它成爲一個包裝在它自己的函數中的Lusty請求時,上面的導入失敗,Lusty返回一個404錯誤,我相信這實際上是一個500錯誤,因爲如果我註釋掉火炬代碼,它將返回200

重要的是要注意我已經使用了預構建容器附帶的luarocks命令。此外,當我檢查了本地安裝文件夾我發現下面的軟件包,包括nnimage

installed packages

下面是我使用的執行代碼,任何想法,爲什麼我不能導入這些包?

package.path在classify.lua

.\/app\/?.lua;\/opt\/openresty\/lualib\/?.lua;\/opt\/openresty\/lualib\/?\/init.lua;\/root\/.luarocks\/share\/lua\/5.1\/?.lua;\/root\/.luarocks\/share\/lua\/5.1\/?\/init.lua;\/root\/torch\/install\/share\/lua\/5.1\/?.lua;\/root\/torch\/install\/share\/lua\/5.1\/?\/init.lua;.\/?.lua;\/root\/torch\/install\/share\/luajit-2.1.0-beta1\/?.lua;\/usr\/local\/share\/lua\/5.1\/?.lua;\/usr\/local\/share\/lua\/5.1\/?\/init.lua; 

lusty_project /應用/請求/ classify.lua

-- these require statements throw an error 
require 'nn' 
require 'image' 


function classifyImage() 
    local ParamBank = require 'ParamBank' 
    local label  = require 'classifier_label' 
    torch.setdefaulttensortype('torch.FloatTensor') 
    torch.setnumthreads(opt.threads) 


    -- set modules to be in use 
    if opt.backend == 'nn' or opt.backend == 'cunn' then 
    require(opt.backend) 
    SpatialConvolution = nn.SpatialConvolutionMM 
    SpatialMaxPooling = nn.SpatialMaxPooling 
    ReLU = nn.ReLU 
    SpatialSoftMax = nn.SpatialSoftMax 
    else 
    assert(false, 'Unknown backend type') 
    end 


    local net = nn.Sequential() 
    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)) 
    if not opt.spatial then net:add(nn.View(1000)) end 
    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) 
    -- close file pointer 
    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(error) 
    return {message: "error"} 
end 

context.template = { 
    type = "mustache", 
    name = "app/templates/layout", 

    partials = { 
    content = "app/templates/classify", 
    } 
} 


context.output = { 
    message = xpcall(classifyImage, errorHandler) 
} 

context.response.status = 200 

謝謝你的人誰可以提供幫助。我是Lua n00b,還沒有習慣它的包裝能力。

更新

我已經想通了如何在最終結果返回錯誤,意識到自己可以捕捉使用xpcall的第二返回變量(local resultCode, error = xpcall(func, errHandler))錯誤後。

的錯誤是這樣的:

{"message":false,"errorMessage":"[string \".\/app\/requests\/classify.lua\"]:3: module 'nn' not found:\n\tno field package.preload['nn']\n\tno file '.\/app\/nn.lua'\n\tno file '\/opt\/openresty\/lualib\/nn.lua'\n\tno file '\/opt\/openresty\/lualib\/nn\/init.lua'\n\tno file '\/root\/.luarocks\/share\/lua\/5.1\/nn.lua'\n\tno file '\/root\/.luarocks\/share\/lua\/5.1\/nn\/init.lua'\n\tno file '\/root\/torch\/install\/share\/lua\/5.1\/nn.lua'\n\tno file '\/root\/torch\/install\/share\/lua\/5.1\/nn\/init.lua'\n\tno file '.\/nn.lua'\n\tno file '\/root\/torch\/install\/share\/luajit-2.1.0-beta1\/nn.lua'\n\tno file '\/usr\/local\/share\/lua\/5.1\/nn.lua'\n\tno file '\/usr\/local\/share\/lua\/5.1\/nn\/init.lua'\n\tno file '\/opt\/openresty\/lualib\/nn.so'\n\tno file '\/root\/torch\/install\/lib\/nn.so'\n\tno file '\/root\/.luarocks\/lib\/lua\/5.1\/nn.so'\n\tno file '\/root\/torch\/install\/lib\/lua\/5.1\/nn.so'\n\tno file '.\/nn.so'\n\tno file '\/usr\/local\/lib\/lua\/5.1\/nn.so'\n\tno file '\/usr\/local\/lib\/lua\/5.1\/loadall.so'"} 

[string "./app/requests/classify.lua"]:5: module 'nn' not found: 
    no field package.preload['nn'] 
    no file './app/nn.lua' 
    no file '/opt/openresty/lualib/nn.lua' 
    no file '/opt/openresty/lualib/nn/init.lua' 
    no file '/root/.luarocks/share/lua/5.1/nn.lua' 
    no file '/root/.luarocks/share/lua/5.1/nn/init.lua' 
    no file '/root/torch/install/share/lua/5.1/nn.lua' 
    no file '/root/torch/install/share/lua/5.1/nn/init.lua' 
    no file './nn.lua' 
    no file '/root/torch/install/share/luajit-2.1.0-beta1/nn.lua' 
    no file '/usr/local/share/lua/5.1/nn.lua' 
    no file '/usr/local/share/lua/5.1/nn/init.lua' 
    no file '/root/torch/extra/nn/nn.lua' 
    no file '/opt/openresty/lualib/nn.lua' 
    no file '/opt/openresty/lualib/nn/init.lua' 
    no file '/root/.luarocks/share/lua/5.1/nn.lua' 
    no file '/root/.luarocks/share/lua/5.1/nn/init.lua' 
    no file '/root/torch/install/share/lua/5.1/nn.lua' 
    no file '/root/torch/install/share/lua/5.1/nn/init.lua' 
    no file './nn.lua' 
    no file '/root/torch/install/share/luajit-2.1.0-beta1/nn.lua' 
    no file '/usr/local/share/lua/5.1/nn.lua' 
    no file '/usr/local/share/lua/5.1/nn/init.lua' 
    no file '/root/torch/extra/image/nn.lua' 
    no file '/root/torch/extra/nn/nn.lua' 
    no file '/opt/openresty/lualib/nn.so' 
    no file '/root/torch/install/lib/nn.so' 
    no file '/root/.luarocks/lib/lua/5.1/nn.so' 
    no file '/root/torch/install/lib/lua/5.1/nn.so' 
    no file './nn.so' 
    no file '/usr/local/lib/lua/5.1/nn.so' 
    no file '/usr/local/lib/lua/5.1/loadall.so' 

當我通過find -name nnnn做搜索,我得到:

./root/torch/install/share/lua/5.1/nn 
./root/torch/install/share/lua/5.1/nngraph 
./root/torch/install/share/lua/5.1/nnx 
./root/torch/install/lib/luarocks/rocks/nn 
./root/torch/install/lib/luarocks/rocks/nn/scm-1/nn-scm-1.rockspec 
./root/torch/install/lib/luarocks/rocks/nngraph 
./root/torch/install/lib/luarocks/rocks/nngraph/scm-1/nngraph-scm-1.rockspec 
./root/torch/install/lib/luarocks/rocks/nnx 
./root/torch/install/lib/luarocks/rocks/nnx/0.1-1/nnx-0.1-1.rockspec 
./root/torch/extra/nngraph 
./root/torch/extra/nngraph/nngraph-scm-1.rockspec 
./root/torch/extra/nnx 
./root/torch/extra/nnx/build/CMakeFiles/nnx.dir 
./root/torch/extra/nnx/nnx-0.1-1.rockspec 
./root/torch/extra/nn 
./root/torch/extra/nn/rocks/nn-scm-1.rockspec 
./root/torch/.git/modules/extra/nngraph 
./root/torch/.git/modules/extra/nnx 
./root/torch/.git/modules/extra/nn 
./usr/local/include/opencv2/flann/nn_index.h 
./usr/share/locale/nn 
./usr/share/i18n/locales/nn_NO 
./usr/share/perl/5.18.2/Unicode/Collate/Locale/nn.pl 
./usr/lib/python3.4/nntplib.py 
./usr/lib/python2.7/nntplib.pyc 
./usr/lib/python2.7/nntplib.py 
+0

我應該在這裏添加我已經嘗試修改'classify.lua'頂部的'package.path',但是它不會嘗試在堆棧跟蹤中檢查該文件。 – crockpotveggies

回答

0

終於能夠解決這一問題。這是一個相當複雜的權限問題,因爲nginx默認是以用戶nobody的身份運行其工作進程。由於OpenResty綁定到這個特定的工作進程,所有兒童Lua進程也綁定到同一個用戶。

最簡單的解決方案是將所有相關文件的權限更改爲同一用戶。所以,你可以這樣做:

chown -R nobody:nogroup openresty_project/ 
chmod -R 755 openresty_project/ 

一旦這樣做,你會重新進入先前已經安裝Luarocks。

請注意,您必須找到所有相關的lua目錄並更改默認用戶。

你也有另一種選擇更改用戶在nginx的配置,它可以像這樣nginx.conf來完成:

user myuser mygroup; 

請注意,以root身份運行是非常危險的,不建議使用。

相關問題