JaidedAI / EasyOCR

Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.
https://www.jaided.ai
Apache License 2.0
23.67k stars 3.1k forks source link

How to use custom CRAFT model with EasyOCR #825

Open Shidhani opened 2 years ago

Shidhani commented 2 years ago

I trained the CRAFT detection model using a custom dataset and got the new weights as a .pth file. Now, how can I use this model with EasyOCR? I tried to pass the model directory to the model_storage_directory parameter but it doesn't seem to work.

import easyocr
reader = easyocr.Reader(['en'], 
                        download_enabled=False,
                        model_storage_directory='custom_example/1/model')

when I run the above script I get: FileNotFoundError: Missing custom_example/1/model/craft_mlt_25k.pth and downloads disabled

rkcosmos commented 2 years ago

Go to config.py in easyocr folder, you have to change filename and md5sum of the detector model there.

Shidhani commented 2 years ago

@rkcosmos I got RuntimeError: Error(s) in loading state_dict for CRAFT: Missing key(s) in state_dict: "basenet.slice1.0.weight", "basenet.slice1.0.bias", "basenet.slice1.1.weight", ..

Shidhani commented 2 years ago

@gmuffiness

Mahmuod1 commented 2 years ago

may be when train use the vgg backbone not the craft

dilerbatu commented 2 years ago

@Shidhani Can you tell me how to train your own CRAFT model ? I need lightweight CRAFT model.

yes89929 commented 1 year ago

Go to config.py in easyocr folder, you have to change filename and md5sum of the detector model there.

How about url?

    'craft' : {
        'filename': 'craft_mlt_25k.pth',
        'url': 'C:/Users/res/source/sheet_recognition/test_code/text/EasyOCR-1.6.2/trainer/craft/exp/custom_data_train/CRAFT_clr_amp_200.pth',
        'md5sum': '50D96F50EDD083B92CC5CCFE9E9C0A5B0D476D8BCF4F4194DC75CC71DC6EEF44'
    },
kidserge-yong commented 1 year ago

@rkcosmos I got RuntimeError: Error(s) in loading state_dict for CRAFT: Missing key(s) in state_dict: "basenet.slice1.0.weight", "basenet.slice1.0.bias", "basenet.slice1.1.weight", ..

When you use train.py to train the CRAFT model, the .pth file will be dict_state of 3 item { "iter": train_step, "craft": craft.state_dict(), "optimizer": optimizer.state_dict(), } You only need craft.state_dict(), so

  1. load .pth into torch. ex save_pth= torch.load(''path_to_pth_file)
  2. extract only craft. ex model = save_pth["craft"]
  3. save the model into .pth. ex torch.save(model , "craft_model_save_location")
AbdirayimovS commented 1 year ago

@Shidhani Can you tell me how to train your own CRAFT model ? I need lightweight CRAFT model.

I also need guide on how to annotate images on specific format for craft

FrankwaP commented 1 year ago

Go to config.py in easyocr folder, you have to change filename and md5sum of the detector model there.

Just in case someone does not feel like editing the source code of the environment, you can use this:

# for craft:
import easyocr
from easyocr.detection import get_detector, get_textbox
# or for dbnet
# from .detection_db import get_detector, get_textbox

reader = easyocr.Reader(
    lang_list=["fr"],
    detector=False,
)
reader.get_detector, reader.get_textbox = get_detector, get_textbox
reader.detector = reader.initDetector('path_to_the_filtered_model')

path_to_the_filtered_model is for the model obtained using @kidserge-yong method (thank you btw!).

zahrasadat9 commented 1 month ago

@FrankwaP hi! now that this issue is solved, I have an unrelated question I'm pretty new to programming and saw your answer, it amazed me. Did you read all the code before writing this or it was somehow a pattern that was present in other codes? btw, how should I read a long code like this one? thanks!

FrankwaP commented 1 week ago

Hi @zahrasadat9 :-)

I don't remember exactly what I did, but here's the general process I use most of the time:

  1. I open an IDE such as VSCode or Spyder
  2. then write the few lines of code needed to use the library
  3. and run it with the debugger so I can "step into" each interesting calls; that way it leads me straight to the interesting parts of the library
  4. then the final trick is to use "ctrl+left click" on each object of interest, which leads you straight to its definition.

That way you skip all the "useless" code, which makes you save a lot of time depending on how deep in the code your problem is.

Here, I might have skipped the debugging part and just used "ctrl + left click" on the Reader part of easyocr.Reader, as it looks pretty straight forward.

Hope it helps! :-D