如何修复错误 “模块不可调用”

huangapple go评论146阅读模式
英文:

How to fix error "Module is not callable"

问题

你遇到了一个错误,错误信息是 "'module' object is not callable"。这个错误可能是由于模块和类之间的混淆引起的。要解决这个问题,你可以检查以下几点:

  1. 确保导入了正确的库和模块,特别是 pytorchvideopytorch_lightning

  2. 检查 pytorchvideo.data.ucf101pytorchvideo.data.make_clip_sampler 是否被正确使用。可能需要查看这些函数的文档或示例以确保正确的调用方式。

  3. 确保 dataset_root_pathtrain_transformval_transform 变量已经定义并且可用。

  4. 可以尝试在导入模块时使用别名,以避免与类名混淆。例如,使用 import pytorchvideo.data as pvdata,然后在代码中使用 pvdata.ucf101pvdata.make_clip_sampler

请检查这些方面,看看是否可以解决这个错误。如果问题仍然存在,可能需要更多的上下文信息来帮助进一步调试。

英文:

I am getting error "Module is not callable" at the function val_dataloader step. I already saw that this error may be caused by pyhton confusion between module and class, but still I am not able to resolve it.

class UCFDataModule(pytorch_lightning.LightningDataModule):

  _CLIP_DURATION = 2  # Duration of sampled clip for each video
  _BATCH_SIZE = 8
  _NUM_WORKERS = 8  # Number of parallel processes fetching data
    
  def train_dataloader(self):

    train_dataset = pytorchvideo.data.ucf101(
        data_path = os.path.join(dataset_root_path, "train"),
        clip_sampler = pytorchvideo.data.make_clip_sampler("random", self._CLIP_DURATION),
        transform = train_transform
    )

    return torch.utils.data.DataLoader(
        train_dataset,
        batch_size = self._BATCH_SIZE,
        num_workers = self._NUM_WORKERS,
    )


  def val_dataloader(self):

    val_dataset = pytorchvideo.data.ucf101(
        data_path = os.path.join(dataset_root_path, "val"),
        clip_sampler = pytorchvideo.data.make_clip_sampler("uniform", self._CLIP_DURATION),
        decode_audio = False,
        transform = val_transform
    )

    return torch.utils.data.DataLoader(
        val_dataset,
        batch_size = self._BATCH_SIZE,
        num_workers = self._NUM_WORKERS
    )

  def test_dataloader(self):

    test_dataset = pytorchvideo.data.ucf101(
        data_path = os.path.join(dataset_root_path, "test"),
        clip_sampler = pytorchvideo.data.make_clip_sampler("uniform", self._CLIP_DURATION),
        decode_audio = False,
        transform = val_transform 
    )

    return torch.utils.data.DataLoader(
        test_dataset,
        batch_size = self._BATCH_SIZE,
        num_workers = self._NUM_WORKERS
    )
def train():
  classification_module = VideoClassificationLightningModule()
  data_module = UCFDataModule()
  trainer = pytorch_lightning.Trainer()
  trainer.fit(classification_module, data_module)
  pdb.set_trace()
train()
INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False
INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores
INFO:pytorch_lightning.utilities.rank_zero:IPU available: False, using: 0 IPUs
INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs
/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/utilities.py:70: PossibleUserWarning: `max_epochs` was not set. Setting it to 1000 epochs. To train without an epoch limit, set `max_epochs=-1`.
  rank_zero_warn(
WARNING:pytorch_lightning.loggers.tensorboard:Missing logger folder: /content/lightning_logs
INFO:pytorch_lightning.callbacks.model_summary:
  | Name  | Type | Params
-------------------------------
0 | model | Net  | 31.7 M
-------------------------------
31.7 M    Trainable params
0         Non-trainable params
31.7 M    Total params
126.695   Total estimated model params size (MB)
Sanity Checking:
0/? [00:00<?, ?it/s]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-ff9f91c35b9e> in <cell line: 1>()
----> 1 train_results = train()

13 frames
<ipython-input-11-c166a5edf011> in val_dataloader(self)
     22   def val_dataloader(self):
     23 
---> 24     val_dataset = pytorchvideo.data.ucf101(
     25         data_path = os.path.join(dataset_root_path, "val"),
     26         clip_sampler = pytorchvideo.data.make_clip_sampler("uniform", self._CLIP_DURATION),

TypeError: 'module' object is not callable

答案1

得分: 2

ucf101 是一个模块,我认为你想要使用Ucf101类,你可以直接从pytorchvideo.data中使用它,或者你可以从pytorchvideo.data.ucf101中导入它。

因此,你的最终代码将如下所示:

test_dataset = pytorchvideo.data.Ucf101(
        data_path = os.path.join(dataset_root_path, "test"),
        clip_sampler = pytorchvideo.data.make_clip_sampler(
                         "uniform", self._CLIP_DURATION
                       ),
        decode_audio = False,
        transform = val_transform 
)
英文:

ucf101 is a module I think you want to use Ucf101 class you can directly use it from pytorchvideo.data or you can import it from pytorchvideo.data.ucf101.

So your final code will look like this

test_dataset = pytorchvideo.data.Ucf101(
        data_path = os.path.join(dataset_root_path, "test"),
        clip_sampler = pytorchvideo.data.make_clip_sampler(
                         "uniform", self._CLIP_DURATION
                       ),
        decode_audio = False,
        transform = val_transform 
)

huangapple
  • 本文由 发表于 2023年5月21日 18:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定