When I try to run this code:
learn = ConvLearner.pretrained(f_model, md, metrics=[accuracy])
I get an error because the kernel on Kaggle cannot download the weights from the internet
Downloading: "https://download.pytorch.org/models/resnet34-333f7ec4.pth" to /tmp/.torch/models/resnet34-333f7ec4.pth
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
/opt/conda/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1317 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
/opt/conda/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
/opt/conda/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1284 body = _encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
/opt/conda/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
/opt/conda/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
1025 del self._buffer[:]
-> 1026 self.send(msg)
1027
/opt/conda/lib/python3.6/http/client.py in send(self, data)
963 if self.auto_open:
--> 964 self.connect()
965 else:
/opt/conda/lib/python3.6/http/client.py in connect(self)
1391
-> 1392 super().connect()
1393
/opt/conda/lib/python3.6/http/client.py in connect(self)
935 self.sock = self._create_connection(
--> 936 (self.host,self.port), self.timeout, self.source_address)
937 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
/opt/conda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
703 err = None
--> 704 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
705 af, socktype, proto, canonname, sa = res
/opt/conda/lib/python3.6/socket.py in getaddrinfo(host, port, family, type, proto, flags)
744 addrlist = []
--> 745 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
746 af, socktype, proto, canonname, sa = res
gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
<ipython-input-68-b0bc3f5d394a> in <module>()
----> 1 learn = ConvLearner.pretrained(f_model, md, metrics=[accuracy])
2 learn.opt_fn = optim.Adam
/opt/conda/lib/python3.6/site-packages/fastai-0.6-py3.6.egg/fastai/conv_learner.py in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, custom_head, precompute, **kwargs)
98 def pretrained(cls, f, data, ps=None, xtra_fc=None, xtra_cut=0, custom_head=None, precompute=False, **kwargs):
99 models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg,
--> 100 ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut, custom_head=custom_head)
101 return cls(data, models, precompute, **kwargs)
102
/opt/conda/lib/python3.6/site-packages/fastai-0.6-py3.6.egg/fastai/conv_learner.py in __init__(self, f, c, is_multi, is_reg, ps, xtra_fc, xtra_cut, custom_head)
36 else: cut,self.lr_cut = 0,0
37 cut-=xtra_cut
---> 38 layers = cut_model(f(True), cut)
39 self.nf = model_features[f] if f in model_features else (num_features(layers)*2)
40 if not custom_head: layers += [AdaptiveConcatPool2d(), Flatten()]
/opt/conda/lib/python3.6/site-packages/torchvision-0.2.0-py3.6.egg/torchvision/models/resnet.py in resnet34(pretrained, **kwargs)
174 model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
175 if pretrained:
--> 176 model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
177 return model
178
/opt/conda/lib/python3.6/site-packages/torch/utils/model_zoo.py in load_url(url, model_dir, map_location, progress)
63 sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file))
64 hash_prefix = HASH_REGEX.search(filename).group(1)
---> 65 _download_url_to_file(url, cached_file, hash_prefix, progress=progress)
66 return torch.load(cached_file, map_location=map_location)
67
/opt/conda/lib/python3.6/site-packages/torch/utils/model_zoo.py in _download_url_to_file(url, dst, hash_prefix, progress)
68
69 def _download_url_to_file(url, dst, hash_prefix, progress):
---> 70 u = urlopen(url)
71 if requests_available:
72 file_size = int(u.headers["Content-Length"])
/opt/conda/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 else:
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
225 def install_opener(opener):
/opt/conda/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
524 req = meth(req)
525
--> 526 response = self._open(req, data)
527
528 # post-process response
/opt/conda/lib/python3.6/urllib/request.py in _open(self, req, data)
542 protocol = req.type
543 result = self._call_chain(self.handle_open, protocol, protocol +
--> 544 '_open', req)
545 if result:
546 return result
/opt/conda/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
502 for handler in handlers:
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
506 return result
/opt/conda/lib/python3.6/urllib/request.py in https_open(self, req)
1359 def https_open(self, req):
1360 return self.do_open(http.client.HTTPSConnection, req,
-> 1361 context=self._context, check_hostname=self._check_hostname)
1362
1363 https_request = AbstractHTTPHandler.do_request_
/opt/conda/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
-> 1320 raise URLError(err)
1321 r = h.getresponse()
1322 except:
URLError: <urlopen error [Errno -2] Name or service not known>
The weights are available here, but I’m not sure how to load them without having fastai try to download the weights from the internet.
Any ideas?