SwinTransformer / Swin-Transformer-Object-Detection

This is an official implementation for "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows" on Object Detection and Instance Segmentation.
https://arxiv.org/abs/2103.14030
Apache License 2.0
1.82k stars 379 forks source link

Couldn't get it work. I get an error when I try to run test.py #72

Closed CharlesNJ closed 3 years ago

AlphonsG commented 3 years ago

Did you install Apex?

CharlesNJ commented 3 years ago

Did you install Apex?

Yes, I tried that but in vain

CharlesNJ commented 3 years ago

Installing mmdet after installing apex solved it. For some reason, installing apex messed with mmcv (assumed that mmdet gets automatically installed with mmcv), it threw an error saying mmdet was missing so installing it AFTER resolved it. And, now ended up with a new problem.

Traceback (most recent call last): File "tools/test.py", line 220, in main() File "tools/test.py", line 116, in main cfg = Config.fromfile(args.config) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/config.py", line 316, in fromfile use_predefined_variables) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/config.py", line 216, in _file2dict cfg_dict = mmcv.load(temp_config_file.name) File "/usr/local/lib/python3.7/dist-packages/mmcv/fileio/io.py", line 41, in load obj = handler.load_from_path(file, kwargs) File "/usr/local/lib/python3.7/dist-packages/mmcv/fileio/handlers/base.py", line 21, in load_from_path return self.load_from_fileobj(f, kwargs) File "/usr/local/lib/python3.7/dist-packages/mmcv/fileio/handlers/yaml_handler.py", line 16, in load_from_fileobj return yaml.load(file, **kwargs) File "/usr/local/lib/python3.7/dist-packages/yaml/init.py", line 72, in load return loader.get_single_data() File "/usr/local/lib/python3.7/dist-packages/yaml/constructor.py", line 35, in get_single_data node = self.get_single_node() File "/usr/local/lib/python3.7/dist-packages/yaml/composer.py", line 36, in get_single_node document = self.compose_document() File "/usr/local/lib/python3.7/dist-packages/yaml/composer.py", line 58, in compose_document self.get_event() File "/usr/local/lib/python3.7/dist-packages/yaml/parser.py", line 118, in get_event self.current_event = self.state() File "/usr/local/lib/python3.7/dist-packages/yaml/parser.py", line 193, in parse_document_end token = self.peek_token() File "/usr/local/lib/python3.7/dist-packages/yaml/scanner.py", line 128, in peek_token self.fetch_more_tokens() File "/usr/local/lib/python3.7/dist-packages/yaml/scanner.py", line 220, in fetch_more_tokens return self.fetch_value() File "/usr/local/lib/python3.7/dist-packages/yaml/scanner.py", line 576, in fetch_value self.get_mark()) yaml.scanner.ScannerError: mapping values are not allowed here in "/tmp/tmp_qfwepnf/tmp9h7zna48.yaml", line 76, column 100 Exception ignored in: <function _TemporaryFileCloser.del at 0x7f8c86d99cb0> Traceback (most recent call last): File "/usr/lib/python3.7/tempfile.py", line 587, in del self.close() File "/usr/lib/python3.7/tempfile.py", line 583, in close unlink(self.name) FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmp_qfwepnf/tmp9h7zna48.yaml'

AlphonsG commented 3 years ago

@CharlesNJohn Good to hear the install worked. As for the new error, it might mean that there's a syntax error with your config file (e.g. a missing comma somewhere), assuming you've modified the default one.

CharlesNJ commented 3 years ago

Thank you! I haven't touched any of the files, I am just running them as it is.

AlphonsG commented 3 years ago

Ah okay, what command did you run?

CharlesNJ commented 3 years ago

single-gpu testing

!python tools/test.py /content/swin_base_patch4_window12_384.yaml /content/swin_base_patch4_window12_384.pth --eval bbox segm

CharlesNJ commented 3 years ago

Ah okay, what command did you run?

Thank you for taking time to help, I appreciate it.

AlphonsG commented 3 years ago

No worries. Where did you get this 'config' file ('/content/swin_base_patch4_window12_384.yaml ') from? And same with your checkpoint file ('/content/swin_base_patch4_window12_384.pth')?

I think your config file should be a .py file. Example config files and their corresponding checkpoint files for swin object detection are given here. Try downloading this config file and it's checkpoint file and running test.py with those instead. You'll probably need to modify the config file to point to your images but try running it first. If it complains about the images then you'll know you're on the right track.

I could be wrong though.

CharlesNJ commented 3 years ago

I did not realize I have been back and forth with another SwinTr. from Microsoft. https://github.com/microsoft/Swin-Transformer I got those files from here. I think I will go with yours first and then try the msft one. Thank you!

AlphonsG commented 3 years ago

Can I ask which guide you followed when installing mmdetection?

CharlesNJ commented 3 years ago

https://github.com/open-mmlab/mmdetection/blob/master/docs/get_started.md

CharlesNJ commented 3 years ago

Can I ask which guide you followed when installing mmdetection?

Sorry, I thought I should not bother you and just do more googling before asking you, that's why I deleted my previous comment.

CharlesNJ commented 3 years ago

Now you responded, here it is!

Traceback (most recent call last): File "tools/test.py", line 220, in main() File "tools/test.py", line 116, in main cfg = Config.fromfile(args.config) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/config.py", line 316, in fromfile use_predefined_variables) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/config.py", line 234, in _file2dict _cfg_dict, _cfg_text = Config._file2dict(osp.join(cfg_dir, f)) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/config.py", line 180, in _file2dict check_file_exist(filename) File "/usr/local/lib/python3.7/dist-packages/mmcv/utils/path.py", line 23, in check_file_exist raise FileNotFoundError(msg_tmpl.format(filename)) FileNotFoundError: file "/base/models/mask_rcnn_swin_fpn.py" does not exist

AlphonsG commented 3 years ago

https://github.com/open-mmlab/mmdetection/blob/master/docs/get_started.md

Okay thanks. Since we are installing mmdetection for swin, we need to follow this guide not that one:

https://github.com/SwinTransformer/Swin-Transformer-Object-Detection/blob/master/docs/get_started.md

The reason you are getting the error is because the instructions you followed tell you to clone a different repo (https://github.com/open-mmlab/mmdetection/) to the swin one we are currently in (https://github.com/SwinTransformer/Swin-Transformer-Object-Detection). That probably explains the previous error with Apex.

I recommend following the correct guide and doing a reinstall, and remember to clone the right repo (https://github.com/SwinTransformer/Swin-Transformer-Object-Detection) for this step in the guide:

  1. Clone the MMDetection repository.

    git clone https://github.com/open-mmlab/mmdetection.git
    cd mmdetection
CharlesNJ commented 3 years ago

Thank you @AlphonsGwatimba, I have followed the procedure but I still get the same error as above, '..swin_fpn.py doesn't exist'

AlphonsG commented 3 years ago

Hmm that's odd, what's the name of the directory you're running the command from?

CharlesNJ commented 3 years ago

I am running it from the Swin Transfomer Object Detection directory

AlphonsG commented 3 years ago

Does this file exist in that directory:

Swin-Transformer-Object-Detection/configs/base/models/mask_rcnn_swin_fpn.py

CharlesNJ commented 3 years ago

It does exist!

CharlesNJ commented 3 years ago

I wonder if that is taking from the same directory.

CharlesNJ commented 3 years ago

I wonder if that is taking from the same directory.

there are no other base/models though!

CharlesNJ commented 3 years ago

I have tried it with other models (masked rcnn) too, throws the same error that it is not found.

CharlesNJ commented 3 years ago

I see what's happening - It's searching in "Downloads/base/models/mask_rcnn_swin_fpn.py", instead of "Downloads/Swin-Transformer-Object-Detection/configs/base/models/mask_rcnn_swin_fpn.py"

Do I just change the code of the file of the path.py? Do you know a better approach to change the path? "Downloads/mmcv/mmcv/utils/path.py"

CharlesNJ commented 3 years ago

Changed the code in the config file, that issue is solved.

base = [ './configs/base/models/mask_rcnn_swin_fpn.py', './configs/base/datasets/coco_instance.py', './configs/base/schedules/schedule_1x.py', './configs/base/default_runtime.py' ]

AlphonsG commented 3 years ago

@CharlesNJohn Sorry for the late reply but glad you fixed it!

CharlesNJ commented 3 years ago

Thank you for your continuous support, it really helps when you know someone out there is to help. I appreciate it. I am getting so many errors lol, just trying to run a pre-trained model here. Haha

Downloads/mmcv/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.__name__}: {e}') FileNotFoundError: CocoDataset: [Errno 2] No such file or directory: 'data/coco/annotations/instances_val2017.json'

CharlesNJ commented 3 years ago

Thank you for your continuous support, it really helps when you know someone out there is to help. I appreciate it. I am getting so many errors lol, just trying to run a pre-trained model here. Haha

Downloads/mmcv/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.__name__}: {e}') FileNotFoundError: CocoDataset: [Errno 2] No such file or directory: 'data/coco/annotations/instances_val2017.json'

pycocotools is already installed. Do you know which directory to look this up?

AlphonsG commented 3 years ago

That error means it's looking for images and their annotations to use for inference but it can't find them. Remember what I said in a previous comment:

You'll probably need to modify the config file to point to your images but try running it first. If it complains about the images then you'll know you're on the right track.

We are now at that stage where it's complaining about the images so that's good, you're on the right track. From this point on I recommend reading the official guide here which will explain everything about using mmdetection, such as tweaking the config, image/annotations. Maybe start with "1: Inference and train with existing models and standard datasets".