python - 设置代理让keras/tensorflow下载需要的json

标签 python tensorflow keras

我正在尝试这个 article .

似乎在进行预测时,keras 下载失败json因为公司代理在line :

results = imagenet_utils.decode_predictions(preds)

错误如下:

Using TensorFlow backend.
* Loading Keras model and Flask starting server...please wait until server has fully started
2018-11-21 17:47:27.664222: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
 * Serving Flask app "run_keras_server" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
[2018-11-21 19:04:25,054] ERROR in app: Exception on /predict [POST]
Traceback (most recent call last):
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 964, in send
    self.connect()
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 1392, in connect
    super().connect()
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\socket.py", line 724, in create_connection
    raise err
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\keras\utils\data_utils.py", line 222, in get_file
    urlretrieve(origin, fpath, dl_progress)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\Mahesh\workspaces\ignioworkspace6\keras_flask\run_keras_server.py", line 64, in predict
    results = imagenet_utils.decode_predictions(preds)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\keras\applications\__init__.py", line 28, in wrapper
    return base_fun(*args, **kwargs)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\keras\applications\imagenet_utils.py", line 14, in decode_predictions
    *args, **kwargs)
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\keras_applications\imagenet_utils.py", line 228, in decode_predictions
    file_hash='c2c37ea517e94d9795004a39431a14cb')
  File "D:\user123\Programs\python\python-3.6.6-amd64\lib\site-packages\keras\utils\data_utils.py", line 226, in get_file
    raise Exception(error_msg.format(origin, e.errno, e.reason))
Exception: URL fetch failure on https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json: None -- [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
127.0.0.1 - - [21/Nov/2018 19:04:25] "[1m[35mPOST /predict HTTP/1.1[0m" 500 -

如何为keras设置代理让它下载想要的文件?无论如何我可以手动下载这个文件并让keras从文件系统位置使用它。下载 resnet50_weights_tf_dim_ordering_tf_kernels.h5 时出现类似错误。所以,我手动下载它并在 ResNet50() 中指定它。

最佳答案

在 Windows 上,Python 解释器将从您的系统环境变量中获取代理:

设置系统变量HTTP_PROXY 到值 http://yourproxy

和系统变量 HTTPS_PROXY 到值 http://yourproxy

或者,您可以根据这篇文章的答案在特定的 Python 终端 session 中设置环境 set proxy in python session

关于python - 设置代理让keras/tensorflow下载需要的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53413756/

相关文章:

python - 如何在 Python collections.Counter 中执行 most_common 时忽略大小写?

python - 在 Python-3.6 中使用网络摄像头作为 QR 码扫描仪

python - 使用数据读取器在 tensorflow 中读取 tiff 文件的正确方法

python - 类型错误:在 EarlyStopping keras 中使用 restore_best_weights=True 时,类型 'NoneType' 的对象没有 len()

python - 如何修复 'ImportError: No module named openpyxl' ?

python - 为什么我的模型同时具有较低的 MAE 和较低的 R2 分数?

python - TensorFlow 图中的条件评估

python - 从张量中随机移除

python - "ImportError: DLL load failed: The specified procedure could not be found"- 而使用 Keras 在 Python 中使用 CNN 进行数字识别

python - 当为具有多个输出的模型尝试 train_on_batch 时,Keras 中的 sample_weight 出现问题