alan-turing-institute / plankton-cefas-scivision

Example data for applying a pretrained ResNet-50 model to classification of plankton and running this model with the scivision tool
Apache License 2.0
1 stars 4 forks source link

Weight download taking too much time - no error shown on screen. #4

Closed miquelmassot closed 2 years ago

miquelmassot commented 2 years ago

When trying to run the notebook https://github.com/scivision-gallery/plankton-classification locally, it got stuck on this line for >5h. I ended up killing the process. https://github.com/alan-turing-institute/plankton-cefas-scivision/blob/main/resnet50_cefas/model.py#L22

Full traceback below

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Input In [6], in <cell line: 3>()
      1 models = default_catalog.models.to_dataframe()
      2 targetmodel = models[models.name == model_name].url.item()
----> 3 model = load_pretrained_model(targetmodel, allow_install=True)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/scivision/koala.py:57, in koala.<locals>.wrapped_fn(*args, **kwargs)
     55 # now call the function, but log any exceptions
     56 try:
---> 57     output = fn(*args, **kwargs)
     58 except Exception as e:
     59     logger.error(e)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/scivision/io/reader.py:175, in load_pretrained_model(path, branch, allow_install, model, load_multiple, *args, **kwargs)
    172     # try to install the package if necessary
    173     install_package(config, allow_install=allow_install)
--> 175     loaded_models.append(PretrainedModel(config))
    176 if load_multiple:
    177     return loaded_models

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/scivision/io/wrapper.py:14, in PretrainedModel.__init__(self, config)
     12 def __init__(self, config: dict):
     13     self._config = config
---> 14     self._plumbing = AutoPlumber(config)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/scivision/io/autoplumber.py:38, in AutoPlumber.__init__(self, config)
     34 model = getattr(self._module, config["model"])
     36 # we could instantiate the model using the args here
     37 # args = config["args"]
---> 38 self._model = model()
     40 self._fn = getattr(self._model, config["prediction_fn"]["call"])
     42 # get the call signature

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/resnet50_cefas/model.py:22, in resnet50.__init__(self, model_weights, label_level)
     18     model_weights = dict(url="doi:10.5281/zenodo.6143685/cop-non-detritus-20211215.pth",
     19                      known_hash="md5:46fd1665c8b966e472152eb695d22ae3")
     21 # ---- DOWNLOAD
---> 22 self.model_weights = pooch.retrieve(url=model_weights['url'], known_hash=model_weights['known_hash'])
     24 # ---- LABEL LEVEL
     25 self.labels_map = PlanktonLabels(experiment=label_level).labels()

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/pooch/core.py:240, in retrieve(url, known_hash, fname, path, processor, downloader, progressbar)
    237 if downloader is None:
    238     downloader = choose_downloader(url, progressbar=progressbar)
--> 240 stream_download(url, full_path, known_hash, downloader, pooch=None)
    242 if known_hash is None:
    243     get_logger().info(
    244         "SHA256 hash of downloaded file: %s\n"
    245         "Use this value as the 'known_hash' argument of 'pooch.retrieve'"
   (...)
    248         file_hash(str(full_path)),
    249     )

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/pooch/core.py:772, in stream_download(url, fname, known_hash, downloader, pooch, retry_if_failed)
    768 try:
    769     # Stream the file to a temporary so that we can safely check its
    770     # hash before overwriting the original.
    771     with temporary_file(path=str(fname.parent)) as tmp:
--> 772         downloader(url, tmp, pooch)
    773         hash_matches(tmp, known_hash, strict=True, source=str(fname.name))
    774         shutil.move(tmp, str(fname))

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/pooch/downloaders.py:581, in DOIDownloader.__call__(self, url, output_file, pooch)
    573 download_url = converters[repository](
    574     archive_url=archive_url,
    575     file_name=parsed_url["path"].split("/")[-1],
    576     doi=doi,
    577 )
    578 downloader = HTTPDownloader(
    579     progressbar=self.progressbar, chunk_size=self.chunk_size, **self.kwargs
    580 )
--> 581 downloader(download_url, output_file, pooch)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/pooch/downloaders.py:209, in HTTPDownloader.__call__(self, url, output_file, pooch)
    207     progress = self.progressbar
    208     progress.total = total
--> 209 for chunk in content:
    210     if chunk:
    211         output_file.write(chunk)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/requests/models.py:760, in Response.iter_content.<locals>.generate()
    758 if hasattr(self.raw, 'stream'):
    759     try:
--> 760         for chunk in self.raw.stream(chunk_size, decode_content=True):
    761             yield chunk
    762     except ProtocolError as e:

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/urllib3/response.py:576, in HTTPResponse.stream(self, amt, decode_content)
    574 else:
    575     while not is_fp_closed(self._fp):
--> 576         data = self.read(amt=amt, decode_content=decode_content)
    578         if data:
    579             yield data

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/site-packages/urllib3/response.py:519, in HTTPResponse.read(self, amt, decode_content, cache_content)
    517 else:
    518     cache_content = False
--> 519     data = self._fp.read(amt) if not fp_closed else b""
    520     if (
    521         amt != 0 and not data
    522     ):  # Platform-specific: Buggy versions of Python.
   (...)
    528         # not properly close the connection in all cases. There is
    529         # no harm in redundantly calling close.
    530         self._fp.close()

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/http/client.py:463, in HTTPResponse.read(self, amt)
    460 if amt is not None:
    461     # Amount is given, implement using readinto
    462     b = bytearray(amt)
--> 463     n = self.readinto(b)
    464     return memoryview(b)[:n].tobytes()
    465 else:
    466     # Amount is not given (unbounded read) so we must check self.length
    467     # and self.chunked

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/http/client.py:507, in HTTPResponse.readinto(self, b)
    502         b = memoryview(b)[0:self.length]
    504 # we do not use _safe_read() here because this may be a .will_close
    505 # connection, and the user is reading more bytes than will be provided
    506 # (for example, reading in 1k chunks)
--> 507 n = self.fp.readinto(b)
    508 if not n and b:
    509     # Ideally, we would raise IncompleteRead if the content-length
    510     # wasn't satisfied, but it might break compatibility.
    511     self._close_conn()

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/socket.py:704, in SocketIO.readinto(self, b)
    702 while True:
    703     try:
--> 704         return self._sock.recv_into(b)
    705     except timeout:
    706         self._timeout_occurred = True

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/ssl.py:1241, in SSLSocket.recv_into(self, buffer, nbytes, flags)
   1237     if flags != 0:
   1238         raise ValueError(
   1239           "non-zero flags not allowed in calls to recv_into() on %s" %
   1240           self.__class__)
-> 1241     return self.read(nbytes, buffer)
   1242 else:
   1243     return super().recv_into(buffer, nbytes, flags)

File ~/anaconda3/envs/plankton-classification-scivision/lib/python3.9/ssl.py:1099, in SSLSocket.read(self, len, buffer)
   1097 try:
   1098     if buffer is not None:
-> 1099         return self._sslobj.read(len, buffer)
   1100     else:
   1101         return self._sslobj.read(len)

KeyboardInterrupt: 
acocac commented 2 years ago

@miquelmassot thanks for reporting the issue. fyi, I'm using the pooch library to retrieve the model weights file from a Zenodo repository. Pooch is a very handy library to download data in cache.

Not sure what's wrong. May I ask your OS? Also can you try to run the following lines in your local environment? I would suggest to create a virtual environmental and install pooch via pip.

model_weights = dict(url="doi:10.5281/zenodo.6143685/cop-non-detritus-20211215.pth", known_hash="md5:46fd1665c8b966e472152eb695d22ae3")
model = pooch.retrieve(url=model_weights['url'], known_hash=model_weights['known_hash'])

It should return a message like Downloading data from 'doi:10.5281/zenodo.6143685/cop-non-detritus-20211215.pth' to file

ps. I double checked the Binder of the Plankton use case, and I don't have any error.

miquelmassot commented 2 years ago

Issue fixed itself, probably issues with Zenodo file serving.