成功解决:http.client.RemoteDisconnected: Remote end closed connection without response

问题描述

运行程序超时,有时可以正常运行,有时候显示如下错误:

http.client.RemoteDisconnected: Remote end closed connection without response

(virtual) lihuanyu@ubuntu-NF5468M5:~/code/03AdaBins$ python infer.py 
Loading base model ()...Traceback (most recent call last):
  File "infer.py", line 175, in <module>
    inferHelper = InferenceHelper()
  File "infer.py", line 75, in __init__
    model = UnetAdaptiveBins.build(n_bins=256, min_val=self.min_depth, max_val=self.max_depth)
  File "/home/lihuanyu/code/03AdaBins/models/unet_adaptive_bins.py", line 125, in build
    basemodel = torch.hub.load('rwightman/gen-efficientnet-pytorch', basemodel_name, pretrained=True)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/site-packages/torch/hub.py", line 397, in load
    repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, verbose, skip_validation)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/site-packages/torch/hub.py", line 165, in _get_cache_or_reload
    repo_owner, repo_name, branch = _parse_repo_info(github)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/site-packages/torch/hub.py", line 119, in _parse_repo_info
    with urlopen(f"https://github.com/{repo_owner}/{repo_name}/tree/main/"):
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 1392, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/urllib/request.py", line 1352, in do_open
    r = h.getresponse()
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/http/client.py", line 1379, in getresponse
    response.begin()
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/http/client.py", line 311, in begin
    version, status, reason = self._read_status()
  File "/home/lihuanyu/.conda/envs/virtual/lib/python3.6/http/client.py", line 280, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

问题定位

basemodel_name = 'tf_efficientnet_b5_ap'  
  
print('Loading base model ()...'.format(basemodel_name), end='')  
basemodel = torch.hub.load('rwightman/gen-efficientnet-pytorch', basemodel_name, pretrained=True)

问题分析:加载模型可能是由于网络问题,导致与训练权重难以下载,从而失败。

问题解决

torch.hub.load('rwightman/gen-efficientnet-pytorch', basemodel_name, pretrained=True)

通过远程访问github进行权重和网络模型的加载(有时候是可以成功的):

basemodel = torch.hub.load('rwightman/gen-efficientnet-pytorch', 'tf_efficientnet_b5_ap', pretrained=True)

将加载的权重和模型离线另存:

torch.save(basemodel,"/home/lihuanyu/code/03AdaBins/weight/tf_efficientnet_b5_ap.pth")

最后再加载保存的离线模型和权重即可:

basemodel_name = 'tf_efficientnet_b5_ap'  
  
print('Loading base model ()...'.format(basemodel_name), end='')  
#basemodel = torch.hub.load('rwightman/gen-efficientnet-pytorch', basemodel_name, pretrained=True)  
### 2023年4月7号 修改
basemodel = torch.load("/home/lihuanyu/code/03AdaBins/weight/tf_efficientnet_b5_ap.pth") #2023年  
basemodel.eval()

更多内容请关注: [安静到无声的博客]