Tensorflow、Keras 预训练的 MobileNetV2 模型无法下载

Tensorflow, Keras pretrained MobileNetV2 Model doesn't download

我正在学习有关在 tensorflow keras 中进行迁移学习的教程。下载mobilenetV2模型时出错

这是失败的代码:

base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                             include_top=False,
                                             weights='imagenet')

抛出此错误:

Downloading data from https://github.com/JonathanCMitchell/mobilenet_v2_keras/releases/download/v1.1/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5
Traceback (most recent call last):
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 1384, in connect
    super().connect()
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/http/client.py", line 928, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

在处理上述异常的过程中,又发生了异常:

Traceback (most recent call last):
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages/tensorflow/python/keras/utils/data_utils.py", line 248, in get_file
    urlretrieve(origin, fpath, dl_progress)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>

在处理上述异常的过程中,又发生了异常:

Traceback (most recent call last):
  File "/Users/user_name/PycharmProjects/ObjDectCount/modelEval.py", line 62, in <module>
    base_model = tf.keras.applications.MobileNetV2()
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages/tensorflow/python/keras/applications/__init__.py", line 70, in wrapper
    return base_fun(*args, **kwargs)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages/tensorflow/python/keras/applications/mobilenet_v2.py", line 32, in MobileNetV2
    return mobilenet_v2.MobileNetV2(*args, **kwargs)
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages/keras_applications/mobilenet_v2.py", line 411, in MobileNetV2
    model_name, weight_path, cache_subdir='models')
  File "/Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages/tensorflow/python/keras/utils/data_utils.py", line 250, in get_file
    raise Exception(error_msg.format(origin, e.errno, e.reason))
Exception: URL fetch failure on https://github.com/JonathanCMitchell/mobilenet_v2_keras/releases/download/v1.1/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5: None -- [Errno 8] nodename nor servname provided, or not known

编辑: 运行pip install --upgrade requests

后的输出
Requirement already up-to-date: 
requests in /Users/user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages (2.22.0)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /Users/ user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages (from requests) (2019.6.16)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /Users/ user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages (from requests) (3.0.4)
Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/ user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages (from requests) (1.25.3)
Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /Users/ user_name/.conda/envs/ObjDectCount/lib/python3.7/site-packages (from requests) (2.8)

我曾经遇到过类似的错误并通过升级 requests 包修复了它:

pip install --upgrade requests

或者您的情况:

conda update requests

编辑: 一个快速但肮脏的解决方法是手动下载权重并将权重文件存储在 ~/.keras/models/