question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot import name 'RandomBatchGeoSampler' from 'torchgeo.samplers.batch'

See original GitHub issue

On torchgeo==0.1.0, the import from torchgeo.samplers import GeoSampler fails with an ImportError. Interestingly, re-running the same import immediately after succeeds.

>>> from torchgeo.samplers import GeoSampler
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/shared/ritwik/miniconda3/envs/overwatch/lib/python3.7/site-packages/torchgeo/samplers/__init__.py", line 6, in <module>
    from .batch import BatchGeoSampler, RandomBatchGeoSampler
  File "/shared/ritwik/miniconda3/envs/overwatch/lib/python3.7/site-packages/torchgeo/samplers/batch.py", line 12, in <module>
    from torchgeo.datasets.geo import GeoDataset
  File "/shared/ritwik/miniconda3/envs/overwatch/lib/python3.7/site-packages/torchgeo/datasets/__init__.py", line 11, in <module>
    from .chesapeake import (
  File "/shared/ritwik/miniconda3/envs/overwatch/lib/python3.7/site-packages/torchgeo/datasets/chesapeake.py", line 26, in <module>
    from ..samplers.batch import RandomBatchGeoSampler
ImportError: cannot import name 'RandomBatchGeoSampler' from 'torchgeo.samplers.batch' (/shared/ritwik/miniconda3/envs/overwatch/lib/python3.7/site-packages/torchgeo/samplers/batch.py)
>>> GeoSampler
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'GeoSampler' is not defined
>>> from torchgeo.samplers import GeoSampler
>>> GeoSampler
<class 'torchgeo.samplers.GeoSampler'>
>>> 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
ashnair1commented, Dec 11, 2021

This is due to a circular dependency. Importing GeoSampler requires importing GeoDataset which in turn imports all datasets (including chesapeake.py which required RadomBatchGeoSampler). This was resolved post 0.1.0 by b67c711 which imports the datasets in torchgeo’s __init__.py to ensure it’s imported first.

In your case, I guess it worked the second time because by then the datasets submodule was already imported by the previous failed call.

0reactions
adamjstewartcommented, Dec 20, 2021

The original issue reported in this issue (circular import errors) has been fixed in the new 0.1.1 release, so I’ll close this PR.

I do still want to think about ways to speed up imports, but that can happen later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

torchgeo.samplers - Read the Docs
This data loader will return 256x256 px images, and has a batch size of 128 and an epoch length of 10,000. Random Batch...
Read more >
"ImportError: cannot import name 'BatchSampler'" - Part 1 (2018)
The error I'm getting is “ImportError: cannot import name 'BatchSampler'” – I have searched on google/on the forums, but can't find anybody ...
Read more >
torchgeo - PyPI
TorchGeo is a PyTorch domain library, similar to torchvision, providing datasets, samplers, transforms, and pre-trained models specific to geospatial data.
Read more >
TorchGeo of PyTorch - E2E Networks
The torchgeo package consists of popular datasets, ... Data samplers appropriate for geospatial data. ... Random Batch Geo Sampler.
Read more >
TorchGeo of PyTorch - LinkedIn
The torchgeo package consists of popular datasets, ... Data samplers appropriate for geospatial data. ... Random Batch Geo Sampler.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found