Internal Server Error on Azure Flask Web App

感情迁移 提交于 2019-12-25 06:51:49

问题


I created a Flask web app hosted on Azure with a Dreamspark subscription. It uses libraries such as Numpy, Scipy and Theano.

When I deploy the app using a simple Theano calculations, it works perfectly. However, when I change the code to a more complex Theano calculation, I get an Internal Server Error.

Here is a sample code. When I call simpleFunction (something like a Theano function for a sum) it works but when calling complexFunction (something like an image classification calculation) then it creates an Internal Server Error:

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)

For reasons of clarity, here is the code for the file theanoCode.py (it is a Convolutional Neural Network Classifier, the load module is used to read mnist dataset from files):

# 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)

Here is the Detailed Error Message:

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)

And here is the event log:

</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>

回答1:


There are many reasons for causing the issue with theano, such as multithreads, using GPUs, memory limits, etc. But according to the code and the error information, I can't be sure what the reason is.

I suggest that you can try to refer to the theano document "Debugging Theano: FAQ and Troubleshooting" and the article "Azure Web App sandbox" to find out the real issue caused by what.

Could you share the code of the complexFunction? I think it's very helpful for analyzing the issue, even try to reproduce it for figuring out the solution.


Update:

According to your code & event log, I think there are two possible reasons which caused the issue.

  1. Some code logic error caused the internal server error, but if the code works good at the local, please ignore this possibility.

  2. Incorrect configuration for your webapp like web.config, please refer to the section web.config to check whether it's configured correctly.



来源:https://stackoverflow.com/questions/37647368/internal-server-error-on-azure-flask-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!