facebookresearch / fastMRI

A large-scale dataset of both raw MRI measurements and clinical MRI images.
https://fastmri.org
MIT License
1.3k stars 372 forks source link

TypeError: unsupported operand type(s) for /: 'str' and 'str' #128

Closed gabrielziegler3 closed 3 years ago

gabrielziegler3 commented 3 years ago

I am trying to replicate the UNet training and I keep getting the following error:

TypeErrorTraceback (most recent call last)
<ipython-input-10-e03ca5cc7d0f> in <module>
      1 trainer = pl.Trainer()
----> 2 trainer.fit(model, datamodule=data_module)

/opt/conda/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders, datamodule)
    463 
    464         # hook
--> 465         self.data_connector.prepare_data(model)
    466         self.callback_connector._attach_model_callbacks(model, self)
    467 

/opt/conda/lib/python3.8/site-packages/pytorch_lightning/trainer/connectors/data_connector.py in prepare_data(self, model)
     58         if self.can_prepare_data():
     59             if self.trainer.datamodule is not None:
---> 60                 self.trainer.datamodule.prepare_data()
     61             model.prepare_data()
     62             self.trainer._is_data_prepared = True

/opt/conda/lib/python3.8/site-packages/pytorch_lightning/core/datamodule.py in wrapped_fn(*args, **kwargs)
     90             obj._has_prepared_data = True
     91 
---> 92         return fn(*args, **kwargs)
     93 
     94     return wrapped_fn

/opt/conda/lib/python3.8/site-packages/pytorch_lightning/utilities/distributed.py in wrapped_fn(*args, **kwargs)
     38     def wrapped_fn(*args, **kwargs):
     39         if rank_zero_only.rank == 0:
---> 40             return fn(*args, **kwargs)
     41 
     42     return wrapped_fn

/mri-reconstruction/fastMRI/fastmri/pl_modules/data_module.py in prepare_data(self)
    223                 test_path = self.data_path / f"{self.challenge}_test"
    224             data_paths = [
--> 225                 self.data_path / f"{self.challenge}_train",
    226                 self.data_path / f"{self.challenge}_val",
    227                 test_path,

TypeError: unsupported operand type(s) for /: 'str' and 'str'

the code I am running:

from fastmri.pl_modules import FastMriDataModule, UnetModule
from fastmri.data.transforms import UnetDataTransform
from fastmri.data.subsample import create_mask_for_mask_type

dataroot = "../../mnt/FastMRI/singlecoil_train/"
test_path = "../../mnt/FastMRI/singlecoil_test_v2/"

# singlecoil or multicoil
challenge = "singlecoil"
# MASK_TYPE is either random (for knee) or equispaced (for brain)
mask_type = "random"
center_fractions = [0.08]
accelerations = [4]

mask = create_mask_for_mask_type(
    mask_type, center_fractions, accelerations
)

train_transform = UnetDataTransform(challenge, mask_func=mask, use_seed=False)
val_transform = UnetDataTransform(challenge, mask_func=mask)
test_transform = UnetDataTransform(challenge)

data_module = FastMriDataModule(
    data_path=dataroot,
    challenge=challenge,
    train_transform=train_transform,
    val_transform=val_transform,
    test_transform=test_transform,
    test_path=test_path
)

model = UnetModule(
    in_chans=1,  # number of input channels to U-Net
    out_chans=1,  # number of output chanenls to U-Net
    chans=32,  # number of top-level U-Net channels
    num_pool_layers=4,  # number of U-Net pooling layers
    drop_prob=0.0,  # dropout probability
    lr=0.001,  # RMSProp learning rate
    lr_step_size=40,  # epoch at which to decrease learning rate
    lr_gamma=0.1,  # extent to which to decrease learning rate
    weight_decay=0.0,  # weight decay regularization strength
)

trainer = pl.Trainer()
trainer.fit(model, datamodule=data_module)

My packages versions:

fastmri                       0.1                 /mri-reconstruction/fastMRI
pytorch-lightning             1.2.1
torch                         1.7.1
torchvision                   0.8.2

Why is there a string "division"? Am I doing something wrong?

mmuckley commented 3 years ago

You passed in a str for the path instead of a Path object. The type annotation indicates only Path objects should be used.

https://docs.python.org/3/library/pathlib.html

In the standard script the fetch_dir function returns a Path object. https://github.com/facebookresearch/fastMRI/blob/4254a6aacd920fd0c30e1b8876614e24fb51fb82/fastmri/data/mri_data.py#L98

Or, if you use argparse, that should be a Path object as well. https://github.com/facebookresearch/fastMRI/blob/4254a6aacd920fd0c30e1b8876614e24fb51fb82/fastmri/pl_modules/data_module.py#L271-L276

So the only thing I can think of is maybe you hardcoded a string?

gabrielziegler3 commented 3 years ago

Indeed I was passing a hardcoded string and didn't notice the type annotation. I apologize and appreciate the very detailed response, Matthew. Have a great week!

mmuckley commented 3 years ago

No need to apologize - perfectly reasonable thing to do. You have a good week as well!