【问题标题】:Tensorflow, Keras pretrained MobileNetV2 Model doesn't downloadTensorflow,Keras 预训练的 MobileNetV2 模型不下载
【发布时间】:2019-08-13 09:57:01
【问题描述】:

我关注了关于 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)

【问题讨论】:

    标签: python python-3.x tensorflow runtime-error tf.keras


    【解决方案1】:

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

    pip install --upgrade requests
    

    或者在你的情况下:

    conda update requests
    

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

    【讨论】:

    • 对不起,这不起作用,输出在主要问题中更新
    • 我是否加载删除下载代码?然后如何加载文件?
    • @PyStraw45 我不完全理解你的问题,对不起。从here 下载.h5 文件,然后将其存储在~/keras/models/ 下。
    • 我发现错误是由我使用的网络引起的。当我回到家并运行相同的代码时,它可以工作,显然网络上有一些严重的过滤和阻塞。
    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多