open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.5k stars 9.44k forks source link

ImportError: cannot import name 'build_dataset' from 'mmdet.datasets' #10135

Open SteveMama opened 1 year ago

SteveMama commented 1 year ago

After following the mmdetection:3.x documentation, I've followed the following:

!pip3 install openmim !mim install mmengine !mim install "mmcv>=2.0.0,<2.1.0"

Subsequently, I followed the Instance Segmentation notebook as it is. I was able to download and import the config file, the checkpoint file, do necessary changes on that front.

however, while I try to run the following code as it is taken from the semantic segmentation colab tutorial:

from mmdet.datasets import build_dataset from mmdet.models import build_detector from mmengine.runner import Runner

build the runner from config

runner = Runner.from_cfg(cfg)

I see the above error, the same error also applies to mmdet.models

I tried checking out if the change-logs have any relevant information, I couldn't find any.

Please help me and the same error can be reproduced by following the instance segementation colab tutorial.

Thanks in advance!

pythonlover22 commented 1 year ago

I faced the same problem while trying to utilize the MMDet_InstanceSeg_Tutorial.ipynb tutorial file and trying to execute the following code:

from mmdet.datasets import build_dataset from mmdet.models import build_detector from mmengine.runner import Runner

# build the runner from config runner = Runner.from_cfg(cfg)

Error: ImportError: cannot import name 'build_dataset' from 'mmdet.datasets'

My environment was set up with the following installations:

Torch version: 2.0.0 with CUDA support MMDetection: 3.0.0 MMCV: 2.0.0 MMEngine: 0.7.3

Given that this issue has persisted for over a month without a resolution, I'm eager to know if anyone has discovered a solution to this problem.

KenYu910645 commented 1 year ago

I encountered the same issue and solve it by using the 2.X old version of mmdetection. I know it's not an ideal solution but at least the tutorials can work there. I think mmdetection hasn't update their tutorial after mmcv upgrade to 2.0 and mmcv changed many package locations, incurring these issues. For those who want to try the old version, just switch to the 2.x branch in the repo and reinstall mmcv by "mim install mmcv-full"

pythonlover22 commented 1 year ago

Hi @KenYu910645, thank you for sharing your experience and offering a potential solution. However, downgrading to a lower version didn't resolve the issue for me. I have actually opened an issue myself, numbered #10298, where I mentioned that I tried using the three tutorial files with three different environment installations. One of them included mmcv-full-1.5, and after that i tried a fourth installtion was a CPU environment with mmcv-full-1.7. Unfortunately, none of these configurations worked.

In addition to the open issues on the mmdetection repository and other platforms like Stack Overflow, I highlighted the lack of solutions provided by the mmdetection team for over a month. It appears that there is a lack of technical support, including updates to the documentation. It's evident that users like us are left to find solutions on our own, which is far from ideal.

Could you kindly share the details of your environment installation following the documentation provided in this link: https://mmcv.readthedocs.io/en/latest/get_started/installation.html ? This information includes: • Torch version: • CUDA compiler version: • MMDetection version: • MMCV version: • MMEngine version or mmcv_runner version: • Python version: • Operating system platform:

Thank you once again for your helpful response.

KenYu910645 commented 1 year ago

Sure, here's my env: • Torch version: 1.12.1 • CUDA compiler version: 10.2 • MMDetection version: 2.28.2 • MMCV-full version: 1.7.0 • MMEngine version: 0.7.3 • Python version: 3.8.16 • Operating system platform: Ubuntu 18.04.3 LTS GCC version: 7.3 GPU: GeForce GTX 1080 Ti And i only run demo/MMDet_Tutorial.ipynb successfully, and i haven't tried the other two tutorial files. I run it on ubuntu and i not sure whether mmdetection support windows11.

akashAD98 commented 1 year ago

im also facing same issue,running it on collab,im running mmdet segmentation.ipynb tutorial

aiakash commented 1 year ago

@hhaAndroid which version should we try? seems like .ipynb is not working & the codebase is changed

aymanaboghonim commented 1 year ago

Try to use mmdetection version .

dbabbitt commented 1 year ago

This is from the MMDetection V3.0.0rc4 Release mmdet/datasets/builder.py (https://github.com/open-mmlab/mmdetection/releases/tag/v3.0.0rc4):

def build_dataset(cfg, default_args=None):
    from mmengine.dataset import ClassBalancedDataset

    from .dataset_wrappers import MultiImageMixDataset

    if cfg['type'] == 'ClassBalancedDataset':
        dataset = ClassBalancedDataset(
            build_dataset(cfg['dataset'], default_args), cfg['oversample_thr'])
    elif cfg['type'] == 'MultiImageMixDataset':
        cp_cfg = copy.deepcopy(cfg)
        cp_cfg['dataset'] = build_dataset(cp_cfg['dataset'])
        cp_cfg.pop('type')
        dataset = MultiImageMixDataset(**cp_cfg)
    elif isinstance(cfg.get('ann_file'), (list, tuple)):
        dataset = _concat_dataset(cfg, default_args)
    else:
        dataset = DATASETS.build(cfg, default_args=default_args)

    return dataset
lujzz commented 1 year ago

mim install mmdet==3.0.0rc4 maybe u can use this command to change the verson of mmdet

ytzfhqs commented 1 year ago

I made a notebook to fine tune the RTMDet model on the balloon dataset, MMdet Balloon Segmentation, it's basically the same as the tutorial steps and it works fine on the colab platform, hope that helps!

hhaAndroid commented 1 year ago

@SteveMama Please update to v3.1.0

MjdMahasneh commented 1 year ago

I am facing the same issue. Can someone explain why this is happening and how to solve it? Any context can help understanding the issue please..

MjdMahasneh commented 1 year ago

following up with @ytzfhqs answer, would you like to adapt the visualization bit as well? I used your notebook but had issues with visualization there since its 2.x based.

based on the 3.x docs, this works fine for me:

from mmengine.visualization import Visualizer
import mmcv
from mmdet.apis import init_detector, inference_detector
import glob
import numpy as np

image = mmcv.imread('./ballondatasets/balloon/train/120853323_d4788431b9_b.jpg',channel_order='rgb')

checkpoint_file = glob.glob('./work_dir/epoch_50.pth')[0]

## get results from model 
model = init_detector(cfg, checkpoint_file, device='cuda:0')
new_result = inference_detector(model, img)
# print('new_result', new_result)

## extract boxes, masks, scores, and lables :
pred_bboxes = new_result.pred_instances.bboxes
pred_labels = new_result.pred_instances.labels
pred_scores = new_result.pred_instances.scores
pred_masks = new_result.pred_instances.masks

## lets filter them using a threshold
confidence_threshold = 0.9
# Move tensors to CPU and convert to numpy
pred_scores_np = pred_scores.cpu().numpy()
# Identify the indices that satisfy the threshold
filtered_indices = np.where(pred_scores_np > confidence_threshold)[0]
# Use these indices to filter the predictions
filtered_bboxes = pred_bboxes[filtered_indices].cpu().numpy()
filtered_labels = pred_labels[filtered_indices].cpu().numpy()
filtered_scores = pred_scores_np[filtered_indices]
filtered_masks = pred_masks[filtered_indices].cpu().numpy()

visualizer = Visualizer(image=image)

# draw multiple bboxes
# single bbox formatted as [xyxy]
visualizer.draw_bboxes(filtered_bboxes, edge_colors='r',
                       line_widths=3, line_styles = '--')
## to draw a box formatted as [xyxy]
# visualizer.draw_bboxes(torch.tensor([[33, 120, 209, 220], [72, 13, 179, 147]]))

# visualizer.draw_binary_masks(new_result.pred_instances.masks) ## this also works
visualizer.draw_binary_masks(filtered_masks, colors=(255, 150, 50), alphas=0.35)

visualizer.show()

Many thanks :)

MjdMahasneh commented 1 year ago

Amazing, thank you :)

ytzfhqs commented 1 year ago

following up with @ytzfhqs answer, would you like to adapt the visualization bit as well? I used your notebook but had issues with visualization there since its 2.x based.

based on the 3.x docs, this works fine for me:

from mmengine.visualization import Visualizer
import mmcv
from mmdet.apis import init_detector, inference_detector
import glob
import numpy as np

image = mmcv.imread('./ballondatasets/balloon/train/120853323_d4788431b9_b.jpg',channel_order='rgb')

checkpoint_file = glob.glob('./work_dir/epoch_50.pth')[0]

## get results from model 
model = init_detector(cfg, checkpoint_file, device='cuda:0')
new_result = inference_detector(model, img)
# print('new_result', new_result)

## extract boxes, masks, scores, and lables :
pred_bboxes = new_result.pred_instances.bboxes
pred_labels = new_result.pred_instances.labels
pred_scores = new_result.pred_instances.scores
pred_masks = new_result.pred_instances.masks

## lets filter them using a threshold
confidence_threshold = 0.9
# Move tensors to CPU and convert to numpy
pred_scores_np = pred_scores.cpu().numpy()
# Identify the indices that satisfy the threshold
filtered_indices = np.where(pred_scores_np > confidence_threshold)[0]
# Use these indices to filter the predictions
filtered_bboxes = pred_bboxes[filtered_indices].cpu().numpy()
filtered_labels = pred_labels[filtered_indices].cpu().numpy()
filtered_scores = pred_scores_np[filtered_indices]
filtered_masks = pred_masks[filtered_indices].cpu().numpy()

visualizer = Visualizer(image=image)

# draw multiple bboxes
# single bbox formatted as [xyxy]
visualizer.draw_bboxes(filtered_bboxes, edge_colors='r',
                       line_widths=3, line_styles = '--')
## to draw a box formatted as [xyxy]
# visualizer.draw_bboxes(torch.tensor([[33, 120, 209, 220], [72, 13, 179, 147]]))

# visualizer.draw_binary_masks(new_result.pred_instances.masks) ## this also works
visualizer.draw_binary_masks(filtered_masks, colors=(255, 150, 50), alphas=0.35)

visualizer.show()

Many thanks :)

I'm very glad that my notebook could help you, I'll update the code for the visualisation section in a later notebook, thanks for the heads up!

kushiluv commented 1 year ago

I made a notebook to fine tune the RTMDet model on the balloon dataset, MMdet Balloon Segmentation, it's basically the same as the tutorial steps and it works fine on the colab platform, hope that helps!

I tried using maskedRcnn in your colab by changing the config files but didn't work , any tips?

ytzfhqs commented 1 year ago

@kushiluv It is recommended to check that the config file has been changed correctly, this may not be an error in the framework itself. Please refer to Mask R-CNN Det Tutorial.If your problem is solved, please let me know what caused the error for you, it will help me to improve the tutorial file, thanks!

MeerAjaz commented 1 year ago

After following the mmdetection:3.x documentation, I've followed the following:

!pip3 install openmim !mim install mmengine !mim install "mmcv>=2.0.0,<2.1.0"

Subsequently, I followed the Instance Segmentation notebook as it is. I was able to download and import the config file, the checkpoint file, do necessary changes on that front.

however, while I try to run the following code as it is taken from the semantic segmentation colab tutorial:

from mmdet.datasets import build_dataset from mmdet.models import build_detector from mmengine.runner import Runner

build the runner from config

runner = Runner.from_cfg(cfg)

I see the above error, the same error also applies to mmdet.models

I tried checking out if the change-logs have any relevant information, I couldn't find any.

Please help me and the same error can be reproduced by following the instance segementation colab tutorial.

Thanks in advance!

Well we're not using build_dataset and build_detector in later point of time in the script so you may comment out these two lines or remove it completely.