Pytorch 可执行文件在 Anaconda 提示符 运行 时有效,但在 Cmd 或 .exe 中无效?

Pytorch executeable works while running from Anaconda prompt but not from Cmd or .exe?

我打包(使用 Pyinstaller)Minimalistic Yolo github 存储库的一个小变体,发现 Here,打包是使用 pyinstaller 完成的 运行 作为服务器的对象检测使用烧瓶。

因此,在尝试 运行 服务器时,它仅在从 Anaconda Prompt(这是我编写 pyinstaller 命令的地方)运行ning 时才有效,除此之外,还会发生以下错误。

我从 (exe,Cmd,PowerShell) 中 运行ning 得到的错误是:

Traceback (most recent call last):
File "flask\app.py", line 2446, in wsgi_app
File "flask\app.py", line 1951, in full_dispatch_request
File "flask\app.py", line 1820, in handle_user_exception
File "flask\_compat.py", line 39, in reraise
File "flask\app.py", line 1949, in full_dispatch_request
File "flask\app.py", line 1935, in dispatch_request
File "FlaskServerV2.py", line 53, in Hello
File "torch\nn\modules\module.py", line 532, in __call__
File "models.py", line 259, in forward
File "torch\nn\modules\module.py", line 532, in __call__
File "models.py", line 177, in forward
RuntimeError: error in LoadLibraryA
127.0.0.1 - - [19/Nov/2020 10:28:53] "GET /detect HTTP/1.1" 500 -

但是当在 conda 中 运行ning 时,代码工作正常并且 运行s。 所以我怀疑这是 PyTorch 依赖项的问题。

当前代码:

from __future__ import division

from flask import Flask, Response, jsonify
app = Flask(__name__)

from models import *
from utils.utils import *
from utils.datasets import *

import os
import sys
import time
import datetime
import argparse

from PIL import Image

import torch
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.ticker import NullLocator

import cv2 
import time 
import json


@app.route('/CheckIfRunning')
def CheckIfRunning():
    return '1'

@app.route('/detect')
def Hello():
    global device
    global model
    global classes
    global colors
    global Tensor
    global a
    img=cv2.imread("temp.jpg")
    PILimg = np.array(Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)))
    imgTensor = transforms.ToTensor()(PILimg)
    imgTensor, _ = pad_to_square(imgTensor, 0)
    imgTensor = resize(imgTensor, 416)
    #add the batch size
    imgTensor = imgTensor.unsqueeze(0)
    imgTensor = Variable(imgTensor.type(Tensor))
    with torch.no_grad():
        detections = model(imgTensor)
        detections = non_max_suppression(detections,0.8, 0.4)
    a.clear()

    Return={}
    ReturnCounter=0
    if detections is not None:
            a.extend(detections)
            b=len(a)
            if len(a)  :
                for detections in a:
                    if detections is not None:
                        detections = rescale_boxes(detections, 416, PILimg.shape[:2])
                        unique_labels = detections[:, -1].cpu().unique()
                        n_cls_preds = len(unique_labels)
                        for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
                            box_w = x2 - x1
                            box_h = y2 - y1
                            color = [int(c) for c in colors[int(cls_pred)]]
                            img = cv2.rectangle(img, (x1, y1 + box_h), (x2, y1), color, 2)
                            cv2.putText(img, classes[int(cls_pred)], (x1, y1),     cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
                            cv2.putText(img, str("%.2f" % float(conf)), (x2, y2 - box_h), cv2.FONT_HERSHEY_SIMPLEX, 0.5,color, 2)
                            Return[ReturnCounter]=    [x1.item(),y1.item(),x2.item(),y2.item(),conf.item(),cls_conf.item(),classes[int(cls_pred)]]
                            ReturnCounter+=1
                        cv2.imwrite("Temp2.jpg",img)
                        return Return
                

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Set up model
model = Darknet("config/yolov3.cfg", img_size=416).to(device)

model.load_darknet_weights("weights/yolov3.weights")

model.eval()  # Set in evaluation mode

classes = load_classes("data/coco.names")  # Extracts class labels from file
colors = np.random.randint(0, 255, size=(len(classes), 3), dtype="uint8")
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor

a=[]
app.run(threaded=True) 

好的,原来这是 pyinstaller 的问题。

如果 Pytorch 是使用 Conda 安装的,它需要 CUDANN ,并且无法使用它(即没有那个环境)

如果你想让它在任何地方都能工作,必须使用 pip 安装 Pytorch。

作为参考, https://github.com/pyinstaller/pyinstaller/issues/2666#issuecomment-508013383