簡體   English   中英

Azure Flask Web App上的內部服務器錯誤

[英]Internal Server Error on Azure Flask Web App

我創建了Flask Web應用程序,該應用程序托管在Azure上,並帶有Dreamspark訂閱。 它使用諸如Numpy,Scipy和Theano之類的庫。

當我使用簡單的Theano計算部署應用程序時,它可以完美運行。 但是,當我將代碼更改為更復雜的Theano計算時,出現內部服務器錯誤。

這是示例代碼。 當我調用simpleFunction(類似於Theano函數的求和)時,它可以工作,但是當調用complexFunction(類似於圖像分類計算)時,則創建內部服務器錯誤:

from flask import Flask
app = Flask(__name__)
wsgi_app = app.wsgi_app

from fileContainingTheanoCode import simpleFunction, complexFunction

@app.route('/')
def hello():
    result = simpleFunction()
    thisStr = str(result)
    return   """<html>
                    <head>
                        <title>Test website</title>
                    </head>
                    <body>
                        <h1>"""+thisStr+"""</h1>
                    </body>
                </html>"""


if __name__ == '__main__':
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT)

為了清楚起見,以下是文件theanoCode.py的代碼(它是卷積神經網絡分類器,使用加載模塊從文件中讀取mnist數據集):

# Import Libraries----------------------------------------------------------
import os
import sys
import pickle
import theano
import numpy as np
from theano import tensor as T
from theano.tensor.nnet.conv import conv2d
from theano.tensor.signal.downsample import max_pool_2d
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
srng = RandomStreams()
import load

testSize = 10
trainSize = 2
#---------------------------------------------------------------------------

# Constants-----------------------------------------------------------------
weightsFile = 'mnist.weights'
#---------------------------------------------------------------------------

# Model---------------------------------------------------------------------
# Stuff variable into numpy array with theano float datatype
def floatX(X):
    return np.asarray(X, dtype=theano.config.floatX)


# Initialize weights randomly
def init_weights(shape):
    return theano.shared(np.asarray(np.random.randn(*shape) * 0.01, dtype=theano.config.floatX),borrow=True)


# ReLU - rectify linear function with Theano
def rectify(X):
    return T.maximum(X, 0.)


# Softmax operation
def softmax(X):
    e_x = T.exp(X - X.max(axis=1).dimshuffle(0, 'x'))
    return e_x / e_x.sum(axis=1).dimshuffle(0, 'x')


# Implements random chance of ignoring a neuron output during training
def dropout(X, p=0.):
    if p > 0:
        retain_prob = 1 - p
        X *= srng.binomial(X.shape, p=retain_prob, dtype=theano.config.floatX)
        X /= retain_prob
    return X


# Gradient Descent with regularization - parameter reduction
def RMSprop(cost, params, lr=0.001, rho=0.9, epsilon=1e-6):
    grads = T.grad(cost=cost, wrt=params)
    updates = []
    for p, g in zip(params, grads):
        acc = theano.shared(p.get_value() * 0.)
        acc_new = rho * acc + (1 - rho) * g ** 2
        gradient_scaling = T.sqrt(acc_new + epsilon)
        g = g / gradient_scaling
        updates.append((acc, acc_new))
        updates.append((p, p - lr * g))
    return updates


# Constructs the model
def model(X, w1, w2, w3, w4, p_drop_conv, p_drop_hidden):
    l1a = rectify(conv2d(X, w1, border_mode='full'))
    l1 = max_pool_2d(l1a, (2, 2),ignore_border=False)
    l1 = dropout(l1, p_drop_conv)

    l2a = rectify(conv2d(l1, w2))
    l2 = max_pool_2d(l2a, (2, 2),ignore_border=False)
    l2 = dropout(l2, p_drop_conv)

    l3a = rectify(conv2d(l2, w3))
    l3b = max_pool_2d(l3a, (2, 2),ignore_border=False)
    l3 = T.flatten(l3b, outdim=2)
    l3 = dropout(l3, p_drop_conv)

    # Fully connected Layer
    l4 = rectify(T.dot(l3, w4))
    l4 = dropout(l4, p_drop_hidden)

    # Classify the Output
    pyx = softmax(T.dot(l4, w_o))
    return l1, l2, l3, l4, pyx


# Load the data (tr = training, te = test)
trX, teX, trY, teY = load.mnist(ntrain=trainSize, ntest=testSize, onehot=True)


# Reshape training set to be 4-dimensional
# negative value is to get ordering right (rather than mirror image)
# Second parameter is color channels
trX = trX.reshape(-1, 1, 28, 28)
teX = teX.reshape(-1, 1, 28, 28)

# Input variables
X = T.dtensor4()
Y = T.fmatrix()


# WEIGHTS
if os.path.isfile(weightsFile):
    # Go get saved weights from file
    [w1, w2, w3, w4, w_o] = pickle.load(open(weightsFile,'rb'))
else:
    # Initialize all layer weights
    # (no. Inputs, no. Outputs, filter height, filter width)
    # ver isto outra vez, minuto 48
    w1 = init_weights((32, 1, 3, 3)) # 1 = num canais de cada imagem (cor)
    w2 = init_weights((64, 32, 3, 3))
    w3 = init_weights((128, 64, 3, 3))
    w4 = init_weights((128 * 3 * 3, 625))
    w_o = init_weights((625, 10)) # from fully connected layer to classifier


# This sets up the model graph and some vars for noise, which are the internal neurons
noise_l1, noise_l2, noise_l3, noise_l4, noise_py_x = model(X, w1, w2, w3, w4, 0.2, 0.5)

# No dropping neurons...so this will be used for prediction
l1, l2, l3, l4, py_x = model(X, w1, w2, w3, w4, 0., 0.)

# This makes predictions
y_x = T.argmax(py_x, axis=1)

# Compile prediction function - here named complexFunction
complexFunction = theano.function(inputs=[X], outputs=y_x, allow_input_downcast=True)

這是詳細的錯誤消息:

Event: MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName  FastCgiModule
Notification    EXECUTE_REQUEST_HANDLER
HttpStatus  500
HttpReason  INTERNAL SERVER ERROR
HttpSubStatus   0
ErrorCode   The operation completed successfully.
 (0x0)

這是事件日志:

</Data></EventData></Event><Event><System><Provider Name="ASP.NET 4.0.30319.0"/><EventID>1325</EventID><Level>3</Level><Task>0</Task><Keywords>Keywords</Keywords><TimeCreated SystemTime="2016-06-05T19:34:20Z"/><EventRecordID>1604538437</EventRecordID><Channel>Application</Channel><Computer>RD000D3A218E0C</Computer><Security/></System><EventData><Data>An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/1568611192/ROOT

Process ID: 647864

Exception: System.Configuration.ConfigurationErrorsException

Message: Couldn't find type for class Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

StackTrace:    at System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type baseType, String initializeData)
   at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
   at System.Diagnostics.ListenerElement.GetRuntimeObject()
   at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
   at System.Diagnostics.TraceInternal.get_Listeners()
   at System.Diagnostics.TraceInternal.WriteLine(String message)
   at System.Diagnostics.Debug.WriteLine(String message)
   at Microsoft.Web.Compilation.Snapshots.SnapshotHelper.TakeSnapshotTimerCallback(Object stateInfo)
   at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.TimerQueueTimer.CallCallback()
   at System.Threading.TimerQueueTimer.Fire()
   at System.Threading.TimerQueue.FireNextTimers()
   at System.Threading.TimerQueue.AppDomainTimerCallback()</Data></EventData></Event><Event><System><Provider Name=".NET Runtime"/><EventID>1026</EventID><Level>0</Level><Task>0</Task><Keywords>Keywords</Keywords><TimeCreated SystemTime="2016-06-05T19:34:20Z"/><EventRecordID>1604538453</EventRecordID><Channel>Application</Channel><Computer>RD000D3A218E0C</Computer><Security/></System><EventData><Data>Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
   at System.Diagnostics.TraceUtils.GetRuntimeObject(System.String, System.Type, System.String)
   at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
   at System.Diagnostics.ListenerElement.GetRuntimeObject()
   at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
   at System.Diagnostics.TraceInternal.get_Listeners()
   at System.Diagnostics.TraceInternal.WriteLine(System.String)
   at System.Diagnostics.Debug.WriteLine(System.String)
   at Microsoft.Web.Compilation.Snapshots.SnapshotHelper.TakeSnapshotTimerCallback(System.Object)
   at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.TimerQueueTimer.CallCallback()
   at System.Threading.TimerQueueTimer.Fire()
   at System.Threading.TimerQueue.FireNextTimers()
   at System.Threading.TimerQueue.AppDomainTimerCallback()

</Data></EventData></Event></Events>

導致theano問題的原因很多,例如多線程,使用GPU,內存限制等。但是根據代碼和錯誤信息,我不確定原因是什么。

我建議您可以嘗試參考theano文檔“ 調試Theano:常見問題和故障排除 ”和文章“ Azure Web App沙箱 ”,以找出由什么引起的真正問題。

您可以共享complexFunction的代碼嗎? 我認為這對於分析問題非常有幫助,甚至嘗試重現它以找出解決方案。


更新

根據您的代碼和事件日志,我認為有兩個可能的原因導致了此問題。

  1. 一些代碼邏輯錯誤導致內部服務器錯誤,但是如果代碼在本地運行良好,請忽略這種可能性。

  2. 您的Web應用程序的配置不正確,例如web.config ,請參閱web.config部分以檢查其配置是否正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM