PatBall1 / detectree2

Python package for automatic tree crown delineation based on the Detectron2 implementation of Mask R-CNN
https://patball1.github.io/detectree2/
MIT License
148 stars 35 forks source link

Register dataset for training #141

Closed yby026 closed 2 months ago

yby026 commented 2 months ago

Hi there,

When I was trying to train the model, a key error occurred as the dataset is not registered. However in the codes no dataset was named "B". Not sure where that is coming from, may I ask if you have any idea how to fix it?

Here's the codes:

train_location = tiles_path + appends + "/train/"
dataset_dicts = combine_dicts(train_location, 2) 
trees_metadata = MetadataCatalog.get(name + "_train")
register_train_data(train_location, 'Bolye', val_fold=1)
base_model = "COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"
trained_model = site_path + "models/model_6.pth"
trains = ("Bolye2_train")
tests = ("Bolye2_val")
out_dir = site_path + "models/DetecteTree/"
cfg = setup_cfg(base_model, trains, tests, trained_model, workers = 4, eval_period=100, max_iter=3000, out_dir=out_dir)
trainer = MyTrainer(cfg, patience = 5)
trainer.resume_or_load(resume=False)
trainer.train()

The full error message is below:

KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/detectron2/data/catalog.py in get(self, name) 50 try: ---> 51 f = self[name] 52 except KeyError as e: 17 frames /usr/lib/python3.10/collections/init.py in getitem(self, key) 1105 return self.class.missing(self, key) -> 1106 raise KeyError(key) 1107 KeyError: 'B' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/detectree2/models/train.py in train(self) 209 self.run_step() --> 210 self.after_step() 211 if self.early_stop: /usr/local/lib/python3.10/dist-packages/detectron2/engine/train_loop.py in after_step(self) 189 for h in self._hooks: --> 190 h.after_step() 191 /usr/local/lib/python3.10/dist-packages/detectron2/engine/hooks.py in after_step(self) 555 if next_iter != self.trainer.max_iter: --> 556 self._do_eval() 557 /usr/local/lib/python3.10/dist-packages/detectron2/engine/hooks.py in _do_eval(self) 528 def _do_eval(self): --> 529 results = self._func() 530 /usr/local/lib/python3.10/dist-packages/detectron2/engine/defaults.py in test_and_save_results() 454 def test_and_save_results(): --> 455 self._last_eval_results = self.test(self.cfg, self.model) 456 return self._last_eval_results /usr/local/lib/python3.10/dist-packages/detectron2/engine/defaults.py in test(cls, cfg, model, evaluators) 603 for idx, dataset_name in enumerate(cfg.DATASETS.TEST): --> 604 data_loader = cls.build_test_loader(cfg, dataset_name) 605 # When evaluators are passed in as arguments, /usr/local/lib/python3.10/dist-packages/detectron2/engine/defaults.py in build_test_loader(cls, cfg, dataset_name) 559 """ --> 560 return build_detection_test_loader(cfg, dataset_name) 561 /usr/local/lib/python3.10/dist-packages/detectron2/config/config.py in wrapped(*args, kwargs) 206 if _called_with_cfg(*args, *kwargs): --> 207 explicit_args = _get_args_from_config(from_config, args, kwargs) 208 return orig_func(explicit_args) /usr/local/lib/python3.10/dist-packages/detectron2/config/config.py in _get_args_from_config(from_config_func, *args, *kwargs) 244 extra_kwargs[name] = kwargs.pop(name) --> 245 ret = from_config_func(args, kwargs) 246 # forward the other arguments to init /usr/local/lib/python3.10/dist-packages/detectron2/data/build.py in _test_loader_from_config(cfg, dataset_name, mapper) 582 --> 583 dataset = get_detection_dataset_dicts( 584 dataset_name, /usr/local/lib/python3.10/dist-packages/detectron2/data/build.py in get_detection_dataset_dicts(names, filter_empty, min_keypoints, proposal_files, check_consistency) 252 --> 253 dataset_dicts = [DatasetCatalog.get(dataset_name) for dataset_name in names] 254 /usr/local/lib/python3.10/dist-packages/detectron2/data/build.py in (.0) 252 --> 253 dataset_dicts = [DatasetCatalog.get(dataset_name) for dataset_name in names] 254 /usr/local/lib/python3.10/dist-packages/detectron2/data/catalog.py in get(self, name) 52 except KeyError as e: ---> 53 raise KeyError( 54 "Dataset '{}' is not registered! Available datasets are: {}".format( KeyError: "Dataset 'B' is not registered! Available datasets are: coco_2014_train, coco_2014_val, coco_2014_minival, coco_2014_valminusminival, coco_2017_train, coco_2017_val, coco_2017_test, coco_2017_test-dev, coco_2017_val_100, keypoints_coco_2014_train, keypoints_coco_2014_val, keypoints_coco_2014_minival, keypoints_coco_2014_valminusminival, keypoints_coco_2017_train, keypoints_coco_2017_val, keypoints_coco_2017_val_100, coco_2017_train_panoptic_separated, coco_2017_train_panoptic_stuffonly, coco_2017_train_panoptic, coco_2017_val_panoptic_separated, coco_2017_val_panoptic_stuffonly, coco_2017_val_panoptic, coco_2017_val_100_panoptic_separated, coco_2017_val_100_panoptic_stuffonly, coco_2017_val_100_panoptic, lvis_v1_train, lvis_v1_val, lvis_v1_test_dev, lvis_v1_test_challenge, lvis_v0.5_train, lvis_v0.5_val, lvis_v0.5_val_rand_100, lvis_v0.5_test, lvis_v0.5_train_cocofied, lvis_v0.5_val_cocofied, cityscapes_fine_instance_seg_train, cityscapes_fine_sem_seg_train, cityscapes_fine_instance_seg_val, cityscapes_fine_sem_seg_val, cityscapes_fine_instance_seg_test, cityscapes_fine_sem_seg_test, cityscapes_fine_panoptic_train, cityscapes_fine_panoptic_val, voc_2007_trainval, voc_2007_train, voc_2007_val, voc_2007_test, voc_2012_trainval, voc_2012_train, voc_2012_val, ade20k_sem_seg_train, ade20k_sem_seg_val, Bolye_train, Bolye_val, Bolye1_train, Bolye1_val, Bolye2_train, Bolye2_val"

I'm running it using Google Colab.

Thanks :)

Jonetz commented 2 weeks ago

Hello,

I have the same problem and saw you closed the issue, did you figure out what the problem was and how to fix it?

Best Regards, Jonas

yby026 commented 2 weeks ago

Hello,

I have the same problem and saw you closed the issue, did you figure out what the problem was and how to fix it?

Best Regards, Jonas

Hi, you'll need to add comma after the trains and tests like this


trains = ("Bolye2_train",)
tests = ("Bolye2_val",)
Jonetz commented 1 week ago

Hey, thank you it worked just fine!