Reputation: 11
I have a json file with labeled images and I'm trying to train detectron2 to identify leaves in a picture of a tree. I keep getting this error:
Traceback (most recent call last):
File "/home/aesa/steps.py", line 71, in <module>
trainer = DefaultTrainer(cfg)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/engine/d efaults.py", line 376, in __init__
model = self.build_model(cfg)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/engine/d efaults.py", line 514, in build_model
model = build_model(cfg)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/modeling /meta_arch/build.py", line 22, in build_model
model = META_ARCH_REGISTRY.get(meta_arch)(cfg)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/config/c onfig.py", line 189, in wrapped
explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/config/c onfig.py", line 245, in _get_args_from_config
ret = from_config_func(*args, **kwargs)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/modeling /meta_arch/rcnn.py", line 76, in from_config
"proposal_generator": build_proposal_generator(cfg, backbone.output_shape()),
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/modeling /proposal_generator/build.py", line 24, in build_proposal_generator
return PROPOSAL_GENERATOR_REGISTRY.get(name)(cfg, input_shape)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/config/c onfig.py", line 189, in wrapped
explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/config/c onfig.py", line 245, in _get_args_from_config
ret = from_config_func(*args, **kwargs)
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/modeling /proposal_generator/rpn.py", line 280, in from_config
ret["anchor_generator"] = build_anchor_generator(cfg, [input_shape[f] for f in in_features])
File "/home/aesa/anaconda3/envs/detectron_env/lib/python3.10/site-packages/detectron2/modeling /proposal_generator/rpn.py", line 280, in <listcomp>
ret["anchor_generator"] = build_anchor_generator(cfg, [input_shape[f] for f in in_features])
KeyError: 'p2'
My code is:
import json
from detectron2.data import MetadataCatalog, DatasetCatalog
def load_data(t="train"):
if t == "train":
with open(".../train/_annotations.coco.json", 'r') as file:
train = json.load(file)
return train
elif t == "val":
with open(".../val.json", 'r') as file:
val = json.load(file)
return val
for d in ["train", "val"]:
DatasetCatalog.register(d, lambda d=d: load_data(d))
MetadataCatalog.get(d).set(thing_classes=["Leaf"])
metadata = MetadataCatalog.get("train")
from detectron2.config import get_cfg
from detectron2 import model_zoo
def custom_config():
cfg = get_cfg()
# get configuration from model_zoo
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
# Model
cfg.MODEL.MASK_ON = True
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
cfg.MODEL.BACKBONE.NAME = "build_resnet_backbone"
cfg.MODEL.RESNETS.DEPTH = 34
cfg.MODEL.RESNETS.RES2_OUT_CHANNELS = 64
# Solver
cfg.SOLVER.BASE_LR = 0.0002
cfg.SOLVER.MAX_ITER = 40000
cfg.SOLVER.STEPS = (20, 10000, 20000)
cfg.SOLVER.gamma = 0.5
cfg.SOLVER.IMS_PER_BATCH = 4
# Test
cfg.TEST.DETECTIONS_PER_IMAGE = 20
# INPUT
cfg.INPUT.MIN_SIZE_TRAIN = (800,)
# DATASETS
cfg.DATASETS.TEST = ('val',)
cfg.DATASETS.TRAIN = ('train',)
# DATASETS
cfg.OUTPUT_DIR = "/train/"
return cfg
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.engine import DefaultTrainer
if __name__ == '__main__':
for d in ["train", "val"]:
#DatasetCatalog.register(d, lambda d=d: load_data(d))
MetadataCatalog.get(d).set(thing_classes=["Leaf"])
metadata = MetadataCatalog.get("train")
cfg = custom_config()
trainer = DefaultTrainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()
I have tried reinstalling Detectron2 but it didn't solve the issue.
Upvotes: 1
Views: 353
Reputation: 1
Had the same problem and after commenting cfg.MODEL.BACKBONE.NAME="build_resnet_backbone"
line, it suddenly worked. This name was not in alignment with the chosen model.
Upvotes: 0
Reputation: 1
To resolve the issue, you can modify the cfg configuration file by changing the values of cfg.MODEL.ROI_HEADS.NAME and cfg.MODEL.ROI_HEADS.IN_FEATURES. This will affect the ROI_Head field of the configuration and may help to resolve the issue.
Upvotes: 0