简体   繁体   English

url Tensorflow Serving Python Request 超过了最大重试次数

[英]Max retries exceeded with url Tensorflow Serving Python Request

I've trained my own model and I've runned docker container with this code:我训练了自己的模型,并使用以下代码运行了 docker 容器:

docker run -t --rm -d -p 8501:8501 -v "%cd%/serving/tensorflow_serving/servables/tensorflow/testdata/optik_gpu:/models/optik_gpu" -e MODEL_NAME=optik_gpu tensorflow/serving

I'm using Windows 10 as OS.我使用 Windows 10 作为操作系统。

And I'm trying to post base64 string to the container to get a prediction.我正在尝试将 base64 字符串发布到容器中以获得预测。 Here is my code as suggested on Google Cloud :这是我在Google Cloud上建议的代码:

import base64 import io import json import requests导入 base64 导入 io 导入 json 导入请求

def container_predict(image_file_path, image_key, port_number=8501):

    with io.open(image_file_path, 'rb') as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode('utf-8')

    instances = {
            'instances': [
                    {'image_bytes': {'b64': str(encoded_image)},
                     'key': image_key}
            ]
    }

    url = 'http://localhost:{}/v1/models/default:predict'.format(port_number)

    response = requests.post(url, data=json.dumps(instances))
    print(response.json())

container_predict("./test.jpg", 1)

When I run the script it gives this error:当我运行脚本时,它给出了这个错误:

Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 184, in connect
    conn = self._new_conn()
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001A75A6B7668>: Failed to establish a new connection: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout

(optik3) E:\Programming\Tensorflow\optik>python try.py
Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request    
    conn.request(method, url, **httplib_request_kw)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 184, in connect
    conn = self._new_conn()
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001BDEB7466D8>: Failed to establish a new connection: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\urllib3\util\retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /v1/models/default:predict (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001BDEB7466D8>: Failed to establish a new connection: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "try.py", line 39, in <module>
    container_predict("./test.jpg", 1, 4444)
  File "try.py", line 36, in container_predict
    response = requests.post(url, data=json.dumps(instances))
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\ProgramData\Anaconda3\envs\optik3\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /v1/models/default:predict (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001BDEB7466D8>: Failed to establish a new connection: [WinError 10061] Hedef makine etkin olarak reddettiğinden bağlantı kurulamadı'))

I don't know what is the problem.我不知道是什么问题。

I will preface this by saying I am new to all of this and am not sure how everything works.我将首先说我对所有这些都不熟悉,并且不确定一切如何运作。

Just had a similar problem and had a look to see what ports were open.刚刚遇到了类似的问题,并查看了哪些端口是开放的。 Turns out that my docker container was on 8502 (don't know why as I thought I had specified it be on 8501 like in the tutorial here https://cloud.google.com/vision/automl/docs/containers-gcs-tutorial?hl=en_GB ).原来我的 docker 容器在 8502 上(不知道为什么,因为我认为我已经指定它在 8501 上,就像这里的教程中的https://cloud.google.com/vision/automl/docs/containers-gcs-教程?hl=en_GB )。 So once I changed the port to 8502 in the python code it started working.因此,一旦我在 python 代码中将端口更改为 8502,它就开始工作了。

Not sure if it will help you, but my code is now working... now I just need to figure out what is going on and why it happened!不确定它是否会帮助你,但我的代码现在正在运行......现在我只需要弄清楚发生了什么以及为什么会发生!

The problem may be caused by the fact that you specified model name "optic_gpu" when launching docker container, but use "default" as model name when trying to do request.问题可能是由于您在启动 docker 容器时指定了模型名称“optic_gpu”,但在尝试执行请求时使用“default”作为模型名称。 At least in that Google Cloud lesson they don't specify model name, and because of this use "default".至少在 Google Cloud 课程中,他们没有指定模型名称,因此使用“默认”。 So you may try to change link like this url = 'http://localhost:{}/v1/models/optic_gpu:predict'.format(port_number) and check if it helps.因此,您可以尝试更改这样的url = 'http://localhost:{}/v1/models/optic_gpu:predict'.format(port_number)并检查它是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM