Flask+gunicorn deploys deep learning error gunicorn: error: argument -b: invalid int value '0.0.0.0:8000'

Flask+gunicorn deploys deep learning error gunicorn: error: argument -b: invalid int value '0.0.0.0:8000'

Project scenario:

Forward the client's request to the Flask program instance via the web server and call the Pytorch deep learning model.
In order to achieve concurrency, gunicorn is used to deploy the Flask service.

For specific calling codes, please refer to the following two blog posts.
The first blog post introduces Flask's implementation of web services and calling Pytorch deep learning model to realize handwritten digit recognition.
The second blog post introduces the use of gunicorn to achieve multiple concurrency in Web service calls.

Flask implements web services calling Python programs
Flask+gunicorn implements web services concurrently calling Python programs to solve multi-thread/multi-process issues


Problem Description:

After the model and framework are deployed, use the following command to start the service:

gunicorn -w 5 -b 0.0.0.0:8000 main:app

An error occurs:

gunicorn: error: argument -b: invalid int value '0.0.0.0:8000'

Cause Analysis:

The blogger tried line-by-line positioning and found that the problem mainly existed in the conflict between the environment parameters in the deep learning model code and gunicorn. Therefore, be sure to pay attention when using it:

An error will be reported if gunicorn and args command line parameters are shared! ! !


solution:

Taking Pytorch's implementation of the cifar-100 classification deep learning model as an example, the following is the code of the model test.py:

import argparse

from matplotlib import pyplot as plt

import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

from conf import settings
from utils import get_network, get_test_dataloader

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-net', type=str, required=True, help='net type')
    parser.add_argument('-weights', type=str, required=True, help='the weights file you want to test')
    parser.add_argument('-gpu', action='store_true', default=False, help='use gpu or not')
    parser.add_argument('-b', type=int, default=16, help='batch size for dataloader')
    args = parser.parse_args()

    net = get_network(args)

    cifar100_test_loader = get_test_dataloader(
        settings.CIFAR100_TRAIN_MEAN,
        settings.CIFAR100_TRAIN_STD,
        #settings.CIFAR100_PATH,
        num_workers=4,
        batch_size=args.b,
    )

    net.load_state_dict(torch.load(args.weights))
    print(net)
    net.eval()

    correct_1 = 0.0
    correct_5 = 0.0
    total = 0

    with torch.no_grad():
        for n_iter, (image, label) in enumerate(cifar100_test_loader):
            print("iteration: {}\ttotal {} iterations".format(n_iter + 1, len(cifar100_test_loader)))

            if args.gpu:
                image = image.cuda()
                label = label.cuda()
                print('GPU INFO.....')
                print(torch.cuda.memory_summary(), end='')


            output = net(image)
            _, pred = output.topk(5, 1, largest=True, sorted=True)

            label = label.view(label.size(0), -1).expand_as(pred)
            correct = pred.eq(label).float()

            #compute top 5
            correct_5 += correct[:, :5].sum()

            #compute top1
            correct_1 += correct[:, :1].sum()

    if args.gpu:
        print('GPU INFO.....')
        print(torch.cuda.memory_summary(), end='')

    print()
    print("Top 1 err: ", 1 - correct_1 / len(cifar100_test_loader.dataset))
    print("Top 5 err: ", 1 - correct_5 / len(cifar100_test_loader.dataset))
    print("Parameter numbers: {}".format(sum(p.numel() for p in net.parameters())))

Since gunicorn and args command line parameters are shared, an error will be reported, so we need to modify line 19 as follows:

args = parser.parse_args() 改为 args = parser.parse_args(args=[])

This pit is really hidden, I hope you can avoid it~

Guess you like

Origin blog.csdn.net/qq_39691492/article/details/122581895