Layout-Parser / layout-parser

A Unified Toolkit for Deep Learning Based Document Image Analysis
https://layout-parser.github.io/
Apache License 2.0
4.75k stars 456 forks source link

Better documentations on how to instantiating different models #96

Closed mzhadigerov closed 2 years ago

mzhadigerov commented 2 years ago

When I run:

model = lp.Detectron2LayoutModel(r"/content/drive/MyDrive/models/id_card_detection/config.yaml",
                                 r"/content/drive/MyDrive/models/id_card_detection/model_final.pth",
                                 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
                                 label_map={1: "id_card"})

everything works fine, but when I run:

model = lp.EfficientDetLayoutModel(config_path = "/content/drive/MyDrive/models/id_card_detection/config.yaml",
                                 model_path = "/content/drive/MyDrive/models/id_card_detection/model_final.pth",
                                 extra_config={"num_classes": 1})

I'm getting the error:

KeyError                                  Traceback (most recent call last)
<ipython-input-20-d4717462bf15> in <module>()
      1 model = lp.EfficientDetLayoutModel(config_path = "/content/drive/MyDrive/models/id_card_detection/config.yaml",
      2                                  model_path = "/content/drive/MyDrive/models/id_card_detection/model_final.pth",
----> 3                                  extra_config={"num_classes": 1})

3 frames
/usr/local/lib/python3.7/dist-packages/layoutparser/models/effdet/layoutmodel.py in __init__(self, config_path, model_path, label_map, extra_config, enforce_cpu, device)
    136         extra_config = extra_config if extra_config is not None else {}
    137 
--> 138         self._initialize_model(config_path, model_path, label_map, extra_config)
    139 
    140         self.output_confidence_threshold = extra_config.get(

/usr/local/lib/python3.7/dist-packages/layoutparser/models/effdet/layoutmodel.py in _initialize_model(self, config_path, model_path, label_map, extra_config)
    192                 bench_task="predict",
    193                 pretrained=True,
--> 194                 checkpoint_path=model_path,
    195             )
    196 

/usr/local/lib/python3.7/dist-packages/effdet/factory.py in create_model(model_name, bench_task, num_classes, pretrained, checkpoint_path, checkpoint_ema, **kwargs)
      9         checkpoint_path='', checkpoint_ema=False, **kwargs):
     10 
---> 11     config = get_efficientdet_config(model_name)
     12     return create_model_from_config(
     13         config, bench_task=bench_task, num_classes=num_classes, pretrained=pretrained,

/usr/local/lib/python3.7/dist-packages/effdet/config/model_config.py in get_efficientdet_config(model_name)
    679     """Get the default config for EfficientDet based on model name."""
    680     h = default_detection_model_configs()
--> 681     h.update(efficientdet_model_param_dict[model_name])
    682     h.num_levels = h.max_level - h.min_level + 1
    683     h = deepcopy(h)  # may be unnecessary, ensure no references to param dict values

KeyError: '/content/drive/MyDrive/models/id_card_detection/config.yaml
lolipopshock commented 2 years ago

Hi @mzhadigerov ! The reason for your issue is that lp.Detectron2LayoutModel and lp.EfficientDetLayoutModel use different configuration "systems". So you might want to directly use some models here https://layout-parser.github.io/platform/ by copying the corresponding weights. For example, if you'd want to try with the MFD model using efficientdet, you can just hit the copy button (indicated by the red arrows in the screenshot below) and it will give you the code for instantiating the corresponding model lp.AutoModel("lp://efficientdet/MFD/tf_efficientdet_d0"). I would say this is more like a documentation issue rather than a bug -- and I'll update the modeling documentations very soon!

image

mzhadigerov commented 2 years ago

But I don't want to use a pre-trained model from https://layout-parser.github.io/platform/. I used https://github.com/Layout-Parser/layout-model-training to train a model on my own dataset. As you can see in the question, I'm referring to

config_path = /content/drive/MyDrive/models/id_card_detection/config.yaml and model_path = /content/drive/MyDrive/models/id_card_detection/model_final.pth.

lolipopshock commented 2 years ago

Yes for now the layout-model-training only works for Detectron2 based models (and thus you can only use the yaml for lp.Detectron2LayoutModel. And we'll provide the code for training EfficientDet models soon.