PCAM: fix DataLoader pickling error by avoiding module on self
PCAM: lazy-import h5py via _get_h5py to keep dataset picklable
Fixes: #9195
Problem
PCAMpreviously stored theh5pymodule on the dataset instance, making it unpicklable.DataLoader(num_workers>0)/ spawn-based DDP pickle the dataset, causingTypeError: cannot pickle 'module' object.
Change
- Introduce
_get_h5py()helper that importsh5pyon demand with a clear error message. - Use
h5py = _get_h5py()in__len__and__getitem__(and any other call sites) instead ofself.h5py. - No import at
__init__; no module stored onself. Error text for missingh5pyremains unchanged.
Tests
-
test_update_PCAM.py- Adds
PCAMTestCase(inheritsImageDatasetTestCase). inject_fake_data(...)writes tiny HDF5 files for each split (train,val,test) with datasetsxandy.- Requires
h5py. Exercises core dataset behaviors on synthetic data (offline).
- Adds
-
test_update_PCAM_multiprocessing.py- Spawn-based DDP smoke test (
torch.multiprocessing.spawnwithworld_size=2, backendgloo). - Uses
DistributedSamplerandDataLoader(num_workers=2, persistent_workers=True). - Iterates a few batches and performs an
all_reducesanity check to ensure workers progress without pickling errors.
- Spawn-based DDP smoke test (
:link: Helpful Links
:test_tube: See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9200
- :page_facing_up: Preview Python docs built from this PR
Note: Links to docs will display an error until the docs builds have been completed.
:heavy_exclamation_mark: 1 Active SEVs
There are 1 currently active SEVs. If your PR is affected, please view them below:
This comment was automatically generated by Dr. CI and updates every 15 minutes.
Hi @nehdiii!
Thank you for your pull request and welcome to our community.
Action Required
In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
Hey @nehdiii , I think we can remove the test/test_update_pcam.py file as we are doing the same at https://github.com/pytorch/vision/blob/ce5b26a9284d119fe9fc63d0552abb35e0ba802a/test/test_datasets.py#L2858
Also, since we don’t currently have multiprocessing tests in the suite, the dedicated multiprocessing test file could be removed as well.
Other than that, this PR looks very crisp.