网址Tensorflow服务Python请求超过了最大重试次数

时间:2020-03-30 22:00:49

标签: python docker tensorflow object-detection tensorflow-serving

我已经训练了自己的模型,并使用以下代码运行了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

我正在使用Windows 10作为操作系统。

我正在尝试将base64字符串发布到容器中以获得预测。这是我在Google Cloud上建议的代码:

导入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)

当我运行脚本时,会出现此错误:

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ı'))

我不知道是什么问题。

2 个答案:

答案 0 :(得分:0)

在开始我之前,我会说我是这一切的新手,并且不确定一切如何运作。

也有类似的问题,并且看看是否打开了哪些端口。原来我的Docker容器在8502上(不知道为什么我以为我已将其指定在8501上,就像这里的教程https://cloud.google.com/vision/automl/docs/containers-gcs-tutorial?hl=en_GB一样)。因此,一旦我在python代码中将端口更改为8502,它便开始工作。

不确定是否会对您有所帮助,但是我的代码现在可以正常工作了...现在,我只需要弄清楚发生了什么以及发生的原因!

答案 1 :(得分:0)

问题可能是由于在启动Docker容器时指定了模型名称“ optic_gpu”,而在尝试执行请求时使用“ default”作为模型名称引起的。至少在该Google Cloud课程中,他们没有指定型号名称,因此使用“默认”名称。 因此,您可以尝试像这样url = 'http://localhost:{}/v1/models/optic_gpu:predict'.format(port_number)更改链接并检查是否有帮助。