Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

NameError: name 'Parser' is not defined #1473

Closed
shravankumar147 opened this issue Oct 25, 2022 · 14 comments
Closed

NameError: name 'Parser' is not defined #1473

shravankumar147 opened this issue Oct 25, 2022 · 14 comments
Assignees
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@shravankumar147
Copy link

shravankumar147 commented Oct 25, 2022

🐛 Bug

When trying out object detection example, got the below error at datamodule.

NameError: name 'Parser' is not defined

Code sample

datamodule = ObjectDetectionData.from_coco(
    train_folder="data/coco128/images/train2017/",
    train_ann_file="data/coco128/annotations/instances_train2017.json",
    val_split=0.1,
    transform_kwargs={"image_size": 512},
    batch_size=4,
)

To Reproduce

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[<ipython-input-5-56b8a0b8bc9b>](https://localhost:8080/#) in <module>
      4     val_split=0.1,
      5     transform_kwargs={"image_size": 512},
----> 6     batch_size=4,
      7 )

5 frames
[/usr/local/lib/python3.7/dist-packages/flash/core/integrations/icevision/data.py](https://localhost:8080/#) in load_data(self, root, ann_file, parser, parser_kwargs)
     43         parser_kwargs = {} if parser_kwargs is None else parser_kwargs
     44         unwrapped_parser = getattr(parser, "func", parser)
---> 45         if inspect.isclass(unwrapped_parser) and issubclass(unwrapped_parser, Parser):
     46             parser = parser(ann_file, root, **parser_kwargs)
     47         elif isinstance(unwrapped_parser, Callable):

NameError: name 'Parser' is not defined

Expected behavior

It should create a data module for further computations.

Environment

  • OS (e.g., Linux): Linux
  • Python version: Python 3.7.15
  • PyTorch/Lightning/Flash Version (e.g., 1.10/1.5/0.7): Flash 0.8.0
  • GPU models and configuration: Colab GPU
  • Any other relevant information: Running on Google Colab
@shravankumar147 shravankumar147 added bug / fix Something isn't working help wanted Extra attention is needed labels Oct 25, 2022
@krshrimali
Copy link
Contributor

Hi, @shravankumar147 - Thank you for raising this issue. Object Detection in Flash internally relies on icevision library. I'll create a PR to raise a better error if icevision isn't available. For now, if this is something that blocks you, could you please try installing icevision: pip install icevision, and see if that resolves the error? Thanks!

@shravankumar147
Copy link
Author

Thanks for the input @krshrimali , I will test it in some time and update back here.

@shravankumar147
Copy link
Author

Hi,

I did try to install icevision using


!pip install icevision[all]

But after installation I observed below issue:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
torchtext 0.13.1 requires torch==1.12.1, but you have torch 1.10.2 which is incompatible.
torchaudio 0.12.1+cu113 requires torch==1.12.1, but you have torch 1.10.2 which is incompatible.
flask 1.1.4 requires click<8.0,>=5.1, but you have click 8.0.4 which is incompatible.
Successfully installed GitPython-3.1.29 albumentations-1.0.3 antlr4-python3-runtime-4.9.3 click-8.0.4 dataclasses-0.6 docker-pycreds-0.4.0 effdet-0.2.4 fastai-2.5.6 fastcore-1.3.29 fire-0.4.0 fvcore-0.1.5.post20220512 gitdb-4.0.9 huggingface-hub-0.10.1 icevision-0.12.0 iopath-0.1.10 jedi-0.18.1 loguru-0.6.0 nose-1.3.7 omegaconf-2.2.3 pathtools-0.1.2 pillow-8.4.0 portalocker-2.6.0 pybboxes-0.1.5 resnest-0.0.6b20221027 sahi-0.10.8 sentry-sdk-1.9.0 setproctitle-1.3.2 shortuuid-1.0.9 smmap-5.0.0 terminaltables-3.1.10 timm-0.6.11 torch-1.10.2 torchvision-0.11.3 wandb-0.13.4 yacs-0.1.8 yolov5-icevision-6.0.0

@shravankumar147
Copy link
Author

Then I tried to upgrade the torch but it still showing more errors, as I import flash.

Sharing the same for reference.

/usr/local/lib/python3.7/dist-packages/torchvision/io/image.py:11: UserWarning: Failed to load image Python extension: /usr/local/lib/python3.7/dist-packages/torchvision/image.so: undefined symbol: _ZNK3c1010TensorImpl36is_contiguous_nondefault_policy_implENS_12MemoryFormatE
  warn(f"Failed to load image Python extension: {e}")
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
[<ipython-input-1-d53de86dc182>](https://localhost:8080/#) in <module>
----> 1 import flash
      2 from flash.core.data.utils import download_data
      3 from flash.image import ObjectDetectionData, ObjectDetector

22 frames
[/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py](https://localhost:8080/#) in script(obj, optimize, _frames_up, _rcb, example_inputs)
   1342             _rcb = _jit_internal.createResolutionCallbackFromClosure(obj)
   1343         fn = torch._C._jit_script_compile(
-> 1344             qualified_name, ast, _rcb, get_default_args(obj)
   1345         )
   1346         # Forward docstrings

RuntimeError: 
object has no attribute nms:
  File "/usr/local/lib/python3.7/dist-packages/torchvision/ops/boxes.py", line 35
    """
    _assert_has_ops()
    return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
           ~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
'nms' is being compiled since it was called from '_batched_nms_vanilla'
  File "/usr/local/lib/python3.7/dist-packages/torchvision/ops/boxes.py", line 102
    for class_id in torch.unique(idxs):
        curr_indices = torch.where(idxs == class_id)[0]
        curr_keep_indices = nms(boxes[curr_indices], scores[curr_indices], iou_threshold)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        keep_mask[curr_indices[curr_keep_indices]] = True
    keep_indices = torch.where(keep_mask)[0]
'_batched_nms_vanilla' is being compiled since it was called from 'batched_nms'
  File "/usr/local/lib/python3.7/dist-packages/torchvision/ops/boxes.py", line 66
    # Ideally for GPU we'd use a higher threshold
    if boxes.numel() > 4_000 and not torchvision._is_tracing():
        return _batched_nms_vanilla(boxes, scores, idxs, iou_threshold)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    else:
        return _batched_nms_coordinate_trick(boxes, scores, idxs, iou_threshold)
'batched_nms' is being compiled since it was called from 'generate_detections'
  File "/usr/local/lib/python3.7/dist-packages/effdet/anchors.py", line 142
        scores[top_detection_idx] = soft_scores
    else:
        top_detection_idx = batched_nms(boxes, scores, classes, iou_threshold=0.5)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE

    # keep only top max_det_per_image scoring predictions
'generate_detections' is being compiled since it was called from '_batch_detection'
  File "/usr/local/lib/python3.7/dist-packages/effdet/bench.py", line 72
        img_scale_i = None if img_scale is None else img_scale[i]
        img_size_i = None if img_size is None else img_size[i]
        detections = generate_detections(
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            class_out[i], box_out[i], anchor_boxes, indices[i], classes[i],
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            img_scale_i, img_size_i, max_det_per_image=max_det_per_image, soft_nms=soft_nms)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        batch_detections.append(detections)
    return torch.stack(batch_detections, dim=0)

@shravankumar147
Copy link
Author

I found a mention of above in the installation guide,

pip uninstall -y torchtext

Let me try and see.

@krshrimali
Copy link
Contributor

Hi, @shravankumar147 - Sorry for the delay in getting back. Were you able to resolve the issue?

@PnthrLeo
Copy link

PnthrLeo commented Nov 8, 2022

Hello everyone! Got the same issue. So, I tried to install icevision as was suggested here. However, there was a problem to do this with python 3.10.

Therefore, I created env with python 3.8 and installed icevision first and then flash-lightning. After this, surprisingly, I started to have this error:
ModuleNotFoundError: Required dependencies not available. Please run: pip install 'icevision'

In the end I tried to downgrade icevision from version 0.12.0 to 0.11.0 and it helped me.

@shravankumar147
Copy link
Author

Hi, @shravankumar147 - Sorry for the delay in getting back. Were you able to resolve the issue?

@krshrimali - In colab, I got multiple issues.

@PnthrLeo Would you mind sharing your colab notebook, if you have successfully able to run this.

@PnthrLeo
Copy link

@shravankumar147
Notebook: https://colab.research.google.com/drive/1sopeaR1XP_rM5ZBYCg5VAPsl2rsE3pD2#scrollTo=ktxt2mcXMSMN

Steps to run notebook:

  1. Run "INSTALLATION" section.
  2. Restart notebook.
  3. Run "CODE section.

Hope, it will help you!

@shravankumar147
Copy link
Author

@PnthrLeo Thanks for sharing.

Unfortunately, I still see similar errors. Here I am attaching my colab notebook for reference.

Please let me know if I missed anything.

Thanks in advance

@shravankumar147
Copy link
Author

@PnthrLeo Thanks for sharing.

Unfortunately, I still see similar errors. Here I am attaching my colab notebook for reference.

Please let me know if I missed anything.

Thanks in advance

Error for reference while importing flash :


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-2-4d6ecab6f6e4>](https://localhost:8080/#) in <module>
      1 from functools import partial
      2 
----> 3 import flash
      4 from flash.core.utilities.imports import example_requires
      5 from flash.image import InstanceSegmentation, InstanceSegmentationData

25 frames
[/usr/local/lib/python3.7/dist-packages/PIL/Image.py](https://localhost:8080/#) in __getattr__(name)
     59         deprecate("Image categories", 10, "is_animated", plural=True)
     60         return categories[name]
---> 61     elif name in ("NEAREST", "NONE"):
     62         deprecate(name, 10, "Resampling.NEAREST or Dither.NONE")
     63         return 0

AttributeError: module 'PIL.Image' has no attribute 'Resampling'

@PnthrLeo
Copy link

@shravankumar147 Probably, you forgot to restart notebook runtime (second step in my instructions to the notebook) after modules installation.

@PnthrLeo
Copy link

PnthrLeo commented Nov 13, 2022

Just in case, I repeat my instructions to the notebook with screenshots:

  1. Run this cell.
    image
  2. Then click on 'Restart runtime'.
    image
  3. After this do not run "Installation" cell. Run only "Code" cells.

I check it again, it should work :)

@shravankumar147
Copy link
Author

Hi @PnthrLeo ,

Thanks for the detailed suggestions, these worked for me.

@Borda Borda closed this as completed Dec 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants