EasyOCR icon indicating copy to clipboard operation
EasyOCR copied to clipboard

How to use custom CRAFT model with EasyOCR

Open Shidhani opened this issue 2 years ago • 11 comments

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

Shidhani avatar Aug 23 '22 18:08 Shidhani

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

rkcosmos avatar Aug 24 '22 07:08 rkcosmos

@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 avatar Aug 24 '22 07:08 Shidhani

@gmuffiness

Shidhani avatar Aug 24 '22 11:08 Shidhani

may be when train use the vgg backbone not the craft

  • update solve is by adding
  •     net.load_state_dict(copyStateDict(torch.load(trained_model, map_location=device)['cuda'])) 
    
  • in this line

Mahmuod1 avatar Aug 25 '22 13:08 Mahmuod1

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

dilerbatu avatar Sep 12 '22 07:09 dilerbatu

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'
    },

yes89929 avatar Oct 19 '22 10:10 yes89929

@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")

kidserge-yong avatar Dec 01 '22 08:12 kidserge-yong

@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

AbdirayimovS avatar Jan 06 '23 23:01 AbdirayimovS

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!).

FrankwaP avatar Aug 16 '23 14:08 FrankwaP